pub struct WeakPointer<T> { /* private fields */ }Expand description
Weak variant of Pointer.
WeakPointers are used to avoid deadlocking when dropping structures with cycled references to each other.
In the following example Storage will stand in memory even after going out of scope, because there is cyclic referencing between Nodes
struct Node {
next: Option<Pointer<Node>>,
}
let ptr1 = storage.create(Node { next: None });
let ptr2 = storage.create(Node { next: Some(ptr1.clone()) });
storage[&ptr1].next = Some(ptr2.clone());To avoid such situations, just replace Option<Pointer<Node>> with Option<WeakPointer<Node>>
§Example
let pointer = storage.create(1i32);
// create WeakPointer to this component
let weak = pointer.downgrade();You will need to upgrade WeakPointer to access component in storage
let pointer = weak.upgrade()?;
storage[&pointer] = 20;Implementations§
Source§impl<T> WeakPointer<T>
impl<T> WeakPointer<T>
Sourcepub fn upgrade(&self) -> Result<Pointer<T>, DeadComponentError>
pub fn upgrade(&self) -> Result<Pointer<T>, DeadComponentError>
Upgrades the WeakPointer to a Pointer, if possible.
§Errors
Returns DeadComponentError if the related component in storage was destroyed.
Examples found in repository?
examples/weak.rs (line 31)
15fn main() {
16 let mut storage = Storage::new();
17 let node1 = storage.create(Node {
18 value: "Node 1".to_string(),
19 next: None,
20 });
21 let node2 = storage.create(Node {
22 value: "Node 2".to_string(),
23 next: None,
24 });
25
26 storage[&node1].next = Some(node2.downgrade());
27 storage[&node2].next = Some(node1.downgrade());
28
29 for node in &storage {
30 let value = node.next.as_ref().map_or("None".into(), |next| {
31 let ptr = next.upgrade().unwrap();
32 storage[&ptr].value.clone()
33 });
34 println!("{} has `next` field with value {}", node.value, value);
35 }
36}Trait Implementations§
Source§impl<T> Clone for WeakPointer<T>
impl<T> Clone for WeakPointer<T>
Source§fn clone(&self) -> WeakPointer<T>
fn clone(&self) -> WeakPointer<T>
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<T: Debug> Debug for WeakPointer<T>
impl<T: Debug> Debug for WeakPointer<T>
impl<T> Eq for WeakPointer<T>
Source§impl<T> PartialEq for WeakPointer<T>
impl<T> PartialEq for WeakPointer<T>
Source§fn eq(&self, other: &WeakPointer<T>) -> bool
fn eq(&self, other: &WeakPointer<T>) -> bool
Tests for
self and other values to be equal, and is used by ==.Auto Trait Implementations§
impl<T> !RefUnwindSafe for WeakPointer<T>
impl<T> !UnwindSafe for WeakPointer<T>
impl<T> Freeze for WeakPointer<T>
impl<T> Send for WeakPointer<T>where
T: Send,
impl<T> Sync for WeakPointer<T>where
T: Sync,
impl<T> Unpin for WeakPointer<T>where
T: Unpin,
impl<T> UnsafeUnpin for WeakPointer<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more