continuwuity/src/database/stream/items_rev.rs
strawberry 77e0b76408
apply new rustfmt.toml changes, fix some clippy lints
Signed-off-by: strawberry <strawberry@puppygock.gay>
2024-12-15 01:00:41 -05:00

51 lines
1.2 KiB
Rust

use std::{convert, pin::Pin, sync::Arc};
use conduwuit::Result;
use futures::{
stream::FusedStream,
task::{Context, Poll},
Stream,
};
use rocksdb::{ColumnFamily, ReadOptions};
use super::{keyval_longevity, Cursor, From, State};
use crate::{keyval::KeyVal, Engine};
pub(crate) struct ItemsRev<'a> {
state: State<'a>,
}
impl<'a> ItemsRev<'a> {
pub(crate) fn new(db: &'a Arc<Engine>, cf: &'a Arc<ColumnFamily>, opts: ReadOptions) -> Self {
Self { state: State::new(db, cf, opts) }
}
}
impl<'a> convert::From<State<'a>> for ItemsRev<'a> {
fn from(state: State<'a>) -> Self { Self { state } }
}
impl<'a> Cursor<'a, KeyVal<'a>> for ItemsRev<'a> {
fn state(&self) -> &State<'a> { &self.state }
fn fetch(&self) -> Option<KeyVal<'a>> { self.state.fetch().map(keyval_longevity) }
#[inline]
fn seek(&mut self) { self.state.seek_rev(); }
#[inline]
fn init(self, from: From<'a>) -> Self { Self { state: self.state.init_rev(from) } }
}
impl<'a> Stream for ItemsRev<'a> {
type Item = Result<KeyVal<'a>>;
fn poll_next(mut self: Pin<&mut Self>, _ctx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Poll::Ready(self.seek_and_get())
}
}
impl FusedStream for ItemsRev<'_> {
#[inline]
fn is_terminated(&self) -> bool { !self.state.init && !self.state.valid() }
}