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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
use super::{from_uint_slice, from_uint_slice_mut, into_uint_slice, into_uint_slice_mut, UintCast};

/// Trait for casting a reference to a collection of colors into a reference to
/// a collection of unsigned integers without copying.
///
/// This trait is meant as a more convenient alternative to the free functions
/// in [`cast`][crate::cast], to allow method chaining among other things.
///
/// ## Examples
///
/// ```
/// use palette::{cast::AsUints, rgb::PackedArgb, Srgba};
///
/// let array: [PackedArgb; 2] = [
///     Srgba::new(0x17, 0xC6, 0x4C, 0xFF).into(),
///     Srgba::new(0x5D, 0x12, 0xD6, 0xFF).into()
/// ];
/// let slice: &[PackedArgb] = &[
///     Srgba::new(0x17, 0xC6, 0x4C, 0xFF).into(),
///     Srgba::new(0x5D, 0x12, 0xD6, 0xFF).into()
/// ];
/// let vec: Vec<PackedArgb> = vec![
///     Srgba::new(0x17, 0xC6, 0x4C, 0xFF).into(),
///     Srgba::new(0x5D, 0x12, 0xD6, 0xFF).into()
/// ];
///
/// assert_eq!(array.as_uints(), &[0xFF17C64C, 0xFF5D12D6]);
/// assert_eq!(slice.as_uints(), &[0xFF17C64C, 0xFF5D12D6]);
/// assert_eq!(vec.as_uints(), &[0xFF17C64C, 0xFF5D12D6]);
/// ```
pub trait AsUints<A: ?Sized> {
    /// Cast this collection of colors into a collection of unsigned integers.
    fn as_uints(&self) -> &A;
}

/// Trait for casting a mutable reference to a collection of colors into a
/// mutable reference to a collection of unsigned integers without copying.
///
/// This trait is meant as a more convenient alternative to the free functions
/// in [`cast`][crate::cast], to allow method chaining among other things.
///
/// ## Examples
///
/// ```
/// use palette::{cast::AsUintsMut, rgb::PackedArgb, Srgba};
///
/// let mut array: [PackedArgb; 2] = [
///     Srgba::new(0x17, 0xC6, 0x4C, 0xFF).into(),
///     Srgba::new(0x5D, 0x12, 0xD6, 0xFF).into()
/// ];
/// let slice_mut: &mut [PackedArgb] = &mut [
///     Srgba::new(0x17, 0xC6, 0x4C, 0xFF).into(),
///     Srgba::new(0x5D, 0x12, 0xD6, 0xFF).into()
/// ];
/// let mut vec: Vec<PackedArgb> = vec![
///     Srgba::new(0x17, 0xC6, 0x4C, 0xFF).into(),
///     Srgba::new(0x5D, 0x12, 0xD6, 0xFF).into()
/// ];
///
/// assert_eq!(array.as_uints_mut(), &mut [0xFF17C64C, 0xFF5D12D6]);
/// assert_eq!(slice_mut.as_uints_mut(), &mut [0xFF17C64C, 0xFF5D12D6]);
/// assert_eq!(vec.as_uints_mut(), &mut [0xFF17C64C, 0xFF5D12D6]);
/// ```
pub trait AsUintsMut<A: ?Sized> {
    /// Cast this collection of colors into a mutable collection of unsigned integers.
    fn as_uints_mut(&mut self) -> &mut A;
}

macro_rules! impl_as_uints {
    ($($owning:ty $(where ($($ty_input:tt)+))?),*) => {
        $(
            impl<'a, C $(, $($ty_input)+)?> AsUints<[C::Uint]> for $owning
            where
                C: UintCast,
            {
                #[inline]
                fn as_uints(&self) -> &[C::Uint] {
                    into_uint_slice(self.as_ref())
                }
            }

            impl<'a, C $(, $($ty_input)+)?> AsUintsMut<[C::Uint]> for $owning
            where
                C: UintCast,
            {
                #[inline]
                fn as_uints_mut(&mut self) -> &mut [C::Uint] {
                    into_uint_slice_mut(self.as_mut())
                }
            }
        )*
    };
}

impl_as_uints!([C], [C; N] where (const N: usize));

#[cfg(feature = "alloc")]
impl_as_uints!(alloc::boxed::Box<[C]>, alloc::vec::Vec<C>);

/// Trait for casting a reference to a collection of unsigned integers into a
/// reference to a collection of colors without copying.
///
/// This trait is meant as a more convenient alternative to the free functions
/// in [`cast`][crate::cast], to allow method chaining among other things.
///
/// ## Examples
///
/// ```
/// use palette::{cast::UintsAs, rgb::PackedArgb, Srgba};
///
/// let array: [_; 2] = [0xFF17C64C, 0xFF5D12D6];
/// let slice: &[_] = &[0xFF17C64C, 0xFF5D12D6];
/// let vec: Vec<_> = vec![0xFF17C64C, 0xFF5D12D6];
///
/// let colors: &[PackedArgb] = array.uints_as();
/// assert_eq!(
///     colors,
///     &[
///         Srgba::new(0x17, 0xC6, 0x4C, 0xFF).into(),
///         Srgba::new(0x5D, 0x12, 0xD6, 0xFF).into()
///     ]
/// );
///
/// let colors: &[PackedArgb] = slice.uints_as();
/// assert_eq!(
///     colors,
///     &[
///         Srgba::new(0x17, 0xC6, 0x4C, 0xFF).into(),
///         Srgba::new(0x5D, 0x12, 0xD6, 0xFF).into()
///     ]
/// );
///
/// let colors: &[PackedArgb] = vec.uints_as();
/// assert_eq!(
///     colors,
///     &[
///         Srgba::new(0x17, 0xC6, 0x4C, 0xFF).into(),
///         Srgba::new(0x5D, 0x12, 0xD6, 0xFF).into()
///     ]
/// );
/// ```
pub trait UintsAs<C: ?Sized> {
    /// Cast this collection of unsigned integers into a collection of colors.
    fn uints_as(&self) -> &C;
}

