I have a function that reads bits and creates an enum. Currently the caller needs to provide both the EnumType and the representation of this enum. How do I change the function such that the representation is deduced from the EnumType, such that the caller only needs to provided the EnumType and not its representation?
// Example enum:
use num_enum::TryFromPrimitive;
#[derive(Debug, Clone, Copy, TryFromPrimitive)]
#[repr(u8)]
pub enum VehicleType {
Boat = 0,
Car = 1,
}
// Example of current functional call
extract_enum_bits::<VehicleType, u8>(&data, 2, 6);
// Current function:
fn extract_enum_bits<EnumType: std::convert::TryFrom<Repr>, Repr: ReadInto>(data: &[u8],start_bit: u64, bits_to_read: u8) -> EnumType
where <EnumType as std::convert::TryFrom<Repr>>::Error : std::fmt::Debug {
let enum_as_repr_int = extract_bits::<Repr>(&data, start_bit, bits_to_read);
EnumType::try_from(enum_as_repr_int).unwrap()
}
Wanted funcion call syntax:
extract_enum_bits::<VehicleType>(&data, 2, 6)
How can the function extract_enum_bits be changed to support this new call syntax?
Or to formulate the question differently. How do I get the enum representation type (in this example u8) at the question marks, while also satisfying the "std::convert::From" requirement imposed by "::try_from" on the last line?
pub fn extract_enum_bits<EnumType: TryFromPrimitive>(data: &[u8],start_bit: u64, bits_to_read: u8) -> EnumType {
let enum_as_repr_int = extract_bits::< ???? >(&data, start_bit, bits_to_read);
EnumType::try_from(enum_as_repr_int).unwrap()
}