frankencell/tokens.rs
1use std::marker::PhantomData;
2
3use crate::cells::Cell;
4
5/// A generic token that can store any data. See examples/arena.rs for an example of how this could
6/// be used.
7pub struct TokenWith<T, const ID: usize> (
8 pub T,
9 PhantomData<()>,
10);
11
12impl<T, const ID: usize> TokenWith<T, ID> {
13 /// Creates a new token with this ID.
14 ///
15 /// # Safety:
16 /// Because tokens represent access (mutable or immutable) to a memory location, creating >1
17 /// tokens is equivalent to creating >1 mutable references to data.
18 pub const unsafe fn new(t: T) -> Self {
19 Self(t, PhantomData)
20 }
21
22 pub const fn cell(&self, t: T) -> Cell<T, ID> {
23 Cell::new(t)
24 }
25}
26
27
28/// A Token that represents access to one or more memory locations, each containing the same or
29/// different data types.
30///
31/// Currently, this crate only provides [Cell](crate::cells::Cell), but you may create your own
32/// ownership primitives. See examples/arena.rs for an example.
33pub type Token<const ID: usize> = TokenWith<(), ID>;