Skip to main content

impl_kind

Macro impl_kind 

Source
impl_kind!() { /* proc-macro */ }
Expand description

Implements a Kind trait for a brand.

This macro simplifies the implementation of a generated Kind trait for a specific brand type. It infers the correct Kind trait to implement based on the signature of the associated types provided in the block.

The signature (names, parameters, and bounds) of the associated types must match the definition used in trait_kind! or Kind! to ensure the correct trait is implemented.

§Syntax

impl_kind! {
    // Optional impl generics
    impl<Generics> for BrandType
    // Optional where clause
    where Bounds
    {
        type AssocName<Params> = ConcreteType;
        // ... more associated types
    }
}
  • Generics: Optional generic parameters for the implementation.
  • BrandType: The brand type to implement the Kind for.
  • Bounds: Optional where clause bounds.
  • Associated Types: The associated type assignments (e.g., type Of<A> = Option<A>;).

§Generates

  1. An implementation of the appropriate Kind_{hash} trait for the brand.
  2. An InferableBrand_{hash} impl for the target type, mapping it back to the brand. This enables brand inference for free functions like map, bind, etc.

The InferableBrand impl is suppressed when:

  • #[no_inferable_brand] is present (for types with multiple brands).
  • The target type is a projection (contains Apply! or ::).
  • Multiple associated types are defined.

§Attributes

Inside the impl_kind! block, you can use these attributes:

  • #[no_inferable_brand]: Suppresses InferableBrand impl generation. Use this for types reachable through multiple brands (e.g., Result at arity 1).
  • #[document_default]: Marks this associated type as the default for resolving bare Self in the generated documentation for this brand within the module.

§Examples

// Invocation
impl_kind! {
    for OptionBrand {
        #[document_default]
        type Of<A> = Option<A>;
    }
}

// Expanded code (Kind impl + InferableBrand impl)
impl Kind_a1b2... for OptionBrand {
    type Of<A> = Option<A>;
}
impl<A> InferableBrand_a1b2... for Option<A> {
    type Brand = OptionBrand;
}
// Invocation with impl generics
impl_kind! {
    impl<E> for ResultBrand<E> {
        type Of<A> = Result<A, E>;
    }
}

// Expanded code
impl<E> Kind_... for ResultBrand<E> {
    type Of<A> = Result<A, E>;
}
impl<A, E> InferableBrand_... for Result<A, E> {
    type Brand = ResultBrand<E>;
}
// Suppressing InferableBrand generation for multi-brand types
impl_kind! {
    #[no_inferable_brand]
    impl<E> for ResultErrAppliedBrand<E> {
        type Of<'a, A: 'a>: 'a = Result<A, E>;
    }
}

// Expanded code (only Kind impl, no InferableBrand)
impl<E> Kind_... for ResultErrAppliedBrand<E> {
    type Of<'a, A: 'a>: 'a = Result<A, E>;
}
// Multiple associated types: InferableBrand skipped automatically
impl_kind! {
    impl<E> for MyBrand<E> where E: Clone {
        type Of<A> = MyType<A, E>;
        type SendOf<A> = MySendType<A, E>;
    }
}

// Expanded code (only Kind impl)
impl<E> Kind_... for MyBrand<E> where E: Clone {
    type Of<A> = MyType<A, E>;
    type SendOf<A> = MySendType<A, E>;
}