Bytecode

Trait Bytecode 

Source
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§

Source

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]);
Source

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.

Implementations on Foreign Types§

Source§

impl Bytecode for i8

Source§

fn encode<B: BufMut>(&self, buf: &mut B)

Source§

fn decode<B: Buf>(buf: &mut B) -> Result<Self, DecodeError>

Source§

impl Bytecode for i16

Source§

fn encode<B: BufMut>(&self, buf: &mut B)

Source§

fn decode<B: Buf>(buf: &mut B) -> Result<Self, DecodeError>

Source§

impl Bytecode for i32

Source§

fn encode<B: BufMut>(&self, buf: &mut B)

Source§

fn decode<B: Buf>(buf: &mut B) -> Result<Self, DecodeError>

Source§

impl Bytecode for i64

Source§

fn encode<B: BufMut>(&self, buf: &mut B)

Source§

fn decode<B: Buf>(buf: &mut B) -> Result<Self, DecodeError>

Source§

impl Bytecode for i128

Source§

fn encode<B: BufMut>(&self, buf: &mut B)

Source§

fn decode<B: Buf>(buf: &mut B) -> Result<Self, DecodeError>

Source§

impl Bytecode for u8

Source§

fn encode<B: BufMut>(&self, buf: &mut B)

Source§

fn decode<B: Buf>(buf: &mut B) -> Result<Self, DecodeError>

Source§

impl Bytecode for u16

Source§

fn encode<B: BufMut>(&self, buf: &mut B)

Source§

fn decode<B: Buf>(buf: &mut B) -> Result<Self, DecodeError>

Source§

impl Bytecode for u32

Source§

fn encode<B: BufMut>(&self, buf: &mut B)

Source§

fn decode<B: Buf>(buf: &mut B) -> Result<Self, DecodeError>

Source§

impl Bytecode for u64

Source§

fn encode<B: BufMut>(&self, buf: &mut B)

Source§

fn decode<B: Buf>(buf: &mut B) -> Result<Self, DecodeError>

Source§

impl Bytecode for u128

Source§

fn encode<B: BufMut>(&self, buf: &mut B)

Source§

fn decode<B: Buf>(buf: &mut B) -> Result<Self, DecodeError>

Implementors§