context-rs
Go has a history of suggesting you provide a ctx context.Context
parameter to
all functions in an async context, such as web servers. This is useful for passing
deadlines and such down into the callstack, to allow leaf-functions to schedule shutdowns.
Rust already passes a context value automatically for you in all async functions,
this is already named Context
but it's heavily under-featured - only providing a 'wake-up' handle.
Making use of the nightly Provider API
,
we can modify this context to provide values on demand down the callstack. This avoids
using thread_locals, which requires std
, or passing through a TypeMap
with every
function call, which is unergonomic and requires alloc
.
Examples
A demonstration of an async deadline, using get_value
and provide_ref
use ;
use ;
// New type makes it easier to have unique keys in the context
;
;
// some top level work - agnostic to the context
async
// some deeply nested work, cares about the deadline context
async
async
If you only need to access the value temporarily, and the value you want
is expensive to clone, you can use with_ref
instead of get_value
.
This will accept a closure with the ref provided for a short lived lifetime.