pub struct TokenRefCell<T: ?Sized, Tk: Token + ?Sized = BoxToken> { /* private fields */ }
Expand description
Implementations§
Source§impl<T, Tk: Token + ?Sized> TokenRefCell<T, Tk>
impl<T, Tk: Token + ?Sized> TokenRefCell<T, Tk>
Sourcepub fn new(value: T, token: &Tk) -> Self
pub fn new(value: T, token: &Tk) -> Self
Creates a new TokenRefCell
containing a value
, synchronized by the given token
.
Sourcepub fn new_const(value: T) -> Self
pub fn new_const(value: T) -> Self
Creates a new TokenRefCell
containing a value
, synchronized by a const token.
Sourcepub fn with_token_id(value: T, token_id: Tk::Id) -> Self
pub fn with_token_id(value: T, token_id: Tk::Id) -> Self
Creates a new TokenRefCell
containing a value
, synchronized by a token with the given id.
It allows creating TokenRefCell
when token is already borrowed.
Source§impl<T: ?Sized, Tk: Token + ?Sized> TokenRefCell<T, Tk>
impl<T: ?Sized, Tk: Token + ?Sized> TokenRefCell<T, Tk>
Sourcepub fn borrow<'a>(&'a self, token: &'a Tk) -> Ref<'a, T, Tk>
pub fn borrow<'a>(&'a self, token: &'a Tk) -> Ref<'a, T, Tk>
Immutably borrows the wrapped value using a shared reference to the token.
The token reference is reborrowed in the returned Ref
, preventing any aliasing
mutable borrow. Multiple immutable borrows can be taken out at the same time.
The check runtime cost is only a token id comparison (when the token is not a
singleton_token!
); there is no write, contrary to RefCell
or locks.
§Panics
Panics if the token doesn’t match the one used at cell initialization (or set with
set_token
). For a non-panicking variant, use
try_borrow
.
Sourcepub fn try_borrow<'a>(
&'a self,
token: &'a Tk,
) -> Result<Ref<'a, T, Tk>, BorrowError>
pub fn try_borrow<'a>( &'a self, token: &'a Tk, ) -> Result<Ref<'a, T, Tk>, BorrowError>
Tries to immutably borrow the wrapped value using a shared reference to the token.
Returns an error if the token doesn’t match the one used at cell initialization
(or set with set_token
). This is the non-panicking variant of
borrow
.
The token reference is reborrowed in the returned Ref
, preventing any aliasing
mutable borrow. Multiple immutable borrows can be taken out at the same time.
The check runtime cost is only a token id comparison (when the token is not a
singleton_token!
); there is no write, contrary to RefCell
or locks.
Sourcepub fn borrow_mut<'a>(&'a self, token: &'a mut Tk) -> RefMut<'a, T, Tk>
pub fn borrow_mut<'a>(&'a self, token: &'a mut Tk) -> RefMut<'a, T, Tk>
Mutably borrows the wrapped value using an exclusive reference to the token.
The token reference is reborrowed in the returned RefMut
, preventing any aliasing
mutable/immutable borrow.
The check runtime cost is only a token id comparison (when the token is not a
singleton_token!
), as well as a token unicity check (mostly a noop for most
of the token implementations); there is no write, contrary to
RefCell
or locks.
§Panics
Panics if the token doesn’t match the one used at cell initialization (or set with
set_token
), or if the token is not unique.
For a non-panicking variant, use try_borrow
.
Examples found in repository?
40 pub fn remove_node(&mut self, id: NodeId) -> bool {
41 let Some(node) = self.nodes.remove(&id) else {
42 return false;
43 };
44 let mut node_mut = node.borrow_mut(&mut self.token);
45 let mut reborrow_mut =
46 node_mut.reborrow_stateful_mut(|node| node.edges.values().map(AsRef::as_ref));
47 while let Some(mut other) = reborrow_mut.reborrow_opt_mut(|edges| edges.next()) {
48 other.edges.remove(&id);
49 }
50 true
51 }
52
53 pub fn insert_edge(&mut self, node1: NodeId, node2: NodeId) {
54 let mut node = |id| {
55 let entry = self.nodes.entry(id);
56 let node = entry.or_insert_with(|| Node::new_cell(node1, &self.token));
57 node.clone()
58 };
59 let node_cell1 = node(node1);
60 let node_cell2 = node(node2);
61 node_cell1
62 .borrow_mut(&mut self.token)
63 .edges
64 .insert(node2, node_cell2.clone());
65 node_cell2
66 .borrow_mut(&mut self.token)
67 .edges
68 .insert(node1, node_cell1);
69 }
70
71 pub fn remove_edge(&mut self, node1: NodeId, node2: NodeId) {
72 let mut remove = |a, b| {
73 let Some(node) = self.nodes.get(&a) else {
74 return false;
75 };
76 node.borrow_mut(&mut self.token).edges.remove(&b);
77 true
78 };
79 if remove(node1, node2) {
80 remove(node2, node1);
81 }
82 }
Sourcepub fn try_borrow_mut<'a>(
&'a self,
token: &'a mut Tk,
) -> Result<RefMut<'a, T, Tk>, BorrowMutError>
pub fn try_borrow_mut<'a>( &'a self, token: &'a mut Tk, ) -> Result<RefMut<'a, T, Tk>, BorrowMutError>
Mutably borrows the wrapped value using an exclusive reference to the token.
Returns an error if the token doesn’t match the one used at cell initialization
(or set with set_token
), or if the token is not
unique. For a non-panicking variant, use
try_borrow
.
The token reference is reborrowed in the returned RefMut
, preventing any aliasing
mutable/immutable borrow.
The check runtime cost is only a token id comparison (when the token is not a
singleton_token!
), as well as a token unicity check (mostly a noop for most
of the token implementations); there is no write, contrary to
RefCell
or locks.
Sourcepub fn set_token_id(&mut self, token_id: Tk::Id)
pub fn set_token_id(&mut self, token_id: Tk::Id)
Set a new token_id to synchronize the cell.
Source§impl<T, Tk: Token<Id = ()> + ?Sized> TokenRefCell<[T], Tk>
impl<T, Tk: Token<Id = ()> + ?Sized> TokenRefCell<[T], Tk>
Sourcepub fn as_slice_of_cells(&self) -> &[TokenRefCell<T, Tk>]
pub fn as_slice_of_cells(&self) -> &[TokenRefCell<T, Tk>]
Returns a &[TokenRefCell<T>]
from a &TokenRefCell<[T]>