Skip to content

Commit

Permalink
Merge pull request #666 from epage/fn
Browse files Browse the repository at this point in the history
fix(stream)!: Change ContainsToken from Fn to FnMut
  • Loading branch information
epage authored Jan 3, 2025
2 parents cc6855f + 424f4b0 commit 49a24a7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 50 deletions.
8 changes: 4 additions & 4 deletions examples/arithmetic/parser_lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,28 +71,28 @@ pub enum Oper {

impl winnow::stream::ContainsToken<Token> 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<Token> 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<const LEN: usize> winnow::stream::ContainsToken<Token> 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<const LEN: usize> winnow::stream::ContainsToken<Token> 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)
}
}
Expand Down
76 changes: 38 additions & 38 deletions src/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ pub trait Stream: Offset<<Self as Stream>::Checkpoint> + crate::lib::std::fmt::D
/// Finds the offset of the next matching token
fn offset_for<P>(&self, predicate: P) -> Option<usize>
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
Expand Down Expand Up @@ -723,9 +723,9 @@ where
}

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

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

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

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

#[inline(always)]
fn offset_for<P>(&self, predicate: P) -> Option<usize>
fn offset_for<P>(&self, mut predicate: P) -> Option<usize>
where
P: Fn(Self::Token) -> bool,
P: FnMut(Self::Token) -> bool,
{
self.iter_offsets()
.find_map(|(o, b)| predicate(b).then_some(o))
Expand Down Expand Up @@ -1112,7 +1112,7 @@ impl<I: Stream> Stream for LocatingSlice<I> {
#[inline(always)]
fn offset_for<P>(&self, predicate: P) -> Option<usize>
where
P: Fn(Self::Token) -> bool,
P: FnMut(Self::Token) -> bool,
{
self.input.offset_for(predicate)
}
Expand Down Expand Up @@ -1170,7 +1170,7 @@ where
#[inline(always)]
fn offset_for<P>(&self, predicate: P) -> Option<usize>
where
P: Fn(Self::Token) -> bool,
P: FnMut(Self::Token) -> bool,
{
self.input.offset_for(predicate)
}
Expand Down Expand Up @@ -1223,7 +1223,7 @@ impl<I: Stream, S: crate::lib::std::fmt::Debug> Stream for Stateful<I, S> {
#[inline(always)]
fn offset_for<P>(&self, predicate: P) -> Option<usize>
where
P: Fn(Self::Token) -> bool,
P: FnMut(Self::Token) -> bool,
{
self.input.offset_for(predicate)
}
Expand Down Expand Up @@ -1276,7 +1276,7 @@ impl<I: Stream> Stream for Partial<I> {
#[inline(always)]
fn offset_for<P>(&self, predicate: P) -> Option<usize>
where
P: Fn(Self::Token) -> bool,
P: FnMut(Self::Token) -> bool,
{
self.input.offset_for(predicate)
}
Expand Down Expand Up @@ -3425,54 +3425,54 @@ impl AsChar for &char {
/// ```
pub trait ContainsToken<T> {
/// Returns true if self contains the token
fn contains_token(&self, token: T) -> bool;
fn contains_token(&mut self, token: T) -> bool;
}

impl ContainsToken<u8> 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<char> 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<C: AsChar> ContainsToken<C> for char {
#[inline(always)]
fn contains_token(&self, token: C) -> bool {
fn contains_token(&mut self, token: C) -> bool {
*self == token.as_char()
}
}

impl<C, F: Fn(C) -> bool> ContainsToken<C> for F {
impl<C, F: FnMut(C) -> bool> ContainsToken<C> for F {
#[inline(always)]
fn contains_token(&self, token: C) -> bool {
fn contains_token(&mut self, token: C) -> bool {
self(token)
}
}

impl<C1: AsChar, C2: AsChar + Clone> ContainsToken<C1> for crate::lib::std::ops::Range<C2> {
#[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())
Expand All @@ -3483,7 +3483,7 @@ impl<C1: AsChar, C2: AsChar + Clone> ContainsToken<C1>
for crate::lib::std::ops::RangeInclusive<C2>
{
#[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())
Expand All @@ -3492,15 +3492,15 @@ impl<C1: AsChar, C2: AsChar + Clone> ContainsToken<C1>

impl<C1: AsChar, C2: AsChar + Clone> ContainsToken<C1> for crate::lib::std::ops::RangeFrom<C2> {
#[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())
}
}

impl<C1: AsChar, C2: AsChar + Clone> ContainsToken<C1> for crate::lib::std::ops::RangeTo<C2> {
#[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())
}
Expand All @@ -3510,70 +3510,70 @@ impl<C1: AsChar, C2: AsChar + Clone> ContainsToken<C1>
for crate::lib::std::ops::RangeToInclusive<C2>
{
#[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())
}
}

impl<C1: AsChar> ContainsToken<C1> 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<C: AsChar> ContainsToken<C> 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)
}
}

impl<C: AsChar> ContainsToken<C> 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)
}
}

impl<const LEN: usize, C: AsChar> ContainsToken<C> 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)
}
}

