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
use crate::{FmtArg, PanicFmt, PanicVal, StdWrapper};

use core::ops::{Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive};

macro_rules! impl_range_panicfmt_one {
    (
        fn(&$self:ident: $ty:ty, $f:ident) -> $pv_count:literal {
            $($content:tt)*
        }
    ) => {
        impl PanicFmt for $ty {
            type This = Self;
            type Kind = crate::fmt::IsStdType;
            const PV_COUNT: usize = $pv_count;
        }

        impl crate::StdWrapper<&$ty> {
            #[doc = concat!(
                "Converts this `", stringify!($ty), "` to a single-element `PanicVal` array."
            )]
            pub const fn to_panicvals($self, $f: FmtArg) -> [PanicVal<'static>; $pv_count] {
                $($content)*
            }
        }
    }
}

macro_rules! impl_range_panicfmt {
    ($elem_ty:ty) => {
        impl_range_panicfmt_one! {
            fn(&self: Range<$elem_ty>, f) -> 3 {
                [
                    StdWrapper(&self.0.start).to_panicval(f),
                    PanicVal::write_str(".."),
                    StdWrapper(&self.0.end).to_panicval(f),
                ]
            }
        }

        impl_range_panicfmt_one! {
            fn(&self: RangeFrom<$elem_ty>, f) -> 2 {
                [
                    StdWrapper(&self.0.start).to_panicval(f),
                    PanicVal::write_str(".."),
                ]
            }
        }

        impl_range_panicfmt_one! {
            fn(&self: RangeTo<$elem_ty>, f) -> 2 {
                [
                    PanicVal::write_str(".."),
                    StdWrapper(&self.0.end).to_panicval(f),
                ]
            }
        }

        impl_range_panicfmt_one! {
            fn(&self: RangeToInclusive<$elem_ty>, f) -> 2 {
                [
                    PanicVal::write_str("..="),
                    StdWrapper(&self.0.end).to_panicval(f),
                ]
            }
        }

        impl_range_panicfmt_one! {
            fn(&self: RangeInclusive<$elem_ty>, f) -> 3 {
                [
                    StdWrapper(self.0.start()).to_panicval(f),
                    PanicVal::write_str("..="),
                    StdWrapper(self.0.end()).to_panicval(f),
                ]
            }
        }
    };
}

impl_range_panicfmt! {usize}

////////////////////////////////////////////////////////////////////////////////

impl_range_panicfmt_one! {
    fn(&self: RangeFull, _f) -> 1 {
        [PanicVal::write_str("..")]
    }
}