[][src]Trait whiteread::stream::FromStream

pub trait FromStream: Sized {
    const REQUEST_CHEAP_ERROR: bool;

    fn read<I: StrStream>(it: &mut I) -> Result<Self>;
}

Trait for values that can be parsed from stream of whitespace-separated words.

Implementations for primitives consume and parse one element from a stream (advancing a stream). Implementations for tuples just parse elements from left to right. Implementation for vector parses till the end of stream.

See adapters module for more types that implement this trait in a special way.

Examples

Using a trait directly

use whiteread::FromStream;
let mut stream = "123".split_whitespace();
assert_eq!(<i32 as FromStream>::read(&mut stream).unwrap(), 123)

Semantics of provided trait implementations:

// tuples (up to 6)
assert_eq!(parse_string("2 1 3 4").ok(), Some( ((2, 1), (3, 4)) ));

// optional elements at the end of stream
assert_eq!(parse_string("2 1 3").ok(), Some( ((2, 1), Some(3)) ));
assert_eq!(parse_string("2 1").ok(), Some( ((2, 1), None::<i32>) ));

// eager vector
assert_eq!(parse_string("2 1 3 4").ok(), Some( vec![2, 1, 3, 4] ));

// vec prefixed with length
assert_eq!(parse_string("2 1 3").ok(), Some( Lengthed(vec![1, 3]) ));

// placeholder
assert_eq!(parse_string("spam 3").ok(), Some( (Skip, 3) ));

// you can mix impls of course
assert_eq!(parse_string("a 1 b 2").ok(), Some( vec![('a', 1), ('b', 2)] ));

There are a few more adapter structs in adapters module, which implement the FromStream trait in various ways.

Associated Constants

const REQUEST_CHEAP_ERROR: bool

Whether a function returning this type should produce cheap errors instead of pretty-printed ones.

For user-facing errors, this should be set to false, so that user can see a nice rendered error. This is the default behaviour. If you expect the error to happen on a hot path and you don't just pass it up, this should be set to false. See WithCheapError.

Loading content...

Required methods

fn read<I: StrStream>(it: &mut I) -> Result<Self>

Reads a value from a str-producing stream.

Loading content...

Implementations on Foreign Types

impl FromStream for bool[src]

impl FromStream for u8[src]

impl FromStream for u16[src]

impl FromStream for u32[src]

impl FromStream for u64[src]

impl FromStream for usize[src]

impl FromStream for i8[src]

impl FromStream for i16[src]

impl FromStream for i32[src]

impl FromStream for i64[src]

impl FromStream for isize[src]

impl FromStream for String[src]

impl FromStream for f32[src]

impl FromStream for f64[src]

impl FromStream for char[src]

impl FromStream for ()[src]

impl<A: FromStream> FromStream for (A,)[src]

impl<A: FromStream, B: FromStream> FromStream for (A, B)[src]

impl<A: FromStream, B: FromStream, C: FromStream> FromStream for (A, B, C)[src]

impl<A: FromStream, B: FromStream, C: FromStream, D: FromStream> FromStream for (A, B, C, D)[src]

impl<A: FromStream, B: FromStream, C: FromStream, D: FromStream, E: FromStream> FromStream for (A, B, C, D, E)[src]

impl<A: FromStream, B: FromStream, C: FromStream, D: FromStream, E: FromStream, F: FromStream> FromStream for (A, B, C, D, E, F)[src]

impl<T: FromStream> FromStream for Option<T>[src]

Option<T> is read from the stream in a similar way as T, except of returning None instead of TooShort(Nothing) error variant.

This allows for graceful handling of end-of-stream condition.

Note that partially parsed value (eg. half of a pair) is still considered an error.

impl<T: FromStream> FromStream for Vec<T>[src]

Vector of elements is read by attempting to read elements from stream until it's drained.

Loading content...

Implementors

impl FromStream for Skip[src]

impl FromStream for SkipAll[src]

impl<T: FromStream + Default + PartialEq> FromStream for Zeroed<T>[src]

impl<T: FromStream> FromStream for Lengthed<T>[src]

impl<T: FromStream> FromStream for WithCheapError<T>[src]

Loading content...