pub trait Generatable<'a, Assert>where
Self: TidAble<'a>,
Assert: 'a + TidAble<'a>,{
type GeneratableData: 'a;
type TemplateData: 'a;
// Required methods
fn generatable(
context: &mut Context<'_>
) -> PassedData<Self::GeneratableData>
where Self: Sized;
fn template(
&self,
context: &mut Context<'_>,
passed: &Self::GeneratableData
) -> PassedData<Self::TemplateData>;
fn assert(
&self,
_context: &mut Context<'_>,
_passed: (&Self::GeneratableData, &Self::TemplateData),
_to_assert: &Assert
) -> Option<TokenStream>;
}
Expand description
This indicates that some type can be turned into tokens that assert something. Be aware that this only relies on rust’s type checking, the code that is generated is never actually meant to be executed. The generation functions in three distinct “Stages”. See below for more information on that.
The type implementing this must also be TidAble
, which is similar to Any
but allows for non-’static lifetimes. The easiest way to implement it is to use the derive macro (#[derive(Tid)]
)
Idents
There is a need to ensure that there are no ident collisions among the generated code.
For that purpose in each stage there is a context provided, which contains some IdentGenerator
.
Other than Idents passed into the methods by either Self
or any parameters, use Idents generates by the IdentGenerator
exclusively to avoid any collisions.
Generation “Stages”
Stage 1: Generatable
These tokens are generated for each type that implemets this trait once. This is the perfect place to put tokens that are needed as “Setup” for any asserts.
Stage 2: Template
These tokens are generated for each unique instance of Self
. You should put any tokens that should be unique
for each instance in here.
Stage 3: Assert
These tokens are generated for each unique Assert, which represent some type that is tested against Self
.
No generation is actually happening until at least one assert is created for Self
.
Passed Data
There may be a need to pass data, for example generated idents, between stages. For that purpose
this trait provides a way to do so. See PassedData
for more information.
Required Associated Types§
sourcetype GeneratableData: 'a
type GeneratableData: 'a
The type of any data passed from the Generatable
stage to proceeding stages.
sourcetype TemplateData: 'a
type TemplateData: 'a
The type of any data passed from the Template
stage to proceeding stages.
Required Methods§
sourcefn generatable(context: &mut Context<'_>) -> PassedData<Self::GeneratableData>where
Self: Sized,
fn generatable(context: &mut Context<'_>) -> PassedData<Self::GeneratableData>where Self: Sized,
This is the method the Generatable
stage is composed of. Should return Tokens (if any) generated by this stage,
plus optionally any data passed to preceding stages.
sourcefn template(
&self,
context: &mut Context<'_>,
passed: &Self::GeneratableData
) -> PassedData<Self::TemplateData>
fn template( &self, context: &mut Context<'_>, passed: &Self::GeneratableData ) -> PassedData<Self::TemplateData>
This is the method the Template
stage is composed of. Should return Tokens (if any) generated by this stage,
plus optionally any data passed to preceding stages.
sourcefn assert(
&self,
_context: &mut Context<'_>,
_passed: (&Self::GeneratableData, &Self::TemplateData),
_to_assert: &Assert
) -> Option<TokenStream>
fn assert( &self, _context: &mut Context<'_>, _passed: (&Self::GeneratableData, &Self::TemplateData), _to_assert: &Assert ) -> Option<TokenStream>
This is the method the Assert
stage is composed of. Should return Tokens generated by this stage.