#[derive(Discrim)]
{
// Attributes available to this derive:
#[dynec]
}
Expand description
Derives a Discrim implementation for the type.
This derive macro is only applicable to single-field structs (both tuple structs and named structs) and enums with only unit fields.
For structs, the only field must be an integer type
(one that is convertible from and to usize through xias::SmallInt).
Note that dynec mostly uses usize to identify isotopes,
so using u8 instead of usize as the backing type does not provide notable benefit;
custom discriminant types are only available for ergonomic reasons.
§Customizing the discriminant map
Implementations for structs use
discrim::BoundedVecMap by default,
which is optimized for small discriminants.
This can be customized by adding #[dynec(map = path::to::another::Impl)] on the struct.
dynec::comp::discrim
is automatically imported for the map reference,
so users only need to specify e.g. #[dynec(map = discrim::LinearVecMap)].
Since maps are generic over T,
the passed type actually can depend on the type parameter T,
e.g. #[dynec(map = discrim::ArrayMap<Self, T, 16>)].
Inputs without trailing type parameters are appended with <Self, T> automatically,
where Self is the derived type.
Enums do not require customization because they always use
ArrayMap.
§Example
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, dynec::Discrim)]
struct Tuple(u16);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, dynec::Discrim)]
#[dynec(map = discrim::SortedVecMap)]
struct Named {
field: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, dynec::Discrim)]
#[dynec(map = discrim::ArrayMap<Self, T, 16>)]
struct UsesArray {
field: u8,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, dynec::Discrim)]
enum Enum {
Foo,
Bar,
}