@@ -11,6 +11,7 @@ use std::marker::PhantomData;
11
11
pub use column:: { Column , ColumnAccess } ;
12
12
pub use item:: { Item , ItemAccess } ;
13
13
pub use map:: { Map , MapAccess } ;
14
+ use storey_storage:: RevIterableStorage ;
14
15
15
16
use crate :: storage:: IterableStorage ;
16
17
@@ -84,30 +85,83 @@ pub trait IterableAccessor: Sized {
84
85
fn storage ( & self ) -> & Self :: Storage ;
85
86
86
87
/// Iterate over key-value pairs in this collection.
87
- fn pairs ( & self ) -> StorableIter < ' _ , Self :: Storable , Self :: Storage > {
88
+ fn pairs (
89
+ & self ,
90
+ ) -> StorableIter < Self :: Storable , <Self :: Storage as IterableStorage >:: PairsIterator < ' _ > > {
88
91
StorableIter {
89
92
inner : self . storage ( ) . pairs ( None , None ) ,
90
93
phantom : PhantomData ,
91
94
}
92
95
}
93
96
94
97
/// Iterate over keys in this collection.
95
- fn keys ( & self ) -> StorableKeys < ' _ , Self :: Storable , Self :: Storage > {
98
+ fn keys (
99
+ & self ,
100
+ ) -> StorableKeys < Self :: Storable , <Self :: Storage as IterableStorage >:: KeysIterator < ' _ > > {
96
101
StorableKeys {
97
102
inner : self . storage ( ) . keys ( None , None ) ,
98
103
phantom : PhantomData ,
99
104
}
100
105
}
101
106
102
107
/// Iterate over values in this collection.
103
- fn values ( & self ) -> StorableValues < ' _ , Self :: Storable , Self :: Storage > {
108
+ fn values (
109
+ & self ,
110
+ ) -> StorableValues < Self :: Storable , <Self :: Storage as IterableStorage >:: ValuesIterator < ' _ > >
111
+ {
104
112
StorableValues {
105
113
inner : self . storage ( ) . values ( None , None ) ,
106
114
phantom : PhantomData ,
107
115
}
108
116
}
109
117
}
110
118
119
+ pub trait RevIterableAccessor
120
+ where
121
+ Self : IterableAccessor ,
122
+ Self :: Storage : RevIterableStorage ,
123
+ {
124
+ /// Iterate over key-value pairs in this collection in reverse order.
125
+ fn rev_pairs (
126
+ & self ,
127
+ ) -> StorableIter < Self :: Storable , <Self :: Storage as RevIterableStorage >:: RevPairsIterator < ' _ > >
128
+ {
129
+ StorableIter {
130
+ inner : self . storage ( ) . rev_pairs ( None , None ) ,
131
+ phantom : PhantomData ,
132
+ }
133
+ }
134
+
135
+ /// Iterate over keys in this collection in reverse order.
136
+ fn rev_keys (
137
+ & self ,
138
+ ) -> StorableKeys < Self :: Storable , <Self :: Storage as RevIterableStorage >:: RevKeysIterator < ' _ > >
139
+ {
140
+ StorableKeys {
141
+ inner : self . storage ( ) . rev_keys ( None , None ) ,
142
+ phantom : PhantomData ,
143
+ }
144
+ }
145
+
146
+ /// Iterate over values in this collection in reverse order.
147
+ fn rev_values (
148
+ & self ,
149
+ ) -> StorableValues < Self :: Storable , <Self :: Storage as RevIterableStorage >:: RevValuesIterator < ' _ > >
150
+ {
151
+ StorableValues {
152
+ inner : self . storage ( ) . rev_values ( None , None ) ,
153
+ phantom : PhantomData ,
154
+ }
155
+ }
156
+ }
157
+
158
+ impl < I > RevIterableAccessor for I
159
+ where
160
+ I : IterableAccessor ,
161
+ I :: Storage : RevIterableStorage ,
162
+ {
163
+ }
164
+
111
165
/// This trait extends [`IterableAccessor`] with methods for bounded iteration. Not every
112
166
/// iterable collection supports it, so this trait is separate.
113
167
///
@@ -129,7 +183,7 @@ pub trait BoundedIterableAccessor: IterableAccessor {
129
183
& self ,
130
184
start : Option < B > ,
131
185
end : Option < B > ,
132
- ) -> StorableIter < ' _ , Self :: Storable , Self :: Storage >
186
+ ) -> StorableIter < Self :: Storable , < Self :: Storage as IterableStorage > :: PairsIterator < ' _ > >
133
187
where
134
188
B : BoundFor < Self :: Storable > ,
135
189
{
@@ -147,7 +201,7 @@ pub trait BoundedIterableAccessor: IterableAccessor {
147
201
& self ,
148
202
start : Option < B > ,
149
203
end : Option < B > ,
150
- ) -> StorableKeys < ' _ , Self :: Storable , Self :: Storage >
204
+ ) -> StorableKeys < Self :: Storable , < Self :: Storage as IterableStorage > :: KeysIterator < ' _ > >
151
205
where
152
206
B : BoundFor < Self :: Storable > ,
153
207
{
@@ -165,7 +219,7 @@ pub trait BoundedIterableAccessor: IterableAccessor {
165
219
& self ,
166
220
start : Option < B > ,
167
221
end : Option < B > ,
168
- ) -> StorableValues < ' _ , Self :: Storable , Self :: Storage >
222
+ ) -> StorableValues < Self :: Storable , < Self :: Storage as IterableStorage > :: ValuesIterator < ' _ > >
169
223
where
170
224
B : BoundFor < Self :: Storable > ,
171
225
{
@@ -179,6 +233,82 @@ pub trait BoundedIterableAccessor: IterableAccessor {
179
233
}
180
234
}
181
235
236
+ /// This trait extends [`BoundedIterableAccessor`] with methods for bounded reverse iteration.
237
+ /// Not every iterable collection supports it, so this trait is separate.
238
+ ///
239
+ /// Bounded reverse iteration allows the user to specify a start and end bound for the iteration,
240
+ /// but in reverse order.
241
+ ///
242
+ /// # Why not always support bounded reverse iteration?
243
+ ///
244
+ /// The same reasons as for [bounded iteration](BoundedIterableAccessor) apply.
245
+ pub trait BoundedRevIterableAccessor
246
+ where
247
+ Self : BoundedIterableAccessor ,
248
+ Self :: Storage : RevIterableStorage ,
249
+ {
250
+ /// Iterate over key-value pairs in this collection in reverse order, respecting the given bounds.
251
+ fn bounded_rev_pairs < B > (
252
+ & self ,
253
+ start : Option < B > ,
254
+ end : Option < B > ,
255
+ ) -> StorableIter < Self :: Storable , <Self :: Storage as RevIterableStorage >:: RevPairsIterator < ' _ > >
256
+ where
257
+ B : BoundFor < Self :: Storable > ,
258
+ {
259
+ let start = start. map ( |b| b. into_bytes ( ) ) ;
260
+ let end = end. map ( |b| b. into_bytes ( ) ) ;
261
+
262
+ StorableIter {
263
+ inner : self . storage ( ) . rev_pairs ( start. as_deref ( ) , end. as_deref ( ) ) ,
264
+ phantom : PhantomData ,
265
+ }
266
+ }
267
+
268
+ /// Iterate over keys in this collection in reverse order, respecting the given bounds.
269
+ fn bounded_rev_keys < B > (
270
+ & self ,
271
+ start : Option < B > ,
272
+ end : Option < B > ,
273
+ ) -> StorableKeys < Self :: Storable , <Self :: Storage as RevIterableStorage >:: RevKeysIterator < ' _ > >
274
+ where
275
+ B : BoundFor < Self :: Storable > ,
276
+ {
277
+ let start = start. map ( |b| b. into_bytes ( ) ) ;
278
+ let end = end. map ( |b| b. into_bytes ( ) ) ;
279
+
280
+ StorableKeys {
281
+ inner : self . storage ( ) . rev_keys ( start. as_deref ( ) , end. as_deref ( ) ) ,
282
+ phantom : PhantomData ,
283
+ }
284
+ }
285
+
286
+ /// Iterate over values in this collection in reverse order, respecting the given bounds.
287
+ fn bounded_rev_values < B > (
288
+ & self ,
289
+ start : Option < B > ,
290
+ end : Option < B > ,
291
+ ) -> StorableValues < Self :: Storable , <Self :: Storage as RevIterableStorage >:: RevValuesIterator < ' _ > >
292
+ where
293
+ B : BoundFor < Self :: Storable > ,
294
+ {
295
+ let start = start. map ( |b| b. into_bytes ( ) ) ;
296
+ let end = end. map ( |b| b. into_bytes ( ) ) ;
297
+
298
+ StorableValues {
299
+ inner : self . storage ( ) . rev_values ( start. as_deref ( ) , end. as_deref ( ) ) ,
300
+ phantom : PhantomData ,
301
+ }
302
+ }
303
+ }
304
+
305
+ impl < I > BoundedRevIterableAccessor for I
306
+ where
307
+ I : BoundedIterableAccessor ,
308
+ I :: Storage : RevIterableStorage ,
309
+ {
310
+ }
311
+
182
312
/// A type that can be used as bounds for iteration over a given collection.
183
313
///
184
314
/// As an example, a collection `Foo` with string-y keys can accept both `String` and
@@ -190,19 +320,15 @@ pub trait BoundFor<T> {
190
320
}
191
321
192
322
/// The iterator over key-value pairs in a collection.
193
- pub struct StorableIter < ' i , S , B >
194
- where
195
- S : Storable ,
196
- B : IterableStorage + ' i ,
197
- {
198
- inner : B :: PairsIterator < ' i > ,
323
+ pub struct StorableIter < S , I > {
324
+ inner : I ,
199
325
phantom : PhantomData < S > ,
200
326
}
201
327
202
- impl < ' i , S , B > Iterator for StorableIter < ' i , S , B >
328
+ impl < S , I > Iterator for StorableIter < S , I >
203
329
where
204
330
S : Storable ,
205
- B : IterableStorage + ' i ,
331
+ I : Iterator < Item = ( Vec < u8 > , Vec < u8 > ) > ,
206
332
{
207
333
type Item = Result < ( S :: Key , S :: Value ) , KVDecodeError < S :: KeyDecodeError , S :: ValueDecodeError > > ;
208
334
@@ -218,19 +344,15 @@ where
218
344
}
219
345
220
346
/// The iterator over keys in a collection.
221
- pub struct StorableKeys < ' i , S , B >
222
- where
223
- S : Storable ,
224
- B : IterableStorage + ' i ,
225
- {
226
- inner : B :: KeysIterator < ' i > ,
347
+ pub struct StorableKeys < S , I > {
348
+ inner : I ,
227
349
phantom : PhantomData < S > ,
228
350
}
229
351
230
- impl < ' i , S , B > Iterator for StorableKeys < ' i , S , B >
352
+ impl < S , I > Iterator for StorableKeys < S , I >
231
353
where
232
354
S : Storable ,
233
- B : IterableStorage + ' i ,
355
+ I : Iterator < Item = Vec < u8 > > ,
234
356
{
235
357
type Item = Result < S :: Key , S :: KeyDecodeError > ;
236
358
@@ -240,19 +362,15 @@ where
240
362
}
241
363
242
364
/// The iterator over values in a collection.
243
- pub struct StorableValues < ' i , S , B >
244
- where
245
- S : Storable ,
246
- B : IterableStorage + ' i ,
247
- {
248
- inner : B :: ValuesIterator < ' i > ,
365
+ pub struct StorableValues < S , I > {
366
+ inner : I ,
249
367
phantom : PhantomData < S > ,
250
368
}
251
369
252
- impl < ' i , S , B > Iterator for StorableValues < ' i , S , B >
370
+ impl < S , I > Iterator for StorableValues < S , I >
253
371
where
254
372
S : Storable ,
255
- B : IterableStorage + ' i ,
373
+ I : Iterator < Item = Vec < u8 > > ,
256
374
{
257
375
type Item = Result < S :: Value , S :: ValueDecodeError > ;
258
376
0 commit comments