tuwunel_database/stream/
keys_rev.rs1use std::pin::Pin;
2
3use futures::{
4 Stream,
5 stream::FusedStream,
6 task::{Context, Poll},
7};
8use tuwunel_core::Result;
9
10use super::{Cursor, State, slice_longevity};
11use crate::keyval::Key;
12
13pub(crate) struct KeysRev<'a> {
19 state: State<'a>,
20}
21
22impl<'a> From<State<'a>> for KeysRev<'a> {
23 #[inline]
24 fn from(state: State<'a>) -> Self { Self { state } }
25}
26
27impl<'a> Cursor<'a, Key<'a>> for KeysRev<'a> {
28 #[inline]
29 fn state(&self) -> &State<'a> { &self.state }
30
31 #[inline]
32 fn state_mut(&mut self) -> &mut State<'a> { &mut self.state }
33
34 #[inline]
35 fn count(&self) -> (usize, Option<usize>) { self.state().count_rev() }
36
37 #[inline]
38 fn fetch(&self) -> Option<Key<'a>> { self.state().fetch_key().map(slice_longevity) }
39
40 #[inline]
41 fn seek(&mut self) { self.state_mut().seek_rev(); }
42}
43
44impl<'a> Stream for KeysRev<'a> {
45 type Item = Result<Key<'a>>;
46
47 fn poll_next(mut self: Pin<&mut Self>, _ctx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
48 Poll::Ready(self.seek_and_get())
49 }
50
51 fn size_hint(&self) -> (usize, Option<usize>) { self.count() }
52}
53
54impl FusedStream for KeysRev<'_> {
55 #[inline]
56 fn is_terminated(&self) -> bool { !self.state().init && !self.state().valid() }
57}