Skip to main content

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__, Code, Input> AsyncComputer<__Context__, Code, Input> for UseContext
where __Context__: CanComputeAsync<Code, Input>,

Source§

type Output = <__Context__ as CanComputeAsync<Code, Input>>::Output

Source§

fn compute_async( __context__: &__Context__, _code: PhantomData<Code>, input: Input, ) -> impl Future<Output = <UseContext as AsyncComputer<__Context__, Code, Input>>::Output>

Source§

impl<__Context__, Code, Input> AsyncComputerRef<__Context__, Code, Input> for UseContext
where __Context__: CanComputeAsyncRef<Code, Input>,

Source§

type Output = <__Context__ as CanComputeAsyncRef<Code, Input>>::Output

Source§

fn compute_async_ref( __context__: &__Context__, _code: PhantomData<Code>, input: &Input, ) -> impl Future<Output = <UseContext as AsyncComputerRef<__Context__, Code, Input>>::Output>

Source§

impl<__Context__, Code, Input> Computer<__Context__, Code, Input> for UseContext
where __Context__: CanCompute<Code, Input>,

Source§

type Output = <__Context__ as CanCompute<Code, Input>>::Output

Source§

fn compute( __context__: &__Context__, _code: PhantomData<Code>, input: Input, ) -> <UseContext as Computer<__Context__, Code, Input>>::Output

Source§

impl<__Context__, Code, Input> ComputerRef<__Context__, Code, Input> for UseContext
where __Context__: CanComputeRef<Code, Input>,

Source§

type Output = <__Context__ as CanComputeRef<Code, Input>>::Output

Source§

fn compute_ref( __context__: &__Context__, _code: PhantomData<Code>, input: &Input, ) -> <UseContext as ComputerRef<__Context__, Code, Input>>::Output

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__, Code, Input> Handler<__Context__, Code, Input> for UseContext
where __Context__: HasErrorType + CanHandle<Code, Input>,

Source§

type Output = <__Context__ as CanHandle<Code, Input>>::Output

Source§

fn handle( __context__: &__Context__, _tag: PhantomData<Code>, input: Input, ) -> impl Future<Output = Result<<UseContext as Handler<__Context__, Code, Input>>::Output, <__Context__ as HasErrorType>::Error>>

Source§

impl<__Context__, Code, Input> HandlerRef<__Context__, Code, Input> for UseContext
where __Context__: HasErrorType + CanHandleRef<Code, Input>,

Source§

type Output = <__Context__ as CanHandleRef<Code, Input>>::Output

Source§

fn handle_ref( __context__: &__Context__, _tag: PhantomData<Code>, input: &Input, ) -> impl Future<Output = Result<<UseContext as HandlerRef<__Context__, Code, Input>>::Output, <__Context__ as HasErrorType>::Error>>

Source§

impl<__Context__, Code> Producer<__Context__, Code> for UseContext
where __Context__: CanProduce<Code>,

Source§

type Output = <__Context__ as CanProduce<Code>>::Output

Source§

fn produce( __context__: &__Context__, _code: PhantomData<Code>, ) -> <UseContext as Producer<__Context__, Code>>::Output

Source§

impl<__Context__, Code> Runner<__Context__, Code> for UseContext
where __Context__: HasErrorType + CanRun<Code>,

Source§

async fn run( __context__: &__Context__, _code: PhantomData<Code>, ) -> Result<(), <__Context__ as HasErrorType>::Error>

Source§

impl<__Context__> RuntimeGetter<__Context__> for UseContext
where __Context__: HasRuntimeType + HasRuntime,

Source§

fn runtime( __context__: &__Context__, ) -> &<__Context__ as HasRuntimeType>::Runtime

Source§

impl<__Context__> RuntimeTypeProvider<__Context__> for UseContext
where __Context__: HasRuntimeType,

Source§

type Runtime = <__Context__ as HasRuntimeType>::Runtime

Source§

impl<__Context__, Code> SendRunner<__Context__, Code> for UseContext
where __Context__: HasErrorType + CanSendRun<Code>,

Source§

fn send_run( __context__: &__Context__, _code: PhantomData<Code>, ) -> impl Future<Output = Result<(), <__Context__ as HasErrorType>::Error>> + Send

Source§

impl<__Context__, Code, Input> TryComputer<__Context__, Code, Input> for UseContext
where __Context__: HasErrorType + CanTryCompute<Code, Input>,

Source§

type Output = <__Context__ as CanTryCompute<Code, Input>>::Output

Source§

fn try_compute( __context__: &__Context__, _code: PhantomData<Code>, input: Input, ) -> Result<<UseContext as TryComputer<__Context__, Code, Input>>::Output, <__Context__ as HasErrorType>::Error>

Source§

impl<__Context__, Code, Input> TryComputerRef<__Context__, Code, Input> for UseContext
where __Context__: HasErrorType + CanTryComputeRef<Code, Input>,

Source§

type Output = <__Context__ as CanTryComputeRef<Code, Input>>::Output

Source§

fn try_compute_ref( __context__: &__Context__, _code: PhantomData<Code>, input: &Input, ) -> Result<<UseContext as TryComputerRef<__Context__, Code, Input>>::Output, <__Context__ as HasErrorType>::Error>

Source§

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

Source§

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

Source§

impl<__Context__, Code, Input> IsProviderFor<AsyncComputerComponent, __Context__, (Code, Input)> for UseContext
where __Context__: CanComputeAsync<Code, Input>,

Source§

impl<__Context__, Code, Input> IsProviderFor<AsyncComputerRefComponent, __Context__, (Code, Input)> for UseContext
where __Context__: CanComputeAsyncRef<Code, Input>,

Source§

impl<__Context__, Code, Input> IsProviderFor<ComputerComponent, __Context__, (Code, Input)> for UseContext
where __Context__: CanCompute<Code, Input>,

Source§

impl<__Context__, Code, Input> IsProviderFor<ComputerRefComponent, __Context__, (Code, Input)> for UseContext
where __Context__: CanComputeRef<Code, Input>,

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__, Code, Input> IsProviderFor<HandlerComponent, __Context__, (Code, Input)> for UseContext
where __Context__: HasErrorType + CanHandle<Code, Input>,

Source§

impl<__Context__, Code, Input> IsProviderFor<HandlerRefComponent, __Context__, (Code, Input)> for UseContext
where __Context__: HasErrorType + CanHandleRef<Code, Input>,

Source§

impl<__Context__, Code> IsProviderFor<ProducerComponent, __Context__, Code> for UseContext
where __Context__: CanProduce<Code>,

Source§

impl<__Context__, Code> IsProviderFor<RunnerComponent, __Context__, Code> for UseContext
where __Context__: HasErrorType + CanRun<Code>,

Source§

impl<__Context__> IsProviderFor<RuntimeGetterComponent, __Context__> for UseContext
where __Context__: HasRuntimeType + HasRuntime,

Source§

impl<__Context__> IsProviderFor<RuntimeTypeProviderComponent, __Context__> for UseContext
where __Context__: HasRuntimeType,

Source§

impl<__Context__, Code> IsProviderFor<SendRunnerComponent, __Context__, Code> for UseContext
where __Context__: HasErrorType + CanSendRun<Code>,

Source§

impl<__Context__, Code, Input> IsProviderFor<TryComputerComponent, __Context__, (Code, Input)> for UseContext
where __Context__: HasErrorType + CanTryCompute<Code, Input>,

Source§

impl<__Context__, Code, Input> IsProviderFor<TryComputerRefComponent, __Context__, (Code, Input)> for UseContext
where __Context__: HasErrorType + CanTryComputeRef<Code, Input>,

Source§

impl<__Context__, Tag> IsProviderFor<TypeProviderComponent, __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<__Context__, Code, Input> CanCompute<Code, Input> for __Context__
where __Context__: Computer<__Context__, Code, Input>,

Source§

type Output = <__Context__ as Computer<__Context__, Code, Input>>::Output

Source§

fn compute( &self, _code: PhantomData<Code>, input: Input, ) -> <__Context__ as CanCompute<Code, Input>>::Output

Source§

impl<__Context__, Code, Input> CanComputeAsync<Code, Input> for __Context__
where __Context__: AsyncComputer<__Context__, Code, Input>,

Source§

type Output = <__Context__ as AsyncComputer<__Context__, Code, Input>>::Output

Source§

fn compute_async( &self, _code: PhantomData<Code>, input: Input, ) -> impl Future<Output = <__Context__ as CanComputeAsync<Code, Input>>::Output>

Source§

impl<__Context__, Code, Input> CanComputeAsyncRef<Code, Input> for __Context__
where __Context__: AsyncComputerRef<__Context__, Code, Input>,

Source§

type Output = <__Context__ as AsyncComputerRef<__Context__, Code, Input>>::Output

Source§

fn compute_async_ref( &self, _code: PhantomData<Code>, input: &Input, ) -> impl Future<Output = <__Context__ as CanComputeAsyncRef<Code, Input>>::Output>

Source§

impl<__Context__, Code, Input> CanComputeRef<Code, Input> for __Context__
where __Context__: ComputerRef<__Context__, Code, Input>,

Source§

type Output = <__Context__ as ComputerRef<__Context__, Code, Input>>::Output

Source§

fn compute_ref( &self, _code: PhantomData<Code>, input: &Input, ) -> <__Context__ as CanComputeRef<Code, Input>>::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<__Context__, Code, Input> CanHandle<Code, Input> for __Context__
where __Context__: HasErrorType + Handler<__Context__, Code, Input>,

Source§

type Output = <__Context__ as Handler<__Context__, Code, Input>>::Output

Source§

fn handle( &self, _tag: PhantomData<Code>, input: Input, ) -> impl Future<Output = Result<<__Context__ as CanHandle<Code, Input>>::Output, <__Context__ as HasErrorType>::Error>>

Source§

impl<__Context__, Code, Input> CanHandleRef<Code, Input> for __Context__
where __Context__: HasErrorType + HandlerRef<__Context__, Code, Input>,

Source§

type Output = <__Context__ as HandlerRef<__Context__, Code, Input>>::Output

Source§

fn handle_ref( &self, _tag: PhantomData<Code>, input: &Input, ) -> impl Future<Output = Result<<__Context__ as CanHandleRef<Code, Input>>::Output, <__Context__ as HasErrorType>::Error>>

Source§

impl<__Context__, Code> CanProduce<Code> for __Context__
where __Context__: Producer<__Context__, Code>,

Source§

type Output = <__Context__ as Producer<__Context__, Code>>::Output

Source§

fn produce( &self, _code: PhantomData<Code>, ) -> <__Context__ as CanProduce<Code>>::Output

Source§

impl<__Context__, SourceError> CanRaiseError<SourceError> for __Context__
where __Context__: HasErrorType + ErrorRaiser<__Context__, SourceError>,

Source§

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

Source§

impl<__Context__, Code> CanRun<Code> for __Context__
where __Context__: HasErrorType + Runner<__Context__, Code>,

Source§

async fn run( &self, _code: PhantomData<Code>, ) -> Result<(), <__Context__ as HasErrorType>::Error>

Source§

impl<__Context__, Code> CanSendRun<Code> for __Context__
where __Context__: HasErrorType + SendRunner<__Context__, Code>,

Source§

fn send_run( &self, _code: PhantomData<Code>, ) -> impl Future<Output = Result<(), <__Context__ as HasErrorType>::Error>> + Send

Source§

impl<__Context__, Code, Input> CanTryCompute<Code, Input> for __Context__
where __Context__: HasErrorType + TryComputer<__Context__, Code, Input>,

Source§

type Output = <__Context__ as TryComputer<__Context__, Code, Input>>::Output

Source§

fn try_compute( &self, _code: PhantomData<Code>, input: Input, ) -> Result<<__Context__ as CanTryCompute<Code, Input>>::Output, <__Context__ as HasErrorType>::Error>

Source§

impl<__Context__, Code, Input> CanTryComputeRef<Code, Input> for __Context__
where __Context__: HasErrorType + TryComputerRef<__Context__, Code, Input>,

Source§

type Output = <__Context__ as TryComputerRef<__Context__, Code, Input>>::Output

Source§

fn try_compute_ref( &self, _code: PhantomData<Code>, input: &Input, ) -> Result<<__Context__ as CanTryComputeRef<Code, Input>>::Output, <__Context__ as HasErrorType>::Error>

Source§

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

Source§

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

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<__Context__> HasErrorType for __Context__
where __Context__: ErrorTypeProvider<__Context__>,

Source§

type Error = <__Context__ as ErrorTypeProvider<__Context__>>::Error

Source§

impl<__Context__> HasRuntime for __Context__
where __Context__: HasRuntimeType + RuntimeGetter<__Context__>,

Source§

fn runtime(&self) -> &<__Context__ as HasRuntimeType>::Runtime

Source§

impl<__Context__> HasRuntimeType for __Context__
where __Context__: RuntimeTypeProvider<__Context__>,

Source§

type Runtime = <__Context__ as RuntimeTypeProvider<__Context__>>::Runtime

Source§

impl<__Context__, Tag> HasType<Tag> for __Context__
where __Context__: TypeProvider<__Context__, Tag>,

Source§

type Type = <__Context__ as TypeProvider<__Context__, Tag>>::Type

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.