#[derive(ReadEnum)]
{
// Attributes available to this derive:
#[type_variant_id]
}
Expand description
The ReadEnum macro generates a FromReader implementation for enums.
For structs, use ReadStruct.
§Attributes
§type_variant_id($ty)
This attribute tells the macro what type represents the enum variant ID.
The enum variant ID is the number written to notify the reader of the variant they should expect to receive.
use tora_derive::ReadEnum;
#[derive(ReadEnum)]
enum Packet {
PlayerJoin, // 0
PlayerQuit // 1
}By default, this macro assumes u8.
In the case that the enum deriving this macro contains more than u8::MAX variants, the user will be required to specify this attribute manually.
§Usage
use tora_derive::ReadEnum;
#[derive(ReadEnum)]
#[type_variant_id(u32)]
enum Packet {
Variant1,
Variant2,
}§Generated code
use std::io;
use std::io::{ErrorKind, Read};
use tora::read::{ToraRead, FromReader};
enum Packet {
Variant1,
Variant2,
}
impl FromReader for Packet {
fn from_reader<R>(r: &mut R) -> io::Result<Self>
where R: Read
{
let id = r.reads::<u32>()?;
Ok(match id {
0 => Self::Variant1,
1 => Self::Variant2,
_ => return Err(io::Error::new(ErrorKind::InvalidInput, "Invalid packet ID"))
})
}
}