Skip to main content

GenericTypeVisitable

Derive Macro GenericTypeVisitable 

Source
#[derive(GenericTypeVisitable)]
{
    // Attributes available to this derive:
    #[generic_type_visitable]
}
Expand description

By default, #[derive(GenericTypeVisitable)] will add GenericTypeVisitable bounds to every field of the item. However, this results in infinite recursion for types whose fields mention Self, such as:

struct List {
    next: Option<Box<Self>>
}

The #[generic_type_visitable(bounds(...))] attribute provides an escape hatch: it allows you to override the list of trait bounds added to the field’s type. Namely, it should contain GenericTypeVisitable bounds for all the non-Self types present in the field.

For the example above, that list will be empty:

#[derive(GenericTypeVisitable)]
struct List {
    #[generic_type_visitable(bounds())]
    next: Option<Box<Self>>
}

For a more complicated type:

#[derive(GenericTypeVisitable)]
struct Foo {
    #[generic_type_visitable(bounds())]
    just_self: Box<Self>,
    #[generic_type_visitable(bounds(Bar: GenericTypeVisitable))]
    contains_self: (Box<Self>, Bar),
}
struct Bar;