Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(stream)!: Avoid cloning Tokens #290

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 43 additions & 19 deletions src/ascii/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ where
I: StreamIsPartial,
I: Stream,
I: Compare<&'static str>,
<I as Stream>::Token: AsChar + Clone,
for<'t> &'t <I as Stream>::Token: AsChar,
{
trace("not_line_ending", move |input: &mut I| {
if <I as StreamIsPartial>::is_partial_supported() {
Expand All @@ -121,7 +121,7 @@ where
I: StreamIsPartial,
I: Stream,
I: Compare<&'static str>,
<I as Stream>::Token: AsChar + Clone,
for<'t> &'t <I as Stream>::Token: AsChar,
{
let res = take_till0(('\r', '\n')).parse_next(input)?;
if input.compare("\r") == CompareResult::Ok {
Expand Down Expand Up @@ -216,7 +216,8 @@ pub fn newline<I, Error: ParserError<I>>(input: &mut I) -> PResult<char, Error>
where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: AsChar + Clone,
<I as Stream>::Token: AsChar,
for<'t> &'t <I as Stream>::Token: AsChar,
{
trace("newline", '\n'.map(AsChar::as_char)).parse_next(input)
}
Expand Down Expand Up @@ -256,7 +257,8 @@ pub fn tab<I, Error: ParserError<I>>(input: &mut I) -> PResult<char, Error>
where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: AsChar + Clone,
<I as Stream>::Token: AsChar,
for<'t> &'t <I as Stream>::Token: AsChar,
{
trace("tab", '\t'.map(AsChar::as_char)).parse_next(input)
}
Expand Down Expand Up @@ -299,6 +301,7 @@ where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: AsChar,
for<'t> &'t <I as Stream>::Token: AsChar,
{
trace("alpha0", take_while(0.., AsChar::is_alpha)).parse_next(input)
}
Expand Down Expand Up @@ -341,6 +344,7 @@ where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: AsChar,
for<'t> &'t <I as Stream>::Token: AsChar,
{
trace("alpha1", take_while(1.., AsChar::is_alpha)).parse_next(input)
}
Expand Down Expand Up @@ -384,6 +388,7 @@ where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: AsChar,
for<'t> &'t <I as Stream>::Token: AsChar,
{
trace("digit0", take_while(0.., AsChar::is_dec_digit)).parse_next(input)
}
Expand Down Expand Up @@ -443,6 +448,7 @@ where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: AsChar,
for<'t> &'t <I as Stream>::Token: AsChar,
{
trace("digit1", take_while(1.., AsChar::is_dec_digit)).parse_next(input)
}
Expand Down Expand Up @@ -485,6 +491,7 @@ where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: AsChar,
for<'t> &'t <I as Stream>::Token: AsChar,
{
trace("hex_digit0", take_while(0.., AsChar::is_hex_digit)).parse_next(input)
}
Expand Down Expand Up @@ -528,6 +535,7 @@ where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: AsChar,
for<'t> &'t <I as Stream>::Token: AsChar,
{
trace("hex_digit1", take_while(1.., AsChar::is_hex_digit)).parse_next(input)
}
Expand Down Expand Up @@ -570,6 +578,7 @@ where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: AsChar,
for<'t> &'t <I as Stream>::Token: AsChar,
{
trace("oct_digit0", take_while(0.., AsChar::is_oct_digit)).parse_next(input)
}
Expand Down Expand Up @@ -612,6 +621,7 @@ where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: AsChar,
for<'t> &'t <I as Stream>::Token: AsChar,
{
trace("oct_digit0", take_while(1.., AsChar::is_oct_digit)).parse_next(input)
}
Expand Down Expand Up @@ -654,6 +664,7 @@ where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: AsChar,
for<'t> &'t <I as Stream>::Token: AsChar,
{
trace("alphanumeric0", take_while(0.., AsChar::is_alphanum)).parse_next(input)
}
Expand Down Expand Up @@ -696,6 +707,7 @@ where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: AsChar,
for<'t> &'t <I as Stream>::Token: AsChar,
{
trace("alphanumeric1", take_while(1.., AsChar::is_alphanum)).parse_next(input)
}
Expand Down Expand Up @@ -724,7 +736,8 @@ pub fn space0<I, E: ParserError<I>>(input: &mut I) -> PResult<<I as Stream>::Sli
where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: AsChar + Clone,
<I as Stream>::Token: AsChar,
for<'t> &'t <I as Stream>::Token: AsChar,
{
trace("space0", take_while(0.., AsChar::is_space)).parse_next(input)
}
Expand Down Expand Up @@ -766,7 +779,8 @@ pub fn space1<I, E: ParserError<I>>(input: &mut I) -> PResult<<I as Stream>::Sli
where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: AsChar + Clone,
<I as Stream>::Token: AsChar,
for<'t> &'t <I as Stream>::Token: AsChar,
{
trace("space1", take_while(1.., AsChar::is_space)).parse_next(input)
}
Expand Down Expand Up @@ -808,7 +822,8 @@ pub fn multispace0<I, E: ParserError<I>>(input: &mut I) -> PResult<<I as Stream>
where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: AsChar + Clone,
<I as Stream>::Token: AsChar,
for<'t> &'t <I as Stream>::Token: AsChar,
{
trace("multispace0", take_while(0.., (' ', '\t', '\r', '\n'))).parse_next(input)
}
Expand Down Expand Up @@ -850,7 +865,8 @@ pub fn multispace1<I, E: ParserError<I>>(input: &mut I) -> PResult<<I as Stream>
where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: AsChar + Clone,
<I as Stream>::Token: AsChar,
for<'t> &'t <I as Stream>::Token: AsChar,
{
trace("multispace1", take_while(1.., (' ', '\t', '\r', '\n'))).parse_next(input)
}
Expand All @@ -869,7 +885,7 @@ pub fn dec_uint<I, O, E: ParserError<I>>(input: &mut I) -> PResult<O, E>
where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: AsChar + Clone,
for<'t> &'t <I as Stream>::Token: AsChar,
O: Uint,
{
trace("dec_uint", move |input: &mut I| {
Expand Down Expand Up @@ -1024,7 +1040,8 @@ pub fn dec_int<I, O, E: ParserError<I>>(input: &mut I) -> PResult<O, E>
where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: AsChar + Clone,
<I as Stream>::Token: AsChar,
for<'t> &'t <I as Stream>::Token: AsChar,
O: Int,
{
trace("dec_int", move |input: &mut I| {
Expand Down Expand Up @@ -1160,6 +1177,7 @@ where
I: Stream,
O: HexUint,
<I as Stream>::Token: AsChar,
for<'t> &'t <I as Stream>::Token: AsChar,
<I as Stream>::Slice: AsBStr,
{
trace("hex_uint", move |input: &mut I| {
Expand Down Expand Up @@ -1305,7 +1323,8 @@ where
I: Stream,
I: Compare<&'static str>,
<I as Stream>::Slice: ParseSlice<O>,
<I as Stream>::Token: AsChar + Clone,
<I as Stream>::Token: AsChar,
for<'t> &'t <I as Stream>::Token: AsChar,
<I as Stream>::IterOffsets: Clone,
I: AsBStr,
{
Expand All @@ -1325,7 +1344,8 @@ where
I: StreamIsPartial,
I: Stream,
I: Compare<&'static str>,
<I as Stream>::Token: AsChar + Clone,
<I as Stream>::Token: AsChar,
for<'t> &'t <I as Stream>::Token: AsChar,
<I as Stream>::IterOffsets: Clone,
I: AsBStr,
{
Expand All @@ -1344,7 +1364,8 @@ where
I: StreamIsPartial,
I: Stream,
I: Compare<&'static str>,
<I as Stream>::Token: AsChar + Clone,
<I as Stream>::Token: AsChar,
for<'t> &'t <I as Stream>::Token: AsChar,
<I as Stream>::IterOffsets: Clone,
I: AsBStr,
{
Expand Down Expand Up @@ -1409,7 +1430,8 @@ pub fn escaped<'a, I: 'a, Error, F, G, O1, O2>(
where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: AsChar + Clone,
<I as Stream>::Token: AsChar,
for<'t> &'t <I as Stream>::Token: AsChar,
F: Parser<I, O1, Error>,
G: Parser<I, O2, Error>,
Error: ParserError<I>,
Expand All @@ -1432,7 +1454,8 @@ fn streaming_escaped_internal<I, Error, F, G, O1, O2>(
where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: AsChar + Clone,
<I as Stream>::Token: AsChar,
for<'t> &'t <I as Stream>::Token: AsChar,
F: Parser<I, O1, Error>,
G: Parser<I, O2, Error>,
Error: ParserError<I>,
Expand Down Expand Up @@ -1474,7 +1497,8 @@ fn complete_escaped_internal<'a, I: 'a, Error, F, G, O1, O2>(
where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: crate::stream::AsChar + Clone,
<I as Stream>::Token: AsChar,
for<'t> &'t <I as Stream>::Token: AsChar,
F: Parser<I, O1, Error>,
G: Parser<I, O2, Error>,
Error: ParserError<I>,
Expand Down Expand Up @@ -1576,7 +1600,7 @@ pub fn escaped_transform<I, Error, F, G, Output>(
where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: crate::stream::AsChar + Clone,
for<'t> &'t <I as Stream>::Token: AsChar,
Output: crate::stream::Accumulate<<I as Stream>::Slice>,
F: Parser<I, <I as Stream>::Slice, Error>,
G: Parser<I, <I as Stream>::Slice, Error>,
Expand All @@ -1600,7 +1624,7 @@ fn streaming_escaped_transform_internal<I, Error, F, G, Output>(
where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: crate::stream::AsChar + Clone,
for<'t> &'t <I as Stream>::Token: AsChar,
Output: crate::stream::Accumulate<<I as Stream>::Slice>,
F: Parser<I, <I as Stream>::Slice, Error>,
G: Parser<I, <I as Stream>::Slice, Error>,
Expand Down Expand Up @@ -1639,7 +1663,7 @@ fn complete_escaped_transform_internal<I, Error, F, G, Output>(
where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: crate::stream::AsChar + Clone,
for<'t> &'t <I as Stream>::Token: AsChar,
Output: crate::stream::Accumulate<<I as Stream>::Slice>,
F: Parser<I, <I as Stream>::Slice, Error>,
G: Parser<I, <I as Stream>::Slice, Error>,
Expand Down
2 changes: 1 addition & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ impl<I, E> Parser<I, <I as Stream>::Token, E> for char
where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: AsChar + Clone,
for<'t> &'t <I as Stream>::Token: AsChar,
E: ParserError<I>,
{
#[inline(always)]
Expand Down
32 changes: 16 additions & 16 deletions src/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,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: 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
Expand Down Expand Up @@ -535,9 +535,9 @@ where
#[inline(always)]
fn offset_for<P>(&self, predicate: P) -> Option<usize>
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<usize, Needed> {
Expand Down Expand Up @@ -597,10 +597,10 @@ impl<'i> Stream for &'i str {
#[inline(always)]
fn offset_for<P>(&self, predicate: P) -> Option<usize>
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);
}
}
Expand Down Expand Up @@ -675,9 +675,9 @@ impl<'i> Stream for &'i Bytes {
#[inline(always)]
fn offset_for<P>(&self, predicate: P) -> Option<usize>
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<usize, Needed> {
Expand Down Expand Up @@ -740,9 +740,9 @@ impl<'i> Stream for &'i BStr {
#[inline(always)]
fn offset_for<P>(&self, predicate: P) -> Option<usize>
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<usize, Needed> {
Expand Down Expand Up @@ -810,10 +810,10 @@ where
#[inline(always)]
fn offset_for<P>(&self, predicate: P) -> Option<usize>
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<usize, Needed> {
Expand Down Expand Up @@ -922,7 +922,7 @@ impl<I: Stream> Stream for Located<I> {
#[inline(always)]
fn offset_for<P>(&self, predicate: P) -> Option<usize>
where
P: Fn(Self::Token) -> bool,
P: for<'t> Fn(&'t Self::Token) -> bool,
{
self.input.offset_for(predicate)
}
Expand Down Expand Up @@ -975,7 +975,7 @@ impl<I: Stream, S: Clone + 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: for<'t> Fn(&'t Self::Token) -> bool,
{
self.input.offset_for(predicate)
}
Expand Down Expand Up @@ -1028,7 +1028,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: for<'t> Fn(&'t Self::Token) -> bool,
{
self.input.offset_for(predicate)
}
Expand Down Expand Up @@ -2520,9 +2520,9 @@ impl<C: AsChar> ContainsToken<C> for char {
}
}

impl<C: AsChar, F: Fn(C) -> bool> ContainsToken<C> 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)
}
}
Expand Down
Loading
Loading