Trait bytecodec::DecodeExt [] [src]

pub trait DecodeExt: Decode + Sized {
    fn map<T, F>(self, f: F) -> Map<Self, T, F>
    where
        F: Fn(Self::Item) -> T
, { ... }
fn try_map<F, T, E>(self, f: F) -> TryMap<Self, F, T, E>
    where
        F: Fn(Self::Item) -> Result<T, E>,
        Error: From<E>
, { ... }
fn map_err<F, E>(self, f: F) -> MapErr<Self, F, E>
    where
        F: Fn(Error) -> E,
        Error: From<E>
, { ... }
fn and_then<D, F>(self, f: F) -> AndThen<Self, D, F>
    where
        F: Fn(Self::Item) -> D,
        D: Decode
, { ... }
fn chain<D: Decode>(self, other: D) -> DecoderChain<Self, D, Self::Item> { ... }
fn collect<T>(self) -> Collect<Self, T>
    where
        T: Extend<Self::Item> + Default
, { ... }
fn length(self, expected_bytes: u64) -> Length<Self> { ... }
fn take(self, n: usize) -> Take<Self> { ... }
fn omit(self, do_omit: bool) -> Omit<Self> { ... }
fn skip_remaining(self) -> SkipRemaining<Self> { ... }
fn max_bytes(self, bytes: u64) -> MaxBytes<Self> { ... }
fn assert<F>(self, f: F) -> Assert<Self, F>
    where
        F: for<'a> Fn(&'a Self::Item) -> bool
, { ... } }

An extension of Decode trait.

Provided Methods

Creates a decoder that converts decoded values by calling the given function.

Examples

use bytecodec::{Decode, DecodeBuf, DecodeExt};
use bytecodec::fixnum::U8Decoder;

let mut decoder = U8Decoder::new().map(|b| b * 2);
let item = decoder.decode(&mut DecodeBuf::new(&[10][..])).unwrap();
assert_eq!(item, Some(20));

Creates a decoder that tries to convert decoded values by calling the given function.

Examples

extern crate bytecodec;
#[macro_use]
extern crate trackable;

use bytecodec::{Decode, DecodeBuf, DecodeExt, ErrorKind, Result};
use bytecodec::fixnum::U8Decoder;

let mut decoder = U8Decoder::new().try_map(|b| -> Result<_> {
    track_assert_ne!(b, 0, ErrorKind::InvalidInput);
    Ok(b * 2)
});
let mut input = DecodeBuf::new(&[0, 4][..]);

let error = decoder.decode(&mut input).err().unwrap();
assert_eq!(*error.kind(), ErrorKind::InvalidInput);

let item = decoder.decode(&mut input).unwrap();
assert_eq!(item, Some(8));

Creates a decoder for modifying decoding errors produced by self.

Examples

The following code shows the idiomatic way to track decoding errors:

extern crate bytecodec;
#[macro_use]
extern crate trackable;

use bytecodec::{Decode, DecodeBuf, DecodeExt};
use bytecodec::fixnum::U16beDecoder;

let mut decoder =
    U16beDecoder::new().map_err(|e| track!(e, "oops!"));
    // or `track_err!(U16beDecoder::new(), "oops!")`

let mut input =
    DecodeBuf::with_remaining_bytes(&[10][..], 0); // Insufficient bytes

let error = track!(decoder.decode(&mut input)).err().unwrap();

