Skip to content

Instantly share code, notes, and snippets.

@sinkuu
Created October 7, 2016 07:11
Show Gist options
  • Select an option

  • Save sinkuu/517212b681ff7806200cb5be630458a3 to your computer and use it in GitHub Desktop.

Select an option

Save sinkuu/517212b681ff7806200cb5be630458a3 to your computer and use it in GitHub Desktop.
Just use Vec<Box<Trait>>;)
#![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