impl<const LEN: usize, C: AsChar> ContainsToken<C> 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)
}
}

impl<const LEN: usize, C: AsChar> ContainsToken<C> 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)
}
}

impl<const LEN: usize, C: AsChar> ContainsToken<C> 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)
}
}

impl<T> ContainsToken<T> for () {
#[inline(always)]
fn contains_token(&self, _token: T) -> bool {
fn contains_token(&mut self, _token: T) -> bool {
false
}
}
Expand All @@ -3587,8 +3587,8 @@ macro_rules! impl_contains_token_for_tuple {
$($haystack: ContainsToken<T>),+
{
#[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
}
}
Expand Down
20 changes: 12 additions & 8 deletions src/token/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,9 @@ where
#[doc(alias = "char")]
#[doc(alias = "token")]
#[doc(alias = "satisfy")]
pub fn one_of<Input, Set, Error>(set: Set) -> impl Parser<Input, <Input as Stream>::Token, Error>
pub fn one_of<Input, Set, Error>(
mut set: Set,
) -> impl Parser<Input, <Input as Stream>::Token, Error>
where
Input: StreamIsPartial + Stream,
<Input as Stream>::Token: Clone,
Expand Down Expand Up @@ -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<Input, Set, Error>(set: Set) -> impl Parser<Input, <Input as Stream>::Token, Error>
pub fn none_of<Input, Set, Error>(
mut set: Set,
) -> impl Parser<Input, <Input as Stream>::Token, Error>
where
Input: StreamIsPartial + Stream,
<Input as Stream>::Token: Clone,
Expand Down Expand Up @@ -485,7 +489,7 @@ where
#[doc(alias = "take_while1")]
pub fn take_while<Set, Input, Error>(
occurrences: impl Into<Range>,
set: Set,
mut set: Set,
) -> impl Parser<Input, <Input as Stream>::Slice, Error>
where
Input: StreamIsPartial + Stream,
Expand Down Expand Up @@ -529,7 +533,7 @@ fn take_till0<P, I: StreamIsPartial + Stream, E: ParserError<I>, const PARTIAL:
predicate: P,
) -> PResult<<I as Stream>::Slice, E>
where
P: Fn(I::Token) -> bool,
P: FnMut(I::Token) -> bool,
{
let offset = match input.offset_for(predicate) {
Some(offset) => offset,
Expand All @@ -546,7 +550,7 @@ fn take_till1<P, I: StreamIsPartial + Stream, E: ParserError<I>, const PARTIAL:
predicate: P,
) -> PResult<<I as Stream>::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) {
Expand All @@ -567,12 +571,12 @@ fn take_till_m_n<P, I, Error: ParserError<I>, const PARTIAL: bool>(
input: &mut I,
m: usize,
n: usize,
predicate: P,
mut predicate: P,
) -> PResult<<I as Stream>::Slice, Error>
where
I: StreamIsPartial,
I: Stream,
P: Fn(I::Token) -> bool,
P: FnMut(I::Token) -> bool,
{
if n < m {
return Err(ErrMode::assert(
Expand Down Expand Up @@ -677,7 +681,7 @@ where
#[doc(alias = "is_not")]
pub fn take_till<Set, Input, Error>(
occurrences: impl Into<Range>,
set: Set,
mut set: Set,
) -> impl Parser<Input, <Input as Stream>::Slice, Error>
where
Input: StreamIsPartial + Stream,
Expand Down

0 comments on commit 49a24a7

Please sign in to comment.