amadeus_types/
data.rs

1// TODO associated_type_defaults https://github.com/rust-lang/rust/issues/29661
2
3use std::{
4	collections::HashMap, hash::{BuildHasher, Hash}
5};
6
7use super::*;
8
9// + AmadeusOrd + DowncastFrom<Value> + Into<Value>
10pub trait Data: Clone + Debug + Send + Sized + 'static {
11	type Vec: ListVec<Self>;
12	type DynamicType;
13	fn new_vec(_type: Self::DynamicType) -> Self::Vec;
14}
15
16impl<T> Data for Option<T>
17where
18	T: Data,
19{
20	type Vec = Vec<Self>;
21	type DynamicType = ();
22
23	fn new_vec(_type: Self::DynamicType) -> Self::Vec {
24		Vec::new()
25	}
26}
27impl<T> Data for Box<T>
28where
29	T: Data,
30{
31	type Vec = Vec<Self>;
32	type DynamicType = ();
33
34	fn new_vec(_type: Self::DynamicType) -> Self::Vec {
35		Vec::new()
36	}
37}
38impl<T> Data for List<T>
39where
40	T: Data,
41{
42	type Vec = Vec<Self>;
43	type DynamicType = ();
44
45	fn new_vec(_type: Self::DynamicType) -> Self::Vec {
46		Vec::new()
47	}
48}
49impl<K, V, S> Data for HashMap<K, V, S>
50where
51	K: Hash + Eq + Data,
52	V: Data,
53	S: BuildHasher + Clone + Default + Send + 'static,
54{
55	type Vec = Vec<Self>;
56	type DynamicType = ();
57
58	fn new_vec(_type: Self::DynamicType) -> Self::Vec {
59		Vec::new()
60	}
61}
62
63macro_rules! impl_data {
64	($($t:ty)*) => ($(
65		impl Data for $t {
66			type Vec = Vec<Self>;
67			type DynamicType = ();
68
69			fn new_vec(_type: Self::DynamicType) -> Self::Vec {
70				Vec::new()
71			}
72		}
73	)*);
74}
75impl_data!(bool u8 i8 u16 i16 u32 i32 u64 i64 f32 f64 String Bson Json Enum Decimal Group Date DateWithoutTimezone Time TimeWithoutTimezone DateTime DateTimeWithoutTimezone Timezone Webpage<'static> Url IpAddr);
76
77// Implement Record for common array lengths.
78macro_rules! array {
79	($($i:tt)*) => {$(
80		impl Data for [u8; $i] {
81			type Vec = Vec<Self>;
82			type DynamicType = ();
83
84			fn new_vec(_type: Self::DynamicType) -> Self::Vec {
85				Vec::new()
86			}
87		}
88		// TODO: impl<T> Data for [T; $i] where T: Data {}
89	)*};
90}
91super::array!(array);
92
93// Implement Record on tuples up to length 12.
94macro_rules! tuple {
95	($len:tt $($t:ident $i:tt)*) => {
96		impl<$($t,)*> Data for ($($t,)*) where $($t: Data,)* {
97			type Vec = Vec<Self>;
98			type DynamicType = ();
99
100			fn new_vec(_type: Self::DynamicType) -> Self::Vec {
101				Vec::new()
102			}
103		}
104	};
105}
106super::tuple!(tuple);