#pointers #recursion #weak #foo #create #structure #struct

weak-self

WeakSelf is simple way to have a Weak pointer to yourself

3 stable releases

1.0.2 Feb 17, 2020
1.0.1 Mar 30, 2019
1.0.0 Mar 28, 2019

#988 in Data structures

Download history 3/week @ 2024-01-09 3/week @ 2024-01-16 1/week @ 2024-02-06 13/week @ 2024-02-13 24/week @ 2024-02-20 40/week @ 2024-02-27 6/week @ 2024-03-05 18/week @ 2024-03-12

89 downloads per month

MIT/Apache

7KB

WeakSelf

Build Status License Cargo Documentation

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:

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.

No runtime deps