Skip to content

Instantly share code, notes, and snippets.

@pzol
Created February 8, 2015 17:12
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 pzol/e32a4e3a3b73cc3f0a4c to your computer and use it in GitHub Desktop.
Save pzol/e32a4e3a3b73cc3f0a4c to your computer and use it in GitHub Desktop.
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