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
impl ByteDeserializerBytes
pub fn new(bytes: Bytes) -> ByteDeserializerBytes
Sourcepub fn idx(&self) -> usize
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()
Sourcepub fn remaining(&self) -> usize
pub fn remaining(&self) -> usize
Number of bytes remaining to be deserialized, this is an inverse of Self::idx()
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
Sourcepub fn deserialize_bytes_slice_remaining(&mut self) -> &[u8] ⓘ
pub fn deserialize_bytes_slice_remaining(&mut self) -> &[u8] ⓘ
consumes all of the remaining bytes in the buffer and returns them as slice
Sourcepub fn deserialize_bytes_slice(&mut self, len: usize) -> Result<&[u8]>
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()
pub fn deserialize_u8(&mut self) -> Result<u8>
pub fn deserialize_i8(&mut self) -> Result<i8>
pub fn peek_bytes_slice(&self, len: usize) -> Result<&[u8]>
pub fn peek_bytes(&self, at: usize) -> Result<Bytes>
pub fn deserialize_bytes_array_ref<const N: usize>( &mut self, ) -> Result<&[u8; N]>
Sourcepub fn deserialize_ne<const N: usize, T: FromNeBytes<N, T>>(
&mut self,
) -> Result<T>
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();Sourcepub fn deserialize_le<const N: usize, T: FromLeBytes<N, T>>(
&mut self,
) -> Result<T>
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);Sourcepub fn deserialize_be<const N: usize, T: FromBeBytes<N, T>>(
&mut self,
) -> Result<T>
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);Sourcepub fn deserialize<T>(&mut self) -> Result<T>where
T: ByteDeserializeBytes<T>,
pub fn deserialize<T>(&mut self) -> Result<T>where
T: ByteDeserializeBytes<T>,
creates a new instance of T type struct, depleting exactly the right amount of bytes from ByteDeserializerBytes
T must implement ByteDeserializeBytes trait
Sourcepub fn deserialize_take<T>(&mut self, len: usize) -> Result<T>where
T: ByteDeserializeBytes<T>,
pub fn deserialize_take<T>(&mut self, len: usize) -> Result<T>where
T: ByteDeserializeBytes<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
impl Clone for ByteDeserializerBytes
Source§fn clone(&self) -> ByteDeserializerBytes
fn clone(&self) -> ByteDeserializerBytes
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ByteDeserializerBytes
impl Debug for ByteDeserializerBytes
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
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