Skip to main content

Writer

Struct Writer 

Source
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

Source

pub fn new() -> Self

Creates an empty writer.

Source

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.

Source

pub fn as_slice(&self) -> &[u8]

Returns the bytes written so far.

Source

pub fn into_bytes(self) -> Vec<u8>

Consumes the writer and returns the bytes written.

Source

pub fn len(&self) -> usize

Returns the number of bytes written so far.

Source

pub fn is_empty(&self) -> bool

Returns true if nothing has been written yet.

Source

pub fn clear(&mut self)

Discards everything written, keeping the allocated capacity for reuse.

Source

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).

Source

pub fn write_i8(&mut self, value: i8)

Writes an i8 as 1 byte.

Source

pub fn write_u8(&mut self, value: u8)

Writes a u8 as 1 byte.

Source

pub fn write_i16(&mut self, value: i16)

Writes an i16 as 2 bytes, Little Endian.

Source

pub fn write_u16(&mut self, value: u16)

Writes a u16 as 2 bytes, Little Endian.

Source

pub fn write_i32(&mut self, value: i32)

Writes an i32 as 4 bytes, Little Endian.

Source

pub fn write_u32(&mut self, value: u32)

Writes a u32 as 4 bytes, Little Endian.

Source

pub fn write_i64(&mut self, value: i64)

Writes an i64 as 8 bytes, Little Endian.

Source

pub fn write_u64(&mut self, value: u64)

Writes a u64 as 8 bytes, Little Endian.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Clone for Writer

Source§

fn clone(&self) -> Writer

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Writer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Writer

Source§

fn default() -> Writer

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.