devela/data/list/array/
ext.rs
1use crate::{Debug, Display, FmtResult, Formatter};
7
8#[repr(transparent)]
12pub struct ArrayFmt<'a, T: ExtArray>(&'a T);
13
14trait ArrayDisplay: ExtArray {
16 fn fmt_display(&self, f: &mut Formatter) -> FmtResult<()>;
17}
18
19trait ArrayDebug: ExtArray {
24 fn fmt_debug(&self, f: &mut Formatter) -> FmtResult<()>;
25}
26
27impl<T: ArrayDisplay> Display for ArrayFmt<'_, T> {
28 fn fmt(&self, f: &mut Formatter) -> FmtResult<()> {
29 self.0.fmt_display(f)
30 }
31}
32impl<T: ArrayDebug> Debug for ArrayFmt<'_, T> {
33 fn fmt(&self, f: &mut Formatter) -> FmtResult<()> {
34 self.0.fmt_debug(f)
35 }
36}
37
38trait Sealed {}
40impl<T, const LEN: usize> Sealed for [T; LEN] {}
41
42#[expect(private_bounds, reason = "Sealed")]
46pub trait ExtArray: Sealed {
47 const LEN: usize;
49
50 #[rustfmt::skip]
52 fn fmt(&self) -> ArrayFmt<Self> where Self: Sized { ArrayFmt(self) }
53}
54
55impl<T, const LEN: usize> ExtArray for [T; LEN] {
56 const LEN: usize = LEN;
57}
58
59impl<T: Display, const LEN: usize> ArrayDisplay for [T; LEN] {
60 fn fmt_display(&self, f: &mut Formatter) -> FmtResult<()> {
61 write!(f, "[")?;
62 for (index, element) in self.iter().enumerate() {
63 if index > 0 {
64 write!(f, ", ")?;
65 }
66 Display::fmt(element, f)?;
67 }
68 write!(f, "]")
69 }
70}
71impl<T: Debug, const LEN: usize> ArrayDebug for [T; LEN] {
72 fn fmt_debug(&self, f: &mut Formatter) -> FmtResult<()> {
73 f.debug_list().entries(self.iter()).finish()
74 }
75}