From 424f4b0014cd259115893521c84d2a8ac867ff2a Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 3 Jan 2025 16:00:22 -0600 Subject: [PATCH] fix(stream)!: Change ContainsToken from Fn to FnMut Fixes #558 --- examples/arithmetic/parser_lexer.rs | 8 +-- src/stream/mod.rs | 76 ++++++++++++++--------------- src/token/mod.rs | 20 +++++--- 3 files changed, 54 insertions(+), 50 deletions(-) diff --git a/examples/arithmetic/parser_lexer.rs b/examples/arithmetic/parser_lexer.rs index 3e3d8427..632efd90 100644 --- a/examples/arithmetic/parser_lexer.rs +++ b/examples/arithmetic/parser_lexer.rs @@ -71,28 +71,28 @@ pub enum Oper { impl winnow::stream::ContainsToken for Token { #[inline(always)] - fn contains_token(&self, token: Token) -> bool { + fn contains_token(&mut self, token: Token) -> bool { *self == token } } impl winnow::stream::ContainsToken for &'_ [Token] { #[inline] - fn contains_token(&self, token: Token) -> bool { + fn contains_token(&mut self, token: Token) -> bool { self.iter().any(|t| *t == token) } } impl winnow::stream::ContainsToken for &'_ [Token; LEN] { #[inline] - fn contains_token(&self, token: Token) -> bool { + fn contains_token(&mut self, token: Token) -> bool { self.iter().any(|t| *t == token) } } impl winnow::stream::ContainsToken for [Token; LEN] { #[inline] - fn contains_token(&self, token: Token) -> bool { + fn contains_token(&mut self, token: Token) -> bool { self.iter().any(|t| *t == token) } } diff --git a/src/stream/mod.rs b/src/stream/mod.rs index 08879829..4017cd88 100644 --- a/src/stream/mod.rs +++ b/src/stream/mod.rs @@ -628,7 +628,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: FnMut(Self::Token) -> bool; /// Get the offset for the number of `tokens` into the stream /// /// This means "0 tokens" will return `0` offset @@ -723,9 +723,9 @@ where } #[inline(always)] - fn offset_for

(&self, predicate: P) -> Option + fn offset_for

(&self, mut predicate: P) -> Option where - P: Fn(Self::Token) -> bool, + P: FnMut(Self::Token) -> bool, { self.iter().position(|b| predicate(b.clone())) } @@ -785,9 +785,9 @@ impl<'i> Stream for &'i str { } #[inline(always)] - fn offset_for

(&self, predicate: P) -> Option + fn offset_for

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

(&self, predicate: P) -> Option + fn offset_for

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

(&self, predicate: P) -> Option + fn offset_for

(&self, mut predicate: P) -> Option where - P: Fn(Self::Token) -> bool, + P: FnMut(Self::Token) -> bool, { self.iter().position(|b| predicate(*b)) } @@ -998,9 +998,9 @@ where } #[inline(always)] - fn offset_for

(&self, predicate: P) -> Option + fn offset_for

