cgp_type/impls/use_type.rs
1use core::marker::PhantomData;
2
3use cgp_component::{IsProviderFor, WithProvider};
4use cgp_macro::cgp_provider;
5
6use crate::TypeComponent;
7use crate::traits::ProvideType;
8
9/**
10    The `UseType` pattern is used to implement a CGP abstract type with the
11    specified `Type`.
12
13    When a CGP type component is defined using the `#[cgp_type]` macro, a
14    provider implementation of `UseType` is automatically generated.
15    With `UseType`, users can instantiate an abstract type with the specified
16    `Type` without having to manually implement the given provider trait.
17
18    ## Example
19
20    Given the following type component definition:
21
22    ```rust,ignore
23    #[cgp_type]
24    pub trait HasNameType {
25        type Name;
26    }
27    ```
28
29    The following `UseType` implementation would be generated:
30
31    ```rust,ignore
32    impl<Context, Type> NameTypeProvider<Context> for UseType<Type> {
33        type Name = Type;
34    }
35    ```
36*/
37pub struct UseType<Type>(pub PhantomData<Type>);
38
39pub type WithType<Type> = WithProvider<UseType<Type>>;
40
41#[cgp_provider(TypeComponent)]
42impl<Context, Tag, Type> ProvideType<Context, Tag> for UseType<Type> {
43    type Type = Type;
44}