Use AtomicU64 for window::Id

This commit is contained in:
Héctor Ramón Jiménez 2023-12-02 20:41:58 +01:00
parent 67408311f4
commit ea42af766f
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
9 changed files with 228 additions and 212 deletions

View file

@ -1,5 +1,6 @@
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::hash::Hash;
use std::sync::atomic::{self, AtomicU64};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
/// The id of the window.
@ -7,15 +8,14 @@ use std::hash::{Hash, Hasher};
/// Internally Iced reserves `window::Id::MAIN` for the first window spawned.
pub struct Id(u64);
static COUNT: AtomicU64 = AtomicU64::new(1);
impl Id {
/// The reserved window [`Id`] for the first window in an Iced application.
pub const MAIN: Self = Id(0);
/// Creates a new unique window [`Id`].
pub fn new(id: impl Hash) -> Id {
let mut hasher = DefaultHasher::new();
id.hash(&mut hasher);
Id(hasher.finish())
pub fn unique() -> Id {
Id(COUNT.fetch_add(1, atomic::Ordering::Relaxed))
}
}