#![no_std]
#![deny(
warnings,
missing_docs,
missing_debug_implementations,
rust_2018_idioms
)]
extern crate alloc;
mod cursor;
pub mod iter;
mod vec;
#[cfg(feature = "atomic")]
pub use self::cursor::AtomicCursor;
pub use self::cursor::{AsCursor, Cursor};
pub use self::vec::CurVec;
use self::iter::{Iter, WrappingIter};
pub unsafe trait Cursed<T> {
fn is_owner(&self, cursor: Cursor<T>) -> bool;
}
pub trait Sequence<T>: Cursed<T> {
fn next(&self, cursor: Cursor<T>) -> Option<Cursor<T>>;
fn prev(&self, cursor: Cursor<T>) -> Option<Cursor<T>>;
fn remaining(&self, cursor: Cursor<T>) -> (usize, Option<usize>);
}
#[allow(clippy::len_without_is_empty)]
pub trait Bounded<T>: Sequence<T> {
fn len(&self) -> usize;
fn head(&self) -> Cursor<T>;
fn tail(&self) -> Cursor<T>;
fn at(&self, offset: usize) -> Option<Cursor<T>>;
}
pub unsafe trait Contiguous<T>: Bounded<T> {}
pub trait CursedExt<T>: Cursed<T> + Sized {
#[inline]
fn get(&self, cursor: Cursor<T>) -> &T {
cursor.get(self)
}
#[inline]
fn get_mut(&mut self, cursor: Cursor<T>) -> &mut T {
cursor.get_mut(self)
}
#[inline]
fn is_head(&self, cursor: Cursor<T>) -> bool
where
Self: Bounded<T>,
{
cursor == self.head()
}
#[inline]
fn is_tail(&self, cursor: Cursor<T>) -> bool
where
Self: Bounded<T>,
{
cursor == self.tail()
}
fn offset(&self, cursor: Cursor<T>) -> usize
where
Self: Bounded<T>,
{
assert!(self.is_owner(cursor));
cursor.offset_from(self.head())
}
#[inline]
fn wrapping_next(&self, cursor: Cursor<T>) -> Cursor<T>
where
Self: Bounded<T>,
{
if cursor == self.tail() {
self.head()
} else {
match self.next(cursor) {
Some(next_cursor) => next_cursor,
None => unreachable!(),
}
}
}
#[inline]
fn wrapping_prev(&self, cursor: Cursor<T>) -> Cursor<T>
where
Self: Bounded<T>,
{
if cursor == self.head() {
self.tail()
} else {
match self.prev(cursor) {
Some(prev_cursor) => prev_cursor,
None => unreachable!(),
}
}
}
#[inline]
fn iter(&self) -> Iter<'_, Self, T>
where
Self: Bounded<T>,
{
self.iter_at(self.head())
}
fn iter_at(&self, cursor: Cursor<T>) -> Iter<'_, Self, T>
where
Self: Sequence<T>,
{
Iter::new(self, cursor)
}
#[inline]
fn wrapping_iter(&self) -> WrappingIter<'_, Self, T>
where
Self: Bounded<T>,
{
self.wrapping_iter_at(self.head())
}
#[inline]
fn wrapping_iter_at(&self, cursor: Cursor<T>) -> WrappingIter<'_, Self, T>
where
Self: Sequence<T>,
{
WrappingIter::new(self, cursor)
}
}
impl<T, U> CursedExt<U> for T where T: Cursed<U> {}
#[cfg(feature = "atomic")]
mod atomic {
#[cfg(feature = "loom")]
pub use loom::sync::atomic::{AtomicPtr, Ordering};
#[cfg(not(feature = "loom"))]
pub use core::sync::atomic::{AtomicPtr, Ordering};
}