Skip to content

Instantly share code, notes, and snippets.

@fero23
Last active November 12, 2016 21:09
Show Gist options
  • Save fero23/423b27cbba9a8bdaa44b6b6aacfe12b9 to your computer and use it in GitHub Desktop.
Save fero23/423b27cbba9a8bdaa44b6b6aacfe12b9 to your computer and use it in GitHub Desktop.
Tuple concatenation in Rust
trait And<B> {
type Result;
fn and(self, rhs: B) -> Self::Result;
}
impl<A, B> And<B> for (A,) {
type Result = (A, B);
fn and(self, rhs: B) -> (A, B) {
(self.0, rhs)
}
}
impl<A, B, C> And<C> for (A, B) {
type Result = (A, B, C);
fn and(self, rhs: C) -> (A, B, C) {
(self.0, self.1, rhs)
}
}
#[derive(Debug)]
struct Container<T>(T);
impl<A, B> And<Container<B>> for Container<A>
where A: And<B>
{
type Result = Container<<A as And<B>>::Result>;
fn and(self, rhs: Container<B>) -> Self::Result {
Container(self.0.and(rhs.0))
}
}
fn main() {
println!("{:?}", (1,).and(2).and(3));
println!("{:?}", Container((1,)).and(Container(2)).and(Container(3)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment