Struct WeakSelf

Source
pub struct WeakSelf<T: ?Sized> { /* private fields */ }
Expand description

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.

 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.2"

§License

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

See LICENSE-MIT and LICENSE-APACHE for details.

Implementations§

Source§

impl<T: ?Sized> WeakSelf<T>

Source

pub fn new() -> WeakSelf<T>

Constructs a new empty WeakSelf

Source

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

Initialize the WeakSelf with an Arc.

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

Source

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

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

Source

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

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

Trait Implementations§

Source§

impl<T: ?Sized + Debug> Debug for WeakSelf<T>

Source§

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

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

impl<T: ?Sized> Default for WeakSelf<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

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

Source§

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

Auto Trait Implementations§

§

impl<T> !Freeze for WeakSelf<T>

§

impl<T> !RefUnwindSafe for WeakSelf<T>

§

impl<T> Unpin for WeakSelf<T>
where T: ?Sized,

§

impl<T> UnwindSafe for WeakSelf<T>
where T: RefUnwindSafe + ?Sized,

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> 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, 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.