use std::{hash, marker::PhantomData, sync::Arc};
use super::{Token, Unsync};
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct ArcToken(UniqueId);
impl ArcToken {
pub fn new() -> Self {
ArcToken(UniqueId::new())
}
pub fn id(&self) -> ArcTokenId {
ArcTokenId(self.0.clone())
}
pub fn borrow_as_unsync(&mut self) -> ArcTokenUnsyncRef<'_> {
ArcTokenUnsyncRef(&mut self.0, PhantomData)
}
}
impl Default for ArcToken {
fn default() -> Self {
Self::new()
}
}
unsafe impl Token<ArcTokenId> for ArcToken {
fn eq_id(&self, id: &ArcTokenId) -> bool {
self.0 == id.0
}
}
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct ArcTokenUnsyncRef<'a>(&'a mut UniqueId, PhantomData<MakeUnsync>);
struct MakeUnsync(std::cell::Cell<()>);
impl ArcTokenUnsyncRef<'_> {
pub fn id(&self) -> ArcTokenId {
ArcTokenId(self.0.clone())
}
}
unsafe impl Token<ArcTokenId> for ArcTokenUnsyncRef<'_> {
fn eq_id(&self, id: &ArcTokenId) -> bool {
*self.0 == id.0
}
}
unsafe impl Unsync for ArcTokenUnsyncRef<'_> {}
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
pub struct ArcTokenId(UniqueId);
#[derive(Debug, Clone)]
struct UniqueId(Arc<()>);
impl PartialEq for UniqueId {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.0, &other.0)
}
}
impl Eq for UniqueId {}
impl hash::Hash for UniqueId {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
(&*self.0 as *const ()).hash(state)
}
}
impl UniqueId {
pub fn new() -> Self {
UniqueId(Arc::new(()))
}
}
#[test]
fn unique_id_hash() {
let id1 = UniqueId::new();
let id2 = id1.clone();
let mut hm = std::collections::HashSet::new();
assert!(hm.insert(id1));
assert!(!hm.insert(id2)); }