Created
October 7, 2016 07:11
-
-
Save sinkuu/517212b681ff7806200cb5be630458a3 to your computer and use it in GitHub Desktop.
Just use Vec<Box<Trait>>;)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #![feature(unsize)] | |
| use std::marker::Unsize; | |
| pub trait Variadic<Trait: ?Sized> { | |
| type OneLess: Variadic<Trait>; | |
| fn pop(self) -> (Option<Box<Trait>>, Self::OneLess); | |
| } | |
| impl<Trait: ?Sized, A, B, C> Variadic<Trait> for (A, B, C) where A: Unsize<Trait>, B: Unsize<Trait>, C: Unsize<Trait> { | |
| type OneLess = (B, C); | |
| fn pop(self) -> (Option<Box<Trait>>, (B, C)) { | |
| let (a, b, c) = self; | |
| let a = Box::new(a) as Box<Trait>; | |
| (Some(a), (b, c)) | |
| } | |
| } | |
| impl<Trait: ?Sized, A, B> Variadic<Trait> for (A, B) where A: Unsize<Trait>, B: Unsize<Trait> { | |
| type OneLess = (B,); | |
| fn pop(self) -> (Option<Box<Trait>>, (B,)) { | |
| let (a, b) = self; | |
| let a = Box::new(a) as Box<Trait>; | |
| (Some(a), (b,)) | |
| } | |
| } | |
| impl<Trait: ?Sized, A> Variadic<Trait> for (A,) where A: Unsize<Trait> { | |
| type OneLess = (); | |
| fn pop(self) -> (Option<Box<Trait>>, ()) { | |
| let (a,) = self; | |
| let a = Box::new(a) as Box<Trait>; | |
| (Some(a), ()) | |
| } | |
| } | |
| impl<Trait: ?Sized> Variadic<Trait> for () { | |
| type OneLess = (); | |
| fn pop(self) -> (Option<Box<Trait>>, ()) { | |
| (None, ()) | |
| } | |
| } | |
| fn print_debug<A: Variadic<std::fmt::Debug + 'static>>(args: A) { | |
| if let (Some(a), rest) = args.pop() { | |
| println!("{:?}", a); | |
| print_debug(rest); | |
| } | |
| } | |
| fn main() { | |
| print_debug((1, "hello", 1.3)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment