|
| 1 | +use std::borrow::Borrow; |
| 2 | +use std::future::Future; |
| 3 | +use std::ops::RangeBounds; |
| 4 | + |
| 5 | +use futures_util::stream::BoxStream; |
| 6 | + |
| 7 | +use crate::map_api::MapApi; |
| 8 | +use crate::map_api::MapApiRO; |
| 9 | +use crate::map_api::MapKey; |
| 10 | +use crate::Level; |
| 11 | +use crate::LevelMap; |
| 12 | +use crate::Ref; |
| 13 | +use crate::RefMut; |
| 14 | +use crate::StaticLevels; |
| 15 | + |
| 16 | +impl LevelMap { |
| 17 | + pub fn new(w: Level, frozen: StaticLevels) -> Self { |
| 18 | + Self { |
| 19 | + writable: w, |
| 20 | + frozen, |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + pub fn iter_levels(&self) -> impl Iterator<Item = &Level> { |
| 25 | + [&self.writable].into_iter().chain(self.frozen.iter_levels()) |
| 26 | + } |
| 27 | + |
| 28 | + pub fn to_ref_mut(&mut self) -> RefMut { |
| 29 | + RefMut::new(&mut self.writable, &self.frozen) |
| 30 | + } |
| 31 | + |
| 32 | + pub fn to_ref(&self) -> Ref { |
| 33 | + Ref::new(&self.writable, &self.frozen) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +impl<'ro_me, K> MapApiRO<K> for &'ro_me LevelMap |
| 38 | +where |
| 39 | + K: MapKey, |
| 40 | + // &'ro_me Level: MapApiRO<K>, |
| 41 | + for<'him> &'him Level: MapApiRO<K>, |
| 42 | +{ |
| 43 | + type GetFut<'f, Q> = impl Future<Output =K::V> + 'f |
| 44 | + where Self: 'f, |
| 45 | + 'ro_me: 'f, |
| 46 | + K: Borrow<Q>, |
| 47 | + Q: Ord + Send + Sync + ?Sized, |
| 48 | + Q: 'f; |
| 49 | + |
| 50 | + fn get<'f, Q>(self, key: &'f Q) -> Self::GetFut<'f, Q> |
| 51 | + where |
| 52 | + 'ro_me: 'f, |
| 53 | + K: Borrow<Q>, |
| 54 | + Q: Ord + Send + Sync + ?Sized, |
| 55 | + { |
| 56 | + self.to_ref().get(key) |
| 57 | + } |
| 58 | + |
| 59 | + type RangeFut<'f, Q, R> = impl Future<Output = BoxStream<'f, (K, K::V)>> |
| 60 | + where |
| 61 | + Self: 'f, |
| 62 | + 'ro_me: 'f, |
| 63 | + K: Borrow<Q>, |
| 64 | + R: RangeBounds<Q> + Send + Sync + Clone, |
| 65 | + Q: Ord + Send + Sync + ?Sized, |
| 66 | + Q: 'f; |
| 67 | + |
| 68 | + fn range<'f, Q, R>(self, range: R) -> Self::RangeFut<'f, Q, R> |
| 69 | + where |
| 70 | + 'ro_me: 'f, |
| 71 | + K: Borrow<Q>, |
| 72 | + Q: Ord + Send + Sync + ?Sized, |
| 73 | + R: RangeBounds<Q> + Clone + Send + Sync, |
| 74 | + { |
| 75 | + self.to_ref().range(range) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl<'me, K> MapApi<'me, K> for &'me mut LevelMap |
| 80 | +where |
| 81 | + K: MapKey, |
| 82 | + for<'e> &'e Level: MapApiRO<K>, |
| 83 | + for<'him> &'him mut Level: MapApi<'him, K>, |
| 84 | +{ |
| 85 | + type SetFut<'f> = impl Future<Output = (K::V, K::V)> + 'f |
| 86 | + where |
| 87 | + Self: 'f, |
| 88 | + 'me: 'f; |
| 89 | + |
| 90 | + fn set<'f>(self, key: K, value: Option<K::V>) -> Self::SetFut<'f> |
| 91 | + where 'me: 'f /* */ |
| 92 | + |
| 93 | +/* 'd: 'f, */ { |
| 94 | + self.to_ref_mut().set(key, value) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +#[cfg(test)] |
| 99 | +mod tests { |
| 100 | + use std::sync::Arc; |
| 101 | + |
| 102 | + use futures_util::StreamExt; |
| 103 | + |
| 104 | + use crate::map_api::MapApi; |
| 105 | + use crate::map_api::MapApiRO; |
| 106 | + use crate::Level; |
| 107 | + use crate::LevelMap; |
| 108 | + use crate::StaticLevels; |
| 109 | + use crate::Val; |
| 110 | + |
| 111 | + #[tokio::test] |
| 112 | + async fn test_level_map() { |
| 113 | + let k = || "a".to_string(); |
| 114 | + |
| 115 | + let mut d = Level { |
| 116 | + kv: Default::default(), |
| 117 | + }; |
| 118 | + |
| 119 | + d.kv.insert(k(), Val(1)); |
| 120 | + |
| 121 | + let static_levels = { |
| 122 | + let mut d1 = Level { |
| 123 | + kv: Default::default(), |
| 124 | + }; |
| 125 | + |
| 126 | + let mut d2 = Level { |
| 127 | + kv: Default::default(), |
| 128 | + }; |
| 129 | + |
| 130 | + d1.kv.insert(k(), Val(3)); |
| 131 | + d2.kv.insert(k(), Val(2)); |
| 132 | + |
| 133 | + StaticLevels::new([Arc::new(d1), Arc::new(d2)]) |
| 134 | + }; |
| 135 | + |
| 136 | + { |
| 137 | + let d = Level { |
| 138 | + kv: Default::default(), |
| 139 | + }; |
| 140 | + let mut lm = LevelMap::new(d, static_levels); |
| 141 | + |
| 142 | + let res = lm.set(k(), Some(Val(7))).await; |
| 143 | + assert_eq!(res, (Val(2), Val(7))); |
| 144 | + |
| 145 | + let got = lm.get(&k()).await; |
| 146 | + assert_eq!(got, Val(7)); |
| 147 | + |
| 148 | + let strm = lm.range(k()..).await; |
| 149 | + let got = strm.collect::<Vec<_>>().await; |
| 150 | + assert_eq!(got, vec![(k(), Val(2)), (k(), Val(7)), (k(), Val(3))]); |
| 151 | + |
| 152 | + // let x = k(); |
| 153 | + // let fu = lm.get(x.as_str()); |
| 154 | + // |
| 155 | + // let fu = foo(fu); |
| 156 | + // |
| 157 | + // fn foo<T: Send>(v: T) -> T { |
| 158 | + // v |
| 159 | + // } |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments