mod inline;
use core::fmt::Debug;
use core::hash::Hash;
use core::ops::{Deref, DerefMut};
pub use inline::{Buffer, Inline};
use crate::{Container, Error, StateTimeline, Timeline};
mod sealed {
pub trait Sealed {}
}
pub trait Slot: Clone + Debug + Eq + Hash + sealed::Sealed {
const EMPTY: Self;
}
pub trait Seq<T: Slot>:
Deref<Target = [T]> + DerefMut + Default + Clone + Debug + Eq + Hash + sealed::Sealed
{
const EMPTY: Self;
fn capacity(&self) -> Option<usize>;
fn try_reserve(&mut self, extra: usize, container: Container) -> Result<(), Error>;
fn try_push(&mut self, item: T, container: Container) -> Result<(), Error>;
fn try_insert(&mut self, index: usize, item: T, container: Container) -> Result<(), Error>;
fn remove(&mut self, index: usize) -> T;
fn pop(&mut self) -> Option<T>;
fn retain(&mut self, keep: impl FnMut(&T) -> bool);
fn truncate(&mut self, len: usize);
fn clear(&mut self) {
self.truncate(0);
}
fn dedup(&mut self)
where
T: Copy,
{
let mut previous: Option<T> = None;
self.retain(|&item| {
let keep = previous != Some(item);
previous = Some(item);
keep
});
}
fn try_extend_from_slice(&mut self, items: &[T], container: Container) -> Result<(), Error> {
self.try_reserve(items.len(), container)?;
for item in items {
self.try_push(item.clone(), container)?;
}
Ok(())
}
}
pub trait Storage: Clone + Copy + Debug + Default + Eq + Hash + sealed::Sealed {
type List<T: Slot>: Seq<T>;
type DoubleList<T: Slot>: Seq<T>;
}
pub trait PlanStorage: Clone + Copy + Debug + Default + Eq + Hash + sealed::Sealed {
type Line: Storage;
type Timelines: Seq<Timeline<Self::Line>>;
type States: Seq<StateTimeline<Self::Line>>;
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Heap;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Fixed<const N: usize>;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FixedPlan<const TIMELINES: usize, const STATES: usize, const N: usize>;
impl sealed::Sealed for Heap {}
#[cfg(feature = "alloc")]
impl Storage for Heap {
type List<T: Slot> = alloc::vec::Vec<T>;
type DoubleList<T: Slot> = alloc::vec::Vec<T>;
}
#[cfg(feature = "alloc")]
impl PlanStorage for Heap {
type Line = Self;
type Timelines = alloc::vec::Vec<Timeline<Self>>;
type States = alloc::vec::Vec<StateTimeline<Self>>;
}
impl<const N: usize> sealed::Sealed for Fixed<N> {}
impl<const N: usize> Storage for Fixed<N> {
type List<T: Slot> = Inline<T, [T; N]>;
type DoubleList<T: Slot> = Inline<T, [[T; 2]; N]>;
}
impl<const T: usize, const S: usize, const N: usize> sealed::Sealed for FixedPlan<T, S, N> {}
impl<const T: usize, const S: usize, const N: usize> PlanStorage for FixedPlan<T, S, N> {
type Line = Fixed<N>;
type Timelines = Inline<Timeline<Fixed<N>>, [Timeline<Fixed<N>>; T]>;
type States = Inline<StateTimeline<Fixed<N>>, [StateTimeline<Fixed<N>>; S]>;
}
#[cfg(feature = "alloc")]
impl<T> sealed::Sealed for alloc::vec::Vec<T> {}
#[cfg(feature = "alloc")]
impl<T: Slot> Seq<T> for alloc::vec::Vec<T> {
const EMPTY: Self = Self::new();
fn capacity(&self) -> Option<usize> {
None
}
fn try_reserve(&mut self, extra: usize, _container: Container) -> Result<(), Error> {
Self::try_reserve(self, extra).map_err(|_| Error::OutOfMemory)
}
fn try_push(&mut self, item: T, container: Container) -> Result<(), Error> {
Seq::try_reserve(self, 1, container)?;
self.push(item);
Ok(())
}
fn try_insert(&mut self, index: usize, item: T, container: Container) -> Result<(), Error> {
Seq::try_reserve(self, 1, container)?;
self.insert(index, item);
Ok(())
}
fn remove(&mut self, index: usize) -> T {
Self::remove(self, index)
}
fn pop(&mut self) -> Option<T> {
Self::pop(self)
}
fn retain(&mut self, keep: impl FnMut(&T) -> bool) {
Self::retain(self, keep);
}
fn truncate(&mut self, len: usize) {
Self::truncate(self, len);
}
}
macro_rules! slots {
($($ty:ty => $empty:expr),* $(,)?) => {
$(
impl sealed::Sealed for $ty {}
impl Slot for $ty {
const EMPTY: Self = $empty;
}
)*
};
}
slots! {
crate::ActivityId => crate::ActivityId(0),
i64 => 0,
(crate::ActivityId, crate::Impact) => (crate::ActivityId(0), crate::Impact::Step { at: 0, delta: 0 }),
(crate::ActivityId, crate::StateImpact) => (crate::ActivityId(0), crate::StateImpact::Set { at: 0, value: 0 }),
(crate::ActivityId, i128) => (crate::ActivityId(0), 0),
}
pub(crate) use sealed::Sealed;