Struct BytesDecoder

Source
pub struct BytesDecoder<B = Vec<u8>> { /* private fields */ }
Expand description

BytesDecoder copies bytes from an input sequence to a slice.

This is a oneshot decoder (i.e., it decodes only one item).

§Examples

use bytecodec::Decode;
use bytecodec::bytes::BytesDecoder;
use bytecodec::io::IoDecodeExt;

let mut decoder = BytesDecoder::new([0; 3]);
assert_eq!(decoder.requiring_bytes().to_u64(), Some(3));

let item = decoder.decode_exact(b"foobar".as_ref()).unwrap();
assert_eq!(item.as_ref(), b"foo");
assert_eq!(decoder.requiring_bytes().to_u64(), Some(0)); // no more items are decoded

Implementations§

Source§

impl<B: AsRef<[u8]> + AsMut<[u8]>> BytesDecoder<B>

Source

pub fn new(bytes: B) -> Self

Makes a new BytesDecoder instance for filling the given byte slice.

Source

pub fn set_bytes(&mut self, bytes: B)

Sets the byte slice to be filled.

Trait Implementations§

Source§

impl<B: Debug> Debug for BytesDecoder<B>

Source§

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

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

impl<B: AsRef<[u8]> + AsMut<[u8]>> Decode for BytesDecoder<B>

Source§

type Item = B

The type of items to be decoded.
Source§

fn decode(&mut self, buf: &[u8], eos: Eos) -> Result<usize>

Consumes the given buffer (a part of a byte sequence), and proceeds the decoding process. Read more
Source§

fn finish_decoding(&mut self) -> Result<Self::Item>

Finishes the current decoding process and returns the decoded item. Read more
Source§

fn requiring_bytes(&self) -> ByteCount

Returns the lower bound of the number of bytes needed to decode the next item. Read more
Source§

fn is_idle(&self) -> bool

Returns true if there are no items to be decoded by the decoder at the next invocation of decode method, otherwise false. Read more
Source§

impl<B> Default for BytesDecoder<B>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<B> Freeze for BytesDecoder<B>
where B: Freeze,

§

impl<B> RefUnwindSafe for BytesDecoder<B>
where B: RefUnwindSafe,

§

impl<B> Send for BytesDecoder<B>
where B: Send,

§

impl<B> Sync for BytesDecoder<B>
where B: Sync,

§

impl<B> Unpin for BytesDecoder<B>
where B: Unpin,

§

impl<B> UnwindSafe for BytesDecoder<B>
where B: UnwindSafe,

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> DecodeExt for T
where T: Decode,

Source§

fn map<T, F>(self, f: F) -> Map<Self, T, F>
where F: Fn(Self::Item) -> T,

Creates a decoder that converts decoded values by calling the given function. Read more
Source§

fn try_map<T, E, F>(self, f: F) -> TryMap<Self, T, E, F>
where F: Fn(Self::Item) -> Result<T, E>, Error: From<E>,

Creates a decoder that tries to convert decoded values by calling the given function. Read more
Source§

fn map_err<E, F>(self, f: F) -> MapErr<Self, E, F>
where F: Fn(Error) -> E, Error: From<E>,

Creates a decoder for modifying decoding errors produced by self. Read more
Source§

fn and_then<D, F>(self, f: F) -> AndThen<Self, D, F>
where F: Fn(Self::Item) -> D, D: Decode,

Creates a decoder that enables conditional decoding. Read more
Source§

fn collect<T>(self) -> Collect<Self, T>
where T: Extend<Self::Item> + Default,

Creates a decoder for collecting decoded items. Read more
Source§

fn collectn<T>(self, n: usize) -> CollectN<Self, T>
where T: Extend<Self::Item> + Default,

Creates a decoder that decodes n items by using self and collecting the result. Read more
Source§

fn length(self, expected_bytes: u64) -> Length<Self>

Creates a decoder that consumes the specified number of bytes exactly. Read more
Source§

fn omit(self, do_omit: bool) -> Omittable<Self>

Creates a decoder that will omit decoding items if do_omit = true is specified. Read more
Source§

fn max_bytes(self, bytes: u64) -> MaxBytes<Self>

Creates a decoder that will fail if the number of consumed bytes exceeds bytes. Read more
Source§

fn chain<T: Decode>(self, other: T) -> TupleDecoder<(Self, T)>

Takes two decoders and creates a new decoder that decodes both items in sequence. Read more
Source§

fn slice(self) -> Slice<Self>

Creates a decoder that makes it possible to slice the input byte sequence in arbitrary units. Read more
Source§

fn peekable(self) -> Peekable<Self>

Creates a decoder that enables to peek decoded items before calling finish_decoding method. Read more
Source§

fn maybe_eos(self) -> MaybeEos<Self>

Creates a decoder that ignores EOS if there is no item being decoded. Read more
Source§

fn decode_from_bytes(&mut self, buf: &[u8]) -> Result<Self::Item>

Decodes an item by consuming the whole part of the given bytes. 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> IoDecodeExt for T
where T: Decode,

Source§

fn decode_from_read_buf<B>(&mut self, buf: &mut ReadBuf<B>) -> Result<()>
where B: AsRef<[u8]>,

Consumes bytes from the given read buffer and proceeds the decoding process.
Source§

fn decode_exact<R: Read>(&mut self, reader: R) -> Result<Self::Item>

Decodes an item from the given reader. 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.