Last active
August 29, 2015 13:57
-
-
Save kmcallister/9356218 to your computer and use it in GitHub Desktop.
faking global, constant-after-init structs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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