/// Trait for casting a mutable reference to a collection of unsigned integers
/// into a mutable reference to a collection of colors without copying.
///
/// This trait is meant as a more convenient alternative to the free functions
/// in [`cast`][crate::cast], to allow method chaining among other things.
///
/// ## Examples
///
/// ```
/// use palette::{cast::UintsAsMut, rgb::PackedArgb, Srgba};
///
/// let mut array: [_; 2] = [0xFF17C64C, 0xFF5D12D6];
/// let slice_mut: &mut [_] = &mut [0xFF17C64C, 0xFF5D12D6];
/// let mut vec: Vec<_> = vec![0xFF17C64C, 0xFF5D12D6];
///
/// let colors: &mut [PackedArgb] = array.uints_as_mut();
/// assert_eq!(
///     colors,
///     &mut [
///         Srgba::new(0x17, 0xC6, 0x4C, 0xFF).into(),
///         Srgba::new(0x5D, 0x12, 0xD6, 0xFF).into()
///     ]
/// );
///
/// let colors: &mut [PackedArgb] = slice_mut.uints_as_mut();
/// assert_eq!(
///     colors,
///     &mut [
///         Srgba::new(0x17, 0xC6, 0x4C, 0xFF).into(),
///         Srgba::new(0x5D, 0x12, 0xD6, 0xFF).into()
///     ]
/// );
///
/// let colors: &mut [PackedArgb] = vec.uints_as_mut();
/// assert_eq!(
///     colors,
///     &mut [
///         Srgba::new(0x17, 0xC6, 0x4C, 0xFF).into(),
///         Srgba::new(0x5D, 0x12, 0xD6, 0xFF).into()
///     ]
/// );
/// ```
pub trait UintsAsMut<C: ?Sized> {
    /// Cast this collection of unsigned integers into a mutable collection of colors.
    fn uints_as_mut(&mut self) -> &mut C;
}

macro_rules! impl_uints_as {
    ($($owning:ty $(where ($($ty_input:tt)+))?),*) => {
        $(
            impl<'a, C $(, $($ty_input)+)?> UintsAs<[C]> for $owning
            where
                C: UintCast,
            {
                #[inline]
                fn uints_as(&self) -> &[C] {
                    from_uint_slice(self.as_ref())
                }
            }

            impl<'a, C $(, $($ty_input)+)?> UintsAsMut<[C]> for $owning
            where
                C: UintCast,
            {
                #[inline]
                fn uints_as_mut(&mut self) -> &mut [C] {
                    from_uint_slice_mut(self.as_mut())
                }
            }
        )*
    };
}

impl_uints_as!([C::Uint], [C::Uint; N] where (const N: usize));

#[cfg(feature = "alloc")]
impl_uints_as!(alloc::boxed::Box<[C::Uint]>, alloc::vec::Vec<C::Uint>);

#[cfg(test)]
mod test {
    use crate::{rgb::PackedRgba, Srgba};

    use super::{AsUints, AsUintsMut, UintsAs, UintsAsMut};

    #[test]
    fn as_uints() {
        let slice: &[PackedRgba] = &[Srgba::new(1, 2, 3, 4).into(), Srgba::new(5, 6, 7, 8).into()];
        let slice_mut: &mut [PackedRgba] =
            &mut [Srgba::new(1, 2, 3, 4).into(), Srgba::new(5, 6, 7, 8).into()];
        let mut slice_box: Box<[PackedRgba]> =
            vec![Srgba::new(1, 2, 3, 4).into(), Srgba::new(5, 6, 7, 8).into()].into_boxed_slice();
        let mut vec: Vec<PackedRgba> =
            vec![Srgba::new(1, 2, 3, 4).into(), Srgba::new(5, 6, 7, 8).into()];
        let mut array: [PackedRgba; 2] =
            [Srgba::new(1, 2, 3, 4).into(), Srgba::new(5, 6, 7, 8).into()];

        let _: &[u32] = slice.as_uints();
        let _: &[u32] = slice_box.as_uints();
        let _: &[u32] = vec.as_uints();
        let _: &[u32] = array.as_uints();

        let _: &mut [u32] = slice_mut.as_uints_mut();
        let _: &mut [u32] = slice_box.as_uints_mut();
        let _: &mut [u32] = vec.as_uints_mut();
        let _: &mut [u32] = array.as_uints_mut();
    }

    #[test]
    fn uints_as() {
        let slice: &[u32] = &[0x01020304, 0x05060708];
        let slice_mut: &mut [u32] = &mut [0x01020304, 0x05060708];
        let mut slice_box: Box<[u32]> = vec![0x01020304, 0x05060708].into_boxed_slice();
        let mut vec: Vec<u32> = vec![0x01020304, 0x05060708];
        let mut array: [u32; 2] = [0x01020304, 0x05060708];

        let _: &[PackedRgba] = slice.uints_as();
        let _: &[PackedRgba] = slice_box.uints_as();
        let _: &[PackedRgba] = vec.uints_as();
        let _: &[PackedRgba] = array.uints_as();

        let _: &mut [PackedRgba] = slice_mut.uints_as_mut();
        let _: &mut [PackedRgba] = slice_box.uints_as_mut();
        let _: &mut [PackedRgba] = vec.uints_as_mut();
        let _: &mut [PackedRgba] = array.uints_as_mut();
    }
}