pub struct BufferReader<'a> { /* private fields */ }Expand description
Decodes values from the WeaveFFI buffer format.
Every read_* method returns BufferDecodeError when the buffer is
exhausted or holds invalid data, so a malformed buffer can never cause an
out-of-bounds read.
Implementations§
Source§impl<'a> BufferReader<'a>
impl<'a> BufferReader<'a>
Sourcepub fn new(data: &'a [u8]) -> Self
pub fn new(data: &'a [u8]) -> Self
Wrap an encoded buffer for reading. Object tokens in it are adopted
by BufferValue::read_value, whose contract covers them.
Sourcepub fn token_free(data: &'a [u8]) -> Self
pub fn token_free(data: &'a [u8]) -> Self
Wrap an encoded buffer whose object tokens must not be adopted:
reading an interface value fails instead. Decoding through such a
reader is sound for any input, which is how the runtime reads a
callback’s error payload (see
ForeignError::domain).
Sourcepub fn adopts_tokens(&self) -> bool
pub fn adopts_tokens(&self) -> bool
Whether this reader may adopt object tokens (it wasn’t built with
token_free).
Sourcepub fn read_bool(&mut self) -> Result<bool, BufferDecodeError>
pub fn read_bool(&mut self) -> Result<bool, BufferDecodeError>
Sourcepub fn read_i8(&mut self) -> Result<i8, BufferDecodeError>
pub fn read_i8(&mut self) -> Result<i8, BufferDecodeError>
Sourcepub fn read_u8(&mut self) -> Result<u8, BufferDecodeError>
pub fn read_u8(&mut self) -> Result<u8, BufferDecodeError>
Sourcepub fn read_i16(&mut self) -> Result<i16, BufferDecodeError>
pub fn read_i16(&mut self) -> Result<i16, BufferDecodeError>
Sourcepub fn read_u16(&mut self) -> Result<u16, BufferDecodeError>
pub fn read_u16(&mut self) -> Result<u16, BufferDecodeError>
Sourcepub fn read_i32(&mut self) -> Result<i32, BufferDecodeError>
pub fn read_i32(&mut self) -> Result<i32, BufferDecodeError>
Read an i32 little-endian. Also decodes C-style enum values and
rich-enum tags.
§Errors
Returns an error when the buffer is exhausted.
Sourcepub fn read_u32(&mut self) -> Result<u32, BufferDecodeError>
pub fn read_u32(&mut self) -> Result<u32, BufferDecodeError>
Sourcepub fn read_i64(&mut self) -> Result<i64, BufferDecodeError>
pub fn read_i64(&mut self) -> Result<i64, BufferDecodeError>
Sourcepub fn read_u64(&mut self) -> Result<u64, BufferDecodeError>
pub fn read_u64(&mut self) -> Result<u64, BufferDecodeError>
Read a u64 little-endian. Also decodes object tokens.
§Errors
Returns an error when the buffer is exhausted.
Sourcepub fn read_f32(&mut self) -> Result<f32, BufferDecodeError>
pub fn read_f32(&mut self) -> Result<f32, BufferDecodeError>
Read an f32 from its IEEE 754 bits, little-endian.
§Errors
Returns an error when the buffer is exhausted.
Sourcepub fn read_f64(&mut self) -> Result<f64, BufferDecodeError>
pub fn read_f64(&mut self) -> Result<f64, BufferDecodeError>
Read an f64 from its IEEE 754 bits, little-endian.
§Errors
Returns an error when the buffer is exhausted.
Sourcepub fn read_len(&mut self) -> Result<usize, BufferDecodeError>
pub fn read_len(&mut self) -> Result<usize, BufferDecodeError>
Read the byte length of a string or byte buffer (a u32).
§Errors
Returns an error when the buffer is exhausted or the decoded length exceeds the bytes remaining, which would make the follow-up read fail anyway; rejecting here gives a clearer error.
Sourcepub fn read_count(&mut self) -> Result<usize, BufferDecodeError>
pub fn read_count(&mut self) -> Result<usize, BufferDecodeError>
Read a collection’s element count (a u32).
Unlike read_len this doesn’t bound the count by
the bytes remaining: an element’s encoding can be empty (a record with
no fields), so a large count is legitimate. Decoders bound their
preallocation by remaining instead, and a hostile
count fails as soon as the elements run out.
§Errors
Returns an error when the buffer is exhausted.
Sourcepub fn read_string(&mut self) -> Result<String, BufferDecodeError>
pub fn read_string(&mut self) -> Result<String, BufferDecodeError>
Read a string: u32 byte length + UTF-8 bytes.
§Errors
Returns an error when the buffer is exhausted or the bytes are not valid UTF-8.
Sourcepub fn read_bytes(&mut self) -> Result<Vec<u8>, BufferDecodeError>
pub fn read_bytes(&mut self) -> Result<Vec<u8>, BufferDecodeError>
Sourcepub fn read_option_flag(&mut self) -> Result<bool, BufferDecodeError>
pub fn read_option_flag(&mut self) -> Result<bool, BufferDecodeError>
Read an optional’s presence flag.
§Errors
Returns an error when the buffer is exhausted or the flag byte is not
0 or 1.
Sourcepub fn expect_end(&self) -> Result<(), BufferDecodeError>
pub fn expect_end(&self) -> Result<(), BufferDecodeError>
Assert the whole buffer was consumed. Called after decoding a complete value to catch trailing garbage.
§Errors
Returns an error when unconsumed bytes remain.