A future that writes a custom value to a shared location for the duration of the poll call, and resets it afterwards. If multiple scoped futures are interlaced or nested, they will each see the value scoped to them.
This capability is provided as the extension trait [ScopeExt] which you will need to import.
use scoped_value::{ScopeExt, WithRef};
use futures::future::join;
use std::cell::RefCell;
// The handle isn't Send, because the strategy we use relies on the stack
thread_local! {
static VAL: RefCell<u32> = RefCell::new(1);
}
// each future sets and resets the value when it's polled to
// ensure that the inner future can only see the correct value.
join(
VAL.async_scope(&mut 2, async {
// ...
VAL.with_ref(|x| assert_eq!(x, &2));
// ...
}),
VAL.async_scope(&mut 3, async {
// ...
VAL.with_ref(|x| assert_eq!(x, &3));
// ...
})
);
This capability is currently implemented for [std::cell::RefCell] and [std::thread::LocalKey], but you can implement it for your own types via the traits [WithRef] and [WithMut]