devela/data/collections/array/
ext.rsuse crate::{Debug, Display, FmtResult, Formatter};
#[repr(transparent)]
pub struct ArrayFmt<'a, T: ExtArray>(&'a T);
trait ArrayDisplay: ExtArray {
fn fmt_display(&self, f: &mut Formatter) -> FmtResult<()>;
}
trait ArrayDebug: ExtArray {
fn fmt_debug(&self, f: &mut Formatter) -> FmtResult<()>;
}
impl<T: ArrayDisplay> Display for ArrayFmt<'_, T> {
fn fmt(&self, f: &mut Formatter) -> FmtResult<()> {
self.0.fmt_display(f)
}
}
impl<T: ArrayDebug> Debug for ArrayFmt<'_, T> {
fn fmt(&self, f: &mut Formatter) -> FmtResult<()> {
self.0.fmt_debug(f)
}
}
trait Sealed {}
impl<T, const LEN: usize> Sealed for [T; LEN] {}
#[expect(private_bounds, reason = "Sealed")]
pub trait ExtArray: Sealed {
const LEN: usize;
#[rustfmt::skip]
fn fmt(&self) -> ArrayFmt<Self> where Self: Sized { ArrayFmt(self) }
}
impl<T, const LEN: usize> ExtArray for [T; LEN] {
const LEN: usize = LEN;
}
impl<T: Display, const LEN: usize> ArrayDisplay for [T; LEN] {
fn fmt_display(&self, f: &mut Formatter) -> FmtResult<()> {
write!(f, "[")?;
for (index, element) in self.iter().enumerate() {
if index > 0 {
write!(f, ", ")?;
}
Display::fmt(element, f)?;
}
write!(f, "]")
}
}
impl<T: Debug, const LEN: usize> ArrayDebug for [T; LEN] {
fn fmt_debug(&self, f: &mut Formatter) -> FmtResult<()> {
f.debug_list().entries(self.iter()).finish()
}
}