use std::fmt;
use std::fmt::{Debug, Formatter};
use std::ptr::NonNull;
use crate::rwstore_id::RwStoreId;
#[derive(Copy, Clone)]
pub struct Id {
ordinal: u32,
slot_address: NonNull<()>,
store_id: RwStoreId,
}
impl Id {
pub(crate) fn new<T>(ordinal: u32, slot: &T, store_id: RwStoreId) -> Self {
Self {
ordinal,
slot_address: NonNull::from(slot).cast(),
store_id,
}
}
pub(crate) fn ordinal(&self) -> u32 {
self.ordinal
}
pub(crate) fn slot<T>(&self) -> NonNull<T> {
self.slot_address.cast()
}
pub(crate) fn store_id(&self) -> RwStoreId {
self.store_id
}
}
impl Debug for Id {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.debug_struct("Id")
.field("id", &self.ordinal)
.field("address", &self.slot_address)
.finish()
}
}
unsafe impl Send for Id {}
unsafe impl Sync for Id {}
#[cfg(test)]
mod test {
use std::panic::{RefUnwindSafe, UnwindSafe};
use crate::RwStore;
#[test]
fn implements_sync() {
let store = RwStore::new();
let id = store.insert(0);
&id as &dyn Sync;
}
#[test]
fn implements_send() {
let store = RwStore::new();
let id = store.insert(0);
&id as &dyn Send;
}
#[test]
fn implements_unwind_safe() {
let store = RwStore::new();
let id = store.insert(0);
&id as &dyn UnwindSafe;
}
#[test]
fn implements_ref_unwind_safe() {
let store = RwStore::new();
let id = store.insert(0);
&id as &dyn RefUnwindSafe;
}
}