pub trait Bytecode: Sized {
// Required methods
fn encode<B: BufMut>(&self, buf: &mut B);
fn decode<B: Buf>(buf: &mut B) -> Result<Self, DecodeError>;
}Expand description
Encode and decode instructions and operands.
Required Methods§
Sourcefn encode<B: BufMut>(&self, buf: &mut B)
fn encode<B: BufMut>(&self, buf: &mut B)
Encodes the instruction or the operand.
§Example
use bytecoding::Bytecode;
#[derive(Bytecode)]
#[bytecode(type = u8)]
enum Instruction {
Add,
Sub,
Jump(u8),
}
let mut buf = Vec::new();
Instruction::Sub.encode(&mut buf);
Instruction::Add.encode(&mut buf);
Instruction::Jump(42).encode(&mut buf);
assert_eq!(buf, vec![1, 0, 2, 42]);Sourcefn decode<B: Buf>(buf: &mut B) -> Result<Self, DecodeError>
fn decode<B: Buf>(buf: &mut B) -> Result<Self, DecodeError>
Decodes a instruction or an operand from the given buffer.
Advances the buffer to the next instruction.
§Example
use bytecoding::Bytecode;
#[derive(Debug, PartialEq, Eq, Bytecode)]
#[bytecode(type = u8)]
enum Instruction {
Add,
Sub,
Jump(u8),
}
let mut buf: &[u8] = &[1, 0, 2, 42];
let instructions = [
Instruction::decode(&mut buf)?,
Instruction::decode(&mut buf)?,
Instruction::decode(&mut buf)?,
];
assert_eq!(instructions, [Instruction::Sub, Instruction::Add, Instruction::Jump(42)]);Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.