Fix bits and bytes functions, expecting Fn instead of FnMut (Closes #1231)

This commit is contained in:
Pierre Chifflier 2020-11-12 08:55:59 +01:00 committed by Geoffroy Couprie
parent 8bb87ae3f2
commit 76c52af52f
2 changed files with 24 additions and 8 deletions

View File

@ -35,11 +35,11 @@ use crate::traits::{ErrorConvert, Slice};
/// assert_eq!(take_4_bits( sl ), Ok( (&sl[1..], 0xA) ));
/// ```
pub fn bits<I, O, E1: ParseError<(I, usize)> + ErrorConvert<E2>, E2: ParseError<I>, P>(
parser: P,
) -> impl Fn(I) -> IResult<I, O, E2>
mut parser: P,
) -> impl FnMut(I) -> IResult<I, O, E2>
where
I: Slice<RangeFrom<usize>>,
P: Fn((I, usize)) -> IResult<(I, usize), O, E1>,
P: FnMut((I, usize)) -> IResult<(I, usize), O, E1>,
{
move |input: I| match parser((input, 0)) {
Ok(((rest, offset), res)) => {
@ -59,7 +59,7 @@ pub fn bitsc<I, O, E1: ParseError<(I, usize)> + ErrorConvert<E2>, E2: ParseError
) -> IResult<I, O, E2>
where
I: Slice<RangeFrom<usize>>,
P: Fn((I, usize)) -> IResult<(I, usize), O, E1>,
P: FnMut((I, usize)) -> IResult<(I, usize), O, E1>,
{
bits(parser)(input)
}
@ -90,11 +90,11 @@ where
/// assert_eq!(parse( input ), Ok(( &[][..], (0xd, 0xea, &[0xbe, 0xaf][..]) )));
/// ```
pub fn bytes<I, O, E1: ParseError<I> + ErrorConvert<E2>, E2: ParseError<(I, usize)>, P>(
parser: P,
) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E2>
mut parser: P,
) -> impl FnMut((I, usize)) -> IResult<(I, usize), O, E2>
where
I: Slice<RangeFrom<usize>> + Clone,
P: Fn(I) -> IResult<I, O, E1>,
P: FnMut(I) -> IResult<I, O, E1>,
{
move |(input, offset): (I, usize)| {
let inner = if offset % 8 != 0 {
@ -123,7 +123,7 @@ pub fn bytesc<I, O, E1: ParseError<I> + ErrorConvert<E2>, E2: ParseError<(I, usi
) -> IResult<(I, usize), O, E2>
where
I: Slice<RangeFrom<usize>> + Clone,
P: Fn(I) -> IResult<I, O, E1>,
P: FnMut(I) -> IResult<I, O, E1>,
{
bytes(parser)(input)
}

View File

@ -347,3 +347,19 @@ named!(issue_962<&[u8], Vec<&[u8]>>,
}
)
);
#[test]
fn issue_1231_bits_expect_fn_closure() {
use nom::bits::{bits, complete::take};
use nom::error::Error;
use nom::sequence::tuple;
pub fn example(input: &[u8]) -> IResult<&[u8], (u8, u8)> {
bits::<_, _, Error<_>, _, _>(tuple((
take(1usize), take(1usize)
)))(input)
}
assert_eq!(
example(&[0xff]),
Ok((&b""[..], (1, 1)))
);
}