#[cfg(feature = "alloc")]
use crate::alloc;
use core::{alloc::Allocator, fmt};
use crate::{DynList, Ends};
use super::super::node::Node;
pub struct Cursor<
'a,
U: ?Sized,
#[cfg(feature = "alloc")] A = alloc::Global,
#[cfg(not(feature = "alloc"))] A,
> where
A: Allocator,
{
pub(crate) current: Option<Node<U>>,
pub(crate) list: &'a DynList<U, A>,
}
impl<U, A> Clone for Cursor<'_, U, A>
where
U: ?Sized,
A: Allocator,
{
#[inline]
fn clone(&self) -> Self {
Self {
current: self.current,
list: self.list,
}
}
}
impl<'a, U, A> Cursor<'a, U, A>
where
U: ?Sized,
A: Allocator,
{
pub fn move_next(&mut self) {
self.current = match self.current {
None => self.list.ends.map(|Ends { front, .. }| front),
Some(node) => unsafe { node.header_ptr().as_ref() }.next,
}
}
pub fn move_previous(&mut self) {
self.current = match self.current {
None => self.list.ends.map(|Ends { back, .. }| back),
Some(node) => unsafe { node.header_ptr().as_ref() }.previous,
}
}
#[must_use]
pub fn current(&self) -> Option<&'a U> {
self.current.map(|node| {
let ptr = unsafe { node.data_ptr() };
unsafe { ptr.as_ref() }
})
}
#[must_use]
#[inline]
pub const fn as_list(&self) -> &'a DynList<U, A> {
self.list
}
}
unsafe impl<U, A> Send for Cursor<'_, U, A>
where
U: ?Sized + Sync,
A: Allocator + Sync,
{
}
unsafe impl<U, A> Sync for Cursor<'_, U, A>
where
U: ?Sized + Sync,
A: Allocator + Sync,
{
}
impl<U, A> fmt::Debug for Cursor<'_, U, A>
where
U: ?Sized + fmt::Debug,
A: Allocator,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Cursor").field(self).finish()
}
}