Implement nom::sequence::Tuple for () (Unit)

This commit is contained in:
LoganDark 2022-05-12 10:32:53 -07:00 committed by Geoffroy Couprie
parent 1a686f5180
commit 761ab0a24f
2 changed files with 17 additions and 1 deletions

View File

@ -252,6 +252,15 @@ macro_rules! tuple_trait_inner(
tuple_trait!(FnA A, FnB B, FnC C, FnD D, FnE E, FnF F, FnG G, FnH H, FnI I, FnJ J, FnK K, FnL L,
FnM M, FnN N, FnO O, FnP P, FnQ Q, FnR R, FnS S, FnT T, FnU U);
// Special case: implement `Tuple` for `()`, the unit type.
// This can come up in macros which accept a variable number of arguments.
// Literally, `()` is an empty tuple, so it should simply parse nothing.
impl<I, E: ParseError<I>> Tuple<I, (), E> for () {
fn parse(&mut self, input: I) -> IResult<I, (), E> {
Ok((input, ()))
}
}
///Applies a tuple of parsers one by one and returns their results as a tuple.
///There is a maximum of 21 parsers
/// ```rust

View File

@ -1,6 +1,6 @@
use super::*;
use crate::bytes::streaming::{tag, take};
use crate::error::ErrorKind;
use crate::error::{ErrorKind, Error};
use crate::internal::{Err, IResult, Needed};
use crate::number::streaming::be_u16;
@ -272,3 +272,10 @@ fn tuple_test() {
Err(Err::Error(error_position!(&b"jk"[..], ErrorKind::Tag)))
);
}
#[test]
fn unit_type() {
assert_eq!(tuple::<&'static str, (), Error<&'static str>, ()>(())("abxsbsh"), Ok(("abxsbsh", ())));
assert_eq!(tuple::<&'static str, (), Error<&'static str>, ()>(())("sdfjakdsas"), Ok(("sdfjakdsas", ())));
assert_eq!(tuple::<&'static str, (), Error<&'static str>, ()>(())(""), Ok(("", ())));
}