Skip to content

Instantly share code, notes, and snippets.

@kmcallister
Last active August 29, 2015 13:57
Show Gist options
  • Save kmcallister/9356218 to your computer and use it in GitHub Desktop.
Save kmcallister/9356218 to your computer and use it in GitHub Desktop.
faking global, constant-after-init structs
use std::cast;
struct FooStatics {
x: int,
}
static mut globalFooStatics: *FooStatics = 0 as *FooStatics;
impl FooStatics {
unsafe fn init() {
let global = ~FooStatics {
x: 2 + 2, // initialization value is computed at runtime
};
globalFooStatics = cast::transmute(global);
}
fn get() -> &'static FooStatics {
unsafe {
cast::transmute(globalFooStatics)
}
}
}
fn main() {
unsafe { FooStatics::init(); }
println!("{:d}", FooStatics::get().x);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment