tuwunel_database/stream/
items.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, keyval_longevity};
11use crate::keyval::KeyVal;
12
13pub(crate) struct Items<'a> {
14 state: State<'a>,
15}
16
17impl<'a> From<State<'a>> for Items<'a> {
18 #[inline]
19 fn from(state: State<'a>) -> Self { Self { state } }
20}
21
22impl<'a> Cursor<'a, KeyVal<'a>> for Items<'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_fwd() }
31
32 #[inline]
33 fn fetch(&self) -> Option<KeyVal<'a>> { self.state().fetch().map(keyval_longevity) }
34
35 #[inline]
36 fn seek(&mut self) { self.state_mut().seek_fwd(); }
37}
38
39impl<'a> Stream for Items<'a> {
40 type Item = Result<KeyVal<'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 Items<'_> {
50 #[inline]
51 fn is_terminated(&self) -> bool { !self.state().init && !self.state().valid() }
52}