1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
// TODO associated_type_defaults https://github.com/rust-lang/rust/issues/29661

use std::{
	collections::HashMap, hash::{BuildHasher, Hash}
};

use super::*;

// + AmadeusOrd + DowncastFrom<Value> + Into<Value>
pub trait Data: Clone + Debug + Send + Sized + 'static {
	type Vec: ListVec<Self>;
	type DynamicType;
	fn new_vec(_type: Self::DynamicType) -> Self::Vec;
}

impl<T> Data for Option<T>
where
	T: Data,
{
	type Vec = Vec<Self>;
	type DynamicType = ();

	fn new_vec(_type: Self::DynamicType) -> Self::Vec {
		Vec::new()
	}
}
impl<T> Data for Box<T>
where
	T: Data,
{
	type Vec = Vec<Self>;
	type DynamicType = ();

	fn new_vec(_type: Self::DynamicType) -> Self::Vec {
		Vec::new()
	}
}
impl<T> Data for List<T>
where
	T: Data,
{
	type Vec = Vec<Self>;
	type DynamicType = ();

	fn new_vec(_type: Self::DynamicType) -> Self::Vec {
		Vec::new()
	}
}
impl<K, V, S> Data for HashMap<K, V, S>
where
	K: Hash + Eq + Data,
	V: Data,
	S: BuildHasher + Clone + Default + Send + 'static,
{
	type Vec = Vec<Self>;
	type DynamicType = ();

	fn new_vec(_type: Self::DynamicType) -> Self::Vec {
		Vec::new()
	}
}

macro_rules! impl_data {
	($($t:ty)*) => ($(
		impl Data for $t {
			type Vec = Vec<Self>;
			type DynamicType = ();

			fn new_vec(_type: Self::DynamicType) -> Self::Vec {
				Vec::new()
			}
		}
	)*);
}
impl_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);

// Implement Record for common array lengths.
macro_rules! array {
	($($i:tt)*) => {$(
		impl Data for [u8; $i] {
			type Vec = Vec<Self>;
			type DynamicType = ();

			fn new_vec(_type: Self::DynamicType) -> Self::Vec {
				Vec::new()
			}
		}
		// TODO: impl<T> Data for [T; $i] where T: Data {}
	)*};
}
super::array!(array);

// Implement Record on tuples up to length 12.
macro_rules! tuple {
	($len:tt $($t:ident $i:tt)*) => {
		impl<$($t,)*> Data for ($($t,)*) where $($t: Data,)* {
			type Vec = Vec<Self>;
			type DynamicType = ();

			fn new_vec(_type: Self::DynamicType) -> Self::Vec {
				Vec::new()
			}
		}
	};
}
super::tuple!(tuple);