ByteDeserializerBytes

Struct ByteDeserializerBytes 

Source
pub struct ByteDeserializerBytes { /* private fields */ }
Expand description

Utility struct with a number of methods to enable deserialization of bytes into various types

use ::byteserde::prelude::ByteDeserializerBytes;
let bytes = &[0x01, 0x00, 0x02, 0x00, 0x00, 0x03];
let mut des = ByteDeserializerBytes::new(bytes.to_vec().into());
assert_eq!(des.remaining(), 6);
assert_eq!(des.idx(), 0);
assert_eq!(des.len(), 6);

let first: u8 = des.deserialize_bytes_slice(1).unwrap()[0];
assert_eq!(first , 1);

let second: &[u8; 2] = des.deserialize_bytes_array_ref().unwrap();
assert_eq!(second, &[0x00, 0x02]);

let remaining: &[u8] = des.deserialize_bytes_slice_remaining();
assert_eq!(remaining, &[0x00, 0x00, 0x03]);

Implementations§

Source§

impl ByteDeserializerBytes

Source

pub fn new(bytes: Bytes) -> ByteDeserializerBytes

Source

pub fn idx(&self) -> usize

Tracks the bytes read and always set to the next unread byte in the buffer. This is an inverse of Self::remaining()

Source

pub fn remaining(&self) -> usize

Number of bytes remaining to be deserialized, this is an inverse of Self::idx()

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

pub fn deserialize_bytes_slice_remaining(&mut self) -> &[u8]

consumes all of the remaining bytes in the buffer and returns them as slice

Source

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

consumes len bytes from the buffer and returns them as slice if successful. Fails if len is greater then Self::remaining()

Source

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

Source

pub fn deserialize_i8(&mut self) -> Result<i8>

Source

pub fn peek_bytes_slice(&self, len: usize) -> Result<&[u8]>

Source

pub fn peek_bytes(&self, at: usize) -> Result<Bytes>

Source

pub fn deserialize_bytes_array_ref<const N: usize>( &mut self, ) -> Result<&[u8; N]>

Source

pub fn deserialize_ne<const N: usize, T: FromNeBytes<N, T>>( &mut self, ) -> Result<T>

depletes 2 bytes for u16, etc. and returns after deserializing using native endianess FromNeBytes trait is already implemented for all rust’s numeric primitives in this crate

use ::byteserde::des_bytes::ByteDeserializerBytes;
let mut des = ByteDeserializerBytes::new([0x00, 0x01].to_vec().into());
let v: u16 = des.deserialize_ne().unwrap();
Source

pub fn deserialize_le<const N: usize, T: FromLeBytes<N, T>>( &mut self, ) -> Result<T>

depletes 2 bytes for u16, etc. and returns after deserializing using little endianess FromLeBytes trait is already implemented for all rust’s numeric primitives in this crate

use ::byteserde::prelude::ByteDeserializerBytes;
let mut des = ByteDeserializerBytes::new([0x01, 0x00].to_vec().into());
let v: u16 = des.deserialize_le().unwrap();
assert_eq!(v, 1);
Source

pub fn deserialize_be<const N: usize, T: FromBeBytes<N, T>>( &mut self, ) -> Result<T>

depletes 2 bytes for u16, etc. and returns after deserializing using big endianess FromBeBytes trait is already implemented for all rust’s numeric primitives in this crate

use ::byteserde::prelude::ByteDeserializerBytes;
let mut des = ByteDeserializerBytes::new([0x00, 0x01].to_vec().into());
let v: u16 = des.deserialize_be().unwrap();
assert_eq!(v, 1);
Source

pub fn deserialize<T>(&mut self) -> Result<T>

creates a new instance of T type struct, depleting exactly the right amount of bytes from ByteDeserializerBytes T must implement ByteDeserializeBytes trait

Source

pub fn deserialize_take<T>(&mut self, len: usize) -> Result<T>

creates a new instance of T type struct, depleting exactly len bytes from ByteDeserializerBytes. Intended for types with variable length such as Strings, Vec, etc.

Trait Implementations§

Source§

impl Clone for ByteDeserializerBytes

Source§

fn clone(&self) -> ByteDeserializerBytes

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for ByteDeserializerBytes

Source§

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

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

impl From<Vec<u8>> for ByteDeserializerBytes

This is a unit testing only impl

Source§

fn from(value: Vec<u8>) -> Self

This is a unit testing only impl

Source§

impl LowerHex for ByteDeserializerBytes

Provides a convenient way to view buffer content as both HEX and ASCII bytes where printable. supports both forms of alternate

use byteserde::des_bytes::ByteDeserializerBytes;
use bytes::Bytes;

let mut des = ByteDeserializerBytes::new(b"1234567890".as_ref().to_vec().into());
println ! ("{:#x}", des); // up to 16 bytes per line
println ! ("{:x}", des);  // single line
Source§

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

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

impl PartialEq for ByteDeserializerBytes

Source§

fn eq(&self, other: &ByteDeserializerBytes) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for ByteDeserializerBytes

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.