(&self, mut predicate: P) -> Option where - P: Fn(Self::Token) -> bool, + P: FnMut(Self::Token) -> bool, { self.iter_offsets() .find_map(|(o, b)| predicate(b).then_some(o)) @@ -1112,7 +1112,7 @@ impl Stream for LocatingSlice { #[inline(always)] fn offset_for

(&self, predicate: P) -> Option where - P: Fn(Self::Token) -> bool, + P: FnMut(Self::Token) -> bool, { self.input.offset_for(predicate) } @@ -1170,7 +1170,7 @@ where #[inline(always)] fn offset_for

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

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

(&self, predicate: P) -> Option where - P: Fn(Self::Token) -> bool, + P: FnMut(Self::Token) -> bool, { self.input.offset_for(predicate) } @@ -3425,54 +3425,54 @@ impl AsChar for &char { /// ``` pub trait ContainsToken { /// Returns true if self contains the token - fn contains_token(&self, token: T) -> bool; + fn contains_token(&mut self, token: T) -> bool; } impl ContainsToken for u8 { #[inline(always)] - fn contains_token(&self, token: u8) -> bool { + fn contains_token(&mut self, token: u8) -> bool { *self == token } } impl ContainsToken<&u8> for u8 { #[inline(always)] - fn contains_token(&self, token: &u8) -> bool { + fn contains_token(&mut self, token: &u8) -> bool { self.contains_token(*token) } } impl ContainsToken for u8 { #[inline(always)] - fn contains_token(&self, token: char) -> bool { + fn contains_token(&mut self, token: char) -> bool { self.as_char() == token } } impl ContainsToken<&char> for u8 { #[inline(always)] - fn contains_token(&self, token: &char) -> bool { + fn contains_token(&mut self, token: &char) -> bool { self.contains_token(*token) } } impl ContainsToken for char { #[inline(always)] - fn contains_token(&self, token: C) -> bool { + fn contains_token(&mut self, token: C) -> bool { *self == token.as_char() } } -impl bool> ContainsToken for F { +impl bool> ContainsToken for F { #[inline(always)] - fn contains_token(&self, token: C) -> bool { + fn contains_token(&mut self, token: C) -> bool { self(token) } } impl ContainsToken for crate::lib::std::ops::Range { #[inline(always)] - fn contains_token(&self, token: C1) -> bool { + fn contains_token(&mut self, token: C1) -> bool { let start = self.start.clone().as_char(); let end = self.end.clone().as_char(); (start..end).contains(&token.as_char()) @@ -3483,7 +3483,7 @@ impl ContainsToken for crate::lib::std::ops::RangeInclusive { #[inline(always)] - fn contains_token(&self, token: C1) -> bool { + fn contains_token(&mut self, token: C1) -> bool { let start = self.start().clone().as_char(); let end = self.end().clone().as_char(); (start..=end).contains(&token.as_char()) @@ -3492,7 +3492,7 @@ impl ContainsToken impl ContainsToken for crate::lib::std::ops::RangeFrom { #[inline(always)] - fn contains_token(&self, token: C1) -> bool { + fn contains_token(&mut self, token: C1) -> bool { let start = self.start.clone().as_char(); (start..).contains(&token.as_char()) } @@ -3500,7 +3500,7 @@ impl ContainsToken for crate::lib::std::ops: impl ContainsToken for crate::lib::std::ops::RangeTo { #[inline(always)] - fn contains_token(&self, token: C1) -> bool { + fn contains_token(&mut self, token: C1) -> bool { let end = self.end.clone().as_char(); (..end).contains(&token.as_char()) } @@ -3510,7 +3510,7 @@ impl ContainsToken for crate::lib::std::ops::RangeToInclusive { #[inline(always)] - fn contains_token(&self, token: C1) -> bool { + fn contains_token(&mut self, token: C1) -> bool { let end = self.end.clone().as_char(); (..=end).contains(&token.as_char()) } @@ -3518,14 +3518,14 @@ impl ContainsToken impl ContainsToken for crate::lib::std::ops::RangeFull { #[inline(always)] - fn contains_token(&self, _token: C1) -> bool { + fn contains_token(&mut self, _token: C1) -> bool { true } } impl ContainsToken for &'_ [u8] { #[inline] - fn contains_token(&self, token: C) -> bool { + fn contains_token(&mut self, token: C) -> bool { let token = token.as_char(); self.iter().any(|t| t.as_char() == token) } @@ -3533,7 +3533,7 @@ impl ContainsToken for &'_ [u8] { impl ContainsToken for &'_ [char] { #[inline] - fn contains_token(&self, token: C) -> bool { + fn contains_token(&mut self, token: C) -> bool { let token = token.as_char(); self.iter().any(|t| *t == token) } @@ -3541,7 +3541,7 @@ impl ContainsToken for &'_ [char] { impl ContainsToken for &'_ [u8; LEN] { #[inline] - fn contains_token(&self, token: C) -> bool { + fn contains_token(&mut self, token: C) -> bool { let token = token.as_char(); self.iter().any(|t| t.as_char() == token) } @@ -3549,7 +3549,7 @@ impl ContainsToken for &'_ [u8; LEN] { impl ContainsToken for &'_ [char; LEN] { #[inline] - fn contains_token(&self, token: C) -> bool { + fn contains_token(&mut self, token: C) -> bool { let token = token.as_char(); self.iter().any(|t| *t == token) } @@ -3557,7 +3557,7 @@ impl ContainsToken for &'_ [char; LEN] { impl ContainsToken for [u8; LEN] { #[inline] - fn contains_token(&self, token: C) -> bool { + fn contains_token(&mut self, token: C) -> bool { let token = token.as_char(); self.iter().any(|t| t.as_char() == token) } @@ -3565,7 +3565,7 @@ impl ContainsToken for [u8; LEN] { impl ContainsToken for [char; LEN] { #[inline] - fn contains_token(&self, token: C) -> bool { + fn contains_token(&mut self, token: C) -> bool { let token = token.as_char(); self.iter().any(|t| *t == token) } @@ -3573,7 +3573,7 @@ impl ContainsToken for [char; LEN] { impl ContainsToken for () { #[inline(always)] - fn contains_token(&self, _token: T) -> bool { + fn contains_token(&mut self, _token: T) -> bool { false } } @@ -3587,8 +3587,8 @@ macro_rules! impl_contains_token_for_tuple { $($haystack: ContainsToken),+ { #[inline] - fn contains_token(&self, token: T) -> bool { - let ($(ref $haystack),+,) = *self; + fn contains_token(&mut self, token: T) -> bool { + let ($(ref mut $haystack),+,) = *self; $($haystack.contains_token(token.clone()) || )+ false } } diff --git a/src/token/mod.rs b/src/token/mod.rs index 0357693f..fe7c8d52 100644 --- a/src/token/mod.rs +++ b/src/token/mod.rs @@ -267,7 +267,9 @@ where #[doc(alias = "char")] #[doc(alias = "token")] #[doc(alias = "satisfy")] -pub fn one_of(set: Set) -> impl Parser::Token, Error> +pub fn one_of( + mut set: Set, +) -> impl Parser::Token, Error> where Input: StreamIsPartial + Stream, ::Token: Clone, @@ -320,7 +322,9 @@ where /// assert_eq!(none_of::<_, _, InputError<_>>('a').parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1)))); /// ``` #[inline(always)] -pub fn none_of(set: Set) -> impl Parser::Token, Error> +pub fn none_of( + mut set: Set, +) -> impl Parser::Token, Error> where Input: StreamIsPartial + Stream, ::Token: Clone, @@ -485,7 +489,7 @@ where #[doc(alias = "take_while1")] pub fn take_while( occurrences: impl Into, - set: Set, + mut set: Set, ) -> impl Parser::Slice, Error> where Input: StreamIsPartial + Stream, @@ -529,7 +533,7 @@ fn take_till0, const PARTIAL: predicate: P, ) -> PResult<::Slice, E> where - P: Fn(I::Token) -> bool, + P: FnMut(I::Token) -> bool, { let offset = match input.offset_for(predicate) { Some(offset) => offset, @@ -546,7 +550,7 @@ fn take_till1, const PARTIAL: predicate: P, ) -> PResult<::Slice, E> where - P: Fn(I::Token) -> bool, + P: FnMut(I::Token) -> bool, { let e: ErrorKind = ErrorKind::Slice; let offset = match input.offset_for(predicate) { @@ -567,12 +571,12 @@ fn take_till_m_n, const PARTIAL: bool>( input: &mut I, m: usize, n: usize, - predicate: P, + mut predicate: P, ) -> PResult<::Slice, Error> where I: StreamIsPartial, I: Stream, - P: Fn(I::Token) -> bool, + P: FnMut(I::Token) -> bool, { if n < m { return Err(ErrMode::assert( @@ -677,7 +681,7 @@ where #[doc(alias = "is_not")] pub fn take_till( occurrences: impl Into, - set: Set, + mut set: Set, ) -> impl Parser::Slice, Error> where Input: StreamIsPartial + Stream,