weak_static 0.1.1

A macro for declaring lazy droppable statics.
Documentation
  • Coverage
  • 50%
    1 out of 2 items documented1 out of 1 items with examples
  • Size
  • Source code size: 3.11 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • panicbit/weak_static
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • panicbit

This crate provides a macro to create a static value that gets created when needed and dropped as soon as its not needed anymore. It requires the lazy_static macro to be imported.

Example

#[macro_use] extern crate lazy_static;
#[macro_use] extern crate weak_static;

struct Foo;

impl Foo {
    fn new() -> Self {
        println!("new");
        Foo
    }
}

impl Drop for Foo {
    fn drop(&mut self) {
        println!("drop");
    }
}

weak_static! {
    static FOO: Foo = Foo::new();
}

fn main() {
    {
        let _foo1 = FOO();
        let _foo2 = FOO();
        let _foo3 = FOO();
    }
    
    {
        let _foo4 = FOO();
        let _foo5 = FOO();
        let _foo6 = FOO();
    }
}

Outputs:

new
drop
new
drop

The new prints corresponds to the FOO() calls of _foo1 and _foo4. The drop prints correspond to the last FOO reference being dropped.