```rust # #[macro_use] # extern crate error_chain; extern crate byteorder; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; #[derive(Default, PartialEq, Debug)] struct Payload { kind: u8, value: u16, } # # error_chain! { # foreign_links { # Io(std::io::Error); # } # } fn run() -> Result<()> { let original_payload = Payload::default(); let encoded_bytes = encode(&original_payload)?; let decoded_payload = decode(&encoded_bytes)?; assert_eq!(original_payload, decoded_payload); Ok(()) } fn encode(payload: &Payload) -> Result> { let mut bytes = vec![]; bytes.write_u8(payload.kind)?; bytes.write_u16::(payload.value)?; Ok(bytes) } fn decode(mut bytes: &[u8]) -> Result { let payload = Payload { kind: bytes.read_u8()?, value: bytes.read_u16::()?, }; Ok(payload) } # # quick_main!(run); ```