macro_rules! let_ref_thread_stack_value {
($new_variable:ident, $thread_stack:expr) => { ... };
}
Expand description
Create a local reference to the value at the top of the
threadstack. Even though the top value may have been pushed at a
much higher layer in the call stack, the reference has a
conservative lifetime to guarantee safety – the same lifetime as
a local variable created on the stack where the macro is invoked.
If you don’t want to have to worry about lifetimes consider using
clone_thread_stack_value
instead.
Note that this can panic, but only if you did not provide an initial value when you declared your thread stack.
use threadstack::*;
declare_thread_stacks!(
FOO: String = String::from("hello world");
);
let_ref_thread_stack_value!(my_reference, FOO);
assert!(my_reference == "hello world");
{
push_thread_stack_value!("hello universe".into(), FOO);
let_ref_thread_stack_value!(my_other_reference, FOO);
assert!(my_other_reference == "hello universe");
}
assert!(my_reference == "hello world");
push_thread_stack_value!("hello galaxy".into(), FOO);
assert!(my_reference == "hello world"); // still is reference to old value!
let_ref_thread_stack_value!(my_reference, FOO); // shadows the old reference
assert!(my_reference == "hello galaxy");