Skip to content

Commit 2b59c45

Browse files
committed
Create Parser implementation for rest
1 parent a44b52e commit 2b59c45

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

src/bits/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ where
7373
/// bits::<_, _, Error<(&[u8], usize)>, _, _>((
7474
/// take(4usize),
7575
/// take(8usize),
76-
/// bytes::<_, _, Error<&[u8]>, _, _>(rest)
76+
/// bytes::<_, _, Error<&[u8]>, _, _>(rest())
7777
/// ))(input)
7878
/// }
7979
///

src/combinator/mod.rs

+27-7
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,37 @@ mod tests;
2424
/// Return the remaining input.
2525
///
2626
/// ```rust
27-
/// # use nom::error::ErrorKind;
27+
/// use nom::error;
28+
/// use nom::Parser;
2829
/// use nom::combinator::rest;
29-
/// assert_eq!(rest::<_,(_, ErrorKind)>("abc"), Ok(("", "abc")));
30-
/// assert_eq!(rest::<_,(_, ErrorKind)>(""), Ok(("", "")));
30+
/// assert_eq!(rest::<&str, error::Error<&str>>().parse_complete("abc"), Ok(("", "abc")));
31+
/// assert_eq!(rest::<&str, error::Error<&str>>().parse_complete(""), Ok(("", "")));
3132
/// ```
3233
#[inline]
33-
pub fn rest<T, E: ParseError<T>>(input: T) -> IResult<T, T, E>
34+
pub fn rest<T, E: ParseError<T>>() -> impl Parser<T, Output = T, Error = E>
3435
where
3536
T: Input,
3637
{
37-
Ok(input.take_split(input.input_len()))
38+
Rest {
39+
i: PhantomData,
40+
e: PhantomData,
41+
}
42+
}
43+
44+
/// Parser implementation for [rest]
45+
pub struct Rest<I, E> {
46+
i: PhantomData<I>,
47+
e: PhantomData<E>,
48+
}
49+
50+
impl<I: Input, E: ParseError<I>> Parser<I> for Rest<I, E> {
51+
type Output = I;
52+
type Error = E;
53+
54+
fn process<OM: OutputMode>(&mut self, input: I) -> PResult<OM, I, Self::Output, Self::Error> {
55+
let (i, o) = input.take_split(input.input_len());
56+
Ok((i, OM::Output::bind(|| o)))
57+
}
3858
}
3959

4060
/// Return the length of the remaining input.
@@ -766,7 +786,7 @@ where
766786
/// fn parser(input: &str) -> IResult<&str, &str> {
767787
/// alt((
768788
/// preceded(one_of("+-"), digit1),
769-
/// rest
789+
/// rest()
770790
/// )).parse(input)
771791
/// }
772792
///
@@ -789,7 +809,7 @@ where
789809
/// fn parser(input: &str) -> IResult<&str, &str> {
790810
/// alt((
791811
/// preceded(one_of("+-"), cut(digit1)),
792-
/// rest
812+
/// rest()
793813
/// )).parse(input)
794814
/// }
795815
///

src/combinator/tests.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::internal::{Err, IResult, Needed};
77
#[cfg(feature = "alloc")]
88
use crate::lib::std::boxed::Box;
99
use crate::number::complete::u8;
10+
use crate::Parser;
1011

1112
macro_rules! assert_parse(
1213
($left: expr, $right: expr) => {
@@ -73,14 +74,14 @@ fn end_of_input() {
7374
fn rest_on_slices() {
7475
let input: &[u8] = &b"Hello, world!"[..];
7576
let empty: &[u8] = &b""[..];
76-
assert_parse!(rest(input), Ok((empty, input)));
77+
assert_parse!(rest().parse_complete(input), Ok((empty, input)));
7778
}
7879

7980
#[test]
8081
fn rest_on_strs() {
8182
let input: &str = "Hello, world!";
8283
let empty: &str = "";
83-
assert_parse!(rest(input), Ok((empty, input)));
84+
assert_parse!(rest().parse_complete(input), Ok((empty, input)));
8485
}
8586

8687
#[test]

0 commit comments

Comments
 (0)