UseContext

Struct UseContext 

Source
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 UseContext
where __Context__: HasErrorType + CanRaiseError<SourceError>,

The CanRaiseError trait is used to raise any concrete error type into an abstract error provided by HasErrorType.

Source§

fn raise_error(error: SourceError) -> <__Context__ as HasErrorType>::Error

Source§

impl<__Context__> ErrorTypeProvider<__Context__> for UseContext
where __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

Source§

type Error = <__Context__ as HasErrorType>::Error

Source§

impl<__Context__, Detail> ErrorWrapper<__Context__, Detail> for UseContext
where __Context__: HasErrorType + CanWrapError<Detail>,

Source§

fn wrap_error( error: <__Context__ as HasErrorType>::Error, detail: Detail, ) -> <__Context__ as HasErrorType>::Error

Source§

impl<Context, Tag, Field> FieldGetter<Context, Tag> for UseContext
where Context: HasField<Tag, Value = Field>,

Source§

type Value = Field

Source§

fn get_field( context: &Context, _tag: PhantomData<Tag>, ) -> &<UseContext as FieldGetter<Context, Tag>>::Value

Source§

impl<__Context__, Tag> ProvideType<__Context__, Tag> for UseContext
where __Context__: HasType<Tag>,

Source§

type Type = <__Context__ as HasType<Tag>>::Type

Source§

impl<__Context__, SourceError> IsProviderFor<ErrorRaiserComponent, __Context__, SourceError> for UseContext
where __Context__: HasErrorType + CanRaiseError<SourceError>,

Source§

impl<__Context__> IsProviderFor<ErrorTypeProviderComponent, __Context__> for UseContext
where __Context__: HasErrorType,

Source§

impl<__Context__, Detail> IsProviderFor<ErrorWrapperComponent, __Context__, Detail> for UseContext
where __Context__: HasErrorType + CanWrapError<Detail>,

Source§

impl<__Context__, Tag> IsProviderFor<TypeComponent, __Context__, Tag> for UseContext
where __Context__: HasType<Tag>,

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<Builder, Source, Output> CanBuildFrom<Source> for Builder
where Source: HasFields + IntoBuilder, <Source as HasFields>::Fields: FieldsBuilder<<Source as IntoBuilder>::Builder, Builder, Output = Output>,

Source§

type Output = Output

Source§

fn build_from(self, source: Source) -> Output

Source§

impl<Source, Target, Remainder> CanDowncastFields<Target> for Source
where Target: HasFields, <Target as HasFields>::Fields: FieldsExtractor<Source, Target, Remainder = Remainder>,

Source§

type Remainder = Remainder

Source§

fn downcast_fields( self, _tag: PhantomData<Target>, ) -> Result<Target, <Source as CanDowncastFields<Target>>::Remainder>

Source§

impl<Getter, Context, Tag> FieldMapper<Context, Tag> for Getter
where Getter: FieldGetter<Context, Tag> + 'static, Tag: 'static,

Source§

fn map_field<T>( context: &Context, tag: PhantomData<Tag>, mapper: impl for<'a> FnOnce(&'a <Getter as FieldGetter<Context, Tag>>::Value) -> &'a T, ) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.