assert_eq!(error.to_string(), "\
UnexpectedEos (cause; assertion failed: `!buf.is_eos()`)
HISTORY:
  [0] at src/bytes.rs:144
  [1] at src/fixnum.rs:199
  [2] at src/decode.rs:11 -- oops!
  [3] at src/decode.rs:17\n");

Creates a decoder that enables conditional decoding.

If the first item is successfully decoded, it will start decoding the second item by using the decoder returned by f function.

Examples

Decodes a length-prefixed string:

use bytecodec::{Decode, DecodeBuf, DecodeExt};
use bytecodec::bytes::Utf8Decoder;
use bytecodec::fixnum::U8Decoder;

let mut decoder = U8Decoder::new().and_then(|len| Utf8Decoder::new().length(len as u64));
let mut input = DecodeBuf::new(b"\x03foobar");

let item = decoder.decode(&mut input).unwrap();
assert_eq!(item, Some("foo".to_owned()));

Takes two decoders and creates a new decoder that decodes both items in sequence.

Chains are started by calling StartDecoderChain::chain method.

Examples

use bytecodec::{Decode, DecodeBuf, DecodeExt, StartDecoderChain};
use bytecodec::fixnum::U8Decoder;

let mut decoder = StartDecoderChain
    .chain(U8Decoder::new())
    .chain(U8Decoder::new())
    .chain(U8Decoder::new());

let mut input = DecodeBuf::new(b"foobar");

let item = decoder.decode(&mut input).unwrap();
assert_eq!(item, Some((b'f', b'o', b'o')));

let item = decoder.decode(&mut input).unwrap();
assert_eq!(item, Some((b'b', b'a', b'r')));

Creates a decoder for collecting decoded items.

Examples

use bytecodec::{Decode, DecodeBuf, DecodeExt};
use bytecodec::fixnum::U8Decoder;

let mut decoder = U8Decoder::new().collect::<Vec<_>>();
let mut input = DecodeBuf::with_remaining_bytes(b"foo", 0);

let item = decoder.decode(&mut input).unwrap();
assert_eq!(item, Some(vec![b'f', b'o', b'o']));

Creates a decoder that consumes the specified number of bytes exactly.

Examples

use bytecodec::{Decode, DecodeBuf, DecodeExt, ErrorKind};
use bytecodec::bytes::Utf8Decoder;

let mut decoder = Utf8Decoder::new().length(3);
let mut input = DecodeBuf::with_remaining_bytes(b"foobarba", 0);

let item = decoder.decode(&mut input).unwrap();
assert_eq!(item, Some("foo".to_owned()));

let item = decoder.decode(&mut input).unwrap();
assert_eq!(item, Some("bar".to_owned()));

let error = decoder.decode(&mut input).err().unwrap();
assert_eq!(*error.kind(), ErrorKind::UnexpectedEos);

Creates a decoder that decodes n items by using self.

Examples

use bytecodec::{Decode, DecodeBuf, DecodeExt};
use bytecodec::fixnum::U8Decoder;

let mut decoder = U8Decoder::new().take(2).collect::<Vec<_>>();
let mut input = DecodeBuf::new(b"foo");

let item = decoder.decode(&mut input).unwrap();
assert_eq!(item, Some(vec![b'f', b'o']));

Creates a decoder that will omit decoding items if do_omit = true is specified.

Examples

use bytecodec::{Decode, DecodeBuf, DecodeExt};
use bytecodec::fixnum::U8Decoder;

let mut input = DecodeBuf::new(b"foo");

let mut decoder = U8Decoder::new().omit(true);
let item = decoder.decode(&mut input).unwrap();
assert_eq!(item, Some(None));

let mut decoder = U8Decoder::new().omit(false);
let item = decoder.decode(&mut input).unwrap();
assert_eq!(item, Some(Some(b'f')));

Creates a decoder for skipping the remaining bytes in an input byte sequence after decoding an item by using self.

Examples

use bytecodec::{Decode, DecodeBuf, DecodeExt};
use bytecodec::fixnum::U8Decoder;

let mut input = DecodeBuf::with_remaining_bytes(b"foo", 0);

let mut decoder = U8Decoder::new().skip_remaining();
let item = decoder.decode(&mut input).unwrap();
assert_eq!(item, Some(b'f'));
assert!(input.is_empty() && input.is_eos());

Creates a decoder that will fail if the number of consumed bytes exceeds bytes.

Examples

use bytecodec::{Decode, DecodeBuf, DecodeExt, ErrorKind};
use bytecodec::bytes::Utf8Decoder;

let mut decoder = Utf8Decoder::new().max_bytes(3);

let mut input = DecodeBuf::with_remaining_bytes(b"foo", 0);
let item = decoder.decode(&mut input).unwrap();
assert_eq!(item, Some("foo".to_owned())); // OK

let mut input = DecodeBuf::with_remaining_bytes(b"hello", 0);
let error = decoder.decode(&mut input).err();
assert_eq!(error.map(|e| *e.kind()), Some(ErrorKind::InvalidInput)); // Error

Creates a decoder that will fail if the given assertion function returns false.

Examples

use bytecodec::{Decode, DecodeBuf, DecodeExt, ErrorKind};
use bytecodec::fixnum::U8Decoder;

let mut decoder = U8Decoder::new().assert(|&b| b == 3);

let mut input = DecodeBuf::new(&[3, 4][..]);
let item = decoder.decode(&mut input).unwrap();
assert_eq!(item, Some(3));

let error = decoder.decode(&mut input).err();
assert_eq!(error.map(|e| *e.kind()), Some(ErrorKind::InvalidInput));

Implementors