Struct nom8::input::Streaming

source ·
pub struct Streaming<I>(pub I);
Expand description

Mark the input as a partial buffer for streaming input.

Complete input means that we already have all of the data. This will be the common case with small files that can be read entirely to memory.

In contrast, streaming input assumes that we might not have all of the data. This can happen with some network protocol or large file parsers, where the input buffer can be full and need to be resized or refilled.

See also InputIsStreaming to tell whether the input supports complete or streaming parsing.

Example

Here is how it works in practice:

use nom8::{IResult, Err, Needed, error::{Error, ErrorKind}, bytes, character, input::Streaming};

fn take_streaming(i: Streaming<&[u8]>) -> IResult<Streaming<&[u8]>, &[u8]> {
  bytes::take(4u8)(i)
}

fn take_complete(i: &[u8]) -> IResult<&[u8], &[u8]> {
  bytes::take(4u8)(i)
}

// both parsers will take 4 bytes as expected
assert_eq!(take_streaming(Streaming(&b"abcde"[..])), Ok((Streaming(&b"e"[..]), &b"abcd"[..])));
assert_eq!(take_complete(&b"abcde"[..]), Ok((&b"e"[..], &b"abcd"[..])));

// if the input is smaller than 4 bytes, the streaming parser
// will return `Incomplete` to indicate that we need more data
assert_eq!(take_streaming(Streaming(&b"abc"[..])), Err(Err::Incomplete(Needed::new(1))));

// but the complete parser will return an error
assert_eq!(take_complete(&b"abc"[..]), Err(Err::Error(Error::new(&b"abc"[..], ErrorKind::Eof))));

// the alpha0 function recognizes 0 or more alphabetic characters
fn alpha0_streaming(i: Streaming<&str>) -> IResult<Streaming<&str>, &str> {
  character::alpha0(i)
}

fn alpha0_complete(i: &str) -> IResult<&str, &str> {
  character::alpha0(i)
}

// if there's a clear limit to the recognized characters, both parsers work the same way
assert_eq!(alpha0_streaming(Streaming("abcd;")), Ok((Streaming(";"), "abcd")));
assert_eq!(alpha0_complete("abcd;"), Ok((";", "abcd")));

// but when there's no limit, the streaming version returns `Incomplete`, because it cannot
// know if more input data should be recognized. The whole input could be "abcd;", or
// "abcde;"
assert_eq!(alpha0_streaming(Streaming("abcd")), Err(Err::Incomplete(Needed::new(1))));

// while the complete version knows that all of the data is there
assert_eq!(alpha0_complete("abcd"), Ok(("", "abcd")));

Tuple Fields§

§0: I

Implementations§

Convert to complete counterpart

Trait Implementations§

Casts the input type to a byte slice
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Compares self to another value for equality
Compares self to another value for equality independently of the case. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
The resulting type after dereferencing.
Dereferences the value.
The current input type is a sequence of that Item type. Read more
The type that will be produced
Create a new Extend of the correct type
Accumulate the input into an accumulator
Returns the byte position of the substring if it is found
Returns true if self contains the token
Converts the value of self to a hex dump, returning the owned String.
Converts the value of self to a hex dump beginning at from address, returning the owned String.
Complete counterpart Read more
Streaming counterpart Read more
Convert to complete counterpart
Convert to streaming counterpart
The current input type is a sequence of that Item type. Read more
An iterator over the input type, producing the item and its position for use with Slice. If we’re iterating over &str, the position corresponds to the byte index of the character
An iterator over the input type, producing the item
Returns an iterator over the elements and their byte offsets
Returns an iterator over the elements
Finds the byte position of the element
Get the byte offset from the element’s position in the stream
Calculates the input length, as indicated by its name, and the name of the trait itself
Returns a slice of count bytes. panics if count > length
Split the stream at the count byte offset. panics if count > length
The current input type is a sequence of that Item type. Read more
Looks for the first element of the input type for which the condition returns true, and returns the input up to this position. Read more
Looks for the first element of the input type for which the condition returns true, and returns the input up to this position. Read more
Looks for the first element of the input type for which the condition returns true and returns the input up to this position. Read more
Looks for the first element of the input type for which the condition returns true and returns the input up to this position. Read more
Output type
Convert an Input into an appropriate Output type
Convert an Output type to be used as Input
Number of indices input has advanced since start of parsing
Offset between the first byte of self and the first byte of the argument
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
Succeeds if parse() succeeded. The byte slice implementation will first convert it to a &str, then apply the parse() function
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Slices self according to the range argument

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.