Derive Macro fluvio_protocol_api::derive::Decode [−][src]
#[derive(Decode)] { // Attributes available to this derive: #[varint] #[fluvio] }
Expand description
Custom derive for decoding structure or enum from bytes using fluvio protocol format. This assumes all fields implement fluvio decode traits.
Examples
use std::io::Cursor; use fluvio_protocol::Decoder; use fluvio_protocol::derive::Decode; #[derive(Default, Decode)] pub struct SimpleRecord { val: u8 } let data = [ 0x04 ]; let record = SimpleRecord::decode_from(&mut Cursor::new(&data),0).expect("decode"); assert_eq!(record.val, 4);
Decode applies to either Struct of Enum. For enum, it implements TryFrom
trait.
Currently it only supports integer variants.
So this works
#[derive(Decode)] pub enum ThreeChoice { First = 1, Second = 2, Third = 3 }
Also, enum without integer literal works as well
#[derive(Decode)] pub enum ThreeChoice { First, Second, Third }
In this case, 1 is decoded as First, 2 as Second, 3 as Third.
Currently, mixing enum variants are not supported.
Decode support container and field level attributes. Container level applies to struct. For field attributes
#[varint]
force decode using varint format.#fluvio(min_version = <version>)]
decodes only if version is equal or greater than min_version#fluvio(max_version = <version>)]
decodes only if version is less or equal than max_version