[][src]Trait stack_vm::ToByteCode

pub trait ToByteCode {
    fn to_byte_code(&self, _: &mut dyn Write);
}

Convert from operands to byte code.

This trait represents the ability to dump your Operands to bytecode. stack-vm uses the rmp crate to store bytecode as a MsgPack encoded binary. As long as you return a valid vector of messagepack bytes it'll be inserted into the output during the serialization run.

See the rmp docs to find out which functions you can use to write out your types.

Being MsgPack means that you can encode your Operand in any way you require, for example as a single integer, or a map of keys and values provided that you write just a single MsgPack value.

Required methods

fn to_byte_code(&self, _: &mut dyn Write)

Convert your type to MsgPack.

This function takes a mutable reference of type Write into which you write your MsgPack encoded value.

Example


#[derive(PartialEq, Debug)]
struct Operand(i64);

impl ToByteCode for Operand {
    fn to_byte_code(&self, mut buf: &mut Write) {
        rmp::encode::write_sint(&mut buf, self.0).unwrap();
    }
}
let op = Operand(13);
let mut buf: Vec<u8> = vec![];
op.to_byte_code(&mut buf);
assert_eq!(&buf[..], [0xd]);
Loading content...

Implementors

impl<T: ToByteCode + Debug> ToByteCode for Code<T>[src]

fn to_byte_code(&self, buf: &mut dyn Write)[src]

Create bytecode for this Code.

Encodes into a Map of the following format:

{
    "code" => [ 0, 1, 0, 0, 1, 1, 1, 0 ],
    "data" => [ 123, 456 ],
    "symbols" => [ 0, "push", 1, "add" ],
    "labels" => [ 0, "main" ]
}
Loading content...