1 use crate::{ 2 error::ParseResult, 3 stream::{Positioned, RangeStreamOnce, ResetStream, StreamErrorFor, StreamOnce}, 4 }; 5 6 #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)] 7 pub struct Stream<S, U> { 8 pub stream: S, 9 pub state: U, 10 } 11 12 impl<S, U> Positioned for Stream<S, U> 13 where 14 S: Positioned, 15 { 16 #[inline] position(&self) -> Self::Position17 fn position(&self) -> Self::Position { 18 self.stream.position() 19 } 20 } 21 22 impl<S, U> ResetStream for Stream<S, U> 23 where 24 S: ResetStream, 25 { 26 type Checkpoint = S::Checkpoint; 27 28 #[inline] checkpoint(&self) -> Self::Checkpoint29 fn checkpoint(&self) -> Self::Checkpoint { 30 self.stream.checkpoint() 31 } 32 33 #[inline] reset(&mut self, checkpoint: Self::Checkpoint) -> Result<(), Self::Error>34 fn reset(&mut self, checkpoint: Self::Checkpoint) -> Result<(), Self::Error> { 35 self.stream.reset(checkpoint) 36 } 37 } 38 39 impl<S, U> StreamOnce for Stream<S, U> 40 where 41 S: StreamOnce, 42 { 43 type Token = S::Token; 44 type Range = S::Range; 45 type Position = S::Position; 46 type Error = S::Error; 47 48 #[inline] uncons(&mut self) -> Result<S::Token, StreamErrorFor<Self>>49 fn uncons(&mut self) -> Result<S::Token, StreamErrorFor<Self>> { 50 self.stream.uncons() 51 } 52 is_partial(&self) -> bool53 fn is_partial(&self) -> bool { 54 self.stream.is_partial() 55 } 56 } 57 58 impl<S, U> RangeStreamOnce for Stream<S, U> 59 where 60 S: RangeStreamOnce, 61 { 62 #[inline] uncons_range(&mut self, size: usize) -> Result<Self::Range, StreamErrorFor<Self>>63 fn uncons_range(&mut self, size: usize) -> Result<Self::Range, StreamErrorFor<Self>> { 64 self.stream.uncons_range(size) 65 } 66 67 #[inline] uncons_while<F>(&mut self, f: F) -> Result<Self::Range, StreamErrorFor<Self>> where F: FnMut(Self::Token) -> bool,68 fn uncons_while<F>(&mut self, f: F) -> Result<Self::Range, StreamErrorFor<Self>> 69 where 70 F: FnMut(Self::Token) -> bool, 71 { 72 self.stream.uncons_while(f) 73 } 74 uncons_while1<F>(&mut self, f: F) -> ParseResult<Self::Range, StreamErrorFor<Self>> where F: FnMut(Self::Token) -> bool,75 fn uncons_while1<F>(&mut self, f: F) -> ParseResult<Self::Range, StreamErrorFor<Self>> 76 where 77 F: FnMut(Self::Token) -> bool, 78 { 79 self.stream.uncons_while1(f) 80 } 81 82 #[inline] distance(&self, end: &Self::Checkpoint) -> usize83 fn distance(&self, end: &Self::Checkpoint) -> usize { 84 self.stream.distance(end) 85 } 86 87 #[inline] range(&self) -> Self::Range88 fn range(&self) -> Self::Range { 89 self.stream.range() 90 } 91 } 92