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
use crate::pointer::Size;

mod sealed {
    pub trait Sealed<U: ?Sized> {}
}

/// Trait to coerce slice metadata, which defines its length.
///
/// Coercing from one kind of slice to another means that the length must be
/// adjusted. We can only perform upwards adjustments such as `[u32]` to `[u16]`
/// since independent of the length of the slice we know that it defines a
/// region of memory which is appropriately sized.
pub trait CoerceSlice<U: ?Sized>: self::sealed::Sealed<U> {
    /// Resize with the given `factor`.
    #[doc(hidden)]
    fn resize<O: Size>(factor: O) -> O;

    /// Try to resize with the given `factor`.
    #[doc(hidden)]
    fn try_resize<O: Size>(factor: O) -> Option<O>;
}

macro_rules! self_impl_inner {
    ($from:ty, {$($to:ty),*}) => {
        $(
            impl self::sealed::Sealed<[$to]> for [$from] {}

            #[doc = concat!("Defines the coercion from `[", stringify!($from) ,"]` to `[", stringify!($to), "]`.")]
            ///
            /// # Examples
            ///
            /// ```
            /// use musli_zerocopy::Ref;
            ///
            #[doc = concat!("let reference: Ref<", stringify!($from), "> = Ref::zero();")]
            #[doc = concat!("let reference2 = reference.coerce::<[", stringify!($from), "]>();")]
            /// assert_eq!(reference2.len(), 1);
            ///
            #[doc = concat!("let reference3 = reference.coerce::<", stringify!($to), ">();")]
            #[doc = concat!("let reference4 = reference2.coerce::<[", stringify!($to), "]>();")]
            /// assert_eq!(reference4.len(), 1);
            /// ```
            impl CoerceSlice<[$to]> for [$from] {
                #[inline]
                fn resize<O: Size>(len: O) -> O {
                    len
                }

                #[inline]
                fn try_resize<O: Size>(len: O) -> Option<O> {
                    Some(len)
                }
            }
        )*
    }
}

macro_rules! self_impl {
    ([$({$($from:ty),*}),*], [$($to:tt),*]) => {
        $(
            $(
                self_impl_inner!($from, $to);
            )*
        )*
    };
}

macro_rules! coerce_slice_inner {
    ($factor:ident, $value:literal, $from:ty, {$($to:ty),*}) => {
        $(
            impl self::sealed::Sealed<[$to]> for [$from] {}

            #[doc = concat!("Defines the coercion from `[", stringify!($from) ,"]` to `[", stringify!($to), "]`.")]
            ///
            /// # Examples
            ///
            /// ```
            /// use musli_zerocopy::Ref;
            ///
            #[doc = concat!("let reference: Ref<", stringify!($from), "> = Ref::zero();")]
            #[doc = concat!("let reference2 = reference.coerce::<[", stringify!($to), "]>();")]
            #[doc = concat!("assert_eq!(reference2.len(), ", stringify!($value), ");")]
            ///
            #[doc = concat!("let reference: Ref<[", stringify!($from), "]> = Ref::with_metadata(0, 5);")]
            #[doc = concat!("let reference2 = reference.coerce::<[", stringify!($to), "]>();")]
            #[doc = concat!("assert_eq!(reference2.len(), 5 * ", stringify!($value), ");")]
            /// ```
            impl CoerceSlice<[$to]> for [$from] {
                #[inline]
                fn resize<O: Size>(len: O) -> O {
                    len.wrapping_mul(O::$factor)
                }

                #[inline]
                fn try_resize<O: Size>(len: O) -> Option<O> {
                    len.checked_mul(O::$factor)
                }
            }
        )*
    }
}

macro_rules! coerce_slice {
    ($factor:ident, $value:literal, [$({$($from:ty),*}),*], [$($to:tt),*]) => {
        $(
            $(
                coerce_slice_inner!($factor, $value, $from, $to);
            )*
        )*
    }
}

self_impl! {
    [
        {u8, i8},
        {u16, i16},
        {u32, i32},
        {u64, i64},
        {u128, i128}
    ],
    [
        {u8, i8},
        {u16, i16, [u8; 2], [i8; 2]},
        {u32, i32, [u16; 2], [i16; 2], [u8; 4], [i8; 4]},
        {u64, i64, [u32; 2], [i32; 2], [u16; 4], [i16; 4], [u8; 8], [i8; 8]},
        {u128, i128, [u64; 2], [i64; 2], [u32; 4], [i32; 4], [u16; 8], [i16; 8], [u8; 16], [i8; 16]}
    ]
}

coerce_slice! {
    N2, 2,
    [
        {u16, i16},
        {u32, i32},
        {u64, i64},
        {u128, i128}
    ],
    [
        {u8, i8},
        {u16, i16, [u8; 2], [i8; 2]},
        {u32, i32, [u16; 2], [i16; 2], [u8; 4], [i8; 4]},
        {u64, i64, [u32; 2], [i32; 2], [u16; 4], [i16; 4], [u8; 8], [i8; 8]}
    ]
}

coerce_slice! {
    N4, 4,
    [
        {u32, i32},
        {u64, i64},
        {u128, i128}
    ],
    [
        {u8, i8},
        {u16, i16, [u8; 2], [i8; 2]},
        {u32, i32, [u16; 2], [i16; 2], [u8; 4], [i8; 4]}
    ]
}

coerce_slice! {
    N8, 8,
    [
        {u64, i64},
        {u128, i128}
    ],
    [
        {u8, i8},
        {u16, i16, [u8; 2], [i8; 2]}
    ]
}

coerce_slice! {
    N16, 16,
    [
        {u128, i128}
    ],
    [
        {u8, i8}
    ]
}