pub struct UseContext;Expand description
The UseContext pattern is used to define a trivial implementation of
a provider trait, by forwarding the implementation to the consumer trait
implementation of the context.
This pattern is the dual of the blanket implementation of a consumer trait,
which forwards the implementation to its provider through the CgpProvider trait.
The main use case for UseContext is to be used as a higher-order provider
in the argument to a different provider implementation. This decouples the
dependencies between different CGP traits, and allows non-default providers
to be used within another provider implementation.
For obvious reasons, UseContext should not be used as the delegation target
from the context provider, as it would result in a cyclic dependency error.
The UseContext provider is automatically implemented for all CPG traits that
are generated from #[cgp_component].
§Example
Given the following component definition:
#[cgp_component(Greeter)]
pub trait CanGreet {
fn greet(&self);
}The following UseContext implementation would be generated:
impl<Context> Greeter<Context> for UseContext
where
Context: CanGreet,
{
fn greet(context: &Context) {
context.greet()
}
}Trait Implementations§
Source§impl<__Context__, SourceError> ErrorRaiser<__Context__, SourceError> for UseContextwhere
__Context__: HasErrorType + CanRaiseError<SourceError>,
The CanRaiseError trait is used to raise any concrete error type into
an abstract error provided by HasErrorType.
impl<__Context__, SourceError> ErrorRaiser<__Context__, SourceError> for UseContextwhere
__Context__: HasErrorType + CanRaiseError<SourceError>,
The CanRaiseError trait is used to raise any concrete error type into
an abstract error provided by HasErrorType.
fn raise_error(error: SourceError) -> <__Context__ as HasErrorType>::Error
Source§impl<__Context__> ErrorTypeProvider<__Context__> for UseContextwhere
__Context__: HasErrorType,
The HasErrorType trait provides an abstract error type that can be used by
CGP components to decouple the code from any concrete error implementation.
impl<__Context__> ErrorTypeProvider<__Context__> for UseContextwhere
__Context__: HasErrorType,
The HasErrorType trait provides an abstract error type that can be used by
CGP components to decouple the code from any concrete error implementation.
Although it is possible for each context to declare their own associated
Error type, doing so may result in having multiple ambiguous Self::Error types,
if there are multiple associated types with the same name in different traits.
As a result, it is better for context traits to include HasError as their
parent traits, so that multiple traits can all refer to the same abstract
Self::Error type.
The Error associated type is also required to implement Debug.
This is to allow Self::Error to be used in calls like .unwrap(),
as well as for simpler error logging.
More details about how to use HasErrorType is available at
https://patterns.contextgeneric.dev/error-handling.html