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.
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.
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.
- The lifetimes passed to this function will automatically depend on any other lifetime this struct or enum may have. Example:
- The struct is
struct Foo<'a> {}
- You call `generator.impl_for_with_lifetime(“Bar”, &[“b”])
- The code will be
impl<'a, 'b: 'a> Bar<'b> for Foo<'a> {}
- The struct is
trait_name
should 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 { }
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.