[−][src]Struct weak_self::WeakSelf
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.
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
Note: content must point be the only existing Arc, otherwise this method will panic
pub fn try_get(&self) -> Option<&Weak<T>>
[src]
get Some Weak
pub fn get(&self) -> Weak<T>
[src]
get a Weak
Trait Implementations
impl<T: ?Sized + Debug> Debug for WeakSelf<T>
[src]
impl<T: ?Sized> Default for WeakSelf<T>
[src]
impl<T: ?Sized + Sync + Send> Send for WeakSelf<T>
[src]
impl<T: ?Sized + Sync + Send> Sync for WeakSelf<T>
[src]
Auto Trait Implementations
impl<T> !RefUnwindSafe for WeakSelf<T>
impl<T: ?Sized> Unpin for WeakSelf<T>
impl<T: ?Sized> UnwindSafe for WeakSelf<T> where
T: RefUnwindSafe,
T: RefUnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,