[][src]Struct weak_self::WeakSelf

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

WeakSelf is simple way to have a Weak pointer inside a data structure pointing to itself.

Use Case

Sometimes you want to create a struct with a pointer to itself or just some other recursive data structure.

This example is not tested
 struct Foo {
     me: &Foo
 }

 impl Foo {
     pub fn new() -> Foo {
         let foo = Foo{
             me: ????
         };
         foo
     }
 }

This create helps you do that:

 use weak_self::WeakSelf;
 use std::sync::{Arc, Weak};
 pub struct Foo {
     weak_self: WeakSelf<Foo>
 }

 impl Foo {
     pub fn new() -> Arc<Foo> {
         let foo = Arc::new(Foo{
             weak_self: WeakSelf::new()
         });
         foo.weak_self.init(&foo);
         foo
     }

     fn weak(&self) -> Weak<Self> {
         self.weak_self.get()
     }
 }

Dependencies

This package depends on std only

Usage

To use WeakSelf, add this to your Cargo.toml:

[dependencies]
weakself = "1.0.1"

License

Licensed under the terms of MIT license and the Apache License (Version 2.0).

See LICENSE-MIT and LICENSE-APACHE for details.

Methods

impl<T: ?Sized> WeakSelf<T>[src]

pub fn new() -> WeakSelf<T>[src]

Constructs a new empty WeakSelf

pub fn init(&self, content: &Arc<T>)[src]

Initialize the WeakSelf with an Arc.

Note: content must point be the only existing Arc, otherwise this method will panig

pub fn try_get(&self) -> Option<&Weak<T>>[src]

get Some Weak pointer to the content, or None if not yet initialized

pub fn get(&self) -> Weak<T>[src]

get a Weak pointer to the content, or panic if not yet initialized

Trait Implementations

impl<T: ?Sized> Default for WeakSelf<T>[src]

impl<T: ?Sized + Sync + Send> Sync for WeakSelf<T>[src]

impl<T: ?Sized + Sync + Send> Send for WeakSelf<T>[src]

impl<T: ?Sized + Debug> Debug for WeakSelf<T>[src]

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> From for T[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.