Skip to main content

Generator

Struct Generator 

Source
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

Source

pub fn target_name(&self) -> Ident

Return the name for the struct or enum that this is going to be implemented on.

Source

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

Source

pub fn generate_impl(&mut self) -> Impl<'_, Self>

Generate an impl <target_name> implementation. See Impl for more information.

Alias for impl which doesn’t need a r# prefix.

Source

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

Source

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 { }
Source

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 { }
Source

pub fn impl_for_with_lifetimes<ITER, T>( &mut self, trait_name: T, lifetimes: ITER, ) -> ImplFor<'_, Self>
where ITER: IntoIterator, ITER::Item: Into<String>, T: Into<StringOrIdent>,

Generate an for <..lifetimes> <trait_name> for <target_name> implementation. See ImplFor for more information.

Note:

  • Lifetimes should not have the leading apostrophe.
  • 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 { }

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 { }
Source

pub fn generate_struct( &mut self, name: impl Into<String>, ) -> GenStruct<'_, Self>

Generate a struct with the given name. See GenStruct for more info.

Source

pub fn generate_enum(&mut self, name: impl Into<String>) -> GenEnum<'_, Self>

Generate an enum with the given name. See GenEnum for more info.

Source

pub fn generate_mod( &mut self, mod_name: impl Into<String>, ) -> GenerateMod<'_, Self>

Generate a mod <name> { ... }. See GenerateMod for more info.

Source

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.

Source

pub fn finish(self) -> Result<TokenStream>

Consume the contents of this generator. This must be called, or else the generator will panic on drop.

Trait Implementations§

Source§

impl Drop for Generator

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Parent for Generator

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.