nom icon indicating copy to clipboard operation
nom copied to clipboard

many0 returning error when no more data present

Open Lazarus404 opened this issue 2 years ago • 2 comments

Prerequisites

Here are a few things you should provide to help me understand the issue:

  • Rust version : rustc 1.59.0
  • nom version : 7.1

Test case

The test case should return a vec of tuples. However, instead, the returned data is:

Err(Incomplete(Size(2)))

Example test case:

use nom::number::complete::be_u16;
use nom::bytes::streaming::take;
use nom::multi::{many0, length_data};
use nom::combinator::map;
use nom::sequence::tuple;

let input = &[
            0u8, 1u8, 0u8, 2u8, 127u8, 127u8, 0u8, 22u8, 0u8, 3u8, 255u8, 0u8, 255u8, 0u8, 19u8, 0u8, 3u8, 111u8, 222u8, 111u8
        ];
many0(map(
    tuple((
        take(2usize),
        length_data(be_u16),
    )),
    |(fmt, val): (&[u8], &[u8])| {
        (fmt, val)
    },
))(input);

When printing out each iteration within the map, I can see the bytes are correctly consumed in the right quantities.

Lazarus404 avatar Apr 13 '22 22:04 Lazarus404

This is intended behaviour. By using bytes::streaming::take, you're opting in to it - there is no way to know if this is actually the end of the data or if there might be more, so take() returns Incomplete. You can use bytes::complete::take if you just want to read everything until the end of input.

Xiretza avatar Apr 14 '22 07:04 Xiretza

That makes absolute logical sense, and I put this down to me posting the issue at 11pm last night 😜

Thank you for the prompt response.

Lazarus404 avatar Apr 14 '22 08:04 Lazarus404