[−][src]Macro thread_scoped_ref::thread_scoped_ref
A shortcut macro for thread_local! { static IDENTIFIER : Scope<Type> = Scope::default() }
.
See also
Examples
With a struct:
use thread_scoped_ref::{thread_scoped_ref, scoped, with}; struct MyStruct(String); thread_scoped_ref!(MY_STRUCT, MyStruct); // use it: let demo_struct = MyStruct("Hello".to_string()); scoped(&MY_STRUCT, &demo_struct, || { with(&MY_STRUCT, |maybe_my_struct_ref| { assert_eq!("Hello", maybe_my_struct_ref.unwrap().0); }) })
With a trait / dynamic dispatch (note the dyn
):
use thread_scoped_ref::{thread_scoped_ref, scoped, with}; trait MyTrait { fn string(&self) -> &str; } struct StructImplementingMyTrait(String); impl MyTrait for StructImplementingMyTrait { fn string(&self) -> &str { &self.0 } } thread_scoped_ref!(MY_TRAIT, dyn MyTrait); // use it: let my_struct = StructImplementingMyTrait("Hello World".to_string()); scoped(&MY_TRAIT, &my_struct, || { with(&MY_TRAIT, |maybe_my_trait_ref| { assert_eq!("Hello World", maybe_my_trait_ref.unwrap().string()); }) })