Skip to main content

Encode

Trait Encode 

Source
pub trait Encode {
    // Required method
    fn encode(&self, writer: &mut Writer);
}
Expand description

A type that can be written as Cyclone bytes.

The implementation is the schema: it decides which fields are written, in which order, and with which Cyclone type. Nothing here inspects the Rust type to work that out.

use cyclone_runtime::{Encode, Writer};

struct Item { id: u32, name: String }

impl Encode for Item {
    fn encode(&self, writer: &mut Writer) {
        writer.write_u32(self.id);
        writer.write_string(&self.name);
    }
}

let mut writer = Writer::new();
Item { id: 42, name: "Sword".to_owned() }.encode(&mut writer);
assert_eq!(writer.len(), 13);

Required Methods§

Source

fn encode(&self, writer: &mut Writer)

Appends the Cyclone encoding of self to writer.

Fields go in declaration order with nothing between them: no padding, no alignment, no field id, no header (RFC-0002 §5).

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§