Skip to content

Commit 761ab0a

Browse files
LoganDarkGeal
authored andcommitted
Implement nom::sequence::Tuple for () (Unit)
1 parent 1a686f5 commit 761ab0a

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/sequence/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,15 @@ macro_rules! tuple_trait_inner(
252252
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,
253253
FnM M, FnN N, FnO O, FnP P, FnQ Q, FnR R, FnS S, FnT T, FnU U);
254254

255+
// Special case: implement `Tuple` for `()`, the unit type.
256+
// This can come up in macros which accept a variable number of arguments.
257+
// Literally, `()` is an empty tuple, so it should simply parse nothing.
258+
impl<I, E: ParseError<I>> Tuple<I, (), E> for () {
259+
fn parse(&mut self, input: I) -> IResult<I, (), E> {
260+
Ok((input, ()))
261+
}
262+
}
263+
255264
///Applies a tuple of parsers one by one and returns their results as a tuple.
256265
///There is a maximum of 21 parsers
257266
/// ```rust

src/sequence/tests.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::*;
22
use crate::bytes::streaming::{tag, take};
3-
use crate::error::ErrorKind;
3+
use crate::error::{ErrorKind, Error};
44
use crate::internal::{Err, IResult, Needed};
55
use crate::number::streaming::be_u16;
66

@@ -272,3 +272,10 @@ fn tuple_test() {
272272
Err(Err::Error(error_position!(&b"jk"[..], ErrorKind::Tag)))
273273
);
274274
}
275+
276+
#[test]
277+
fn unit_type() {
278+
assert_eq!(tuple::<&'static str, (), Error<&'static str>, ()>(())("abxsbsh"), Ok(("abxsbsh", ())));
279+
assert_eq!(tuple::<&'static str, (), Error<&'static str>, ()>(())("sdfjakdsas"), Ok(("sdfjakdsas", ())));
280+
assert_eq!(tuple::<&'static str, (), Error<&'static str>, ()>(())(""), Ok(("", ())));
281+
}

0 commit comments

Comments
 (0)