Decoder

Struct Decoder 

Source
pub struct Decoder<'buf>(/* private fields */);
Expand description

The type to decode data from a buffer.

§Examples

use co::AsDecoder;

let mut dec = [0, 1, 2, 1, 0].as_decoder();

let a = dec.u16()?;
assert_eq!(a, 1);

let b = dec.u8()?;
assert_eq!(b, 2);

let c = dec.u16()?;
assert_eq!(c, 256);

Implementations§

Source§

impl<'buf> Decoder<'buf>

Source

pub fn decode<D>(&mut self) -> Option<D>
where D: Decode<'buf>,

Decodes a value from the buffer.

Source

pub fn decode_with<D>(&mut self, state: D::State) -> Option<D>
where D: DecodeWith<'buf>,

Decodes a value from the buffer with passed state.

Source

pub fn u8(&mut self) -> Option<u8>

Decodes an u8 value.

§Examples
use co::AsDecoder;

let mut dec = [3].as_decoder();

let n = dec.u8()?;
assert_eq!(n, 3);
Source

pub fn u16(&mut self) -> Option<u16>

Decodes an u16 value with big-endian byte ordering.

§Examples
use co::AsDecoder;

let mut dec = [0, 3].as_decoder();

let n = dec.u16()?;
assert_eq!(n, 3);
Source

pub fn u32(&mut self) -> Option<u32>

Decodes an u32 value with big-endian byte ordering.

§Examples
use co::AsDecoder;

let mut dec = [0, 0, 0, 3].as_decoder();

let n = dec.u32()?;
assert_eq!(n, 3);
Source

pub fn u64(&mut self) -> Option<u64>

Decodes an u64 value with big-endian byte ordering.

§Examples
use co::AsDecoder;

let mut dec = [0, 0, 0, 0, 0, 0, 0, 3].as_decoder();

let n = dec.u64()?;
assert_eq!(n, 3);
Source

pub fn u128(&mut self) -> Option<u128>

Decodes an u128 value with big-endian byte ordering.

§Examples
use co::AsDecoder;

let mut dec = [
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 3,
].as_decoder();

let n = dec.u128()?;
assert_eq!(n, 3);
Source

pub fn usize(&mut self) -> Option<usize>

Decodes an usize value with big-endian byte ordering.

§Examples
use co::AsDecoder;

let mut buf = [0; size_of::<usize>()];
let [.., m] = &mut buf;
*m = 3;

let mut dec = buf.as_decoder();

let n = dec.usize()?;
assert_eq!(n, 3);
Source

pub fn end(&mut self) -> Option<()>

Checks if the buffer has ended.

§Examples
use co::AsDecoder;

let mut dec = [0, 1, 2].as_decoder();

// The buffer has not ended yet
assert!(dec.end().is_none());

dec.u8()?;
dec.u8()?;
dec.u8()?;

// The buffer has already ended
dec.end()?;

Auto Trait Implementations§

§

impl<'buf> Freeze for Decoder<'buf>

§

impl<'buf> RefUnwindSafe for Decoder<'buf>

§

impl<'buf> Send for Decoder<'buf>

§

impl<'buf> Sync for Decoder<'buf>

§

impl<'buf> Unpin for Decoder<'buf>

§

impl<'buf> UnwindSafe for Decoder<'buf>

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> 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, 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.