pub struct Writer { /* private fields */ }Expand description
Appends Cyclone-encoded values to a growable byte buffer.
The writer performs no validation: the generated codec decides what to write, the writer decides only what shape those bytes have. Every multi-byte value is written Little Endian, with no padding, no alignment, and no metadata between values (RFC-0002 §1).
use cyclone_runtime::Writer;
let mut w = Writer::new();
w.write_u32(42);
w.write_string("Sword");
assert_eq!(
w.as_slice(),
&[0x2A, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, b'S', b'w', b'o', b'r', b'd'],
);Implementations§
Source§impl Writer
impl Writer
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates an empty writer with room for capacity bytes.
Cyclone values have sizes that are known from the schema, so a caller that knows the message size can encode without a single reallocation.
Sourcepub fn into_bytes(self) -> Vec<u8> ⓘ
pub fn into_bytes(self) -> Vec<u8> ⓘ
Consumes the writer and returns the bytes written.
Sourcepub fn write_bool(&mut self, value: bool)
pub fn write_bool(&mut self, value: bool)
Writes a bool as one byte: 0x00 for false, 0x01 for true.
No other byte is ever emitted for a bool (RFC-0002 §2.4).
Sourcepub fn write_f32(&mut self, value: f32)
pub fn write_f32(&mut self, value: f32)
Writes an f32 as its raw IEEE 754 bit pattern, 4 bytes Little Endian.
The bit pattern is written unmodified: NaN payloads are preserved,
-0.0 stays distinct from 0.0, and infinities are written as-is
(RFC-0002 §2.3). No canonicalization is performed.
Sourcepub fn write_f64(&mut self, value: f64)
pub fn write_f64(&mut self, value: f64)
Writes an f64 as its raw IEEE 754 bit pattern, 8 bytes Little Endian.
Same rule as write_f32: the bits are preserved
exactly, never normalized.
Sourcepub fn write_string(&mut self, value: &str)
pub fn write_string(&mut self, value: &str)
Writes a string as a u32 UTF-8 byte length followed by those bytes.
The length counts bytes, not characters (RFC-0002 §3).
§Panics
Panics if the string is longer than u32::MAX bytes, which the wire
format cannot represent.
Sourcepub fn write_bytes(&mut self, value: &[u8])
pub fn write_bytes(&mut self, value: &[u8])
Writes a byte blob as a u32 length followed by the raw bytes.
Identical to write_string minus the UTF-8
constraint (RFC-0002 §4).
§Panics
Panics if the blob is longer than u32::MAX bytes, which the wire
format cannot represent.
Sourcepub fn write_array_count(&mut self, count: usize)
pub fn write_array_count(&mut self, count: usize)
Writes the element count of an array as a u32 (RFC-0002 §6).
The elements themselves are written by the generated codec, which is the only party that knows their type.
§Panics
Panics if count exceeds u32::MAX.