Provider-style API for Context
#552
Labels
api-change-proposal
A proposal to add or alter unstable APIs in the standard libraries
I-async-nominated
T-libs-api
Proposal
Changelog
Problem statement
Currently,
Context
only carries aWaker
, but there is interest in having it carry other kinds of data. There is also a general practice in the ecosystem of sharing data between executors and futures via thread-locals or globals that would arguably be better shared viaContext
, if it were possible.Motivating examples or use cases
Use-cases for sharing more kinds of data via
Context
include LocalWaker, Context reactor hook ACP, and context_rs crate.Solution sketch
To solve this, we can change
Context
to carry arbitrary extension data via an API similar to the one used byerror_generic_member_access
. At first it could reuseRequest
and other types directly fromcore::error
, but it could be good to move those things to a common area if both the error & context usages are stabilized.Basic usage:
Inheritance
Combinators that manage their own wakers will want to ensure the provider from an earlier context is carried over into any new contexts. For this, the
ContextBuilder::from()
method fromcontext_ext
can be used.Overriding extensions
Not only do we want a way for
Context
to carry multiple extensions, which the provider-style API solves, we also want a way for extensions to be added/overridden later on. For example, an executor may want to expose "reactor" & "spawner" interfaces from the top level, but then deeper in the call stack a "nursery" may want to override just the spawner (while allowing the reactor to still be accessible), and yet some other code path may want to introduce a "budgeter" for fair/weighted processing of some subset of work. These effects are achieved by having the context forward the provider calls to the parent provider after usingContextBuilder::from()
.Example overriding a "spawner":
Alternatives
This proposal addresses some issues with
context_ext
:context_ext
was intended to be stabilized without knowing what the most desirable data structure/mechanism for managing multiple extensions might be, in order to enable the ecosystem figure it out. Instead, this provider-style API is intended to be the desirable mechanism from the get-go. It supports returning multiple types, and overriding is possible by forwarding calls.Links and related work
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
Second, if there's a concrete solution:
The text was updated successfully, but these errors were encountered: