mew/typemap.rs
1/// A really bad way to genericize struct fields.
2///
3/// This abomination lets you create a struct with one field per type,
4/// accessible through `as_ref()`. Internally this is just a tuple, so there is
5/// the caveat that you need to manually specify the index of each "field".
6///
7/// ```
8/// typemap! { SetOfSomeObjects =>
9/// 0 SomeBindGroupLayoutOne,
10/// 1 SomeOtherBindGroupTwo,
11/// 2 EvenABuffer,
12/// 3 AndHeresAPipeline,
13/// 4 f32,
14/// }
15///
16/// let set_of_some_objects = SetOfSomeObjects(
17/// SomeBindGroupLayoutOne::new(),
18/// ...
19/// );
20///
21/// let buffer: EvenABuffer = set_of_some_objects.as_ref();
22/// let pipeline: AndHeresAPipeline = set_of_some_objects.as_ref();
23/// ```
24#[macro_export]
25macro_rules! typemap {
26 { $struct:ident =>
27 $( $num:tt $ty:ty ),* $(,)?
28 } => {
29 pub struct $struct ( $( $ty, )* );
30
31 $( impl ::std::convert::AsRef<$ty> for $struct {
32 fn as_ref(&self) -> &$ty {
33 &self.$num
34 }
35 } )*
36 }
37}