| 1 | use core::array; |
| 2 | |
| 3 | use crate::DynamicUsage; |
| 4 | |
| 5 | macro_rules! tuple_impls { |
| 6 | ($( |
| 7 | $Tuple:ident { |
| 8 | $(($idx:tt) -> $T:ident)+ |
| 9 | } |
| 10 | )+) => { |
| 11 | $( |
| 12 | impl<$($T),+> DynamicUsage for ($($T,)+) where $($T: DynamicUsage),+ |
| 13 | { |
| 14 | fn dynamic_usage(&self) -> usize { |
| 15 | array::IntoIter::new([$(self.$idx.dynamic_usage()),+]).sum::<usize>() |
| 16 | } |
| 17 | |
| 18 | fn dynamic_usage_bounds(&self) -> (usize, Option<usize>) { |
| 19 | array::IntoIter::new([$(self.$idx.dynamic_usage_bounds()),+]) |
| 20 | .fold( |
| 21 | (0, Some(0)), |
| 22 | |(acc_lower, acc_upper), (lower, upper)| { |
| 23 | (acc_lower + lower, acc_upper.zip(upper).map(|(a, b)| a + b)) |
| 24 | }, |
| 25 | ) |
| 26 | } |
| 27 | } |
| 28 | )+ |
| 29 | }; |
| 30 | |
| 31 | () => {}; |
| 32 | } |
| 33 | |
| 34 | tuple_impls! { |
| 35 | Tuple1 { |
| 36 | (0) -> A |
| 37 | } |
| 38 | Tuple2 { |
| 39 | (0) -> A |
| 40 | (1) -> B |
| 41 | } |
| 42 | Tuple3 { |
| 43 | (0) -> A |
| 44 | (1) -> B |
| 45 | (2) -> C |
| 46 | } |
| 47 | Tuple4 { |
| 48 | (0) -> A |
| 49 | (1) -> B |
| 50 | (2) -> C |
| 51 | (3) -> D |
| 52 | } |
| 53 | Tuple5 { |
| 54 | (0) -> A |
| 55 | (1) -> B |
| 56 | (2) -> C |
| 57 | (3) -> D |
| 58 | (4) -> E |
| 59 | } |
| 60 | Tuple6 { |
| 61 | (0) -> A |
| 62 | (1) -> B |
| 63 | (2) -> C |
| 64 | (3) -> D |
| 65 | (4) -> E |
| 66 | (5) -> F |
| 67 | } |
| 68 | Tuple7 { |
| 69 | (0) -> A |
| 70 | (1) -> B |
| 71 | (2) -> C |
| 72 | (3) -> D |
| 73 | (4) -> E |
| 74 | (5) -> F |
| 75 | (6) -> G |
| 76 | } |
| 77 | Tuple8 { |
| 78 | (0) -> A |
| 79 | (1) -> B |
| 80 | (2) -> C |
| 81 | (3) -> D |
| 82 | (4) -> E |
| 83 | (5) -> F |
| 84 | (6) -> G |
| 85 | (7) -> H |
| 86 | } |
| 87 | Tuple9 { |
| 88 | (0) -> A |
| 89 | (1) -> B |
| 90 | (2) -> C |
| 91 | (3) -> D |
| 92 | (4) -> E |
| 93 | (5) -> F |
| 94 | (6) -> G |
| 95 | (7) -> H |
| 96 | (8) -> I |
| 97 | } |
| 98 | Tuple10 { |
| 99 | (0) -> A |
| 100 | (1) -> B |
| 101 | (2) -> C |
| 102 | (3) -> D |
| 103 | (4) -> E |
| 104 | (5) -> F |
| 105 | (6) -> G |
| 106 | (7) -> H |
| 107 | (8) -> I |
| 108 | (9) -> J |
| 109 | } |
| 110 | Tuple11 { |
| 111 | (0) -> A |
| 112 | (1) -> B |
| 113 | (2) -> C |
| 114 | (3) -> D |
| 115 | (4) -> E |
| 116 | (5) -> F |
| 117 | (6) -> G |
| 118 | (7) -> H |
| 119 | (8) -> I |
| 120 | (9) -> J |
| 121 | (10) -> K |
| 122 | } |
| 123 | Tuple12 { |
| 124 | (0) -> A |
| 125 | (1) -> B |
| 126 | (2) -> C |
| 127 | (3) -> D |
| 128 | (4) -> E |
| 129 | (5) -> F |
| 130 | (6) -> G |
| 131 | (7) -> H |
| 132 | (8) -> I |
| 133 | (9) -> J |
| 134 | (10) -> K |
| 135 | (11) -> L |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | #[cfg(feature = "std")] |
| 140 | #[cfg(test)] |
| 141 | mod tests { |
| 142 | use alloc::vec::Vec; |
| 143 | use std::collections::HashSet; |
| 144 | |
| 145 | use super::*; |
| 146 | use crate::hash::WIDTH as HASH_WIDTH; |
| 147 | |
| 148 | #[test] |
| 149 | fn tuples() { |
| 150 | let a = (0u8, 0u16, 0u32); |
| 151 | assert_eq!(a.dynamic_usage(), 0); |
| 152 | assert_eq!(a.dynamic_usage_bounds(), (0, Some(0))); |
| 153 | |
| 154 | let b = (Vec::<u8>::with_capacity(10), Vec::<u64>::with_capacity(2)); |
| 155 | assert_eq!(b.dynamic_usage(), 26); |
| 156 | assert_eq!(b.dynamic_usage_bounds(), (26, Some(26))); |
| 157 | |
| 158 | let c = ( |
| 159 | HashSet::<u8>::with_capacity(10), |
| 160 | Vec::<u64>::with_capacity(2), |
| 161 | ); |
| 162 | |
| 163 | // HashSet has capacity 10 -> 16 buckets |
| 164 | let lower_bound = 32 + HASH_WIDTH + 16; |
| 165 | assert_eq!(c.dynamic_usage(), lower_bound); |
| 166 | assert_eq!(c.dynamic_usage_bounds(), (lower_bound, None)); |
| 167 | } |
| 168 | } |