1#![allow(non_snake_case)]
2
3use alloc::{vec::Vec, string::String};
4use crate::{Columnar, Container, Borrow, Len, Clear, Index, IndexMut, Push};
5
6macro_rules! tuple_impl {
10 ( $($name:ident,$name2:ident,$idx:tt)+) => (
11
12 impl<$($name: Columnar),*> Columnar for ($($name,)*) {
13 #[inline(always)]
14 fn copy_from<'a>(&mut self, other: crate::Ref<'a, Self>) {
15 let ($($name,)*) = self;
16 let ($($name2,)*) = other;
17 $(crate::Columnar::copy_from($name, $name2);)*
18 }
19 #[inline(always)]
20 fn into_owned<'a>(other: crate::Ref<'a, Self>) -> Self {
21 let ($($name2,)*) = other;
22 ($($name::into_owned($name2),)*)
23 }
24 type Container = ($($name::Container,)*);
25 }
26 impl<$($name2: Borrow,)*> Borrow for ($($name2,)*) {
27 type Ref<'a> = ($($name2::Ref<'a>,)*) where $($name2: 'a,)*;
28 type Borrowed<'a> = ($($name2::Borrowed<'a>,)*) where $($name2: 'a,)*;
29 #[inline(always)]
30 fn borrow<'a>(&'a self) -> Self::Borrowed<'a> {
31 let ($($name,)*) = self;
32 ($($name.borrow(),)*)
33 }
34 #[inline(always)]
35 fn reborrow<'b, 'a: 'b>(thing: Self::Borrowed<'a>) -> Self::Borrowed<'b> where $($name2: 'a,)* {
36 let ($($name,)*) = thing;
37 ($($name2::reborrow($name),)*)
38 }
39 #[inline(always)]
40 fn reborrow_ref<'b, 'a: 'b>(thing: Self::Ref<'a>) -> Self::Ref<'b> where Self: 'a {
41 let ($($name2,)*) = thing;
42 ($($name2::reborrow_ref($name2),)*)
43 }
44 }
45 impl<$($name2: Container,)*> Container for ($($name2,)*) {
46 #[inline(always)]
47 fn extend_from_self(&mut self, other: Self::Borrowed<'_>, range: core::ops::Range<usize>) {
48 let ($($name,)*) = self;
49 let ($($name2,)*) = other;
50 $( $name.extend_from_self($name2, range.clone()); )*
51 }
52
53 fn reserve_for<'a, I>(&mut self, selves: I) where Self: 'a, I: Iterator<Item = Self::Borrowed<'a>> + Clone {
54 let ($($name,)*) = self;
55 $( $name.reserve_for(selves.clone().map(|x| x.$idx)); )*
56 }
57 }
58
59 #[allow(non_snake_case)]
60 impl<'a, $($name: crate::AsBytes<'a>),*> crate::AsBytes<'a> for ($($name,)*) {
61 #[inline(always)]
62 fn as_bytes(&self) -> impl Iterator<Item=(u64, &'a [u8])> {
63 let ($($name,)*) = self;
64 let iter = None.into_iter();
65 $( let iter = crate::chain(iter, $name.as_bytes()); )*
66 iter
67 }
68 }
69 impl<'a, $($name: crate::FromBytes<'a>),*> crate::FromBytes<'a> for ($($name,)*) {
70 const SLICE_COUNT: usize = 0 $(+ $name::SLICE_COUNT)*;
71 #[inline(always)]
72 #[allow(non_snake_case)]
73 fn from_bytes(bytes: &mut impl Iterator<Item=&'a [u8]>) -> Self {
74 $(let $name = crate::FromBytes::from_bytes(bytes);)*
75 ($($name,)*)
76 }
77 #[inline(always)]
78 #[allow(non_snake_case)]
79 fn from_store(store: &crate::bytes::indexed::DecodedStore<'a>, offset: &mut usize) -> Self {
80 $(let $name = $name::from_store(store, offset);)*
81 ($($name,)*)
82 }
83 fn element_sizes(sizes: &mut Vec<usize>) -> Result<(), String> {
84 $($name::element_sizes(sizes)?;)*
85 Ok(())
86 }
87 }
88
89 impl<$($name: Len),*> Len for ($($name,)*) {
90 #[inline(always)]
91 fn len(&self) -> usize {
92 self.0.len()
93 }
94 }
95 impl<$($name: Clear),*> Clear for ($($name,)*) {
96 #[inline(always)]
97 fn clear(&mut self) {
98 let ($($name,)*) = self;
99 $($name.clear();)*
100 }
101 }
102 impl<$($name: Index),*> Index for ($($name,)*) {
103 type Ref = ($($name::Ref,)*);
104 #[inline(always)]
105 fn get(&self, index: usize) -> Self::Ref {
106 let ($($name,)*) = self;
107 ($($name.get(index),)*)
108 }
109 }
110 impl<'a, $($name),*> Index for &'a ($($name,)*) where $( &'a $name: Index),* {
111 type Ref = ($(<&'a $name as Index>::Ref,)*);
112 #[inline(always)]
113 fn get(&self, index: usize) -> Self::Ref {
114 let ($($name,)*) = self;
115 ($($name.get(index),)*)
116 }
117 }
118
119 impl<$($name: IndexMut),*> IndexMut for ($($name,)*) {
120 type IndexMut<'a> = ($($name::IndexMut<'a>,)*) where $($name: 'a),*;
121 #[inline(always)]
122 fn get_mut(&mut self, index: usize) -> Self::IndexMut<'_> {
123 let ($($name,)*) = self;
124 ($($name.get_mut(index),)*)
125 }
126 }
127 impl<$($name2, $name: Push<$name2>),*> Push<($($name2,)*)> for ($($name,)*) {
128 #[inline]
129 fn push(&mut self, item: ($($name2,)*)) {
130 let ($($name,)*) = self;
131 let ($($name2,)*) = item;
132 $($name.push($name2);)*
133 }
134 }
135 impl<'a, $($name2, $name: Push<&'a $name2>),*> Push<&'a ($($name2,)*)> for ($($name,)*) {
136 #[inline]
137 fn push(&mut self, item: &'a ($($name2,)*)) {
138 let ($($name,)*) = self;
139 let ($($name2,)*) = item;
140 $($name.push($name2);)*
141 }
142 }
143 )
144}
145
146tuple_impl!(A,AA,0);
147tuple_impl!(A,AA,0 B,BB,1);
148tuple_impl!(A,AA,0 B,BB,1 C,CC,2);
149tuple_impl!(A,AA,0 B,BB,1 C,CC,2 D,DD,3);
150tuple_impl!(A,AA,0 B,BB,1 C,CC,2 D,DD,3 E,EE,4);
151tuple_impl!(A,AA,0 B,BB,1 C,CC,2 D,DD,3 E,EE,4 F,FF,5);
152tuple_impl!(A,AA,0 B,BB,1 C,CC,2 D,DD,3 E,EE,4 F,FF,5 G,GG,6);
153tuple_impl!(A,AA,0 B,BB,1 C,CC,2 D,DD,3 E,EE,4 F,FF,5 G,GG,6 H,HH,7);
154tuple_impl!(A,AA,0 B,BB,1 C,CC,2 D,DD,3 E,EE,4 F,FF,5 G,GG,6 H,HH,7 I,II,8);
155tuple_impl!(A,AA,0 B,BB,1 C,CC,2 D,DD,3 E,EE,4 F,FF,5 G,GG,6 H,HH,7 I,II,8 J,JJ,9);
156
157#[cfg(test)]
158mod test {
159 use alloc::string::{String, ToString};
160 #[test]
161 fn round_trip() {
162
163 use crate::common::{Index, Push, Len};
164
165 let mut column: crate::ContainerOf<(u64, u8, String)> = Default::default();
166 for i in 0..100 {
167 column.push((i, i as u8, &i.to_string()));
168 column.push((i, i as u8, &"".to_string()));
169 }
170
171 assert_eq!(column.len(), 200);
172
173 for i in 0..100u64 {
174 assert_eq!((&column).get((2*i+0) as usize), (&i, &(i as u8), i.to_string().as_bytes()));
175 assert_eq!((&column).get((2*i+1) as usize), (&i, &(i as u8), &b""[..]));
176 }
177
178 }
179}