global-ref 0.1.0

A crate to share references between functions through statics.
Documentation
  • Coverage
  • 94.12%
    16 out of 17 items documented0 out of 16 items with examples
  • Size
  • Source code size: 8.91 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 378.25 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 7s Average build duration of successful builds.
  • all releases: 7s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • qt2/global-ref
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • qt2

global-ref

Overview

This crate is used to share references between functions through statics.

Because the implementation internally converts raw pointers to usize and shares them between threads, fetching references is essentially unsafe. If you use this crate, please verify its safety before using it.

Examples

use std::thread;
use global_ref::GlobalMut;

fn main() {
    static GLOBAL: GlobalMut<i32> = GlobalMut::new();

    let mut content = 0;

    GLOBAL.with(&mut content, || {
        fn add_one() {
            *GLOBAL.get_mut() += 1;
        }

        let handle = thread::spawn(add_one);
        handle.join().unwrap();
        assert_eq!(*GLOBAL.get(), 1);
    });

    assert!(GLOBAL.try_get().is_none());
}

License

MIT