virtue::generate

Struct ImplFor

Source
pub struct ImplFor<'a, P: Parent> { /* private fields */ }
Expand description

A helper struct for implementing a trait for a given struct or enum.

Implementations§

Source§

impl<'a, P: Parent> ImplFor<'a, P>

Source

pub fn new_lifetimes_depend_on_existing(self) -> Self

Make the new lifetimes added by Generator::impl_for_with_lifetimes depend on the existing lifetimes from the original derive.

See impl_for_with_lifetimes for more information.

Calling this method in any other context has no effect.

Source

pub fn with_trait_generics<ITER>(self, generics: ITER) -> Self
where ITER: IntoIterator, ITER::Item: Into<String>,

Add generic parameters to the trait implementation.

 generator.impl_for("Foo")
          .with_trait_generics(["Baz"]);

Generates:

impl Foo for <struct or enum> {
    const BAR: u8 = 5;
}
Source

pub fn with_impl_generics<ITER>(self, generics: ITER) -> Self
where ITER: IntoIterator, ITER::Item: Into<String>,

Add generic parameters to the impl block.

 generator.impl_for("Foo")
          .with_impl_generics(["Baz"]);

Generates:

impl<Baz> Foo for Bar { }
Source

pub fn impl_outer_attr(&mut self, attr: impl AsRef<str>) -> Result

Add a outer attribute to the trait implementation

Source

pub fn impl_inner_attr(&mut self, attr: impl AsRef<str>) -> Result

Add a inner attribute to the trait implementation

Source

pub fn generate_const( &mut self, name: impl Into<String>, ty: impl Into<String>, ) -> GenConst<'_>

Add a const to the trait implementation

generator.impl_for("Foo")
         .generate_const("BAR", "u8")
         .with_value(|b| {
            b.push_parsed("5")?;
            Ok(())
         })?;

Generates:

impl Foo for <struct or enum> {
    const BAR: u8 = 5;
}
Source

pub fn generate_fn( &mut self, name: impl Into<String>, ) -> FnBuilder<'_, ImplFor<'a, P>>

Add a function to the trait implementation.

generator.impl_for("Foo").generate_fn("bar") results in code like:

impl Foo for <struct or enum> {
    fn bar() {}
}

See FnBuilder for more options, as well as information on how to fill the function body.

Source

pub fn impl_type( &mut self, name: impl AsRef<str>, value: impl AsRef<str>, ) -> Result

Add a type to the impl

generator.impl_for("Foo").impl_type("Bar", "u8") results in code like:

impl Foo for <struct or enum> {
    type Bar = u8;
}
Source

pub fn modify_generic_constraints<CB>(&mut self, cb: CB) -> Result<&mut Self>

Modify the generic constraints of a type. This can be used to add additional type constraints to your implementation.

// Your derive:
#[derive(YourTrait)]
pub struct Foo<B> {
    ...
}

// With this code:
generator
    .impl_for("YourTrait")
    .modify_generic_constraints(|generics, constraints| {
        for g in generics.iter_generics() {
            constraints.push_generic(g, "YourTrait");
        }
    })

// will generate:
impl<B> YourTrait for Foo<B>
    where B: YourTrait // <-
{
}

Trait Implementations§

Source§

impl<P: Parent> Drop for ImplFor<'_, P>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'a, P> Freeze for ImplFor<'a, P>

§

impl<'a, P> RefUnwindSafe for ImplFor<'a, P>
where P: RefUnwindSafe,

§

impl<'a, P> !Send for ImplFor<'a, P>

§

impl<'a, P> !Sync for ImplFor<'a, P>

§

impl<'a, P> Unpin for ImplFor<'a, P>

§

impl<'a, P> !UnwindSafe for ImplFor<'a, P>

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.