diff options
author | 2022-08-05 22:31:26 +0200 | |
---|---|---|
committer | 2022-08-05 22:31:26 +0200 | |
commit | f294c2d16249ce8b7989f8af6332678f53d8fb12 (patch) | |
tree | bb8d6f00fe25de9187b1d3b90a88ae95225b7428 /native/src/widget/id.rs | |
parent | a003e797e8a1bb5d365c1db5de6af88e61a47329 (diff) | |
parent | ad5bd0970d7106a97d455a164a582ab1d0bff18b (diff) | |
download | iced-f294c2d16249ce8b7989f8af6332678f53d8fb12.tar.gz iced-f294c2d16249ce8b7989f8af6332678f53d8fb12.tar.bz2 iced-f294c2d16249ce8b7989f8af6332678f53d8fb12.zip |
Merge pull request #1399 from iced-rs/widget-operations
Widget Operations
Diffstat (limited to 'native/src/widget/id.rs')
-rw-r--r-- | native/src/widget/id.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/native/src/widget/id.rs b/native/src/widget/id.rs new file mode 100644 index 00000000..4b8fedf1 --- /dev/null +++ b/native/src/widget/id.rs @@ -0,0 +1,43 @@ +use std::borrow; +use std::sync::atomic::{self, AtomicUsize}; + +static NEXT_ID: AtomicUsize = AtomicUsize::new(0); + +/// The identifier of a generic widget. +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct Id(Internal); + +impl Id { + /// Creates a custom [`Id`]. + pub fn new(id: impl Into<borrow::Cow<'static, str>>) -> Self { + Self(Internal::Custom(id.into())) + } + + /// Creates a unique [`Id`]. + /// + /// This function produces a different [`Id`] every time it is called. + pub fn unique() -> Self { + let id = NEXT_ID.fetch_add(1, atomic::Ordering::Relaxed); + + Self(Internal::Unique(id)) + } +} + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub enum Internal { + Unique(usize), + Custom(borrow::Cow<'static, str>), +} + +#[cfg(test)] +mod tests { + use super::Id; + + #[test] + fn unique_generates_different_ids() { + let a = Id::unique(); + let b = Id::unique(); + + assert_ne!(a, b); + } +} |