Skip to content

WeakSelf is a way to create self-referencing data structures in Rust

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

eun-ice/weak-self

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

WeakSelf is a way to create self-referencing data structures in Rust

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages