pub struct Generator { /* private fields */ }Expand description
The generator is used to generate code.
Often you will want to use impl_for to generate an impl <trait_name> for <target_name()>.
Implementations§
Source§impl Generator
impl Generator
Sourcepub fn target_name(&self) -> Ident
pub fn target_name(&self) -> Ident
Return the name for the struct or enum that this is going to be implemented on.
Sourcepub fn impl(&mut self) -> Impl<'_, Self>
pub fn impl(&mut self) -> Impl<'_, Self>
Generate an impl <target_name> implementation. See Impl for more information.
This will default to the type that is associated with this generator. If you need to generate an impl for another type you can use impl_for_other_type
Sourcepub fn generate_impl(&mut self) -> Impl<'_, Self>
pub fn generate_impl(&mut self) -> Impl<'_, Self>
Sourcepub fn impl_for(&mut self, trait_name: impl Into<String>) -> ImplFor<'_, Self>
pub fn impl_for(&mut self, trait_name: impl Into<String>) -> ImplFor<'_, Self>
Generate an for <trait_name> for <target_name> implementation. See ImplFor for more information.
This will default to the type that is associated with this generator. If you need to generate an impl for another type you can use impl_trait_for_other_type
Sourcepub fn impl_for_other_type(
&mut self,
type_name: impl Into<StringOrIdent>,
) -> ImplFor<'_, Self>
pub fn impl_for_other_type( &mut self, type_name: impl Into<StringOrIdent>, ) -> ImplFor<'_, Self>
Generate an impl <type_name> block. See ImplFor for more information.
generator.impl_for_other_type("Foo");
// will output:
// impl Foo { }Sourcepub fn impl_trait_for_other_type(
&mut self,
trait_name: impl Into<StringOrIdent>,
type_name: impl Into<StringOrIdent>,
) -> ImplFor<'_, Self>
pub fn impl_trait_for_other_type( &mut self, trait_name: impl Into<StringOrIdent>, type_name: impl Into<StringOrIdent>, ) -> ImplFor<'_, Self>
Generate an impl <trait_name> for <type_name> block. See ImplFor for more information.
generator.impl_trait_for_other_type("Foo", "Bar");
// will output:
// impl Foo for Bar { }Sourcepub fn impl_for_with_lifetimes<ITER, T>(
&mut self,
trait_name: T,
lifetimes: ITER,
) -> ImplFor<'_, Self>
pub fn impl_for_with_lifetimes<ITER, T>( &mut self, trait_name: T, lifetimes: ITER, ) -> ImplFor<'_, Self>
Generate an for <..lifetimes> <trait_name> for <target_name> implementation. See ImplFor for more information.
Note:
- Lifetimes should not have the leading apostrophe.
trait_nameshould not have custom lifetimes. These will be added automatically.
generator.impl_for_with_lifetimes("Foo", ["a", "b"]);
// will output:
// impl<'a, 'b> Foo<'a, 'b> for StructOrEnum { }The new lifetimes are not associated with any existing lifetimes. If you want this behavior you can call .impl_for_with_lifetimes(...).new_lifetimes_depend_on_existing()
// given a derive on `struct<'a> Bar<'a>`
generator.impl_for_with_lifetimes("Foo", ["b"]).new_lifetimes_depend_on_existing();
// will output:
// impl<'a, 'b> Foo<'b> for Bar<'a> where 'b: 'a { }Sourcepub fn generate_struct(
&mut self,
name: impl Into<String>,
) -> GenStruct<'_, Self>
pub fn generate_struct( &mut self, name: impl Into<String>, ) -> GenStruct<'_, Self>
Generate a struct with the given name. See GenStruct for more info.
Sourcepub fn generate_enum(&mut self, name: impl Into<String>) -> GenEnum<'_, Self>
pub fn generate_enum(&mut self, name: impl Into<String>) -> GenEnum<'_, Self>
Generate an enum with the given name. See GenEnum for more info.
Sourcepub fn generate_mod(
&mut self,
mod_name: impl Into<String>,
) -> GenerateMod<'_, Self>
pub fn generate_mod( &mut self, mod_name: impl Into<String>, ) -> GenerateMod<'_, Self>
Generate a mod <name> { ... }. See GenerateMod for more info.
Sourcepub fn export_to_file(&self, crate_name: &str, file_postfix: &str) -> bool
pub fn export_to_file(&self, crate_name: &str, file_postfix: &str) -> bool
Export the current stream to a file, making it very easy to debug the output of a derive macro.
This will try to find rust’s target directory, and write target/generated/<crate_name>/<name>_<file_postfix>.rs.
Will return true if the file is written, false otherwise.
The outputted file is unformatted. Use cargo fmt -- target/generated/<crate_name>/<file>.rs to format the file.
Sourcepub fn finish(self) -> Result<TokenStream>
pub fn finish(self) -> Result<TokenStream>
Consume the contents of this generator. This must be called, or else the generator will panic on drop.