From f57337cb675425138b4cc98893e8ca27c0d97a26 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 13 Jul 2023 10:50:29 -0500 Subject: [PATCH] WIP --- src/ascii/mod.rs | 62 ++++++++++++++++++++++++++++++++--------------- src/parser.rs | 2 +- src/stream/mod.rs | 32 ++++++++++++------------ src/token/mod.rs | 32 ++++++++++++------------ 4 files changed, 75 insertions(+), 53 deletions(-) diff --git a/src/ascii/mod.rs b/src/ascii/mod.rs index 8b3119fb..720bf966 100644 --- a/src/ascii/mod.rs +++ b/src/ascii/mod.rs @@ -102,7 +102,7 @@ where I: StreamIsPartial, I: Stream, I: Compare<&'static str>, - ::Token: AsChar + Clone, + for<'t> &'t ::Token: AsChar, { trace("not_line_ending", move |input: &mut I| { if ::is_partial_supported() { @@ -121,7 +121,7 @@ where I: StreamIsPartial, I: Stream, I: Compare<&'static str>, - ::Token: AsChar + Clone, + for<'t> &'t ::Token: AsChar, { let res = take_till0(('\r', '\n')).parse_next(input)?; if input.compare("\r") == CompareResult::Ok { @@ -216,7 +216,8 @@ pub fn newline>(input: &mut I) -> PResult where I: StreamIsPartial, I: Stream, - ::Token: AsChar + Clone, + ::Token: AsChar, + for<'t> &'t ::Token: AsChar, { trace("newline", '\n'.map(AsChar::as_char)).parse_next(input) } @@ -256,7 +257,8 @@ pub fn tab>(input: &mut I) -> PResult where I: StreamIsPartial, I: Stream, - ::Token: AsChar + Clone, + ::Token: AsChar, + for<'t> &'t ::Token: AsChar, { trace("tab", '\t'.map(AsChar::as_char)).parse_next(input) } @@ -299,6 +301,7 @@ where I: StreamIsPartial, I: Stream, ::Token: AsChar, + for<'t> &'t ::Token: AsChar, { trace("alpha0", take_while(0.., AsChar::is_alpha)).parse_next(input) } @@ -341,6 +344,7 @@ where I: StreamIsPartial, I: Stream, ::Token: AsChar, + for<'t> &'t ::Token: AsChar, { trace("alpha1", take_while(1.., AsChar::is_alpha)).parse_next(input) } @@ -384,6 +388,7 @@ where I: StreamIsPartial, I: Stream, ::Token: AsChar, + for<'t> &'t ::Token: AsChar, { trace("digit0", take_while(0.., AsChar::is_dec_digit)).parse_next(input) } @@ -443,6 +448,7 @@ where I: StreamIsPartial, I: Stream, ::Token: AsChar, + for<'t> &'t ::Token: AsChar, { trace("digit1", take_while(1.., AsChar::is_dec_digit)).parse_next(input) } @@ -485,6 +491,7 @@ where I: StreamIsPartial, I: Stream, ::Token: AsChar, + for<'t> &'t ::Token: AsChar, { trace("hex_digit0", take_while(0.., AsChar::is_hex_digit)).parse_next(input) } @@ -528,6 +535,7 @@ where I: StreamIsPartial, I: Stream, ::Token: AsChar, + for<'t> &'t ::Token: AsChar, { trace("hex_digit1", take_while(1.., AsChar::is_hex_digit)).parse_next(input) } @@ -570,6 +578,7 @@ where I: StreamIsPartial, I: Stream, ::Token: AsChar, + for<'t> &'t ::Token: AsChar, { trace("oct_digit0", take_while(0.., AsChar::is_oct_digit)).parse_next(input) } @@ -612,6 +621,7 @@ where I: StreamIsPartial, I: Stream, ::Token: AsChar, + for<'t> &'t ::Token: AsChar, { trace("oct_digit0", take_while(1.., AsChar::is_oct_digit)).parse_next(input) } @@ -654,6 +664,7 @@ where I: StreamIsPartial, I: Stream, ::Token: AsChar, + for<'t> &'t ::Token: AsChar, { trace("alphanumeric0", take_while(0.., AsChar::is_alphanum)).parse_next(input) } @@ -696,6 +707,7 @@ where I: StreamIsPartial, I: Stream, ::Token: AsChar, + for<'t> &'t ::Token: AsChar, { trace("alphanumeric1", take_while(1.., AsChar::is_alphanum)).parse_next(input) } @@ -724,7 +736,8 @@ pub fn space0>(input: &mut I) -> PResult<::Sli where I: StreamIsPartial, I: Stream, - ::Token: AsChar + Clone, + ::Token: AsChar, + for<'t> &'t ::Token: AsChar, { trace("space0", take_while(0.., AsChar::is_space)).parse_next(input) } @@ -766,7 +779,8 @@ pub fn space1>(input: &mut I) -> PResult<::Sli where I: StreamIsPartial, I: Stream, - ::Token: AsChar + Clone, + ::Token: AsChar, + for<'t> &'t ::Token: AsChar, { trace("space1", take_while(1.., AsChar::is_space)).parse_next(input) } @@ -808,7 +822,8 @@ pub fn multispace0>(input: &mut I) -> PResult< where I: StreamIsPartial, I: Stream, - ::Token: AsChar + Clone, + ::Token: AsChar, + for<'t> &'t ::Token: AsChar, { trace("multispace0", take_while(0.., (' ', '\t', '\r', '\n'))).parse_next(input) } @@ -850,7 +865,8 @@ pub fn multispace1>(input: &mut I) -> PResult< where I: StreamIsPartial, I: Stream, - ::Token: AsChar + Clone, + ::Token: AsChar, + for<'t> &'t ::Token: AsChar, { trace("multispace1", take_while(1.., (' ', '\t', '\r', '\n'))).parse_next(input) } @@ -869,7 +885,7 @@ pub fn dec_uint>(input: &mut I) -> PResult where I: StreamIsPartial, I: Stream, - ::Token: AsChar + Clone, + for<'t> &'t ::Token: AsChar, O: Uint, { trace("dec_uint", move |input: &mut I| { @@ -1024,7 +1040,8 @@ pub fn dec_int>(input: &mut I) -> PResult where I: StreamIsPartial, I: Stream, - ::Token: AsChar + Clone, + ::Token: AsChar, + for<'t> &'t ::Token: AsChar, O: Int, { trace("dec_int", move |input: &mut I| { @@ -1160,6 +1177,7 @@ where I: Stream, O: HexUint, ::Token: AsChar, + for<'t> &'t ::Token: AsChar, ::Slice: AsBStr, { trace("hex_uint", move |input: &mut I| { @@ -1305,7 +1323,8 @@ where I: Stream, I: Compare<&'static str>, ::Slice: ParseSlice, - ::Token: AsChar + Clone, + ::Token: AsChar, + for<'t> &'t ::Token: AsChar, ::IterOffsets: Clone, I: AsBStr, { @@ -1325,7 +1344,8 @@ where I: StreamIsPartial, I: Stream, I: Compare<&'static str>, - ::Token: AsChar + Clone, + ::Token: AsChar, + for<'t> &'t ::Token: AsChar, ::IterOffsets: Clone, I: AsBStr, { @@ -1344,7 +1364,8 @@ where I: StreamIsPartial, I: Stream, I: Compare<&'static str>, - ::Token: AsChar + Clone, + ::Token: AsChar, + for<'t> &'t ::Token: AsChar, ::IterOffsets: Clone, I: AsBStr, { @@ -1409,7 +1430,8 @@ pub fn escaped<'a, I: 'a, Error, F, G, O1, O2>( where I: StreamIsPartial, I: Stream, - ::Token: AsChar + Clone, + ::Token: AsChar, + for<'t> &'t ::Token: AsChar, F: Parser, G: Parser, Error: ParserError, @@ -1432,7 +1454,8 @@ fn streaming_escaped_internal( where I: StreamIsPartial, I: Stream, - ::Token: AsChar + Clone, + ::Token: AsChar, + for<'t> &'t ::Token: AsChar, F: Parser, G: Parser, Error: ParserError, @@ -1474,7 +1497,8 @@ fn complete_escaped_internal<'a, I: 'a, Error, F, G, O1, O2>( where I: StreamIsPartial, I: Stream, - ::Token: crate::stream::AsChar + Clone, + ::Token: AsChar, + for<'t> &'t ::Token: AsChar, F: Parser, G: Parser, Error: ParserError, @@ -1576,7 +1600,7 @@ pub fn escaped_transform( where I: StreamIsPartial, I: Stream, - ::Token: crate::stream::AsChar + Clone, + for<'t> &'t ::Token: AsChar, Output: crate::stream::Accumulate<::Slice>, F: Parser::Slice, Error>, G: Parser::Slice, Error>, @@ -1600,7 +1624,7 @@ fn streaming_escaped_transform_internal( where I: StreamIsPartial, I: Stream, - ::Token: crate::stream::AsChar + Clone, + for<'t> &'t ::Token: AsChar, Output: crate::stream::Accumulate<::Slice>, F: Parser::Slice, Error>, G: Parser::Slice, Error>, @@ -1639,7 +1663,7 @@ fn complete_escaped_transform_internal( where I: StreamIsPartial, I: Stream, - ::Token: crate::stream::AsChar + Clone, + for<'t> &'t ::Token: AsChar, Output: crate::stream::Accumulate<::Slice>, F: Parser::Slice, Error>, G: Parser::Slice, Error>, diff --git a/src/parser.rs b/src/parser.rs index d160a8ef..318d73b6 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -678,7 +678,7 @@ impl Parser::Token, E> for char where I: StreamIsPartial, I: Stream, - ::Token: AsChar + Clone, + for<'t> &'t ::Token: AsChar, E: ParserError, { #[inline(always)] diff --git a/src/stream/mod.rs b/src/stream/mod.rs index fcacbd78..026a447f 100644 --- a/src/stream/mod.rs +++ b/src/stream/mod.rs @@ -442,7 +442,7 @@ pub trait Stream: Offset<::Checkpoint> + crate::lib::std::fmt::D /// Finds the offset of the next matching token fn offset_for

(&self, predicate: P) -> Option where - P: Fn(Self::Token) -> bool; + P: for<'t> Fn(&'t Self::Token) -> bool; /// Get the offset for the number of `tokens` into the stream /// /// This means "0 tokens" will return `0` offset @@ -535,9 +535,9 @@ where #[inline(always)] fn offset_for

(&self, predicate: P) -> Option where - P: Fn(Self::Token) -> bool, + P: for<'t> Fn(&'t Self::Token) -> bool, { - self.iter().position(|b| predicate(b.clone())) + self.iter().position(|b| predicate(b)) } #[inline(always)] fn offset_at(&self, tokens: usize) -> Result { @@ -597,10 +597,10 @@ impl<'i> Stream for &'i str { #[inline(always)] fn offset_for

(&self, predicate: P) -> Option where - P: Fn(Self::Token) -> bool, + P: for<'t> Fn(&'t Self::Token) -> bool, { for (o, c) in self.iter_offsets() { - if predicate(c) { + if predicate(&c) { return Some(o); } } @@ -675,9 +675,9 @@ impl<'i> Stream for &'i Bytes { #[inline(always)] fn offset_for

(&self, predicate: P) -> Option where - P: Fn(Self::Token) -> bool, + P: for<'t> Fn(&'t Self::Token) -> bool, { - self.iter().position(|b| predicate(*b)) + self.iter().position(|b| predicate(b)) } #[inline(always)] fn offset_at(&self, tokens: usize) -> Result { @@ -740,9 +740,9 @@ impl<'i> Stream for &'i BStr { #[inline(always)] fn offset_for

(&self, predicate: P) -> Option where - P: Fn(Self::Token) -> bool, + P: for<'t> Fn(&'t Self::Token) -> bool, { - self.iter().position(|b| predicate(*b)) + self.iter().position(|b| predicate(b)) } #[inline(always)] fn offset_at(&self, tokens: usize) -> Result { @@ -810,10 +810,10 @@ where #[inline(always)] fn offset_for

(&self, predicate: P) -> Option where - P: Fn(Self::Token) -> bool, + P: for<'t> Fn(&'t Self::Token) -> bool, { self.iter_offsets() - .find_map(|(o, b)| predicate(b).then_some(o)) + .find_map(|(o, b)| predicate(&b).then_some(o)) } #[inline(always)] fn offset_at(&self, tokens: usize) -> Result { @@ -922,7 +922,7 @@ impl Stream for Located { #[inline(always)] fn offset_for

(&self, predicate: P) -> Option where - P: Fn(Self::Token) -> bool, + P: for<'t> Fn(&'t Self::Token) -> bool, { self.input.offset_for(predicate) } @@ -975,7 +975,7 @@ impl Stream for Stateful(&self, predicate: P) -> Option where - P: Fn(Self::Token) -> bool, + P: for<'t> Fn(&'t Self::Token) -> bool, { self.input.offset_for(predicate) } @@ -1028,7 +1028,7 @@ impl Stream for Partial { #[inline(always)] fn offset_for

(&self, predicate: P) -> Option where - P: Fn(Self::Token) -> bool, + P: for<'t> Fn(&'t Self::Token) -> bool, { self.input.offset_for(predicate) } @@ -2520,9 +2520,9 @@ impl ContainsToken for char { } } -impl bool> ContainsToken for F { +impl<'c, C: AsChar, F: Fn(&'c C) -> bool> ContainsToken<&'c C> for F { #[inline(always)] - fn contains_token(&self, token: C) -> bool { + fn contains_token(&self, token: &'c C) -> bool { self(token) } } diff --git a/src/token/mod.rs b/src/token/mod.rs index fba019c9..7de688aa 100644 --- a/src/token/mod.rs +++ b/src/token/mod.rs @@ -295,12 +295,11 @@ pub fn one_of>(list: T) -> impl Parser::Token: Clone, - T: ContainsToken<::Token>, + T: for<'t> ContainsToken<&'t ::Token>, { trace( "one_of", - any.verify(move |t: &::Token| list.contains_token(t.clone())), + any.verify(move |t: &::Token| list.contains_token(t)), ) } @@ -335,12 +334,11 @@ pub fn none_of>(list: T) -> impl Parser::Token: Clone, - T: ContainsToken<::Token>, + for<'t> T: ContainsToken<&'t ::Token>, { trace( "none_of", - any.verify(move |t: &::Token| !list.contains_token(t.clone())), + any.verify(move |t: &::Token| !list.contains_token(t)), ) } @@ -487,7 +485,7 @@ pub fn take_while>( where I: StreamIsPartial, I: Stream, - T: ContainsToken<::Token>, + T: for<'t> ContainsToken<&'t ::Token>, { let Range { start_inclusive, @@ -528,7 +526,7 @@ fn take_while0_, const PARTIAL: bool>( where I: StreamIsPartial, I: Stream, - T: ContainsToken<::Token>, + T: for<'t> ContainsToken<&'t ::Token>, { if PARTIAL && input.is_partial() { take_till0_partial(input, |c| !list.contains_token(c)) @@ -544,7 +542,7 @@ fn take_while1_, const PARTIAL: bool>( where I: StreamIsPartial, I: Stream, - T: ContainsToken<::Token>, + T: for<'t> ContainsToken<&'t ::Token>, { let e: ErrorKind = ErrorKind::Slice; if PARTIAL && input.is_partial() { @@ -563,7 +561,7 @@ fn take_till0_partial>( predicate: P, ) -> PResult<::Slice, E> where - P: Fn(I::Token) -> bool, + P: for<'t> Fn(&'t I::Token) -> bool, { let offset = input .offset_for(predicate) @@ -583,7 +581,7 @@ fn take_till1_partial>( e: ErrorKind, ) -> PResult<::Slice, E> where - P: Fn(I::Token) -> bool, + P: for<'t> Fn(&'t I::Token) -> bool, { let offset = input .offset_for(predicate) @@ -604,7 +602,7 @@ fn take_till0_complete>( predicate: P, ) -> PResult<::Slice, E> where - P: Fn(I::Token) -> bool, + P: for<'t> Fn(&'t I::Token) -> bool, { let offset = input .offset_for(predicate) @@ -624,7 +622,7 @@ fn take_till1_complete>( e: ErrorKind, ) -> PResult<::Slice, E> where - P: Fn(I::Token) -> bool, + P: for<'t> Fn(&'t I::Token) -> bool, { let offset = input .offset_for(predicate) @@ -645,7 +643,7 @@ fn take_while_m_n_, const PARTIAL: bool>( where I: StreamIsPartial, I: Stream, - T: ContainsToken<::Token>, + T: for<'t> ContainsToken<&'t ::Token>, { if n < m { return Err(ErrMode::assert(input, "`m` should be <= `n`")); @@ -653,7 +651,7 @@ where let mut final_count = 0; for (processed, (offset, token)) in input.iter_offsets().enumerate() { - if !list.contains_token(token) { + if !list.contains_token(&token) { if processed < m { return Err(ErrMode::from_error_kind(input, ErrorKind::Slice)); } else { @@ -730,7 +728,7 @@ pub fn take_till0>( where I: StreamIsPartial, I: Stream, - T: ContainsToken<::Token>, + T: for<'t> ContainsToken<&'t ::Token>, { trace("take_till0", move |i: &mut I| { if ::is_partial_supported() && i.is_partial() { @@ -807,7 +805,7 @@ pub fn take_till1>( where I: StreamIsPartial, I: Stream, - T: ContainsToken<::Token>, + T: for<'t> ContainsToken<&'t ::Token>, { trace("take_till1", move |i: &mut I| { let e: ErrorKind = ErrorKind::Slice;