Skip to content

Instantly share code, notes, and snippets.

@singulared
Forked from rust-play/playground.rs
Last active February 5, 2020 00:03
Show Gist options
  • Save singulared/d8ae275513541eaa002370ae758273f3 to your computer and use it in GitHub Desktop.
Save singulared/d8ae275513541eaa002370ae758273f3 to your computer and use it in GitHub Desktop.
impl From variadic like :D
#[derive(Debug)]
struct CM {
id: i32,
topic_id: i32,
}
#[derive(Debug)]
struct TM {
id: i32,
}
#[derive(Debug)]
struct RM {
id: i32,
}
#[derive(Debug)]
struct Count(i32);
#[derive(Default, Debug)]
struct Cluster {
id: i32,
topic: Option<Topic>,
count: Option<i32>,
resource: Option<Resource>
}
#[derive(Default, Debug)]
struct Topic {
id: i32,
}
#[derive(Default, Debug)]
struct Resource {
id: i32,
}
impl From<CM> for Cluster {
fn from(cluster: CM) -> Self {
Self {
id: cluster.id,
..Default::default()
}
}
}
impl From<TM> for Topic {
fn from(topic: TM) -> Self {
Self {
id: topic.id,
}
}
}
impl From<RM> for Resource {
fn from(resource: RM) -> Self {
Self {
id: resource.id,
}
}
}
impl<T> From<(T, TM)> for Cluster
where
T: Into<Cluster>
{
fn from(data: (T, TM)) -> Self {
let (base, topic) = data;
Self {
topic: Some(Topic::from(topic)),
..base.into()
}
}
}
impl<T> From<(T, Count)> for Cluster
where
T: Into<Cluster>
{
fn from(data: (T, Count)) -> Self {
let (base, count) = data;
Self {
count: Some(count.0),
..base.into()
}
}
}
impl<T> From<(T, RM)> for Cluster
where
T: Into<Cluster>
{
fn from(data: (T, RM)) -> Self {
let (base, resource) = data;
Self {
resource: Some(Resource::from(resource)),
..base.into()
}
}
}
macro_rules! paired {
($base:expr,) => ($base);
($base:expr, $other:expr,) => (($base, $other));
($base:expr, $other:expr, $($tail:expr,)+) => (
(paired!(($base, $other), $($tail,)+))
);
}
macro_rules! tpaired {
($base:ty,) => ($base);
($base:ty, $other:ty,) => (($base, $other));
($base:ty, $other:ty, $($tail:ty,)+) => (
tpaired!(($base, $other), $($tail,)+)
);
}
macro_rules! peel {
($name:ident, $($other:ident,)*) => (impl_paired! { $($other,)* })
}
macro_rules! impl_paired {
() => ();
( $($name:ident,)+ ) => (
impl<$($name),+> Paired for ($($name,)+) {
type Output = tpaired!($($name,)+);
#[allow(non_snake_case, unused_assignments)]
fn paired(self) -> Self::Output {
let ($($name,)+) = self;
paired!($($name,)+)
}
}
peel! { $($name,)+ }
)
}
impl_paired! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
trait Paired {
type Output;
fn paired(self) -> Self::Output;
}
/*impl<T1, T2, T3> Paired for (T1, T2, T3) {
type Output = tpaired!(T1, T2, T3,);
#[allow(non_snake_case, unused_assignments)]
fn paired(self) -> Self::Output {
let (T1, T2, T3) = self;
paired!(T1, T2, T3)
}
}
impl<T1, T2, T3, T4> Paired for (T1, T2, T3, T4) {
type Output = tpaired!(T1, T2, T3, T4,);
fn paired(self) -> Self::Output {
paired!(self.0, self.1, self.2, self.3)
}
}*/
fn main() {
let t = (4, 5, "t");
dbg!(t.paired());
/*dbg!(paired!(1));
dbg!(paired!(1, 2));
dbg!(paired!(1, 2, 3, 4));*/
/*
let c = CM { id: 42, topic_id: 28 };
let cluster = Cluster::from(paired!(c));
dbg!(cluster);
let c = CM { id: 42, topic_id: 28 };
let t = TM { id: 142 };
let cluster = Cluster::from(paired!(c, t));
dbg!(cluster);
let c = CM { id: 42, topic_id: 28 };
let cnt = Count(28);
let cluster = Cluster::from(paired!(c, cnt));
dbg!(cluster);
let c = CM { id: 42, topic_id: 28 };
let t = TM { id: 142 };
let cnt = Count(28);
let cluster = Cluster::from(paired!(c, t, cnt));
dbg!(cluster);
*/
// let t = (42, "42", 'x', 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
// dbg!(tup!(t));
let c = CM { id: 42, topic_id: 28 };
let t = TM { id: 142 };
let r = RM { id: 128 };
let cnt = Count(28);
// let cluster = Cluster::from((((c, t), r), cnt));
let tp = (c, cnt, r, t);
//let l = paired!(tp.0, tp.1, tp.2, tp.3);
// let l = paired!(tp);
// let (c2, cnt2, r2, t2) = tp;
// let l = paired!(c2, cnt2, t2, r2);
// dbg!(&l);
let cluster = Cluster::from(tp.paired());
dbg!(cluster);
let test = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
assert_eq!(test.paired(),
((((((((((1, 2), 3), 4), 5), 6), 7), 8), 9), 10), 11));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment