#[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
use crate::error::NetabaseError;
#[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
pub struct BorrowedGuard<'txn, M>
where
M: redb::Value + 'static,
{
guard: redb::AccessGuard<'txn, M>,
}
#[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
impl<'txn, M> BorrowedGuard<'txn, M>
where
M: redb::Value,
{
pub(crate) fn _new(guard: redb::AccessGuard<'txn, M>) -> Self {
Self { guard }
}
pub fn value(&self) -> M::SelfType<'_> {
self.guard.value()
}
pub fn to_owned(&self) -> M
where
M: for<'a> From<M::SelfType<'a>>,
{
M::from(self.value())
}
pub fn with_value<F, R>(&self, f: F) -> R
where
F: FnOnce(M::SelfType<'_>) -> R,
{
f(self.value())
}
}
#[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
impl<'txn, M> std::fmt::Debug for BorrowedGuard<'txn, M>
where
M: redb::Value,
for<'a> M::SelfType<'a>: std::fmt::Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("BorrowedGuard")
.field("value", &self.value())
.finish()
}
}
#[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
pub struct BorrowedIter<'txn, K, V>
where
K: redb::Key + 'static,
V: redb::Value + 'static,
{
#[allow(dead_code)]
iter: redb::Range<'txn, K, V>,
}
#[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
impl<'txn, K, V> BorrowedIter<'txn, K, V>
where
K: redb::Key + 'static,
V: redb::Value + 'static,
{
#[allow(dead_code)]
pub(crate) fn new(iter: redb::Range<'txn, K, V>) -> Self {
Self { iter }
}
}
#[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
impl<'txn, K, V> Iterator for BorrowedIter<'txn, K, V>
where
K: redb::Key + 'static,
V: redb::Value + 'static,
{
type Item = Result<(K::SelfType<'txn>, V::SelfType<'txn>), NetabaseError>;
fn next(&mut self) -> Option<Self::Item> {
None
}
}
#[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
impl<'txn, K, V> std::fmt::Debug for BorrowedIter<'txn, K, V>
where
K: redb::Key,
V: redb::Value,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("BorrowedIter").finish_non_exhaustive()
}
}
#[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
impl<'txn, K, V> BorrowedIter<'txn, K, V>
where
K: redb::Key,
V: redb::Value,
{
pub fn collect_owned(self) -> Result<Vec<(K, V)>, NetabaseError>
where
K: for<'a> From<K::SelfType<'a>>,
V: for<'a> From<V::SelfType<'a>>,
{
self.map(|result| result.map(|(k, v)| (K::from(k), V::from(v))))
.collect()
}
pub fn filter_borrowed<F>(self, predicate: F) -> FilterBorrowed<'txn, K, V, F>
where
F: FnMut(&V::SelfType<'_>) -> bool,
{
FilterBorrowed {
iter: self,
predicate,
}
}
}
#[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
pub struct FilterBorrowed<'txn, K, V, F>
where
K: redb::Key + 'static,
V: redb::Value + 'static,
F: FnMut(&V::SelfType<'_>) -> bool,
{
iter: BorrowedIter<'txn, K, V>,
predicate: F,
}
#[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
impl<'txn, K, V, F> Iterator for FilterBorrowed<'txn, K, V, F>
where
K: redb::Key + 'static,
V: redb::Value + 'static,
F: FnMut(&V::SelfType<'_>) -> bool,
{
type Item = Result<(K::SelfType<'txn>, V::SelfType<'txn>), NetabaseError>;
fn next(&mut self) -> Option<Self::Item> {
loop {
match self.iter.next() {
Some(Ok((k, v))) => {
if (self.predicate)(&v) {
return Some(Ok((k, v)));
}
}
Some(Err(e)) => return Some(Err(e)),
None => return None,
}
}
}
}
#[cfg(test)]
#[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
mod tests {
use super::*;
}