iced_core/widget/
id.rs
1use std::borrow;
2use std::sync::atomic::{self, AtomicUsize};
3
4static NEXT_ID: AtomicUsize = AtomicUsize::new(0);
5
6#[derive(Debug, Clone, PartialEq, Eq, Hash)]
8pub struct Id(Internal);
9
10impl Id {
11 pub fn new(id: impl Into<borrow::Cow<'static, str>>) -> Self {
13 Self(Internal::Custom(id.into()))
14 }
15
16 pub fn unique() -> Self {
20 let id = NEXT_ID.fetch_add(1, atomic::Ordering::Relaxed);
21
22 Self(Internal::Unique(id))
23 }
24}
25
26impl From<&'static str> for Id {
27 fn from(value: &'static str) -> Self {
28 Self::new(value)
29 }
30}
31
32#[derive(Debug, Clone, PartialEq, Eq, Hash)]
33enum Internal {
34 Unique(usize),
35 Custom(borrow::Cow<'static, str>),
36}
37
38#[cfg(test)]
39mod tests {
40 use super::Id;
41
42 #[test]
43 fn unique_generates_different_ids() {
44 let a = Id::unique();
45 let b = Id::unique();
46
47 assert_ne!(a, b);
48 }
49}