macro_rules! first_item {
    ($id_type:ty, $(({$($first:tt)*}, $second:expr)),* $(,)?) => { ... };
    ($id_type:ty, $(($first:expr, $second:expr)),* $(,)?) => { ... };
}
Expand description

Converts a comma-separated list of pairs into an array with the first element. That is, it discards the second element of the pair.

Additionally, it automatically introduces a type if the first element is warpped in curly braces, for example, if it’s {v: 10}, it becomes X { v: 10 }; this is to avoid repeating the type.

Examples


#[derive(PartialEq, Debug)]
struct X {
    v: u32,
}

assert_eq!([] as [X; 0], first_item!(X, ));
assert_eq!([X { v: 10 }], first_item!(X, ({ v: 10 }, Y)));
assert_eq!([X { v: 10 }], first_item!(X, ({ v: 10 }, Y),));
assert_eq!([X { v: 10 }], first_item!(X, (X { v: 10 }, Y)));
assert_eq!([X { v: 10 }], first_item!(X, (X { v: 10 }, Y),));
assert_eq!([X { v: 10 }, X { v: 20 }], first_item!(X, ({ v: 10 }, Y), ({ v: 20 }, Y)));
assert_eq!([X { v: 10 }, X { v: 20 }], first_item!(X, ({ v: 10 }, Y), ({ v: 20 }, Y),));
assert_eq!([X { v: 10 }, X { v: 20 }], first_item!(X, (X { v: 10 }, Y), (X { v: 20 }, Y)));
assert_eq!([X { v: 10 }, X { v: 20 }], first_item!(X, (X { v: 10 }, Y), (X { v: 20 }, Y),));
assert_eq!([X { v: 10 }, X { v: 20 }, X { v: 30 }],
           first_item!(X, ({ v: 10 }, Y), ({ v: 20 }, Y), ({v: 30}, Y)));
assert_eq!([X { v: 10 }, X { v: 20 }, X { v: 30 }],
           first_item!(X, ({ v: 10 }, Y), ({ v: 20 }, Y), ({v: 30}, Y),));
assert_eq!([X { v: 10 }, X { v: 20 }, X { v: 30 }],
           first_item!(X, (X { v: 10 }, Y), (X { v: 20 }, Y), (X {v: 30}, Y)));
assert_eq!([X { v: 10 }, X { v: 20 }, X { v: 30 }],
           first_item!(X, (X { v: 10 }, Y), (X { v: 20 }, Y), (X {v: 30}, Y),));