Skip to content

Commit 6369ccb

Browse files
committed
Change the standard Input impl to support any slice
The main change is that the Item is a reference instead of a copy
1 parent d3d2540 commit 6369ccb

File tree

10 files changed

+380
-338
lines changed

10 files changed

+380
-338
lines changed

src/bits/complete.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ use crate::traits::{Input, ToUsize};
3030
/// // Tries to consume 12 bits but only 8 are available
3131
/// assert_eq!(parser(([0b00010010].as_ref(), 0), 12), Err(nom::Err::Error(Error{input: ([0b00010010].as_ref(), 0), code: ErrorKind::Eof })));
3232
/// ```
33-
pub fn take<I, O, C, E: ParseError<(I, usize)>>(
33+
pub fn take<'a, I, O, C, E: ParseError<(I, usize)>>(
3434
count: C,
3535
) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E>
3636
where
37-
I: Input<Item = u8>,
37+
I: Input<Item = &'a u8>,
3838
C: ToUsize,
3939
O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O>,
4040
{
@@ -59,7 +59,7 @@ where
5959
break;
6060
}
6161
let val: O = if offset == 0 {
62-
byte.into()
62+
(*byte).into()
6363
} else {
6464
((byte << offset) >> offset).into()
6565
};
@@ -80,12 +80,12 @@ where
8080
}
8181

