Created
February 8, 2015 17:12
-
-
Save pzol/e32a4e3a3b73cc3f0a4c to your computer and use it in GitHub Desktop.
This file contains hidden or 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::cell::RefCell; | |
use std::mem; | |
struct Foo { | |
bar: String | |
} | |
thread_local!(static LOOKUP: RefCell<Option<Vec<Foo>>> = RefCell::new(None)); | |
fn lookup<'a>(idx: usize) -> Option<&'static str> { | |
LOOKUP.with(|slot| { | |
match *slot.borrow() { | |
None => panic!("init lookup first"), | |
Some(v) => Some(v[idx].bar.as_slice()) | |
} | |
}) | |
} | |
fn set(vs: Vec<Foo>) { | |
LOOKUP.with(|slot| { | |
*slot.borrow_mut() = Some(vs); | |
}) | |
} | |
fn main() { | |
let vs = vec![Foo { bar: "foobar".to_string() }]; | |
set(vs); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment