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()
    }
}

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<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.