use crate::cmd::Cmd;
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
#[inline]
pub fn changed<T: PartialEq>(prev: &T, next: T) -> Option<T> {
if *prev == next {
None
} else {
Some(next)
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Tape<C> {
#[cfg(feature = "alloc")]
items: Vec<C>,
#[cfg(not(feature = "alloc"))]
last: Option<C>,
}
impl<C> Tape<C> {
pub const fn new() -> Self {
Self {
#[cfg(feature = "alloc")]
items: Vec::new(),
#[cfg(not(feature = "alloc"))]
last: None,
}
}
pub fn record(&mut self, cmd: Cmd<C>) {
#[cfg(feature = "alloc")]
{
self.items.extend(cmd);
}
#[cfg(not(feature = "alloc"))]
{
for c in cmd {
self.last = Some(c);
}
}
}
#[cfg(feature = "alloc")]
pub fn as_slice(&self) -> &[C] {
&self.items
}
pub fn last(&self) -> Option<&C> {
#[cfg(feature = "alloc")]
{
self.items.last()
}
#[cfg(not(feature = "alloc"))]
{
self.last.as_ref()
}
}
}
#[cfg(feature = "alloc")]
impl<C> Tape<C> {
pub fn into_vec(self) -> Vec<C> {
self.items
}
}
#[cfg(test)]
mod tests {
use super::Tape;
use crate::cmd::Cmd;
#[test]
fn changed_is_none_when_equal() {
assert_eq!(super::changed(&3u8, 3), None);
assert_eq!(super::changed(&3u8, 4), Some(4));
}
#[test]
fn last_tracks_record() {
let mut tape = Tape::new();
assert!(tape.last().is_none());
tape.record(Cmd::single(1u8));
assert_eq!(tape.last(), Some(&1));
tape.record(Cmd::single(2));
assert_eq!(tape.last(), Some(&2));
}
}