parseme/parser/
stream.rs

1// extern crate core;
2
3use core::marker::PhantomData;
4
5use crate::{parser::Parser, stream::Stream};
6
7/// A [Stream] which wraps a [Parser] and its input.
8///
9/// Calling [Stream::next] will call the [Parser::parse] method of the wrapped [Parser], with the
10/// input that the [ParserStream] was initialized with.
11#[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
12pub struct ParserStream<'input, 'parser, In: ?Sized, Out, Err, P: Parser<In, Out, Err>> {
13    marker: PhantomData<(Out, Err)>,
14    input: &'input mut In,
15    parser: &'parser mut P,
16}
17
18impl<'input, 'parser, In: ?Sized, Out, Err, P: Parser<In, Out, Err>>
19    ParserStream<'input, 'parser, In, Out, Err, P>
20{
21    /// Creates a new [ParserStream] from the provided input and [Parser].
22    pub fn new(input: &'input mut In, parser: &'parser mut P) -> Self {
23        Self {
24            marker: PhantomData,
25            input,
26            parser,
27        }
28    }
29}
30
31impl<'input, 'parser, In: ?Sized, Out, Err, P: Parser<In, Out, Err>> Stream
32    for ParserStream<'input, 'parser, In, Out, Err, P>
33{
34    type Item = Out;
35    type Error = Err;
36
37    #[inline]
38    fn next(&mut self) -> Result<Self::Item, Self::Error> {
39        self.parser.parse(self.input)
40    }
41}