Struct kiwi_schema::ByteBuffer

source ·
pub struct ByteBuffer<'a> { /* private fields */ }
Expand description

A Kiwi byte buffer meant for reading.

Example usage:

use std::borrow::Cow;
let mut bb = kiwi_schema::ByteBuffer::new(&[240, 159, 141, 149, 0, 133, 242, 210, 237]);
assert_eq!(bb.read_string(), Ok(Cow::Borrowed("🍕")));
assert_eq!(bb.read_var_float(), Ok(123.456));

Implementations§

source§

impl<'a> ByteBuffer<'a>

source

pub fn new(data: &[u8]) -> ByteBuffer<'_>

Create a new ByteBuffer that wraps the provided byte slice. The lifetime of the returned ByteBuffer must not outlive the lifetime of the byte slice.

source

pub fn data(&self) -> &'a [u8]

Retrieves the underlying byte slice.

source

pub fn index(&self) -> usize

Retrieves the current index into the underlying byte slice. This starts off as 0 and ends up as self.data().len() when everything has been read.

source

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

Try to read a boolean value starting at the current index.

source

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

Try to read a byte starting at the current index.

source

pub fn read_bytes(&mut self, len: usize) -> Result<&'a [u8], ()>

Try to read a byte starting at the current index.

source

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

Try to read a variable-length signed 32-bit integer starting at the current index.

source

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

Try to read a variable-length unsigned 32-bit integer starting at the current index.

source

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

Try to read a variable-length 32-bit floating-point number starting at the current index.

source

pub fn read_string(&mut self) -> Result<Cow<'a, str>, ()>

Try to read a UTF-8 string starting at the current index. This string is returned as a slice so it just aliases the underlying memory.

source

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

Try to read a variable-length signed 64-bit integer starting at the current index.

source

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

Try to read a variable-length unsigned 64-bit integer starting at the current index.

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for ByteBuffer<'a>

§

impl<'a> Send for ByteBuffer<'a>

§

impl<'a> Sync for ByteBuffer<'a>

§

impl<'a> Unpin for ByteBuffer<'a>

§

impl<'a> UnwindSafe for ByteBuffer<'a>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.