Skip to content
/ global-ref Public

A crate to share references between functions through statics.

License

Notifications You must be signed in to change notification settings

qt2/global-ref

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

global-ref

Crates.io API reference

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

About

A crate to share references between functions through statics.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages