Struct virtue::generate::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.

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.

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<String>,

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> {}
  • 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 { }
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>,

§

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>,

§

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.