Skip to main content

Reader

Struct Reader 

Source
pub struct Reader<'a> { /* private fields */ }
Expand description

Reads Cyclone-encoded values from a borrowed byte buffer.

The reader holds a cursor into buf and advances it by exactly the bytes each value occupies. It borrows rather than owns, so decoding a message costs no copy beyond the String and Bytes values that must be owned.

Malformed input is always reported as DecodeError; the reader never panics on it, and the cursor is left untouched when a read fails, so an error cannot desynchronize a caller that chooses to continue.

use cyclone_runtime::Reader;

let bytes = [0x2A, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, b'a', b'b', b'c'];
let mut r = Reader::new(&bytes);
assert_eq!(r.read_u32()?, 42);
assert_eq!(r.read_string()?, "abc");
assert!(r.is_empty());

Implementations§

Source§

impl<'a> Reader<'a>

Source

pub fn new(buf: &'a [u8]) -> Self

Creates a reader over buf with Limits::UNLIMITED.

Source

pub fn with_limits(buf: &'a [u8], limits: Limits) -> Self

Creates a reader over buf with explicit allocation guards.

Source

pub fn position(&self) -> usize

Returns the cursor position, in bytes from the start of the buffer.

Source

pub fn remaining(&self) -> usize

Returns the number of bytes left to read.

Source

pub fn is_empty(&self) -> bool

Returns true when the cursor has reached the end of the buffer.

After decoding a complete message this MUST be true; trailing bytes mean the sender and receiver disagree about the schema.

Source

pub fn limits(&self) -> Limits

Returns the limits this reader enforces.

Source

pub fn read_bool(&mut self) -> Result<bool, DecodeError>

Reads a bool from 1 byte.

§Errors

DecodeError::InvalidBool if the byte is neither 0x00 nor 0x01 - “non-zero means true” is not permitted (RFC-0002 §2.4). DecodeError::UnexpectedEof if the buffer is exhausted.

Source

pub fn read_i8(&mut self) -> Result<i8, DecodeError>

Reads an i8 from 1 byte.

§Errors

DecodeError::UnexpectedEof if fewer than 1 byte remains.

Source

pub fn read_u8(&mut self) -> Result<u8, DecodeError>

Reads a u8 from 1 byte.

§Errors

DecodeError::UnexpectedEof if fewer than 1 byte remains.

Source

pub fn read_i16(&mut self) -> Result<i16, DecodeError>

Reads an i16 from 2 bytes, Little Endian.

§Errors

DecodeError::UnexpectedEof if fewer than 2 bytes remain.

Source

pub fn read_u16(&mut self) -> Result<u16, DecodeError>

Reads a u16 from 2 bytes, Little Endian.

§Errors

DecodeError::UnexpectedEof if fewer than 2 bytes remain.

Source

pub fn read_i32(&mut self) -> Result<i32, DecodeError>

Reads an i32 from 4 bytes, Little Endian.

§Errors

DecodeError::UnexpectedEof if fewer than 4 bytes remain.

Source

pub fn read_u32(&mut self) -> Result<u32, DecodeError>

Reads a u32 from 4 bytes, Little Endian.

§Errors

DecodeError::UnexpectedEof if fewer than 4 bytes remain.

Source

pub fn read_i64(&mut self) -> Result<i64, DecodeError>

Reads an i64 from 8 bytes, Little Endian.

§Errors

DecodeError::UnexpectedEof if fewer than 8 bytes remain.

Source

pub fn read_u64(&mut self) -> Result<u64, DecodeError>

Reads a u64 from 8 bytes, Little Endian.

§Errors

DecodeError::UnexpectedEof if fewer than 8 bytes remain.

Source

pub fn read_f32(&mut self) -> Result<f32, DecodeError>

Reads an f32 from its raw 4-byte IEEE 754 bit pattern.

The bits are reinterpreted, not normalized: a signaling NaN stays signaling, its payload survives, and -0.0 does not collapse to 0.0 (RFC-0002 §2.3).

§Errors

DecodeError::UnexpectedEof if fewer than 4 bytes remain.

Source

pub fn read_f64(&mut self) -> Result<f64, DecodeError>

Reads an f64 from its raw 8-byte IEEE 754 bit pattern.

Same rule as read_f32: bits are preserved exactly.

§Errors

DecodeError::UnexpectedEof if fewer than 8 bytes remain.

Source

pub fn read_string(&mut self) -> Result<String, DecodeError>

Reads a string: a u32 UTF-8 byte length followed by that many bytes.

The length is checked against Limits::max_string_len and against the bytes actually remaining before anything is allocated.

§Errors

DecodeError::LengthOverflow if the length exceeds the configured limit, DecodeError::UnexpectedEof if it exceeds the remaining bytes, and DecodeError::InvalidUtf8 if the byte region is not valid UTF-8.

Source

pub fn read_bytes(&mut self) -> Result<Vec<u8>, DecodeError>

Reads a byte blob: a u32 length followed by that many raw bytes.

Identical to read_string minus the UTF-8 check, and guarded by Limits::max_bytes_len.

§Errors

DecodeError::LengthOverflow if the length exceeds the configured limit; DecodeError::UnexpectedEof if it exceeds the remaining bytes.

Source

pub fn read_array_count(&mut self) -> Result<usize, DecodeError>

Reads the element count of an array as a u32 (RFC-0002 §6).

The elements themselves are read by the generated codec, which is the only party that knows their type. The count is checked against Limits::max_array_count so a codec never sizes a collection from an unbounded number.

Unlike a string length, a count cannot be compared against the remaining bytes here - an element is not one byte, and the runtime does not know how wide it is. The per-element reads enforce that bound as they run.

§Errors

DecodeError::UnexpectedEof if fewer than 4 bytes remain; DecodeError::LengthOverflow if the count exceeds the configured limit.

Trait Implementations§

Source§

impl<'a> Clone for Reader<'a>

Source§

fn clone(&self) -> Reader<'a>

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<'a> Debug for Reader<'a>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Reader<'a>

§

impl<'a> RefUnwindSafe for Reader<'a>

§

impl<'a> Send for Reader<'a>

§

impl<'a> Sync for Reader<'a>

§

impl<'a> Unpin for Reader<'a>

§

impl<'a> UnsafeUnpin for Reader<'a>

§

impl<'a> UnwindSafe for Reader<'a>

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.