Skip to main content

tuwunel_database/stream/
keys_rev.rs

1use 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> {
14	state: State<'a>,
15}
16
17impl<'a> From<State<'a>> for KeysRev<'a> {
18	#[inline]
19	fn from(state: State<'a>) -> Self { Self { state } }
20}
21
22impl<'a> Cursor<'a, Key<'a>> for KeysRev<'a> {
23	#[inline]
24	fn state(&self) -> &State<'a> { &self.state }
25
26	#[inline]
27	fn state_mut(&mut self) -> &mut State<'a> { &mut self.state }
28
29	#[inline]
30	fn count(&self) -> (usize, Option<usize>) { self.state().count_rev() }
31
32	#[inline]
33	fn fetch(&self) -> Option<Key<'a>> { self.state().fetch_key().map(slice_longevity) }
34
35	#[inline]
36	fn seek(&mut self) { self.state_mut().seek_rev(); }
37}
38
39impl<'a> Stream for KeysRev<'a> {
40	type Item = Result<Key<'a>>;
41
42	fn poll_next(mut self: Pin<&mut Self>, _ctx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
43		Poll::Ready(self.seek_and_get())
44	}
45
46	fn size_hint(&self) -> (usize, Option<usize>) { self.count() }
47}
48
49impl FusedStream for KeysRev<'_> {
50	#[inline]
51	fn is_terminated(&self) -> bool { !self.state().init && !self.state().valid() }
52}