1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use crate::{
    fmt::{FmtArg, PackedFmtArg, PanicFmt},
    panic_val::{PanicVal, PanicVariant},
    utils::Packed,
    StdWrapper,
};

macro_rules! impl_panicfmt_array {
    ($(($variant:ident, $panicval_ctor:ident, $ty:ty)),* $(,)*) => {

        #[derive(Copy, Clone)]
        #[repr(packed)]
        pub(crate) struct Slice<'s> {
            pub(crate) fmtarg: PackedFmtArg,
            pub(crate) vari: SliceV<'s>,
        }

        #[repr(u8)]
        #[derive(Copy, Clone)]
        pub(crate) enum SliceV<'s> {
            $(
                $variant(Packed<&'s [$ty]>),
            )*
        }


        impl<'s> Slice<'s> {
            // length in elements
            pub(crate) const fn arr_len(self) -> usize {
                match self.vari {
                    $(
                        SliceV::$variant(Packed(arr)) => arr.len(),
                    )*
                }
            }
        }

        impl<'s> SliceV<'s> {
            const fn get(self, index: usize, fmtarg: FmtArg) -> PanicVal<'s> {
                match self {
                    $(
                        SliceV::$variant(Packed(arr)) => {
                            let elem: &'s <$ty as PanicFmt>::This = &arr[index];
                            StdWrapper(elem).to_panicval(fmtarg)
                        },
                    )*
                }
            }
        }

        #[cfg_attr(feature = "docsrs", doc(cfg(feature = "non_basic")))]
        impl<'s> PanicVal<'s> {
            $(
                /// Constructs a `PanicVal` from a slice.
                pub const fn $panicval_ctor(this: &'s [$ty], mut fmtarg: FmtArg) -> PanicVal<'s> {
                    fmtarg = fmtarg.indent();
                    if this.is_empty() {
                        fmtarg = fmtarg.set_alternate(false);
                    }
                    PanicVal::__new(
                        PanicVariant::Slice(Slice{
                            fmtarg: fmtarg.pack(),
                            vari: SliceV::$variant(Packed(this)),
                        })
                    )
                }
            )*
        }

        $(
            impl<'s> PanicFmt for [$ty] {
                type This = Self;
                type Kind = crate::fmt::IsStdType;
                const PV_COUNT: usize = 1;
            }
            impl<'s, const LEN: usize> PanicFmt for [$ty; LEN] {
                type This = Self;
                type Kind = crate::fmt::IsStdType;
                const PV_COUNT: usize = 1;
            }

            #[cfg_attr(feature = "docsrs", doc(cfg(feature = "non_basic")))]
            impl<'s> StdWrapper<&'s [$ty]> {
                /// Converts the slice to a single-element `PanicVal` array.
                pub const fn to_panicvals(self: Self, f:FmtArg) -> [PanicVal<'s>;1] {
                    [PanicVal::$panicval_ctor(self.0, f)]
                }
                /// Converts the slice to a `PanicVal`.
                pub const fn to_panicval(self: Self, f:FmtArg) -> PanicVal<'s> {
                    PanicVal::$panicval_ctor(self.0, f)
                }
            }

            #[cfg_attr(feature = "docsrs", doc(cfg(feature = "non_basic")))]
            impl<'s, const LEN: usize> StdWrapper<&'s [$ty; LEN]> {
                /// Converts the array to a single-element `PanicVal` array.
                pub const fn to_panicvals(self: Self, f:FmtArg) -> [PanicVal<'s>;1] {
                    [PanicVal::$panicval_ctor(self.0, f)]
                }
                /// Converts the array to a `PanicVal`.
                pub const fn to_panicval(self: Self, f:FmtArg) -> PanicVal<'s> {
                    PanicVal::$panicval_ctor(self.0, f)
                }
            }
        )*

    };
}

impl_panicfmt_array! {
    (U8, from_slice_u8, u8),
    (U16, from_slice_u16, u16),
    (U32, from_slice_u32, u32),
    (U64, from_slice_u64, u64),
    (U128, from_slice_u128, u128),
    (Usize, from_slice_usize, usize),
    (I8, from_slice_i8, i8),
    (I16, from_slice_i16, i16),
    (I32, from_slice_i32, i32),
    (I64, from_slice_i64, i64),
    (I128, from_slice_i128, i128),
    (Isize, from_slice_isize, isize),
    (Bool, from_slice_bool, bool),
    (Str, from_slice_str, &'s str),
}

#[derive(Copy, Clone)]
pub(crate) struct SliceIter<'s> {
    slice: SliceV<'s>,
    fmtarg: FmtArg,
    state: IterState,
    arr_len: u32,
}

#[derive(Copy, Clone, PartialEq, Eq)]
struct IterState(u32);

#[allow(non_upper_case_globals)]
impl IterState {
    const Start: Self = Self(u32::MAX - 1);
    const End: Self = Self(u32::MAX);
}

impl<'s> Slice<'s> {
    pub(crate) const fn iter<'b>(&'b self) -> SliceIter<'s> {
        SliceIter {
            slice: self.vari,
            fmtarg: self.fmtarg.unpack(),
            state: IterState::Start,
            arr_len: self.arr_len() as u32,
        }
    }
}

impl<'s> SliceIter<'s> {
    pub(crate) const fn next(mut self) -> ([PanicVal<'s>; 2], Option<Self>) {
        let fmtarg = self.fmtarg;

        let ret = match self.state {
            IterState::Start => {
                self.state = if self.arr_len == 0 {
                    IterState::End
                } else {
                    IterState(0)
                };

                [crate::fmt::OpenBracket.to_panicval(fmtarg), PanicVal::EMPTY]
            }
            IterState::End => {
                let close_brace = crate::fmt::CloseBracket.to_panicval(fmtarg.unindent());
                return ([close_brace, PanicVal::EMPTY], None);
            }
            IterState(x) => {
                let comma = if x + 1 == self.arr_len {
                    self.state = IterState::End;
                    crate::fmt::COMMA_TERM
                } else {
                    self.state = IterState(x + 1);
                    crate::fmt::COMMA_SEP
                }
                .to_panicval(fmtarg);

                [self.slice.get(x as usize, fmtarg), comma]
            }
        };

        (ret, Some(self))
    }
}