instance_code/
primitives.rs1use std::marker::PhantomData;
2
3use super::*;
4
5macro_rules! primitive {
6 ($($type:ty),*) => {
7 $(
8 impl InstanceCode for $type {
9 fn instance_code(&self) -> TokenStream {
10 quote! {
11 #self
12 }
13 }
14 }
15 )*
16 }
17}
18
19primitive!(
20 u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64, &str, char, bool
21);
22
23impl<'a, T: ?Sized> InstanceCode for &'a T
24where
25 T: InstanceCode,
26{
27 fn instance_code(&self) -> TokenStream {
28 let t = <T as InstanceCode>::instance_code(*self);
29 quote! {
30 &#t
31 }
32 }
33}
34
35impl<T> InstanceCode for [T]
36where
37 T: InstanceCode,
38{
39 fn instance_code(&self) -> TokenStream {
40 let slice = self.iter().map(|d| d.instance_code());
41 quote!([#(#slice),*])
42 }
43}
44
45impl<T, const N: usize> InstanceCode for [T; N]
46where
47 T: InstanceCode,
48{
49 fn instance_code(&self) -> TokenStream {
50 self.as_slice().instance_code()
51 }
52}
53
54impl<T> InstanceCode for Option<T>
55where
56 T: InstanceCode,
57{
58 fn instance_code(&self) -> TokenStream {
59 match self {
60 None => quote! { None },
61 Some(t) => {
62 let t = t.instance_code();
63 quote! { Some(#t) }
64 }
65 }
66 }
67}
68
69impl<T, E> InstanceCode for Result<T, E>
70where
71 T: InstanceCode,
72 E: InstanceCode,
73{
74 fn instance_code(&self) -> TokenStream {
75 match self {
76 Ok(ok) => {
77 let ok = ok.instance_code();
78 quote!(Ok(#ok))
79 }
80 Err(e) => {
81 let e = e.instance_code();
82 quote!(Err(#e))
83 }
84 }
85 }
86}
87
88impl<T> InstanceCode for PhantomData<T> {
89 fn instance_code(&self) -> TokenStream {
90 quote! {
91 ::core::marker::PhantomData
92 }
93 }
94}
95
96macro_rules! tuple {
97 ($($ty:ident),*) => {
98 #[allow(non_snake_case)]
99 impl<$($ty),*> InstanceCode for ($($ty,)*) where $($ty: InstanceCode),* {
100 fn instance_code(&self) -> TokenStream {
101 let ($($ty,)*) = self;
102 $(
103 let $ty = $ty.instance_code();
104 )*
105 quote! {
106 ($(#$ty),*)
107 }
108 }
109 }
110 }
111}
112
113tuple!();
114tuple!(A);
115tuple!(A, B);
116tuple!(A, B, C);
117tuple!(A, B, C, D);
118tuple!(A, B, C, D, E);
119tuple!(A, B, C, D, E, F);
120tuple!(A, B, C, D, E, F, G);
121tuple!(A, B, C, D, E, F, G, H);
122tuple!(A, B, C, D, E, F, G, H, I);
123tuple!(A, B, C, D, E, F, G, H, I, J);