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>
impl<T: ?Sized> WeakSelf<T>
Trait Implementations§
impl<T: ?Sized + Sync + Send> Send for WeakSelf<T>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more