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>
impl<'a, P: Parent> ImplFor<'a, P>
Sourcepub fn new_lifetimes_depend_on_existing(self) -> Self
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.
Sourcepub fn with_trait_generics<ITER>(self, generics: ITER) -> Self
pub fn with_trait_generics<ITER>(self, generics: ITER) -> Self
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;
}
Sourcepub fn with_impl_generics<ITER>(self, generics: ITER) -> Self
pub fn with_impl_generics<ITER>(self, generics: ITER) -> Self
Add generic parameters to the impl block.
generator.impl_for("Foo")
.with_impl_generics(["Baz"]);
Generates:
impl<Baz> Foo for Bar { }
Sourcepub fn impl_outer_attr(&mut self, attr: impl AsRef<str>) -> Result
pub fn impl_outer_attr(&mut self, attr: impl AsRef<str>) -> Result
Add a outer attribute to the trait implementation
Sourcepub fn impl_inner_attr(&mut self, attr: impl AsRef<str>) -> Result
pub fn impl_inner_attr(&mut self, attr: impl AsRef<str>) -> Result
Add a inner attribute to the trait implementation
Sourcepub fn generate_const(
&mut self,
name: impl Into<String>,
ty: impl Into<String>,
) -> GenConst<'_>
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;
}
Sourcepub fn impl_type(
&mut self,
name: impl AsRef<str>,
value: impl AsRef<str>,
) -> Result
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;
}
Sourcepub fn modify_generic_constraints<CB>(&mut self, cb: CB) -> Result<&mut Self>
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 // <-
{
}