pub struct ByteLen { /* private fields */ }
Expand description
Used to encode the number of items that a sequence type
(such as String::len
or a Vec::len
) has. This allows us
to represent that length with as few bytes as possible
while allowing it to be dynamic.
Implementations§
source§impl ByteLen
impl ByteLen
pub const MAX: u32 = 268_435_455u32
sourcepub fn encode(len: usize) -> Result<Self>
pub fn encode(len: usize) -> Result<Self>
Encodes the given length as bytes to be serialized.
Returns an Error::ByteLenTooLong
if the length
is bigger than ByteLen::MAX
.
sourcepub fn as_bytes(&self) -> &[u8] ⓘ
pub fn as_bytes(&self) -> &[u8] ⓘ
The raw bytes the represent the encoded length.
The size of the slice is always between 1 and ByteLen::MAX_SIZE
.
sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
The size in bytes of the encoded length.
Is always between 1 and ByteLen::MAX_SIZE
.
sourcepub fn decode(de: &mut Deserializer<'_>) -> Result<usize>
pub fn decode(de: &mut Deserializer<'_>) -> Result<usize>
Decodes a length from a sequence of bytes as a usize
.
It relies on the bytes to have previously been encoded
using ByteLen::encode
and for the given Deserializer
to be at the correct position at which those were written before.
It is up to the implementor to ensure that these invariants hold true.
Trait Implementations§
impl Copy for ByteLen
Auto Trait Implementations§
impl RefUnwindSafe for ByteLen
impl Send for ByteLen
impl Sync for ByteLen
impl Unpin for ByteLen
impl UnwindSafe for ByteLen
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> ToHex for Twhere
T: AsRef<[u8]>,
impl<T> ToHex for Twhere T: AsRef<[u8]>,
source§fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex<U>(&self) -> Uwhere U: FromIterator<char>,
self
into the result. Lower case
letters are used (e.g. f9b4ca
)source§fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex_upper<U>(&self) -> Uwhere U: FromIterator<char>,
self
into the result. Upper case
letters are used (e.g. F9B4CA
)