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) -> Omittable<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, { ... } fn slice(self) -> Slice<Self> { ... } fn buffered(self) -> Buffered<Self> { ... } }
An extension of Decode
trait.
Provided Methods
fn map<T, F>(self, f: F) -> Map<Self, T, F> where
F: Fn(Self::Item) -> T,
F: Fn(Self::Item) -> T,
Creates a decoder that converts decoded values by calling the given function.
Examples
use bytecodec::{Decode, DecodeExt}; use bytecodec::fixnum::U8Decoder; use bytecodec::io::IoDecodeExt; let mut decoder = U8Decoder::new().map(|b| b * 2); let item = decoder.decode_exact([10].as_ref()).unwrap(); assert_eq!(item, 20);
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>,
F: Fn(Self::Item) -> Result<T, E>,
Error: From<E>,
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, DecodeExt, ErrorKind, Result}; use bytecodec::fixnum::U8Decoder; use bytecodec::io::IoDecodeExt; let mut decoder = U8Decoder::new().try_map(|b| -> Result<_> { track_assert_ne!(b, 0, ErrorKind::InvalidInput); Ok(b * 2) }); let error = decoder.decode_exact([0].as_ref()).err().unwrap(); assert_eq!(*error.kind(), ErrorKind::InvalidInput); let item = decoder.decode_exact([4].as_ref()).unwrap(); assert_eq!(item, 8);
fn map_err<F, E>(self, f: F) -> MapErr<Self, F, E> where
F: Fn(Error) -> E,
Error: From<E>,
F: Fn(Error) -> E,
Error: From<E>,
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, DecodeExt}; use bytecodec::fixnum::U16beDecoder; use bytecodec::io::IoDecodeExt; let mut decoder = U16beDecoder::new().map_err(|e| track!(e, "oops!")); // or `track_err!(U16beDecoder::new(), "oops!")` let input = [0]; // Insufficient bytes let error = track!(decoder.decode_exact(input.as_ref())).err().unwrap(); assert_eq!(error.to_string(), "\ UnexpectedEos (cause; assertion failed: `!eos.is_reached()`; \ self.offset=1, self.bytes.as_ref().len()=2) HISTORY: [0] at src/bytes.rs:155 [1] at src/fixnum.rs:196 [2] at src/decode.rs:12 -- oops! [3] at src/io.rs:44 [4] at src/decode.rs:16\n");
fn and_then<D, F>(self, f: F) -> AndThen<Self, D, F> where
F: Fn(Self::Item) -> D,
D: Decode,
F: Fn(Self::Item) -> D,
D: Decode,
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, DecodeExt}; use bytecodec::bytes::Utf8Decoder; use bytecodec::fixnum::U8Decoder; use bytecodec::io::IoDecodeExt; let mut decoder = U8Decoder::new().and_then(|len| Utf8Decoder::new().length(len as u64)); let item = decoder.decode_exact(b"\x03foobar".as_ref()).unwrap(); assert_eq!(item, "foo");
fn chain<D: Decode>(self, other: D) -> DecoderChain<Self, D, Self::Item>
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, DecodeExt, StartDecoderChain}; use bytecodec::fixnum::U8Decoder; use bytecodec::io::IoDecodeExt; let mut decoder = StartDecoderChain .chain(U8Decoder::new()) .chain(U8Decoder::new()) .chain(U8Decoder::new()); let mut input = &b"foobar"[..]; let item = decoder.decode_exact(&mut input).unwrap(); assert_eq!(item, (b'f', b'o', b'o')); let item = decoder.decode_exact(&mut input).unwrap(); assert_eq!(item, (b'b', b'a', b'r'));
fn collect<T>(self) -> Collect<Self, T> where
T: Extend<Self::Item> + Default,
T: Extend<Self::Item> + Default,
Creates a decoder for collecting decoded items.
Examples
use bytecodec::{Decode, DecodeExt}; use bytecodec::fixnum::U8Decoder; use bytecodec::io::IoDecodeExt; let mut decoder = U8Decoder::new().collect::<Vec<_>>(); let item = decoder.decode_exact(b"foo".as_ref()).unwrap(); assert_eq!(item, vec![b'f', b'o', b'o']);
fn length(self, expected_bytes: u64) -> Length<Self>
Creates a decoder that consumes the specified number of bytes exactly.
Examples
use bytecodec::{Decode, DecodeExt, ErrorKind}; use bytecodec::bytes::Utf8Decoder; use bytecodec::io::IoDecodeExt; let mut decoder = Utf8Decoder::new().length(3); let mut input = &b"foobarba"[..]; let item = decoder.decode_exact(&mut input).unwrap(); assert_eq!(item, "foo"); let item = decoder.decode_exact(&mut input).unwrap(); assert_eq!(item, "bar"); let error = decoder.decode_exact(&mut input).err().unwrap(); assert_eq!(*error.kind(), ErrorKind::UnexpectedEos);
fn take(self, n: usize) -> Take<Self>
Creates a decoder that decodes n
items by using self
.
Examples
use bytecodec::{Decode, DecodeExt}; use bytecodec::fixnum::U8Decoder; use bytecodec::io::IoDecodeExt; let mut decoder = U8Decoder::new().take(2).collect::<Vec<_>>(); let item = decoder.decode_exact(b"foo".as_ref()).unwrap(); assert_eq!(item, vec![b'f', b'o']);
fn omit(self, do_omit: bool) -> Omittable<Self>
Creates a decoder that will omit decoding items if do_omit = true
is specified.
Examples
use bytecodec::{Decode, DecodeExt}; use bytecodec::fixnum::U8Decoder; use bytecodec::io::IoDecodeExt; let mut input = &b"foo"[..]; let mut decoder = U8Decoder::new().omit(true); let item = decoder.decode_exact(&mut input).unwrap(); assert_eq!(item, None); let mut decoder = U8Decoder::new().omit(false); let item = decoder.decode_exact(&mut input).unwrap(); assert_eq!(item, Some(b'f'));
fn skip_remaining(self) -> SkipRemaining<Self>
Creates a decoder for skipping the remaining bytes in an input byte sequence
after decoding an item by using self
.
Examples
use bytecodec::{Decode, DecodeExt}; use bytecodec::fixnum::U8Decoder; use bytecodec::io::IoDecodeExt; let mut input = &b"foo"[..]; let mut decoder = U8Decoder::new().skip_remaining(); let item = decoder.decode_exact(&mut input).unwrap(); assert_eq!(item, b'f'); assert!(input.is_empty());
fn max_bytes(self, bytes: u64) -> MaxBytes<Self>
Creates a decoder that will fail if the number of consumed bytes exceeds bytes
.
Examples
use bytecodec::{Decode, DecodeExt, ErrorKind}; use bytecodec::bytes::Utf8Decoder; use bytecodec::io::IoDecodeExt; let mut decoder = Utf8Decoder::new().max_bytes(3); let item = decoder.decode_exact(b"foo".as_ref()).unwrap(); assert_eq!(item, "foo"); // OK let error = decoder.decode_exact(b"hello".as_ref()).err(); assert_eq!(error.map(|e| *e.kind()), Some(ErrorKind::InvalidInput)); // Error
fn assert<F>(self, f: F) -> Assert<Self, F> where
F: for<'a> Fn(&'a Self::Item) -> bool,
F: for<'a> Fn(&'a Self::Item) -> bool,
Creates a decoder that will fail if the given assertion function returns false
.
Examples
use bytecodec::{Decode, DecodeExt, ErrorKind}; use bytecodec::fixnum::U8Decoder; use bytecodec::io::IoDecodeExt; let mut decoder = U8Decoder::new().assert(|&b| b == 3); let mut input = &[3, 4][..]; let item = decoder.decode_exact(&mut input).unwrap(); assert_eq!(item, 3); let error = decoder.decode_exact(&mut input).err(); assert_eq!(error.map(|e| *e.kind()), Some(ErrorKind::InvalidInput));
fn slice(self) -> Slice<Self>
Creates a decoder that makes it possible to slice the input byte sequence in arbitrary units.
Slicing an input byte sequence makes it easier to demultiplex multiple sequences from it.
Examples
use bytecodec::{Decode, DecodeExt, Eos}; use bytecodec::bytes::Utf8Decoder; let mut decoder0 = Utf8Decoder::new().length(3).slice(); let mut decoder1 = Utf8Decoder::new().length(3).slice(); let eos = Eos::new(true); let input = b"fboaor"; let mut offset = 0; let mut last_item0 = None; let mut last_item1 = None; for _ in 0..3 { decoder0.set_consumable_bytes(1); let (size, item) = decoder0.decode(&input[offset..], eos).unwrap(); offset += size; last_item0 = item; decoder1.set_consumable_bytes(1); let (size, item) = decoder1.decode(&input[offset..], eos).unwrap(); offset += size; last_item1 = item; } assert_eq!(offset, input.len()); assert_eq!(last_item0, Some("foo".to_owned())); assert_eq!(last_item1, Some("bar".to_owned()));
fn buffered(self) -> Buffered<Self>
Creates a decoder that buffers the last decoded item.
Examples
use bytecodec::{Decode, DecodeExt, Eos, StartDecoderChain}; use bytecodec::fixnum::U8Decoder; let mut decoder = StartDecoderChain .chain(U8Decoder::new()) .chain(U8Decoder::new()) .chain(U8Decoder::new()) .buffered(); let (size, item) = decoder.decode(b"foo", Eos::new(false)).unwrap(); assert_eq!(size, 3); assert_eq!(item, None); assert_eq!(decoder.take_item(), Some((b'f', b'o', b'o'))); assert_eq!(decoder.has_item(), false);
Implementors
impl<T: Decode> DecodeExt for T