Skip to content

Commit

Permalink
Implement nom::sequence::Tuple for () (Unit)
Browse files Browse the repository at this point in the history
  • Loading branch information
LoganDark authored and Geal committed May 21, 2022
1 parent 1a686f5 commit 761ab0a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/sequence/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion src/sequence/tests.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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(("", ())));
}

0 comments on commit 761ab0a

Please sign in to comment.