-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Remove lifetime from Event type
#1456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
tangmi
wants to merge
19
commits into
rust-windowing:master
from
tangmi:remove-lifetime-from-event-proto
Closed
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
da991b1
wip:
tangmi 736dad4
update docs
tangmi bca09a4
Remove BufferedEvent, custom Clone impls for Event/WindowEvent
tangmi 2792652
Add scoped_arc_cell from Osspial/scoped_arc_cell
tangmi 0017517
Use scoped_arc_cell instead of NewInnerSizeInteriorMutCoolThing
tangmi 28684cd
Make web target build
tangmi fef925d
Implement macOS backend support
tangmi 1841bd6
Implement iOS backend support
tangmi c28fc76
windows: rename of variable names
tangmi 3477339
Merge remote-tracking branch 'origin/remove-lifetime-from-event-proto…
tangmi 2fa7e68
Use scopes instead of std::mem::drop for iOS/macOS/Windows backends
tangmi 428455c
Attempt implementation at linux/andoid backends
tangmi a7f96fd
Whoop! Fix some missed build breaks
tangmi 9c04903
Fix x11 build
tangmi 12a461e
Merge branch 'rename-new-inner-size' into remove-lifetime-from-event-…
tangmi 12d0afa
Merge remote-tracking branch 'upstream/master' into remove-lifetime-f…
tangmi d5700df
wip impl on mac
tangmi 295bcd5
Fix build on other platforms?
tangmi 4fb6fdd
Impl using ArcCell on web
tangmi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| use winit::dpi::LogicalSize; | ||
| use winit::{ | ||
| event::{Event, WindowEvent}, | ||
| event_loop::{ControlFlow, EventLoop}, | ||
| window::WindowBuilder, | ||
| }; | ||
|
|
||
| /// Change the DPI settings in Windows while running this. | ||
| fn main() { | ||
| simple_logger::init().unwrap(); | ||
| let event_loop = EventLoop::new(); | ||
|
|
||
| let window = WindowBuilder::new() | ||
| .with_title("A fantastic window!") | ||
| .with_inner_size(winit::dpi::LogicalSize::new(128.0, 128.0)) | ||
| .build(&event_loop) | ||
| .unwrap(); | ||
|
|
||
| event_loop.run(move |event, _, control_flow| { | ||
| *control_flow = ControlFlow::Wait; | ||
|
|
||
| match event { | ||
| Event::WindowEvent { | ||
| event: WindowEvent::CloseRequested, | ||
| window_id, | ||
| } if window_id == window.id() => *control_flow = ControlFlow::Exit, | ||
| Event::MainEventsCleared => { | ||
| window.request_redraw(); | ||
| } | ||
|
|
||
| // DPI changed happened! | ||
| Event::WindowEvent { | ||
| event: | ||
| WindowEvent::ScaleFactorChanged { | ||
| scale_factor, | ||
| new_inner_size, | ||
| }, | ||
| .. | ||
| } => { | ||
| dbg!((scale_factor, new_inner_size.get())); | ||
|
|
||
| let _os_suggested_value = new_inner_size | ||
| .replace( | ||
| LogicalSize { | ||
| width: 100, | ||
| height: 200, | ||
| } | ||
| .to_physical(scale_factor), | ||
| ) | ||
| .unwrap(); | ||
| } | ||
|
|
||
| _ => (), | ||
| } | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| [package] | ||
| name = "scoped_arc_cell" | ||
| version = "0.1.0" | ||
| authors = ["Osspial <osspial@gmail.com>"] | ||
| edition = "2018" | ||
|
|
||
| # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
| [dependencies] | ||
| crossbeam-utils = { version = "0.7", no-default-features = true } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| //! Shared mutable cell containing data, with the mutability tied to the liveliness of a lifetime ScopedMutabilityOwner struct. | ||
| //! | ||
| //! Use `scoped_arc_cell` to create a matching `ArcCell` and `ScopedMutabilityOwner` pair. The data can be shared by cloning the `ArcCell`. | ||
| //! | ||
| //! As long as the `ScopedMutabilityOwner` is alive, mutating the inner data will succeed. Once the `ScopedMutabilityOwner` has been `drop`ped, the calls will fail. | ||
|
|
||
| #![warn(missing_docs)] | ||
|
|
||
| use crossbeam_utils::atomic::AtomicCell; | ||
| use std::{ | ||
| error::Error, | ||
| fmt::{self, Debug, Display, Formatter}, | ||
| sync::{ | ||
| atomic::{AtomicBool, Ordering}, | ||
| Arc, | ||
| }, | ||
| }; | ||
|
|
||
| /// Create a matching `ArcCell` and `ScopedMutabilityOwner` pair wrapping some value. | ||
| /// | ||
| /// See crate-level documentation for mutability rules. | ||
| pub fn scoped_arc_cell<T: Copy>(val: T) -> (ArcCell<T>, ScopedMutabilityOwner<T>) { | ||
| let aaaa = ScopedMutabilityOwner::new(val); | ||
| (aaaa.create_reference(), aaaa) | ||
| } | ||
|
|
||
| /// Container for shared atomic interior mutability. | ||
| /// | ||
| /// See crate-level documentation for mutability rules. | ||
| #[derive(Debug, Clone)] | ||
| pub struct ArcCell<T: Copy> { | ||
| data: Arc<Data<T>>, | ||
| } | ||
|
|
||
| /// A type that owns the the mutability lifetime of the matching `ArcCell`. | ||
| /// | ||
| /// See crate-level documentation for mutability rules. | ||
| #[derive(Debug)] | ||
| pub struct ScopedMutabilityOwner<T: Copy> { | ||
| /// TODO(tangmi): Osspial, this is semantically an Arc<Mutex<Cell<_>>>, but implemented with AtomicCell for perf/avoiding deadlock issues? | ||
| data: Arc<Data<T>>, | ||
| } | ||
|
|
||
| /// An error returned when trying to mutate a `ArcCell` that is read-only because the `ScopedMutabilityOwner` has already been `drop`ped. | ||
| #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] | ||
| pub struct StoreError<T>(pub T); | ||
|
|
||
| #[derive(Debug)] | ||
| struct Data<T: Copy> { | ||
| val: AtomicCell<T>, | ||
| is_read_only: AtomicBool, | ||
| } | ||
|
|
||
| impl<T: Copy> ArcCell<T> { | ||
| /// Replaces the contained value, and returns it. | ||
| pub fn replace(&self, val: T) -> Result<T, StoreError<T>> { | ||
| match self.data.is_read_only.load(Ordering::Acquire) { | ||
| false => Ok(self.data.val.swap(val)), | ||
| true => Err(StoreError(val)), | ||
| } | ||
| } | ||
|
|
||
| /// Returns a copy of the contained value. | ||
| pub fn get(&self) -> T { | ||
| self.data.val.load() | ||
| } | ||
|
|
||
| /// Returns a raw pointer to the underlying data in this cell. | ||
| pub fn as_ptr(&self) -> *mut T { | ||
| self.data.val.as_ptr() | ||
| } | ||
| } | ||
|
|
||
| /// Manually implement `PartialEq` to treat `Self` like just a `T`. | ||
| impl<T: Copy + PartialEq> PartialEq<ArcCell<T>> for ArcCell<T> { | ||
| fn eq(&self, other: &ArcCell<T>) -> bool { | ||
| // Note: does not compare `is_read_only` flag. | ||
| self.data.val.load() == other.data.val.load() | ||
| } | ||
| } | ||
|
|
||
| impl<T: Copy> ScopedMutabilityOwner<T> { | ||
| fn new(val: T) -> ScopedMutabilityOwner<T> { | ||
| let data = Arc::new(Data { | ||
| val: AtomicCell::new(val), | ||
| is_read_only: AtomicBool::new(false), | ||
| }); | ||
| ScopedMutabilityOwner { data } | ||
| } | ||
|
|
||
| /// Create a new reference from the underlying data. | ||
| /// | ||
| /// The data will remain immutably alive even after this struct's lifetime. | ||
| pub fn create_reference(&self) -> ArcCell<T> { | ||
| ArcCell { | ||
| data: self.data.clone(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<T: Copy> Drop for ScopedMutabilityOwner<T> { | ||
| fn drop(&mut self) { | ||
| self.data.is_read_only.store(true, Ordering::Release); | ||
| } | ||
| } | ||
|
|
||
| impl<T: Debug> Error for StoreError<T> {} | ||
|
|
||
| impl<T> Display for StoreError<T> { | ||
| fn fmt(&self, f: &mut Formatter) -> fmt::Result { | ||
| write!( | ||
| f, | ||
| "the ScopedMutabilityOwner was destroyed, making this ArcCell read-only" | ||
| ) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.