Skip to content

Instantly share code, notes, and snippets.

@brson
Created April 17, 2014 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brson/11015838 to your computer and use it in GitHub Desktop.
Save brson/11015838 to your computer and use it in GitHub Desktop.
static mut my_global_data: Option<MyData> = None;
// Do this before launching any threads
fn init() {
unsafe { my_global_data = Some(load()); }
}
// The ref this returns isn't actually safe because it could be mutated away.
// As long is MyData is deeply immutable though there shouldn't otherwise be a data race here.
fn <'a>get_data() -> &'a MyData {
unsafe {
transmute(my_global_data.as_ref().unwrap())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment