pub struct Request<'a>(/* private fields */);
error_generic_member_access
)Expand description
Request
supports generic, type-driven access to data. Its use is currently restricted to the
standard library in cases where trait authors wish to allow trait implementors to share generic
information across trait boundaries. The motivating and prototypical use case is
core::error::Error
which would otherwise require a method per concrete type (eg.
std::backtrace::Backtrace
instance that implementors want to expose to users).
§Data flow
To describe the intended data flow for Request objects, let’s consider two conceptual users separated by API boundaries:
-
Consumer - the consumer requests objects using a Request instance; eg a crate that offers fancy
Error
/Result
reporting to users wants to request a Backtrace from a givendyn Error
. -
Producer - the producer provides objects when requested via Request; eg. a library with an an
Error
implementation that automatically captures backtraces at the time instances are created.
The consumer only needs to know where to submit their request and are expected to handle the
request not being fulfilled by the use of Option<T>
in the responses offered by the producer.
- A Producer initializes the value of one of its fields of a specific type. (or is otherwise
prepared to generate a value requested). eg,
backtrace::Backtrace
orstd::backtrace::Backtrace
- A Consumer requests an object of a specific type (say
std::backtrace::Backtrace
). In the case of adyn Error
trait object (the Producer), there are functions calledrequest_ref
andrequest_value
to simplify obtaining anOption<T>
for a given type. - The Producer, when requested, populates the given Request object which is given as a mutable reference.
- The Consumer extracts a value or reference to t