Struct std::sync::Weak1.4.0 [] [src]

pub struct Weak<T> where T: ?Sized { /* fields omitted */ }

A weak pointer to an Arc.

Weak pointers will not keep the data inside of the Arc alive, and can be used to break cycles between Arc pointers.

A Weak<T> pointer can be upgraded to an Arc<T> pointer, but will return None if the value has already been dropped.

For example, a tree with parent pointers can be represented by putting the nodes behind strong Arc<T> pointers, and then storing the parent pointers as Weak<T> pointers.

Methods

impl<T> Weak<T>

Constructs a new Weak<T> without an accompanying instance of T.

This allocates memory for T, but does not initialize it. Calling Weak::upgrade() on the return value always gives None.

Examples

fn main() { use std::sync::Weak; let empty: Weak<i64> = Weak::new(); }
use std::sync::Weak;

let empty: Weak<i64> = Weak::new();Run

impl<T> Weak<T> where T: ?Sized

Upgrades a weak reference to a strong reference.

Upgrades the Weak<T> reference to an Arc<T>, if possible.

Returns None if there were no strong references and the data was destroyed.

Examples

fn main() { use std::sync::Arc; let five = Arc::new(5); let weak_five = Arc::downgrade(&five); let strong_five: Option<Arc<_>> = weak_five.upgrade(); }
use std::sync::Arc;

let five = Arc::new(5);

let weak_five = Arc::downgrade(&five);

let strong_five: Option<Arc<_>> = weak_five.upgrade();Run

Trait Implementations

impl<T> Clone for Weak<T> where T: ?Sized

Makes a clone of the Weak<T>.

This increases the weak reference count.

Examples

fn main() { use std::sync::Arc; let weak_five = Arc::downgrade(&Arc::new(5)); weak_five.clone(); }
use std::sync::Arc;

let weak_five = Arc::downgrade(&Arc::new(5));

weak_five.clone();Run

Performs copy-assignment from source. Read more

impl<T> Default for Weak<T>
1.10.0

Constructs a new Weak<T> without an accompanying instance of T.

impl<T> Drop for Weak<T> where T: ?Sized

Drops the Weak<T>.

This will decrement the weak reference count.

Examples

fn main() { use std::sync::Arc; { let five = Arc::new(5); let weak_five = Arc::downgrade(&five); // stuff drop(weak_five); // explicit drop } { let five = Arc::new(5); let weak_five = Arc::downgrade(&five); // stuff } // implicit drop }
use std::sync::Arc;

{
    let five = Arc::new(5);
    let weak_five = Arc::downgrade(&five);

    // stuff

    drop(weak_five); // explicit drop
}
{
    let five = Arc::new(5);
    let weak_five = Arc::downgrade(&five);

    // stuff

} // implicit dropRun

impl<T> Sync for Weak<T> where T: Send + Sync + ?Sized

impl<T> Send for Weak<T> where T: Send + Sync + ?Sized

impl<T, U> CoerceUnsized<Weak<U>> for Weak<T> where T: Unsize<U> + ?Sized, U: ?Sized

impl<T> Debug for Weak<T> where T: Debug + ?Sized

Formats the value using the given formatter.