use core::fmt;
use core::hash::{Hash, Hasher};
use alloc::boxed::Box;
use crate::inner::RingPairInner;
use crate::ring_pair::RingPair;
#[derive(Clone, Default)]
pub struct BoxedRingPair<T> {
pub(crate) inner: RingPairInner<Box<[T; 2]>>,
}
impl<T: Clone> BoxedRingPair<T> {
pub fn new(initial: T) -> Self {
Self {
inner: RingPairInner {
buffer: Box::new([initial.clone(), initial]),
newest: false,
},
}
}
}
impl<T> BoxedRingPair<T> {
pub fn push(&mut self, value: T) {
self.inner.push(value);
}
pub fn push_with<F: FnOnce(&mut T)>(&mut self, f: F) {
self.inner.push_with(f);
}
#[expect(
clippy::must_use_candidate,
reason = "pure getter; ignoring the result is never a useful pattern"
)]
pub fn newer(&self) -> &T {
self.inner.newer()
}
#[expect(
clippy::must_use_candidate,
reason = "pure getter; ignoring the result is never a useful pattern"
)]
pub fn older(&self) -> &T {
self.inner.older()
}
#[expect(
clippy::must_use_candidate,
reason = "pure getter; ignoring the result is never a useful pattern"
)]
pub fn as_pair(&self) -> (&T, &T) {
(self.inner.older(), self.inner.newer())
}
#[must_use]
pub fn iter(&self) -> crate::Iter<'_, T> {
crate::Iter::new(self.inner.older(), self.inner.newer())
}
}
impl<T: fmt::Debug> fmt::Debug for BoxedRingPair<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("BoxedRingPair")
.field("older", self.inner.older())
.field("newer", self.inner.newer())
.finish()
}
}
impl<T: PartialEq> PartialEq for BoxedRingPair<T> {
fn eq(&self, other: &Self) -> bool {
self.inner == other.inner
}
}
impl<T: Eq> Eq for BoxedRingPair<T> {}
impl<T: Hash> Hash for BoxedRingPair<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.inner.hash(state);
}
}
impl<T> From<[T; 2]> for BoxedRingPair<T> {
fn from([older, newer]: [T; 2]) -> Self {
Self {
inner: RingPairInner {
buffer: Box::new([newer, older]),
newest: false,
},
}
}
}
impl<T> From<(T, T)> for BoxedRingPair<T> {
fn from((older, newer): (T, T)) -> Self {
Self {
inner: RingPairInner {
buffer: Box::new([newer, older]),
newest: false,
},
}
}
}
impl<T> From<RingPair<T>> for BoxedRingPair<T> {
fn from(pair: RingPair<T>) -> Self {
Self {
inner: RingPairInner {
buffer: Box::new(pair.inner.buffer),
newest: pair.inner.newest,
},
}
}
}