Skip to main content

WeakPointer

Struct WeakPointer 

Source
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>

Source

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>

Source§

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)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for WeakPointer<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Eq for WeakPointer<T>

Source§

impl<T> PartialEq for WeakPointer<T>

Source§

fn eq(&self, other: &WeakPointer<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.