8282
/// Generates a parser taking `count` bits and comparing them to `pattern`
83-
pub fn tag<I, O, C, E: ParseError<(I, usize)>>(
83+
pub fn tag<'a, I, O, C, E: ParseError<(I, usize)>>(
8484
pattern: O,
8585
count: C,
8686
) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E>
8787
where
88-
I: Input<Item = u8> + Clone,
88+
I: Input<Item = &'a u8> + Clone,
8989
C: ToUsize,
9090
O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O> + PartialEq,
9191
{
@@ -118,9 +118,9 @@ where
118118
/// assert_eq!(parse(([0b10000000].as_ref(), 0)), Ok((([0b10000000].as_ref(), 1), true)));
119119
/// assert_eq!(parse(([0b10000000].as_ref(), 1)), Ok((([0b10000000].as_ref(), 2), false)));
120120
/// ```
121-
pub fn bool<I, E: ParseError<(I, usize)>>(input: (I, usize)) -> IResult<(I, usize), bool, E>
121+
pub fn bool<'a, I, E: ParseError<(I, usize)>>(input: (I, usize)) -> IResult<(I, usize), bool, E>
122122
where
123-
I: Input<Item = u8>,
123+
I: Input<Item = &'a u8>,
124124
{
125125
let (res, bit): (_, u32) = take(1usize)(input)?;
126126
Ok((res, bit != 0))

src/bits/streaming.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ use crate::lib::std::ops::{AddAssign, Div, Shl, Shr};
77
use crate::traits::{Input, ToUsize};
88

99
/// Generates a parser taking `count` bits
10-
pub fn take<I, O, C, E: ParseError<(I, usize)>>(
10+
pub fn take<'a, I, O, C, E: ParseError<(I, usize)>>(
1111
count: C,
1212
) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E>
1313
where
14-
I: Input<Item = u8>,
14+
I: Input<Item = &'a u8>,
1515
C: ToUsize,
1616
O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O>,
1717
{
@@ -34,7 +34,7 @@ where
3434
break;
3535
}
3636
let val: O = if offset == 0 {
37-
byte.into()
37+
(*byte).into()
3838
} else {
3939
((byte << offset) >> offset).into()
4040
};
@@ -56,12 +56,12 @@ where
5656
}
5757

5858
/// Generates a parser taking `count` bits and comparing them to `pattern`
59-
pub fn tag<I, O, C, E: ParseError<(I, usize)>>(
59+
pub fn tag<'a, I, O, C, E: ParseError<(I, usize)>>(
6060
pattern: O,
6161
count: C,
6262
) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E>
6363
where
64-
I: Input<Item = u8> + Clone,
64+
I: Input<Item = &'a u8> + Clone,
6565
C: ToUsize,
6666
O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O> + PartialEq,
6767
{
@@ -94,9 +94,9 @@ where
9494
/// assert_eq!(parse(([0b10000000].as_ref(), 0)), Ok((([0b10000000].as_ref(), 1), true)));
9595
/// assert_eq!(parse(([0b10000000].as_ref(), 1)), Ok((([0b10000000].as_ref(), 2), false)));
9696
/// ```
97-
pub fn bool<I, E: ParseError<(I, usize)>>(input: (I, usize)) -> IResult<(I, usize), bool, E>
97+
pub fn bool<'a, I, E: ParseError<(I, usize)>>(input: (I, usize)) -> IResult<(I, usize), bool, E>
9898
where
99-
I: Input<Item = u8>,
99+
I: Input<Item = &'a u8>,
100100
{
101101
let (res, bit): (_, u32) = take(1usize)(input)?;
102102
Ok((res, bit != 0))

src/bytes/complete.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use core::marker::PhantomData;
55
use crate::error::ParseError;
66
use crate::internal::{IResult, Parser};
77
use crate::traits::{Compare, FindSubstring, FindToken, ToUsize};
8-
use crate::Complete;
98
use crate::Emit;
109
use crate::Input;
1110
use crate::OutputM;
11+
use crate::{Complete, IntoInput};
1212

1313
/// Recognizes a pattern
1414
///
@@ -358,8 +358,8 @@ where
358358
/// ```
359359
pub fn take_until<T, I, Error: ParseError<I>>(tag: T) -> impl FnMut(I) -> IResult<I, I, Error>
360360
where
361-
I: Input + FindSubstring<T>,
362-
T: Input + Clone,
361+
I: Input + FindSubstring<T::Input>,
362+
T: IntoInput,
363363
{
364364
let mut parser = super::take_until(tag);
365365

src/bytes/mod.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use crate::error::ParseError;
1212
use crate::internal::{Err, Needed, Parser};
1313
use crate::lib::std::result::Result::*;
1414
use crate::traits::{Compare, CompareResult};
15-
use crate::AsChar;
1615
use crate::Check;
1716
use crate::ExtendInto;
1817
use crate::FindSubstring;
@@ -23,6 +22,7 @@ use crate::Mode;
2322
use crate::OutputM;
2423
use crate::OutputMode;
2524
use crate::ToUsize;
25+
use crate::{AsChar, IntoInput};
2626

2727
/// Recognizes a pattern.
2828
///
@@ -44,11 +44,11 @@ use crate::ToUsize;
4444
/// ```
4545
pub fn tag<T, I, Error: ParseError<I>>(tag: T) -> impl Parser<I, Output = I, Error = Error>
4646
where
47-
I: Input + Compare<T>,
48-
T: Input + Clone,
47+
I: Input + Compare<T::Input>,
48+
T: IntoInput,
4949
{
5050
Tag {
51-
tag,
51+
tag: tag.into_input(),
5252
e: PhantomData,
5353
}
5454
}
@@ -113,11 +113,11 @@ where
113113
/// ```
114114
pub fn tag_no_case<T, I, Error: ParseError<I>>(tag: T) -> impl Parser<I, Output = I, Error = Error>
115115
where
116-
I: Input + Compare<T>,
117-
T: Input + Clone,
116+
I: Input + Compare<T::Input>,
117+
T: IntoInput,
118118
{
119119
TagNoCase {
120-
tag,
120+
tag: tag.into_input(),
121121
e: PhantomData,
122122
}
123123
}
@@ -278,10 +278,10 @@ where
278278
/// ```rust
279279
/// # use nom::{Err, error::ErrorKind, Needed, IResult};
280280
/// use nom::bytes::complete::take_while;
281-
/// use nom::character::is_alphabetic;
281+
/// use nom::AsChar;
282282
///
283283
/// fn alpha(s: &[u8]) -> IResult<&[u8], &[u8]> {
284-
/// take_while(is_alphabetic)(s)
284+
/// take_while(AsChar::is_alpha)(s)
285285
/// }
286286
///
287287
/// assert_eq!(alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..])));
@@ -314,10 +314,10 @@ where
314314
/// ```rust
315315
/// # use nom::{Err, error::{Error, ErrorKind}, Needed, IResult};
316316
/// use nom::bytes::streaming::take_while1;
317-
/// use nom::character::is_alphabetic;
317+
/// use nom::AsChar;
318318
///
319319
/// fn alpha(s: &[u8]) -> IResult<&[u8], &[u8]> {
320-
/// take_while1(is_alphabetic)(s)
320+
/// take_while1(AsChar::is_alpha)(s)
321321
/// }
322322
///
323323
/// assert_eq!(alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..])));
@@ -349,10 +349,10 @@ where
349349
/// ```rust
350350
/// # use nom::{Err, error::{Error, ErrorKind}, Needed, IResult};
351351
/// use nom::bytes::streaming::take_while_m_n;
352-
/// use nom::character::is_alphabetic;
352+
/// use nom::AsChar;
353353
///
354354
/// fn short_alpha(s: &[u8]) -> IResult<&[u8], &[u8]> {
355-
/// take_while_m_n(3, 6, is_alphabetic)(s)
355+
/// take_while_m_n(3, 6, AsChar::is_alpha)(s)
356356
/// }
357357
///
358358
/// assert_eq!(short_alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..])));
@@ -595,11 +595,12 @@ where
595595
/// ```
596596
pub fn take_until<T, I, Error: ParseError<I>>(tag: T) -> impl Parser<I, Output = I, Error = Error>
597597
where
598-
I: Input + FindSubstring<T>,
599-
T: Clone,
598+
I: Input + FindSubstring<T::Input>,
599+
T: IntoInput,
600+
T::Input: Clone,
600601
{
601602
TakeUntil {
602-
tag,
603+
tag: tag.into_input(),
603604
e: PhantomData,
604605
}
605606
}

src/bytes/streaming.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use core::marker::PhantomData;
55
use crate::error::ParseError;
66
use crate::internal::{IResult, Parser};
77
use crate::traits::{Compare, FindSubstring, FindToken, ToUsize};
8-
use crate::Emit;
98
use crate::Input;
109
use crate::OutputM;
1110
use crate::Streaming;
11+
use crate::{Emit, IntoInput};
1212

1313
/// Recognizes a pattern.
1414
///
@@ -30,12 +30,12 @@ use crate::Streaming;
3030
/// ```
3131
pub fn tag<T, I, Error: ParseError<I>>(tag: T) -> impl Fn(I) -> IResult<I, I, Error>
3232
where
33-
I: Input + Compare<T>,
34-
T: Input + Clone,
33+
I: Input + Compare<T::Input>,
34+
T: IntoInput + Clone,
3535
{
3636
move |i: I| {
3737
let mut parser = super::Tag {
38-
tag: tag.clone(),
38+
tag: tag.clone().into_input(),
3939
e: PhantomData,
4040
};
4141

@@ -64,12 +64,12 @@ where
6464
/// ```
6565
pub fn tag_no_case<T, I, Error: ParseError<I>>(tag: T) -> impl Fn(I) -> IResult<I, I, Error>
6666
where
67-
I: Input + Compare<T>,
68-
T: Input + Clone,
67+
I: Input + Compare<T::Input>,
68+
T: IntoInput + Clone,
6969
{
7070
move |i: I| {
7171
let mut parser = super::TagNoCase {
72-
tag: tag.clone(),
72+
tag: tag.clone().into_input(),
7373
e: PhantomData,
7474
};
7575

@@ -369,8 +369,9 @@ where
369369
/// ```
370370
pub fn take_until<T, I, Error: ParseError<I>>(tag: T) -> impl FnMut(I) -> IResult<I, I, Error>
371371
where
372-
I: Input + FindSubstring<T>,
373-
T: Clone,
372+
I: Input + FindSubstring<T::Input>,
373+
T: IntoInput,
374+
T::Input: Clone,
374375
{
375376
let mut parser = super::take_until(tag);
376377

0 commit comments

Comments
 (0)