use lender::{Lend, Lender, Lending};
use crate::seekable::delegate_seekable;
use crate::{
comparator::Comparator, lending_iterator_support::LentItem,
pooled::PooledIterator, seekable::Seekable,
};
use crate::cursor::{CursorLendingIterator, CursorPooledIterator};
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(docsrs, doc(cfg(feature = "lender")))]
pub struct LenderAdapter<I>(I);
impl<I> LenderAdapter<I> {
#[inline]
#[must_use]
pub(crate) const fn new(iter: I) -> Self {
Self(iter)
}
#[inline]
#[must_use]
pub fn into_inner(self) -> I {
self.0
}
}
impl<'lend, I: CursorLendingIterator> Lending<'lend> for LenderAdapter<I> {
type Lend = LentItem<'lend, I>;
}
impl<I: CursorLendingIterator> Lender for LenderAdapter<I> {
#[inline]
fn next(&mut self) -> Option<Lend<'_, Self>> {
self.0.next()
}
}
impl<I: CursorLendingIterator> LenderAdapter<I> {
#[inline]
#[must_use]
pub fn valid(&self) -> bool {
self.0.valid()
}
#[inline]
#[must_use]
pub fn current(&self) -> Option<Lend<'_, Self>> {
self.0.current()
}
#[inline]
#[must_use]
pub fn prev(&mut self) -> Option<Lend<'_, Self>> {
self.0.prev()
}
}
delegate_seekable!(LenderAdapter.0);
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(docsrs, doc(cfg(feature = "lender")))]
pub struct PooledLenderAdapter<I: PooledIterator> {
iter: I,
item: Option<I::Item>,
}
impl<I: PooledIterator> PooledLenderAdapter<I> {
#[inline]
#[must_use]
pub(crate) const fn new(iter: I) -> Self {
Self {
iter,
item: None,
}
}
#[inline]
#[must_use]
pub fn into_inner(self) -> I {
self.iter
}
}
impl<'lend, I: PooledIterator> Lending<'lend> for PooledLenderAdapter<I> {
type Lend = &'lend I::Item;
}
impl<I: PooledIterator> Lender for PooledLenderAdapter<I> {
#[inline]
fn next(&mut self) -> Option<Lend<'_, Self>> {
self.item = None;
self.item = self.iter.next();
self.item.as_ref()
}
}
impl<I: CursorPooledIterator> PooledLenderAdapter<I> {
#[inline]
#[must_use]
pub fn valid(&self) -> bool {
self.iter.valid()
}
#[inline]
#[must_use]
pub const fn current(&self) -> Option<Lend<'_, Self>> {
self.item.as_ref()
}
#[inline]
#[must_use]
pub fn prev(&mut self) -> Option<Lend<'_, Self>> {
self.item = None;
self.item = self.iter.prev();
self.item.as_ref()
}
}
delegate_seekable!(PooledLenderAdapter.iter PooledIterator);