thread_scoped_ref/helper.rs
1use crate::Scope;
2use std::thread::LocalKey;
3
4/// Execute a function scoped with given value reference.
5///
6/// See [`thread_scoped_ref`] for an example.
7///
8/// [`thread_scoped_ref`]: macro.thread_scoped_ref.html
9#[inline]
10pub fn scoped<T, TFn, TRet>(key: &'static LocalKey<Scope<T>>, value: &T, fun: TFn) -> TRet
11where
12 TFn: FnOnce() -> TRet,
13 T: ?Sized,
14{
15 key.with(|scope| scope.scoped(value, fun))
16}
17
18/// Gets the reference to value from the current scope. Given function will receive
19/// `None` if this is not called within a scope.
20///
21/// See [`thread_scoped_ref`] for an example.
22///
23/// [`thread_scoped_ref`]: macro.thread_scoped_ref.html
24#[inline]
25pub fn with<T, TFn, TRet>(key: &'static LocalKey<Scope<T>>, fun: TFn) -> TRet
26where
27 TFn: FnOnce(Option<&T>) -> TRet,
28 T: ?Sized,
29{
30 key.with(|scope| scope.with(fun))
31}