-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow attaching custom user data to a screenshot command
- Loading branch information
Showing
9 changed files
with
112 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
|
@@ -3,5 +3,7 @@ | |
pub mod input; | ||
mod key; | ||
pub mod output; | ||
mod user_data; | ||
|
||
pub use key::Key; | ||
pub use user_data::UserData; |
This file contains 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,57 @@ | ||
use std::{any::Any, sync::Arc}; | ||
|
||
/// A wrapper around `dyn Any`, used for passing custom user data | ||
/// to [`crate::ViewportCommand::Screenshot`]. | ||
#[derive(Clone, Debug, Default)] | ||
pub struct UserData { | ||
/// A user value given to the screenshot command, | ||
/// that will be returned in [`crate::Event::Screenshot`]. | ||
pub data: Option<Arc<dyn Any + Send + Sync>>, | ||
} | ||
|
||
impl UserData { | ||
/// You can also use [`Self::default`]. | ||
pub fn new(user_info: impl Any + Send + Sync) -> Self { | ||
Self { | ||
data: Some(Arc::new(user_info)), | ||
} | ||
} | ||
} | ||
|
||
impl PartialEq for UserData { | ||
fn eq(&self, other: &Self) -> bool { | ||
match (&self.data, &other.data) { | ||
(Some(a), Some(b)) => Arc::ptr_eq(a, b), | ||
(None, None) => true, | ||
_ => false, | ||
} | ||
} | ||
} | ||
|
||
impl Eq for UserData {} | ||
|
||
impl std::hash::Hash for UserData { | ||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) { | ||
self.data.as_ref().map(Arc::as_ptr).hash(state); | ||
} | ||
} | ||
|
||
#[cfg(feature = "serde")] | ||
impl serde::Serialize for UserData { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: serde::Serializer, | ||
{ | ||
().serialize(serializer) // can't serialize an `Any` | ||
} | ||
} | ||
|
||
#[cfg(feature = "serde")] | ||
impl<'de> serde::Deserialize<'de> for UserData { | ||
fn deserialize<D>(_deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: serde::Deserializer<'de>, | ||
{ | ||
Ok(Self::default()) // can't serialize an `Any` | ||
} | ||
} |
This file contains 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 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 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