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>
impl<'a> Reader<'a>
Sourcepub fn new(buf: &'a [u8]) -> Self
pub fn new(buf: &'a [u8]) -> Self
Creates a reader over buf with Limits::UNLIMITED.
Sourcepub fn with_limits(buf: &'a [u8], limits: Limits) -> Self
pub fn with_limits(buf: &'a [u8], limits: Limits) -> Self
Creates a reader over buf with explicit allocation guards.
Sourcepub fn position(&self) -> usize
pub fn position(&self) -> usize
Returns the cursor position, in bytes from the start of the buffer.
Sourcepub fn is_empty(&self) -> bool
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.
Sourcepub fn read_bool(&mut self) -> Result<bool, DecodeError>
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.
Sourcepub fn read_i8(&mut self) -> Result<i8, DecodeError>
pub fn read_i8(&mut self) -> Result<i8, DecodeError>
Sourcepub fn read_u8(&mut self) -> Result<u8, DecodeError>
pub fn read_u8(&mut self) -> Result<u8, DecodeError>
Sourcepub fn read_i16(&mut self) -> Result<i16, DecodeError>
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.
Sourcepub fn read_u16(&mut self) -> Result<u16, DecodeError>
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.
Sourcepub fn read_i32(&mut self) -> Result<i32, DecodeError>
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.
Sourcepub fn read_u32(&mut self) -> Result<u32, DecodeError>
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.
Sourcepub fn read_i64(&mut self) -> Result<i64, DecodeError>
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.
Sourcepub fn read_u64(&mut self) -> Result<u64, DecodeError>
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.
Sourcepub fn read_f32(&mut self) -> Result<f32, DecodeError>
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.
Sourcepub fn read_f64(&mut self) -> Result<f64, DecodeError>
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.
Sourcepub fn read_string(&mut self) -> Result<String, DecodeError>
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.
Sourcepub fn read_bytes(&mut self) -> Result<Vec<u8>, DecodeError>
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.
Sourcepub fn read_array_count(&mut self) -> Result<usize, DecodeError>
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.