Struct Request
struct Request<'a>(_)
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/Resultreporting 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
Errorimplementation 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::Backtraceorstd::backtrace::Backtrace - A Consumer requests an object of a specific type (say
std::backtrace::Backtrace). In the case of adyn Errortrait object (the Producer), there are functions calledrequest_refandrequest_valueto 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 the requested type from the
Requestobject wrapped in anOption<T>; in the case ofdyn Errorthe aforementionedrequest_refandrequest_valuemethods mean thatdyn Errorusers don't have to deal with theRequesttype at all (butErrorimplementors do). TheNonecase of theOptionsuggests only that the Producer cannot currently offer an instance of the requested type, not it can't or never will.
Examples
The best way to demonstrate this is using an example implementation of Error's provide trait
method:
use fmt;
use Request;
use request_ref;
Implementations
impl<'a> Request<'a>
fn provide_value<T>(self: &mut Self, value: T) -> &mut Self where T: 'staticProvides a value or other type with only static lifetimes.
Examples
Provides an
u8.use Request;fn provide_value_with<T, impl FnOnce() -> T: FnOnce() -> T>(self: &mut Self, fulfil: impl FnOnce() -> T) -> &mut Self where T: 'staticProvides a value or other type with only static lifetimes computed using a closure.
Examples
Provides a
Stringby cloning.use Request;fn provide_ref<T: ?Sized + 'static>(self: &mut Self, value: &'a T) -> &mut SelfProvides a reference. The referee type must be bounded by
'static, but may be unsized.Examples
Provides a reference to a field as a
&str.use Request;fn provide_ref_with<T: ?Sized + 'static, impl FnOnce() -> &'a T: FnOnce() -> &'a T>(self: &mut Self, fulfil: impl FnOnce() -> &'a T) -> &mut SelfProvides a reference computed using a closure. The referee type must be bounded by
'static, but may be unsized.Examples
Provides a reference to a field as a
&str.use Request;fn would_be_satisfied_by_value_of<T>(self: &Self) -> bool where T: 'staticChecks if the
Requestwould be satisfied if provided with a value of the specified type. If the type does not match or has already been provided, returns false.Examples
Checks if a
u8still needs to be provided and then provides it.use Request; use request_value; ; let parent = Parent; let child = Child ; assert_eq!; let parent = Parent; let child = Child ; assert_eq!;fn would_be_satisfied_by_ref_of<T>(self: &Self) -> bool where T: ?Sized + 'staticChecks if the
Requestwould be satisfied if provided with a reference to a value of the specified type.If the type does not match or has already been provided, returns false.
Examples
Checks if a
&strstill needs to be provided and then provides it.use Request; use request_ref; ; let parent = Parent; let child = Child ; assert_eq!; let parent = Parent; let child = Child ; assert_eq!;
impl<'a> Debug for Request<'a>
fn fmt(self: &Self, f: &mut Formatter<'_>) -> fmt::Result
impl<'a> Freeze for Request<'a>
impl<'a> RefUnwindSafe for Request<'a>
impl<'a> Send for Request<'a>
impl<'a> Sized for Request<'a>
impl<'a> Sync for Request<'a>
impl<'a> Unpin for Request<'a>
impl<'a> UnwindSafe for Request<'a>
impl<T> Any for Request<'a>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for Request<'a>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for Request<'a>
fn borrow_mut(self: &mut Self) -> &mut T