Skip to content

Commit baa70da

Browse files
Unwrap unary tuple suffixes
1 parent 1215e3b commit baa70da

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/prefix.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
/// valid prefix, since `P` must be a tuple.
66
pub trait Split<P>: Sized {
77
/// The remaining fields after the input has been split.
8+
///
9+
/// As a convenience, we unwrap unary tuples, so `<(A, B) as Split<(A,)>::Suffix` is `B`,
10+
/// **not** `(B,)`. We cannot do this for `P` itself though, due to overlapping impls.
811
type Suffix: Sized;
912

1013
/// Splits a tuple into a prefix and a suffix.
@@ -21,6 +24,12 @@ pub trait Split<P>: Sized {
2124
}
2225
}
2326

27+
macro_rules! fwd {
28+
() => { () };
29+
($T:ident) => { $T };
30+
($($T:ident)*) => { ($($T),*) };
31+
}
32+
2433
macro_rules! rev {
2534
($($T:ident)*) => {
2635
rev!(@REV [$($T)*])
@@ -61,13 +70,15 @@ macro_rules! impls {
6170
impls!(@imp [$($t)*] [] [$($r)*]);
6271
};
6372

73+
(@imp [$($t:ident)?] [$($p:ident)*] [$($r:ident)*]) => {};
74+
6475
(@imp [$($t:ident)+] [$($p:ident)*] [$($r:ident)*]) => {
6576
impl<$($t),*> Split<rev!($($p)*)> for rev!($($t)*) {
66-
type Suffix = ($($r,)*);
77+
type Suffix = fwd!($($r)*);
6778

6879
fn split(self) -> (rev!($($p)*), Self::Suffix) {
6980
let rev!($($t)*) = self;
70-
(rev!($($p)*), ($($r,)*))
81+
(rev!($($p)*), fwd!($($r)*))
7182
}
7283
}
7384
};

0 commit comments

Comments
 (0)