Skip to main content

StateFilterConversion

Derive Macro StateFilterConversion 

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

Implements StateFilterInputConversion and StateFilterInputCombination, so that this struct can be split into every individual field and later combined together.

The types each field can be converted to must be specified.

Use conversion on the struct fields to convert them to a different type:

#[derive(StateFilterConversion)]
struct ExampleStruct {
    #[conversion(AdminUser)]
    some_value: UnknownUser,
}

The above code will let a StateFilter take ExampleStruct as an input, and output a new struct whose some_value is of type AdminUser or the original UnknownUser.

In some cases, your filters may result in more data output than what was given. In those cases, you can use the conversion attribute on the struct itself for extra fields:

#[derive(StateFilterConversion)]
#[conversion(Age)]
struct ExampleStruct {
    some_value: UnknownUser,
}

The above code will allow ExampleStruct to be deconstructed and then reconstructed into a new struct which also contains the field age: Age.

If you use generics, use this syntax:

#[derive(StateFilterConversion)]
struct ExampleStruct {
    #[conversion(T = UserWithData<T>)]
    some_value: UnknownUser,
}

And if you want more than one generic, be sure to use a different generic name:

#[derive(StateFilterConversion)]
#[conversion(T0 = SomeMoreData<T0>)]
struct ExampleStruct {
    #[conversion(T1, T2 = UserWithData<T1, T2>)]
    some_value: UnknownUser,
}

The conversion attribute can be used multiple times on a single field for different conversion types:

#[derive(StateFilterConversion)]
struct ExampleStruct {
    #[conversion(AdminUser)]
    #[conversion(UserWithData)]
    some_value: UnknownUser,
}