Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trait TupleAppend<T> {
- type ResultType;
- fn append(self, t: T) -> Self::ResultType;
- }
- macro_rules! impl_tuple_append {
- (
- // Toss on variadic parameters to slightly reduce boilerplate.
- $(
- ( $($types:ident),* );
- )*
- ) => {
- $(
- impl<$($types,)* T> TupleAppend<T> for ($($types,)*) {
- // Note the trailing comma, ensuring we are always dealing
- // with a tuple and not a parenthesized type/expr.
- type ResultType = ($($types,)* T,);
- fn append(self, t: T) -> Self::ResultType {
- // Reuse the type identifiers to destructure ourselves:
- let ($($types,)*) = self;
- // Create a new tuple with the original elements, plus the new one:
- ($($types,)* t,)
- }
- }
- )*
- };
- }
- impl_tuple_append! {
- ();
- (_1);
- (_1, _2);
- (_1, _2, _3);
- (_1, _2, _3, _4);
- (_1, _2, _3, _4, _5);
- (_1, _2, _3, _4, _5, _6);
- (_1, _2, _3, _4, _5, _6, _7);
- (_1, _2, _3, _4, _5, _6, _7, _8);
- (_1, _2, _3, _4, _5, _6, _7, _8, _9);
- (_1, _2, _3, _4, _5, _6, _7, _8, _9, _10);
- // Continue until the largest size you wish to support.
- // May be worth adding a build script to automate this.
- }
- fn main() {
- let some_tuple: (i32, &str, bool) = (1, "Hello", true);
- println!("{:?}", some_tuple);
- let with_world: (i32, &str, bool, &str) = some_tuple.append("World");
- println!("{:?}", with_world);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement