Debian Codegraph @

src/cast/array.rs

raw symbols SCIP
1 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::{transmute_copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/transmute_copy()., ManuallyDroprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/ManuallyDrop#};
2
3 #[cfg(all(feature = "alloc", not(feature = "std")))]
4 use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::{boxedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/::Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/::Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#};
5
6 pub use palette_deriverust-analyzer cargo palette_derive 0.7.6 crate/::ArrayCastrust-analyzer cargo palette_derive 0.7.6 ArrayCast:;
7
8 use craterust-analyzer cargo palette 0.7.6 crate/::ArrayExtrust-analyzer cargo palette 0.7.6 ArrayExt#;
9
10 /// Marker trait for types that can be represented as a fixed size array.
11 ///
12 /// A type that implements this trait is assumed to have the exact same memory
13 /// layout and representation as a fixed size array. This implies a couple of
14 /// useful properties:
15 ///
16 /// * Casting between `T` and `T::Array` is free and will (or should) be
17 /// optimized away.
18 /// * `[T]` can be cast to and from `[T::Array]`, which can be cast to and from
19 /// `[U]` where `T::Array = [U; N]` and the length is a multiple of `N`.
20 ///
21 /// This allows a number of common and useful optimizations, including casting
22 /// buffers and reusing memory. It does however come with some strict
23 /// requirements and the recommendation is to use `#[derive(ArrayCast)]` which
24 /// checks for them automatically.
25 ///
26 /// # Deriving
27 ///
28 /// `ArrayCast` can be automatically derived. The only requirements are that the
29 /// type is a `struct`, that it has a `#[repr(C)]` or `#[repr(transparent)]`
30 /// attribute, and that all of its fields have the same types. It stays on the
31 /// conservative side and will show an error if any of those requirements are
32 /// not fulfilled. If some fields have different types, but the same memory
33 /// layout, or are zero-sized, they can be marked with attributes to show that
34 /// their types are safe to use.
35 ///
36 /// ## Field Attributes
37 ///
38 /// * `#[palette_unsafe_same_layout_as = "SomeType"]`: Mark the field as having
39 /// the same memory layout as `SomeType`.
40 ///
41 /// **Safety:** corrupt data and undefined behavior may occur if this is not
42 /// true!
43 ///
44 /// * `#[palette_unsafe_zero_sized]`: Mark the field as being zero-sized, and
45 /// thus not taking up any memory space. This means that it can be ignored.
46 ///
47 /// **Safety:** corrupt data and undefined behavior may occur if this is not
48 /// true!
49 ///
50 /// ## Examples
51 ///
52 /// Basic use:
53 ///
54 /// ```rust
55 /// use palette::cast::{self, ArrayCast};
56 ///
57 /// #[derive(PartialEq, Debug, ArrayCast)]
58 /// #[repr(C)]
59 /// struct MyCmyk {
60 /// cyan: f32,
61 /// magenta: f32,
62 /// yellow: f32,
63 /// key: f32,
64 /// }
65 ///
66 /// let buffer = [0.1, 0.2, 0.3, 0.4];
67 /// let color: MyCmyk = cast::from_array(buffer);
68 ///
69 /// assert_eq!(
70 /// color,
71 /// MyCmyk {
72 /// cyan: 0.1,
73 /// magenta: 0.2,
74 /// yellow: 0.3,
75 /// key: 0.4,
76 /// }
77 /// );
78 /// ```
79 ///
80 /// Heterogenous field types:
81 ///
82 /// ```rust
83 /// use std::marker::PhantomData;
84 ///
85 /// use palette::{cast::{self, ArrayCast}, encoding::Srgb, RgbHue};
86 ///
87 /// #[derive(PartialEq, Debug, ArrayCast)]
88 /// #[repr(C)]
89 /// struct MyCoolColor<S> {
90 /// #[palette(unsafe_zero_sized)]
91 /// standard: PhantomData<S>,
92 /// // RgbHue is a wrapper with `#[repr(C)]`, so it can safely
93 /// // be converted straight from `f32`.
94 /// #[palette(unsafe_same_layout_as = "f32")]
95 /// hue: RgbHue<f32>,
96 /// lumen: f32,
97 /// chroma: f32,
98 /// }
99 ///
100 /// let buffer = [172.0, 100.0, 0.3];
101 /// let color: MyCoolColor<Srgb> = cast::from_array(buffer);
102 ///
103 /// assert_eq!(
104 /// color,
105 /// MyCoolColor {
106 /// hue: 172.0.into(),
107 /// lumen: 100.0,
108 /// chroma: 0.3,
109 /// standard: PhantomData,
110 /// }
111 /// );
112 /// ```
113 ///
114 /// ## Safety
115 ///
116 /// * The type must be inhabited (eg: no
117 /// [Infallible](std::convert::Infallible)).
118 /// * The type must allow any values in the array items (eg: either no
119 /// requirements or some ability to recover from invalid values).
120 /// * The type must be homogeneous (eg: all fields have the same type, or are
121 /// wrappers that implement `ArrayCast` with the same field type, or are zero
122 /// sized).
123 /// * The length of `Array` must be the sum of the number of color component
124 /// fields in the type and in any possible compound fields.
125 /// * The type must be `repr(C)` or `repr(transparent)`.
126 /// * The type must have the same size and alignment as `Self::Array`.
127 ///
128 /// Note also that the type is assumed to not implement `Drop`. This will
129 /// rarely, if ever, be an issue. The requirements above ensures that the
130 /// underlying field types stay the same and will be dropped.
131 ///
132 /// For example:
133 ///
134 /// * `Srgb<T>` can be cast to `[T; 3]` because it has three non-zero sized
135 /// fields of type `T`.
136 /// * `Alpha<Srgb<T>, T>` can be cast to `[T; 4]`, that is `3 + 1` items,
137 /// because it's the sum of the three items from `Srgb` and the one extra
138 /// `alpha` field.
139 /// * `Alpha<Srgb<T>, U>` is not allowed because `T` and `U` are different
140 /// types.
141 pub unsafe trait ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# {
142 /// The output type of a cast to an array.
143 type Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#: ArrayExtrust-analyzer cargo palette 0.7.6 ArrayExt#;
144 }
145
146 /// Cast from a color type to an array.
147 ///
148 /// ```
149 /// use palette::{cast, Srgb};
150 ///
151 /// let color = Srgb::new(23u8, 198, 76);
152 /// assert_eq!(cast::into_array(color), [23, 198, 76]);
153 /// ```
154 ///
155 /// It's also possible to use `From` and `Into` when casting built-in types:
156 ///
157 /// ```
158 /// use palette::Srgb;
159 ///
160 /// let color = Srgb::new(23u8, 198, 76);
161 ///
162 /// // Colors implement `Into`:
163 /// let array1: [_; 3] = color.into();
164 ///
165 /// // Arrays implement `From`:
166 /// let array2 = <[_; 3]>::from(color);
167 /// ```
168 #[inline]
169 pub fn into_arrayrust-analyzer cargo palette 0.7.6 cast/array/into_array().<T>(color: T) -> T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#
170 where
171 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
172 {
173 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(), corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>());
174
175 // Safety: The requirements of implementing `ArrayCast`, as well as the size
176 // assert, ensures that transmuting `T` into `T::Array` is safe.
177 unsafe { transmute_copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/transmute_copy().(&ManuallyDroprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/ManuallyDrop#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/impl#[`ManuallyDrop<T>`]new().(color)) }
178 }
179
180 /// Cast from an array to a color type.
181 ///
182 /// ```
183 /// use palette::{cast, Srgb};
184 ///
185 /// let array = [23, 198, 76];
186 /// assert_eq!(cast::from_array::<Srgb<u8>>(array), Srgb::new(23, 198, 76));
187 /// ```
188 ///
189 /// It's also possible to use `From` and `Into` when casting built-in types:
190 ///
191 /// ```
192 /// use palette::Srgb;
193 ///
194 /// let array = [23, 198, 76];
195 ///
196 /// // Arrays implement `Into`:
197 /// let color1: Srgb<u8> = array.into();
198 ///
199 /// // Colors implement `From`:
200 /// let color2 = Srgb::from(array);
201 /// ```
202 #[inline]
203 pub fn from_arrayrust-analyzer cargo palette 0.7.6 cast/array/from_array().<T>(array: T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#) -> T
204 where
205 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
206 {
207 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(), corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>());
208
209 // Safety: The requirements of implementing `ArrayCast`, as well as the size
210 // assert, ensures that transmuting `T::Array` into `T` is safe.
211 unsafe { transmute_copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/transmute_copy().(&ManuallyDroprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/ManuallyDrop#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/impl#[`ManuallyDrop<T>`]new().(array)) }
212 }
213
214 /// Cast from a color type reference to an array reference.
215 ///
216 /// ```
217 /// use palette::{cast, Srgb};
218 ///
219 /// let color = Srgb::new(23u8, 198, 76);
220 /// assert_eq!(cast::into_array_ref(&color), &[23, 198, 76]);
221 /// ```
222 ///
223 /// It's also possible to use `From`, `Into` and `AsRef` when casting built-in
224 /// types:
225 ///
226 /// ```
227 /// use palette::Srgb;
228 ///
229 /// let color = Srgb::new(23u8, 198, 76);
230 ///
231 /// // Colors implement `AsRef`:
232 /// let array1: &[_; 3] = color.as_ref();
233 ///
234 /// // Color references implement `Into`:
235 /// let array2: &[_; 3] = (&color).into();
236 //
237 /// // Array references implement `From`:
238 /// let array3 = <&[_; 3]>::from(&color);
239 /// ```
240 #[inline]
241 pub fn into_array_refrust-analyzer cargo palette 0.7.6 cast/array/into_array_ref().<T>(value: &T) -> &T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#
242 where
243 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
244 {
245 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(), corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>());
246 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
247 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(),
248 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T>()
249 );
250
251 let value: *const T = value;
252
253 // Safety: The requirements of implementing `ArrayCast`, as well as the size
254 // and alignment asserts, ensures that reading `T` as `T::Array` is safe.
255 unsafe { &*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().value.castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/const_ptr/impl#[`*const T`]cast().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>() }
256 }
257
258 /// Cast from an array reference to a color type reference.
259 ///
260 /// ```
261 /// use palette::{cast, Srgb};
262 ///
263 /// let array = [23, 198, 76];
264 /// assert_eq!(cast::from_array_ref::<Srgb<u8>>(&array), &Srgb::new(23, 198, 76));
265 /// ```
266 ///
267 /// It's also possible to use `From`, `Into` and `AsRef` when casting built-in
268 /// types:
269 ///
270 /// ```
271 /// use palette::Srgb;
272 ///
273 /// let array = [23, 198, 76];
274 ///
275 /// // Arrays implement `AsRef`:
276 /// let color1: &Srgb<u8> = array.as_ref();
277 ///
278 /// // Array references implement `Into`:
279 /// let color2: &Srgb<u8> = (&array).into();
280 ///
281 /// // Color references implement `From`:
282 /// let color3 = <&Srgb<u8>>::from(&array);
283 /// ```
284 #[inline]
285 pub fn from_array_refrust-analyzer cargo palette 0.7.6 cast/array/from_array_ref().<T>(value: &T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#) -> &T
286 where
287 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
288 {
289 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(), corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>());
290 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
291 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(),
292 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T>()
293 );
294
295 let value: *const T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array# = value;
296
297 // Safety: The requirements of implementing `ArrayCast`, as well as the size
298 // and alignment asserts, ensures that reading `T::Array` as `T` is safe.
299 unsafe { &*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().value.castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/const_ptr/impl#[`*const T`]cast().::<T>() }
300 }
301
302 /// Cast from a mutable color type reference to a mutable array reference.
303 ///
304 /// ```
305 /// use palette::{cast, Srgb};
306 ///
307 /// let mut color = Srgb::new(23u8, 198, 76);
308 /// assert_eq!(cast::into_array_mut(&mut color), &mut [23, 198, 76]);
309 /// ```
310 ///
311 /// It's also possible to use `From`, `Into` and `AsMut` when casting built-in
312 /// types:
313 ///
314 /// ```
315 /// use palette::Srgb;
316 ///
317 /// let mut color = Srgb::new(23u8, 198, 76);
318 ///
319 /// // Colors implement `AsMut`:
320 /// let array1: &mut [_; 3] = color.as_mut();
321 ///
322 /// // Color references implement `Into`:
323 /// let array2: &mut [_; 3] = (&mut color).into();
324 //
325 /// // Array references implement `From`:
326 /// let array3 = <&mut [_; 3]>::from(&mut color);
327 /// ```
328 #[inline]
329 pub fn into_array_mutrust-analyzer cargo palette 0.7.6 cast/array/into_array_mut().<T>(value: &mut T) -> &mut T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#
330 where
331 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
332 {
333 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(), corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>());
334 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
335 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(),
336 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T>()
337 );
338
339 let value: *mut T = value;
340
341 // Safety: The requirements of implementing `ArrayCast`, as well as the size
342 // and alignment asserts, ensures that reading `T` as `T::Array` is safe.
343 unsafe { &mut *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().value.castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]cast().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>() }
344 }
345
346 /// Cast from a mutable array reference to a mutable color type reference.
347 ///
348 /// ```
349 /// use palette::{cast, Srgb};
350 ///
351 /// let mut array = [23, 198, 76];
352 /// assert_eq!(cast::from_array_mut::<Srgb<u8>>(&mut array), &mut Srgb::new(23, 198, 76));
353 /// ```
354 ///
355 /// It's also possible to use `From`, `Into` and `AsMut` when casting built-in
356 /// types:
357 ///
358 /// ```
359 /// use palette::Srgb;
360 ///
361 /// let mut array = [23, 198, 76];
362 ///
363 /// // Arrays implement `AsMut`:
364 /// let color1: &mut Srgb<u8> = array.as_mut();
365 ///
366 /// // Array references implement `Into`:
367 /// let color2: &mut Srgb<u8> = (&mut array).into();
368 ///
369 /// // Color references implement `From`:
370 /// let color3 = <&mut Srgb<u8>>::from(&mut array);
371 /// ```
372 #[inline]
373 pub fn from_array_mutrust-analyzer cargo palette 0.7.6 cast/array/from_array_mut().<T>(value: &mut T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#) -> &mut T
374 where
375 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
376 {
377 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(), corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>());
378 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
379 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(),
380 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T>()
381 );
382
383 let value: *mut T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array# = value;
384
385 // Safety: The requirements of implementing `ArrayCast`, as well as the size
386 // and alignment asserts, ensures that reading `T::Array` as `T` is safe.
387 unsafe { &mut *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().value.castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]cast().::<T>() }
388 }
389
390 /// Cast from an array of colors to an array of arrays.
391 ///
392 /// ```
393 /// use palette::{cast, Srgb};
394 ///
395 /// let colors = [Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)];
396 /// assert_eq!(cast::into_array_array(colors), [[64, 139, 10], [93, 18, 214]])
397 /// ```
398 #[inline]
399 pub fn into_array_arrayrust-analyzer cargo palette 0.7.6 cast/array/into_array_array().<T, const N: usize>(values: [T; N]) -> [T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#; N]
400 where
401 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
402 {
403 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(), corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>());
404 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
405 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(),
406 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T>()
407 );
408
409 // Safety: The requirements of implementing `ArrayCast`, as well as the size
410 // and alignment asserts, ensures that reading `T` as `T::Array` is safe.
411 // The length and memory layout of the array is the same because the size is
412 // the same.
413 unsafe { transmute_copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/transmute_copy().(&ManuallyDroprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/ManuallyDrop#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/impl#[`ManuallyDrop<T>`]new().(values)) }
414 }
415
416 /// Cast from an array of colors to an array of color components.
417 ///
418 /// ## Panics
419 ///
420 /// It's unfortunately not able to infer the length of the component array,
421 /// until generic const expressions are stabilized. In the meantime, it's going
422 /// to panic if `M` isn't `N * T::Array::LENGTH`. A future version will remove
423 /// the `M` parameter and make the mismatch a compiler error.
424 ///
425 /// No `try_*` alternative is provided, since the array size can't be changed
426 /// during runtime.
427 ///
428 /// ## Examples
429 ///
430 /// ```
431 /// use palette::{cast, Srgb};
432 ///
433 /// let colors = [Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)];
434 /// assert_eq!(cast::into_component_array(colors), [64, 139, 10, 93, 18, 214])
435 /// ```
436 ///
437 /// It panics when the array lengths don't match up:
438 ///
439 /// ```should_panic
440 /// use palette::{cast, Srgb};
441 ///
442 /// let colors = [Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)];
443 /// assert_eq!(cast::into_component_array(colors), [64, 139, 10]) // Too short
444 /// ```
445 #[inline]
446 pub fn into_component_arrayrust-analyzer cargo palette 0.7.6 cast/array/into_component_array().<T, const N: usize, const M: usize>(
447 values: [T; N],
448 ) -> [<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array# as ArrayExtrust-analyzer cargo palette 0.7.6 ArrayExt#>::Itemrust-analyzer cargo palette 0.7.6 ArrayExt#Item#; M]
449 where
450 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
451 {
452 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(), corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>());
453 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
454 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(),
455 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T>()
456 );
457
458 // This check can be replaced with `[<T::Array as ArrayExt>::Item; N *
459 // T::Array::LENGTH]` when generic const expressions are stabilized.
460 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
461 N *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#::LENGTHrust-analyzer cargo palette 0.7.6 ArrayExt#LENGTH.,
462 M,
463 "expected the output array to have length {}, but its length is {}",
464 N *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#::LENGTHrust-analyzer cargo palette 0.7.6 ArrayExt#LENGTH.,
465 M
466 );
467 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
468 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<[T; N]>(),
469 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<[<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array# as ArrayExtrust-analyzer cargo palette 0.7.6 ArrayExt#>::Itemrust-analyzer cargo palette 0.7.6 ArrayExt#Item#; M]>()
470 );
471 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
472 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<[T; N]>(),
473 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<[<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array# as ArrayExtrust-analyzer cargo palette 0.7.6 ArrayExt#>::Itemrust-analyzer cargo palette 0.7.6 ArrayExt#Item#; M]>()
474 );
475
476 // Safety: The requirements of implementing `ArrayCast`, as well as the size
477 // and alignment asserts, ensures that reading `T` as `T::Array` is safe.
478 // The sizes and memory layout of the arrays are also asserted to be the
479 // same.
480 unsafe { transmute_copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/transmute_copy().(&ManuallyDroprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/ManuallyDrop#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/impl#[`ManuallyDrop<T>`]new().(values)) }
481 }
482
483 /// Cast from an array of arrays to an array of colors.
484 ///
485 /// ```
486 /// use palette::{cast, Srgb};
487 ///
488 /// let arrays = [[64, 139, 10], [93, 18, 214]];
489 /// assert_eq!(
490 /// cast::from_array_array::<Srgb<u8>, 2>(arrays),
491 /// [Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)]
492 /// )
493 /// ```
494 #[inline]
495 pub fn from_array_arrayrust-analyzer cargo palette 0.7.6 cast/array/from_array_array().<T, const N: usize>(values: [T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#; N]) -> [T; N]
496 where
497 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
498 {
499 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(), corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>());
500 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
501 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(),
502 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T>()
503 );
504
505 // Safety: The requirements of implementing `ArrayCast`, as well as the size
506 // and alignment asserts, ensures that reading `T::Array` as `T` is safe.
507 // The length and memory layout of the array is the same because the size is
508 // the same.
509 unsafe { transmute_copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/transmute_copy().(&ManuallyDroprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/ManuallyDrop#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/impl#[`ManuallyDrop<T>`]new().(values)) }
510 }
511
512 /// Cast from an array of color components to an array of colors.
513 ///
514 /// ## Panics
515 ///
516 /// The cast will panic if the length of the input array is not a multiple of
517 /// the color's array length. This is unfortunately unavoidable until generic
518 /// const expressions are stabilized.
519 ///
520 /// No `try_*` alternative is provided, since the array size can't be changed
521 /// during runtime.
522 ///
523 /// ## Examples
524 ///
525 /// ```
526 /// use palette::{cast, Srgb};
527 ///
528 /// let components = [64, 139, 10, 93, 18, 214];
529 /// assert_eq!(
530 /// cast::from_component_array::<Srgb<u8>, 6, 2>(components),
531 /// [Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)]
532 /// );
533 /// ```
534 ///
535 /// This panics:
536 ///
537 /// ```should_panic
538 /// use palette::{cast, Srgb};
539 ///
540 /// let components = [64, 139, 10, 93, 18]; // Not a multiple of 3
541 /// assert_eq!(
542 /// cast::from_component_array::<Srgb<u8>, 5, 2>(components),
543 /// [Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)]
544 /// );
545 /// ```
546 #[inline]
547 pub fn from_component_arrayrust-analyzer cargo palette 0.7.6 cast/array/from_component_array().<T, const N: usize, const M: usize>(
548 values: [<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array# as ArrayExtrust-analyzer cargo palette 0.7.6 ArrayExt#>::Itemrust-analyzer cargo palette 0.7.6 ArrayExt#Item#; N],
549 ) -> [T; M]
550 where
551 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
552 {
553 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(), corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>());
554 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
555 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(),
556 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T>()
557 );
558
559 // These checks can be replaced with `[<T::Array as ArrayExt>::Item; N /
560 // T::Array::LENGTH]` and a compile time check for `values.len() %
561 // T::Array::LENGTH == 0` when generic const expressions are stabilized.
562 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
563 N %rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem(). T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#::LENGTHrust-analyzer cargo palette 0.7.6 ArrayExt#LENGTH.,
564 0,
565 "expected the array length ({}) to be divisible by {}",
566 N,
567 T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#::LENGTHrust-analyzer cargo palette 0.7.6 ArrayExt#LENGTH.
568 );
569 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
570 N /rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#::LENGTHrust-analyzer cargo palette 0.7.6 ArrayExt#LENGTH.,
571 M,
572 "expected the output array to have length {}, but its length is {}",
573 N /rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#::LENGTHrust-analyzer cargo palette 0.7.6 ArrayExt#LENGTH.,
574 M
575 );
576 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
577 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<[<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array# as ArrayExtrust-analyzer cargo palette 0.7.6 ArrayExt#>::Itemrust-analyzer cargo palette 0.7.6 ArrayExt#Item#; N]>(),
578 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<[T; M]>()
579 );
580 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
581 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<[<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array# as ArrayExtrust-analyzer cargo palette 0.7.6 ArrayExt#>::Itemrust-analyzer cargo palette 0.7.6 ArrayExt#Item#; N]>(),
582 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<[T; M]>()
583 );
584
585 // Safety: The requirements of implementing `ArrayCast`, as well as the size
586 // and alignment asserts, ensures that reading `T` as `T::Array` is safe.
587 // The sizes and memory layout of the arrays are also asserted to be the
588 // same.
589 unsafe { transmute_copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/transmute_copy().(&ManuallyDroprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/ManuallyDrop#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/impl#[`ManuallyDrop<T>`]new().(values)) }
590 }
591
592 /// Cast from a slice of colors to a slice of arrays.
593 ///
594 /// ```
595 /// use palette::{cast, Srgb};
596 ///
597 /// let colors = &[Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)];
598 /// assert_eq!(cast::into_array_slice(colors), &[[64, 139, 10], [93, 18, 214]])
599 /// ```
600 #[inline]
601 pub fn into_array_slicerust-analyzer cargo palette 0.7.6 cast/array/into_array_slice().<T>(values: &[T]) -> &[T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#]
602 where
603 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
604 {
605 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(), corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>());
606 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
607 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(),
608 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T>()
609 );
610
611 // Safety: The requirements of implementing `ArrayCast`, as well as the size
612 // and alignment asserts, ensures that reading `T` as `T::Array` is safe.
613 // The length is the same because the size is the same.
614 unsafe { corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/::from_raw_partsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/raw/from_raw_parts().(values.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_ptr().().castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/const_ptr/impl#[`*const T`]cast().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(), values.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()) }
615 }
616
617 /// Cast from a slice of colors to a slice of color components.
618 ///
619 /// ```
620 /// use palette::{cast, Srgb};
621 ///
622 /// let colors = &[Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)];
623 /// assert_eq!(cast::into_component_slice(colors), &[64, 139, 10, 93, 18, 214])
624 /// ```
625 #[inline]
626 pub fn into_component_slicerust-analyzer cargo palette 0.7.6 cast/array/into_component_slice().<T>(values: &[T]) -> &[<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array# as ArrayExtrust-analyzer cargo palette 0.7.6 ArrayExt#>::Itemrust-analyzer cargo palette 0.7.6 ArrayExt#Item#]
627 where
628 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
629 {
630 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(), corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>());
631 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
632 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(),
633 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T>()
634 );
635
636 let length = values.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#::LENGTHrust-analyzer cargo palette 0.7.6 ArrayExt#LENGTH.;
637
638 // Safety: The requirements of implementing `ArrayCast`, as well as the size
639 // and alignment asserts, ensures that reading `[T]` as `[T::Array::Item]`
640 // is safe. The length is multiplied by the array length.
641 unsafe {
642 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/::from_raw_partsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/raw/from_raw_parts().(
643 values.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_ptr().().castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/const_ptr/impl#[`*const T`]cast().::<<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array# as ArrayExtrust-analyzer cargo palette 0.7.6 ArrayExt#>::Itemrust-analyzer cargo palette 0.7.6 ArrayExt#Item#>(),
644 length,
645 )
646 }
647 }
648
649 /// Cast from a slice of arrays to a slice of colors.
650 ///
651 /// ```
652 /// use palette::{cast, Srgb};
653 ///
654 /// let arrays = &[[64, 139, 10], [93, 18, 214]];
655 /// assert_eq!(
656 /// cast::from_array_slice::<Srgb<u8>>(arrays),
657 /// &[Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)]
658 /// )
659 /// ```
660 #[inline]
661 pub fn from_array_slicerust-analyzer cargo palette 0.7.6 cast/array/from_array_slice().<T>(values: &[T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#]) -> &[T]
662 where
663 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
664 {
665 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(), corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>());
666 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
667 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(),
668 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T>()
669 );
670
671 // Safety: The requirements of implementing `ArrayCast`, as well as the size
672 // and alignment asserts, ensures that reading `T::Array` as `T` is safe.
673 // The length is the same because the size is the same.
674 unsafe { corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/::from_raw_partsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/raw/from_raw_parts().(values.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_ptr().().castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/const_ptr/impl#[`*const T`]cast().::<T>(), values.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()) }
675 }
676
677 /// The same as [`try_from_component_slice`] but panics on error.
678 ///
679 /// ## Panics
680 ///
681 /// The cast will panic if the length of the input slice is not a multiple of
682 /// the color's array length.
683 ///
684 /// ## Examples
685 ///
686 /// ```
687 /// use palette::{cast, Srgb};
688 ///
689 /// let components = &[64, 139, 10, 93, 18, 214];
690 /// assert_eq!(
691 /// cast::from_component_slice::<Srgb<u8>>(components),
692 /// &[Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)]
693 /// )
694 /// ```
695 ///
696 /// This panics:
697 ///
698 /// ```should_panic
699 /// use palette::{cast, Srgb};
700 ///
701 /// let components = &[64, 139, 10, 93, 18, 214, 0, 123]; // Not a multiple of 3
702 /// cast::from_component_slice::<Srgb<u8>>(components);
703 /// ```
704 #[inline]
705 pub fn from_component_slicerust-analyzer cargo palette 0.7.6 cast/array/from_component_slice().<T>(values: &[<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array# as ArrayExtrust-analyzer cargo palette 0.7.6 ArrayExt#>::Itemrust-analyzer cargo palette 0.7.6 ArrayExt#Item#]) -> &[T]
706 where
707 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
708 {
709 try_from_component_slicerust-analyzer cargo palette 0.7.6 cast/array/try_from_component_slice().(values).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()
710 }
711
712 /// Cast from a slice of color components to a slice of colors.
713 ///
714 /// ## Errors
715 ///
716 /// The cast will return an error if the length of the input slice is not a
717 /// multiple of the color's array length.
718 ///
719 /// ## Examples
720 ///
721 /// ```
722 /// use palette::{cast, Srgb};
723 ///
724 /// let components = &[64, 139, 10, 93, 18, 214];
725 /// assert_eq!(
726 /// cast::try_from_component_slice::<Srgb<u8>>(components),
727 /// Ok([Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)].as_ref())
728 /// )
729 /// ```
730 ///
731 /// This produces an error:
732 ///
733 /// ```
734 /// use palette::{cast, Srgb};
735 ///
736 /// let components = &[64, 139, 10, 93, 18]; // Not a multiple of 3
737 /// assert!(cast::try_from_component_slice::<Srgb<u8>>(components).is_err());
738 /// ```
739 #[inline]
740 pub fn try_from_component_slicerust-analyzer cargo palette 0.7.6 cast/array/try_from_component_slice().<T>(
741 values: &[<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array# as ArrayExtrust-analyzer cargo palette 0.7.6 ArrayExt#>::Itemrust-analyzer cargo palette 0.7.6 ArrayExt#Item#],
742 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&[T], SliceCastErrorrust-analyzer cargo palette 0.7.6 cast/array/SliceCastError#>
743 where
744 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
745 {
746 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(), corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>());
747 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
748 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(),
749 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T>()
750 );
751
752 if values.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().%rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#::LENGTHrust-analyzer cargo palette 0.7.6 ArrayExt#LENGTH. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().0 {
753 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(SliceCastErrorrust-analyzer cargo palette 0.7.6 cast/array/SliceCastError#);
754 }
755
756 let length = values.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div().T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#::LENGTHrust-analyzer cargo palette 0.7.6 ArrayExt#LENGTH.;
757 let raw = values.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_ptr().().castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/const_ptr/impl#[`*const T`]cast().::<T>();
758
759 // Safety: The requirements of implementing `ArrayCast`, as well as the size
760 // and alignment asserts, ensures that reading `T` as `T::Array` is safe.
761 unsafe { Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/::from_raw_partsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/raw/from_raw_parts().(raw, length)) }
762 }
763
764 /// Cast from a mutable slice of colors to a mutable slice of arrays.
765 ///
766 /// ```
767 /// use palette::{cast, Srgb};
768 ///
769 /// let colors = &mut [Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)];
770 /// assert_eq!(
771 /// cast::into_array_slice_mut(colors),
772 /// &mut [[64, 139, 10], [93, 18, 214]]
773 /// )
774 /// ```
775 #[inline]
776 pub fn into_array_slice_mutrust-analyzer cargo palette 0.7.6 cast/array/into_array_slice_mut().<T>(values: &mut [T]) -> &mut [T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#]
777 where
778 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
779 {
780 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(), corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>());
781 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
782 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(),
783 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T>()
784 );
785
786 // Safety: The requirements of implementing `ArrayCast`, as well as the size
787 // and alignment asserts, ensures that reading `T` as `T::Array` is safe.
788 // The length is the same because the size is the same.
789 unsafe { corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/::from_raw_parts_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/raw/from_raw_parts_mut().(values.as_mut_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_mut_ptr().().castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]cast().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(), values.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()) }
790 }
791
792 /// Cast from a slice of colors to a slice of color components.
793 ///
794 /// ```
795 /// use palette::{cast, Srgb};
796 ///
797 /// let colors = &mut [Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)];
798 /// assert_eq!(
799 /// cast::into_component_slice_mut(colors),
800 /// &mut [64, 139, 10, 93, 18, 214]
801 /// )
802 /// ```
803 #[inline]
804 pub fn into_component_slice_mutrust-analyzer cargo palette 0.7.6 cast/array/into_component_slice_mut().<T>(values: &mut [T]) -> &mut [<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array# as ArrayExtrust-analyzer cargo palette 0.7.6 ArrayExt#>::Itemrust-analyzer cargo palette 0.7.6 ArrayExt#Item#]
805 where
806 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
807 {
808 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(), corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>());
809 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
810 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(),
811 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T>()
812 );
813
814 let length = values.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#::LENGTHrust-analyzer cargo palette 0.7.6 ArrayExt#LENGTH.;
815
816 // Safety: The requirements of implementing `ArrayCast`, as well as the size
817 // and alignment asserts, ensures that reading `[T]` as `[T::Array::Item]`
818 // is safe. The length is multiplied by the array length.
819 unsafe {
820 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/::from_raw_parts_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/raw/from_raw_parts_mut().(
821 values.as_mut_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_mut_ptr().().castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]cast().::<<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array# as ArrayExtrust-analyzer cargo palette 0.7.6 ArrayExt#>::Itemrust-analyzer cargo palette 0.7.6 ArrayExt#Item#>(),
822 length,
823 )
824 }
825 }
826
827 /// Cast from a mutable slice of arrays to a mutable slice of colors.
828 ///
829 /// ```
830 /// use palette::{cast, Srgb};
831 ///
832 /// let arrays = &mut [[64, 139, 10], [93, 18, 214]];
833 /// assert_eq!(
834 /// cast::from_array_slice_mut::<Srgb<u8>>(arrays),
835 /// &mut [Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)]
836 /// )
837 /// ```
838 #[inline]
839 pub fn from_array_slice_mutrust-analyzer cargo palette 0.7.6 cast/array/from_array_slice_mut().<T>(values: &mut [T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#]) -> &mut [T]
840 where
841 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
842 {
843 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(), corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>());
844 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
845 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(),
846 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T>()
847 );
848
849 // Safety: The requirements of implementing `ArrayCast`, as well as the size
850 // and alignment asserts, ensures that reading `T::Array` as `T` is safe.
851 // The length is the same because the size is the same.
852 unsafe { corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/::from_raw_parts_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/raw/from_raw_parts_mut().(values.as_mut_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_mut_ptr().().castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]cast().::<T>(), values.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()) }
853 }
854
855 /// The same as [`try_from_component_slice_mut`] but panics on error.
856 ///
857 /// ## Panics
858 ///
859 /// The cast will panic if the length of the input slice is not a multiple of
860 /// the color's array length.
861 ///
862 /// ## Examples
863 ///
864 /// ```
865 /// use palette::{cast, Srgb};
866 ///
867 /// let components = &mut [64, 139, 10, 93, 18, 214];
868 /// assert_eq!(
869 /// cast::from_component_slice_mut::<Srgb<u8>>(components),
870 /// &mut [Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)]
871 /// )
872 /// ```
873 ///
874 /// This panics:
875 ///
876 /// ```should_panic
877 /// use palette::{cast, Srgb};
878 ///
879 /// let components = &mut [64, 139, 10, 93, 18, 214, 0, 123]; // Not a multiple of 3
880 /// cast::from_component_slice_mut::<Srgb<u8>>(components);
881 /// ```
882 #[inline]
883 pub fn from_component_slice_mutrust-analyzer cargo palette 0.7.6 cast/array/from_component_slice_mut().<T>(values: &mut [<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array# as ArrayExtrust-analyzer cargo palette 0.7.6 ArrayExt#>::Itemrust-analyzer cargo palette 0.7.6 ArrayExt#Item#]) -> &mut [T]
884 where
885 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
886 {
887 try_from_component_slice_mutrust-analyzer cargo palette 0.7.6 cast/array/try_from_component_slice_mut().(values).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()
888 }
889
890 /// Cast from a mutable slice of color components to a slice of colors.
891 ///
892 /// ## Errors
893 ///
894 /// The cast will return an error if the length of the input slice is not a
895 /// multiple of the color's array length.
896 ///
897 /// ## Examples
898 ///
899 /// ```
900 /// use palette::{cast, Srgb};
901 ///
902 /// let components = &mut [64, 139, 10, 93, 18, 214];
903 /// assert_eq!(
904 /// cast::try_from_component_slice_mut::<Srgb<u8>>(components),
905 /// Ok([Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)].as_mut())
906 /// )
907 /// ```
908 ///
909 /// This produces an error:
910 ///
911 /// ```
912 /// use palette::{cast, Srgb};
913 ///
914 /// let components = &mut [64, 139, 10, 93, 18]; // Not a multiple of 3
915 /// assert!(cast::try_from_component_slice_mut::<Srgb<u8>>(components).is_err());
916 /// ```
917 #[inline]
918 pub fn try_from_component_slice_mutrust-analyzer cargo palette 0.7.6 cast/array/try_from_component_slice_mut().<T>(
919 values: &mut [<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array# as ArrayExtrust-analyzer cargo palette 0.7.6 ArrayExt#>::Itemrust-analyzer cargo palette 0.7.6 ArrayExt#Item#],
920 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&mut [T], SliceCastErrorrust-analyzer cargo palette 0.7.6 cast/array/SliceCastError#>
921 where
922 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
923 {
924 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(), corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>());
925 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
926 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(),
927 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T>()
928 );
929
930 if values.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().%rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#::LENGTHrust-analyzer cargo palette 0.7.6 ArrayExt#LENGTH. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().0 {
931 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(SliceCastErrorrust-analyzer cargo palette 0.7.6 cast/array/SliceCastError#);
932 }
933
934 let length = values.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div().T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#::LENGTHrust-analyzer cargo palette 0.7.6 ArrayExt#LENGTH.;
935 let raw = values.as_mut_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_mut_ptr().().castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]cast().::<T>();
936
937 // Safety: The requirements of implementing `ArrayCast`, as well as the size
938 // and alignment asserts, ensures that reading `T` as `T::Array` is safe.
939 unsafe { Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/::from_raw_parts_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/raw/from_raw_parts_mut().(raw, length)) }
940 }
941
942 /// Cast from a boxed color type to a boxed array.
943 ///
944 /// ```
945 /// use palette::{cast, Srgb};
946 ///
947 /// let color = Box::new(Srgb::new(23u8, 198, 76));
948 /// assert_eq!(cast::into_array_box(color), Box::new([23, 198, 76]));
949 /// ```
950 ///
951 /// It's also possible to use `From` and `Into` when casting built-in types:
952 ///
953 /// ```
954 /// use palette::Srgb;
955 ///
956 /// // Boxed colors implement `Into`:
957 /// let color1 = Box::new(Srgb::new(23u8, 198, 76));
958 /// let array1: Box<[_; 3]> = color1.into();
959 ///
960 /// // Boxed arrays implement `From`:
961 /// let color2 = Box::new(Srgb::new(23u8, 198, 76));
962 /// let array2 = <Box<[_; 3]>>::from(color2);
963 /// ```
964 #[cfg(feature = "alloc")]
965 #[inline]
966 pub fn into_array_boxrust-analyzer cargo palette 0.7.6 cast/array/into_array_box().<T>(value: Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<T>) -> Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>
967 where
968 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
969 {
970 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(), corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>());
971 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
972 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(),
973 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T>()
974 );
975
976 let raw = Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::into_rawrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]into_raw().(value);
977
978 // Safety: The requirements of implementing `ArrayCast`, as well as the size
979 // and alignment asserts, ensures that reading `T` as `T::Array` is safe.
980 unsafe { Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::from_rawrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]from_raw().(raw.castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]cast().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>()) }
981 }
982
983 /// Cast from a boxed array to a boxed color type.
984 ///
985 /// ```
986 /// use palette::{cast, Srgb};
987 ///
988 /// let array = Box::new([23, 198, 76]);
989 /// assert_eq!(cast::from_array_box::<Srgb<u8>>(array), Box::new(Srgb::new(23, 198, 76)));
990 /// ```
991 ///
992 /// It's also possible to use `From` and `Into` when casting built-in types:
993 ///
994 /// ```
995 /// use palette::Srgb;
996 ///
997 ///
998 /// // Boxed arrays implement `Into`:
999 /// let array1 = Box::new([23, 198, 76]);
1000 /// let color1: Box<Srgb<u8>> = array1.into();
1001 ///
1002 /// // Boxed colors implement `From`:
1003 /// let array2 = Box::new([23, 198, 76]);
1004 /// let color2 = <Box<Srgb<u8>>>::from(array2);
1005 /// ```
1006 #[cfg(feature = "alloc")]
1007 #[inline]
1008 pub fn from_array_boxrust-analyzer cargo palette 0.7.6 cast/array/from_array_box().<T>(value: Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>) -> Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<T>
1009 where
1010 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
1011 {
1012 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(), corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>());
1013 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
1014 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(),
1015 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T>()
1016 );
1017
1018 let raw = Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::into_rawrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]into_raw().(value);
1019
1020 // Safety: The requirements of implementing `ArrayCast`, as well as the size
1021 // and alignment asserts, ensures that reading `T::Array` as `T` is safe.
1022 unsafe { Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::from_rawrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]from_raw().(raw.castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]cast().::<T>()) }
1023 }
1024
1025 /// Cast from a boxed slice of colors to a boxed slice of arrays.
1026 ///
1027 /// ```
1028 /// use palette::{cast, Srgb};
1029 ///
1030 /// let colors = vec![Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)].into_boxed_slice();
1031 /// assert_eq!(
1032 /// cast::into_array_slice_box(colors),
1033 /// vec![[64, 139, 10], [93, 18, 214]].into_boxed_slice()
1034 /// )
1035 /// ```
1036 #[cfg(feature = "alloc")]
1037 #[inline]
1038 pub fn into_array_slice_boxrust-analyzer cargo palette 0.7.6 cast/array/into_array_slice_box().<T>(values: Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<[T]>) -> Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<[T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#]>
1039 where
1040 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
1041 {
1042 let raw: *mut [T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#] = into_array_slice_mutrust-analyzer cargo palette 0.7.6 cast/array/into_array_slice_mut().(Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::leakrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T, A>`]leak().(values));
1043
1044 // Safety: We know the pointer comes from a `Box` and thus has the correct lifetime.
1045 unsafe { Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::from_rawrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]from_raw().(raw) }
1046 }
1047
1048 /// Cast from a boxed slice of colors to a boxed slice of color components.
1049 ///
1050 /// ```
1051 /// use palette::{cast, Srgb};
1052 ///
1053 /// let colors = vec![Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)].into_boxed_slice();
1054 /// assert_eq!(
1055 /// cast::into_component_slice_box(colors),
1056 /// vec![64, 139, 10, 93, 18, 214].into_boxed_slice()
1057 /// )
1058 /// ```
1059 #[cfg(feature = "alloc")]
1060 #[inline]
1061 pub fn into_component_slice_boxrust-analyzer cargo palette 0.7.6 cast/array/into_component_slice_box().<T>(values: Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<[T]>) -> Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<[<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array# as ArrayExtrust-analyzer cargo palette 0.7.6 ArrayExt#>::Itemrust-analyzer cargo palette 0.7.6 ArrayExt#Item#]>
1062 where
1063 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
1064 {
1065 let raw: *mut [<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array# as ArrayExtrust-analyzer cargo palette 0.7.6 ArrayExt#>::Itemrust-analyzer cargo palette 0.7.6 ArrayExt#Item#] = into_component_slice_mutrust-analyzer cargo palette 0.7.6 cast/array/into_component_slice_mut().(Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::leakrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T, A>`]leak().(values));
1066
1067 // Safety: We know the pointer comes from a `Box` and thus has the correct lifetime.
1068 unsafe { Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::from_rawrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]from_raw().(raw) }
1069 }
1070
1071 /// Cast from a boxed slice of arrays to a boxed slice of colors.
1072 ///
1073 /// ```
1074 /// use palette::{cast, Srgb};
1075 ///
1076 /// let arrays = vec![[64, 139, 10], [93, 18, 214]].into_boxed_slice();
1077 /// assert_eq!(
1078 /// cast::from_array_slice_box::<Srgb<u8>>(arrays),
1079 /// vec![Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)].into_boxed_slice()
1080 /// )
1081 /// ```
1082 #[cfg(feature = "alloc")]
1083 #[inline]
1084 pub fn from_array_slice_boxrust-analyzer cargo palette 0.7.6 cast/array/from_array_slice_box().<T>(values: Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<[T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#]>) -> Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<[T]>
1085 where
1086 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
1087 {
1088 let raw: *mut [T] = from_array_slice_mutrust-analyzer cargo palette 0.7.6 cast/array/from_array_slice_mut().(Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::leakrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T, A>`]leak().(values));
1089
1090 // Safety: We know the pointer comes from a `Box` and thus has the correct lifetime.
1091 unsafe { Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::from_rawrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]from_raw().(raw) }
1092 }
1093
1094 /// The same as [`try_from_component_slice_box`] but panics on error.
1095 ///
1096 /// ## Panics
1097 ///
1098 /// The cast will panic if the length of the input slice is not a multiple of
1099 /// the color's array length.
1100 ///
1101 /// ## Examples
1102 ///
1103 /// ```
1104 /// use palette::{cast, Srgb};
1105 ///
1106 /// let components = vec![64, 139, 10, 93, 18, 214].into_boxed_slice();
1107 /// assert_eq!(
1108 /// cast::from_component_slice_box::<Srgb<u8>>(components),
1109 /// vec![Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)].into_boxed_slice()
1110 /// )
1111 /// ```
1112 ///
1113 /// This panics:
1114 ///
1115 /// ```should_panic
1116 /// use palette::{cast, Srgb};
1117 ///
1118 /// // Not a multiple of 3:
1119 /// let components = vec![64, 139, 10, 93, 18, 214, 0, 123].into_boxed_slice();
1120 /// cast::from_component_slice_box::<Srgb<u8>>(components);
1121 /// ```
1122 #[cfg(feature = "alloc")]
1123 #[inline]
1124 pub fn from_component_slice_boxrust-analyzer cargo palette 0.7.6 cast/array/from_component_slice_box().<T>(values: Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<[<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array# as ArrayExtrust-analyzer cargo palette 0.7.6 ArrayExt#>::Itemrust-analyzer cargo palette 0.7.6 ArrayExt#Item#]>) -> Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<[T]>
1125 where
1126 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
1127 {
1128 try_from_component_slice_boxrust-analyzer cargo palette 0.7.6 cast/array/try_from_component_slice_box().(values).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()
1129 }
1130
1131 /// Cast from a boxed slice of color components to a boxed slice of colors.
1132 ///
1133 /// ## Errors
1134 ///
1135 /// The cast will return an error if the length of the input slice is not a
1136 /// multiple of the color's array length.
1137 ///
1138 /// ## Examples
1139 ///
1140 /// ```
1141 /// use palette::{cast, Srgb};
1142 ///
1143 /// let components = vec![64, 139, 10, 93, 18, 214].into_boxed_slice();
1144 /// assert_eq!(
1145 /// cast::try_from_component_slice_box::<Srgb<u8>>(components),
1146 /// Ok(vec![Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)].into_boxed_slice())
1147 /// )
1148 /// ```
1149 ///
1150 /// This produces an error:
1151 ///
1152 /// ```
1153 /// use palette::{cast, Srgb};
1154 ///
1155 /// // Not a multiple of 3:
1156 /// let components = vec![64, 139, 10, 93, 18].into_boxed_slice();
1157 ///
1158 /// if let Err(error) = cast::try_from_component_slice_box::<Srgb<u8>>(components) {
1159 /// // We get the original values back on error:
1160 /// assert_eq!(
1161 /// error.values,
1162 /// vec![64, 139, 10, 93, 18].into_boxed_slice()
1163 /// );
1164 /// } else {
1165 /// unreachable!();
1166 /// }
1167 /// ```
1168 #[cfg(feature = "alloc")]
1169 #[inline]
1170 pub fn try_from_component_slice_boxrust-analyzer cargo palette 0.7.6 cast/array/try_from_component_slice_box().<T>(
1171 values: Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<[<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array# as ArrayExtrust-analyzer cargo palette 0.7.6 ArrayExt#>::Itemrust-analyzer cargo palette 0.7.6 ArrayExt#Item#]>,
1172 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<[T]>, BoxedSliceCastErrorrust-analyzer cargo palette 0.7.6 cast/array/BoxedSliceCastError#<<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array# as ArrayExtrust-analyzer cargo palette 0.7.6 ArrayExt#>::Itemrust-analyzer cargo palette 0.7.6 ArrayExt#Item#>>
1173 where
1174 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
1175 {
1176 if values.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().%rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#::LENGTHrust-analyzer cargo palette 0.7.6 ArrayExt#LENGTH. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().0 {
1177 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(BoxedSliceCastErrorrust-analyzer cargo palette 0.7.6 cast/array/BoxedSliceCastError# { valuesrust-analyzer cargo palette 0.7.6 cast/array/BoxedSliceCastError#values. });
1178 }
1179
1180 let raw: *mut [T] = from_component_slice_mutrust-analyzer cargo palette 0.7.6 cast/array/from_component_slice_mut().(Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::leakrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T, A>`]leak().(values));
1181
1182 // Safety: We know the pointer comes from a `Box` and thus has the correct lifetime.
1183 unsafe { Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::from_rawrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]from_raw().(raw)) }
1184 }
1185
1186 /// Cast from a `Vec` of colors to a `Vec` of arrays.
1187 ///
1188 /// ```
1189 /// use palette::{cast, Srgb};
1190 ///
1191 /// let colors = vec![Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)];
1192 /// assert_eq!(
1193 /// cast::into_array_vec(colors),
1194 /// vec![[64, 139, 10], [93, 18, 214]]
1195 /// )
1196 /// ```
1197 #[cfg(feature = "alloc")]
1198 #[inline]
1199 pub fn into_array_vecrust-analyzer cargo palette 0.7.6 cast/array/into_array_vec().<T>(values: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T>) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>
1200 where
1201 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
1202 {
1203 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(), corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>());
1204 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
1205 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(),
1206 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T>()
1207 );
1208 let mut values = ManuallyDroprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/ManuallyDrop#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/impl#[`ManuallyDrop<T>`]new().(values);
1209
1210 let raw = values.as_mut_ptrrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_mut_ptr().();
1211
1212 // Safety: The requirements of implementing `ArrayCast`, as well as the size
1213 // and alignment asserts, ensures that reading `T` as `T::Array` is safe.
1214 // Length and capacity are the same because the size is the same.
1215 unsafe { Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::from_raw_partsrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]from_raw_parts().(raw.castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]cast().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(), values.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(), values.capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]capacity().()) }
1216 }
1217
1218 /// Cast from a `Vec` of colors to a `Vec` of color components.
1219 ///
1220 /// ```
1221 /// use palette::{cast, Srgb};
1222 ///
1223 /// let colors = vec![Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)];
1224 /// assert_eq!(
1225 /// cast::into_component_vec(colors),
1226 /// vec![64, 139, 10, 93, 18, 214]
1227 /// )
1228 /// ```
1229 #[cfg(feature = "alloc")]
1230 #[inline]
1231 pub fn into_component_vecrust-analyzer cargo palette 0.7.6 cast/array/into_component_vec().<T>(values: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T>) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array# as ArrayExtrust-analyzer cargo palette 0.7.6 ArrayExt#>::Itemrust-analyzer cargo palette 0.7.6 ArrayExt#Item#>
1232 where
1233 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
1234 {
1235 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(), corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>());
1236 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
1237 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(),
1238 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T>()
1239 );
1240 let mut values = ManuallyDroprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/ManuallyDrop#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/impl#[`ManuallyDrop<T>`]new().(values);
1241
1242 let raw = values.as_mut_ptrrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_mut_ptr().();
1243 let length = values.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#::LENGTHrust-analyzer cargo palette 0.7.6 ArrayExt#LENGTH.;
1244 let capacity = values.capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]capacity().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#::LENGTHrust-analyzer cargo palette 0.7.6 ArrayExt#LENGTH.;
1245
1246 // Safety: The requirements of implementing `ArrayCast`, as well as the size
1247 // and alignment asserts, ensures that reading `T` as `T::Array` is safe.
1248 // The length and capacity are multiplied by the array length.
1249 unsafe { Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::from_raw_partsrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]from_raw_parts().(raw.castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]cast().::<<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array# as ArrayExtrust-analyzer cargo palette 0.7.6 ArrayExt#>::Itemrust-analyzer cargo palette 0.7.6 ArrayExt#Item#>(), length, capacity) }
1250 }
1251
1252 /// Cast from a `Vec` of arrays to a `Vec` of colors.
1253 ///
1254 /// ```
1255 /// use palette::{cast, Srgb};
1256 ///
1257 /// let arrays = vec![[64, 139, 10], [93, 18, 214]];
1258 /// assert_eq!(
1259 /// cast::from_array_vec::<Srgb<u8>>(arrays),
1260 /// vec![Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)]
1261 /// )
1262 /// ```
1263 #[cfg(feature = "alloc")]
1264 #[inline]
1265 pub fn from_array_vecrust-analyzer cargo palette 0.7.6 cast/array/from_array_vec().<T>(values: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T>
1266 where
1267 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
1268 {
1269 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(), corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>());
1270 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
1271 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(),
1272 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T>()
1273 );
1274 let mut values = ManuallyDroprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/ManuallyDrop#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/impl#[`ManuallyDrop<T>`]new().(values);
1275
1276 let raw = values.as_mut_ptrrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_mut_ptr().();
1277
1278 // Safety: The requirements of implementing `ArrayCast`, as well as the size
1279 // and alignment asserts, ensures that reading `T::Array` as `T` is safe.
1280 // Length and capacity are the same because the size is the same.
1281 unsafe { Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::from_raw_partsrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]from_raw_parts().(raw.castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]cast().::<T>(), values.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(), values.capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]capacity().()) }
1282 }
1283
1284 /// The same as [`try_from_component_vec`] but panics on error.
1285 ///
1286 /// ## Panics
1287 ///
1288 /// The cast will panic if the length or capacity of the input `Vec` is not a
1289 /// multiple of the color's array length.
1290 ///
1291 /// ## Examples
1292 ///
1293 /// ```
1294 /// use palette::{cast, Srgb};
1295 ///
1296 /// let components = vec![64, 139, 10, 93, 18, 214];
1297 /// assert_eq!(
1298 /// cast::from_component_vec::<Srgb<u8>>(components),
1299 /// vec![Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)]
1300 /// )
1301 /// ```
1302 ///
1303 /// This panics due to the incorrect length:
1304 ///
1305 /// ```should_panic
1306 /// use palette::{cast, Srgb};
1307 ///
1308 /// // Not a multiple of 3:
1309 /// let components = vec![64, 139, 10, 93, 18, 214, 0, 123];
1310 /// cast::from_component_vec::<Srgb<u8>>(components);
1311 /// ```
1312 ///
1313 /// This panics due to the incorrect capacity:
1314 ///
1315 /// ```should_panic
1316 /// use palette::{cast, Srgb};
1317 ///
1318 /// let mut components = vec![64, 139, 10, 93, 18, 214];
1319 /// components.reserve_exact(2); // Not a multiple of 3
1320 /// cast::from_component_vec::<Srgb<u8>>(components);
1321 /// ```
1322 #[cfg(feature = "alloc")]
1323 #[inline]
1324 pub fn from_component_vecrust-analyzer cargo palette 0.7.6 cast/array/from_component_vec().<T>(values: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array# as ArrayExtrust-analyzer cargo palette 0.7.6 ArrayExt#>::Itemrust-analyzer cargo palette 0.7.6 ArrayExt#Item#>) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T>
1325 where
1326 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
1327 {
1328 try_from_component_vecrust-analyzer cargo palette 0.7.6 cast/array/try_from_component_vec().(values).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()
1329 }
1330
1331 /// Cast from a `Vec` of color components to a `Vec` of colors.
1332 ///
1333 /// ## Errors
1334 ///
1335 /// The cast will return an error if the length or capacity of the input `Vec`
1336 /// is not a multiple of the color's array length.
1337 ///
1338 /// ## Examples
1339 ///
1340 /// ```
1341 /// use palette::{cast, Srgb};
1342 ///
1343 /// let components = vec![64, 139, 10, 93, 18, 214];
1344 /// assert_eq!(
1345 /// cast::try_from_component_vec::<Srgb<u8>>(components),
1346 /// Ok(vec![Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)])
1347 /// )
1348 /// ```
1349 ///
1350 /// This produces an error due to the incorrect length:
1351 ///
1352 /// ```
1353 /// use palette::{cast, Srgb};
1354 ///
1355 /// // Not a multiple of 3:
1356 /// let components = vec![64, 139, 10, 93, 18];
1357 ///
1358 /// if let Err(error) = cast::try_from_component_vec::<Srgb<u8>>(components) {
1359 /// // We get the original values back on error:
1360 /// assert_eq!(error.values, vec![64, 139, 10, 93, 18]);
1361 /// } else {
1362 /// unreachable!();
1363 /// }
1364 /// ```
1365 ///
1366 /// This produces an error due to the incorrect capacity:
1367 ///
1368 /// ```
1369 /// use palette::{cast, Srgb};
1370 ///
1371 /// let mut components = vec![64, 139, 10, 93, 18, 214];
1372 /// components.reserve_exact(2); // Not a multiple of 3
1373 ///
1374 /// if let Err(error) = cast::try_from_component_vec::<Srgb<u8>>(components) {
1375 /// // We get the original values back on error:
1376 /// assert_eq!(error.values, vec![64, 139, 10, 93, 18, 214]);
1377 /// assert_eq!(error.values.capacity(), 8);
1378 /// } else {
1379 /// unreachable!();
1380 /// }
1381 /// ```
1382 #[cfg(feature = "alloc")]
1383 #[inline]
1384 pub fn try_from_component_vecrust-analyzer cargo palette 0.7.6 cast/array/try_from_component_vec().<T>(
1385 values: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array# as ArrayExtrust-analyzer cargo palette 0.7.6 ArrayExt#>::Itemrust-analyzer cargo palette 0.7.6 ArrayExt#Item#>,
1386 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T>, VecCastErrorrust-analyzer cargo palette 0.7.6 cast/array/VecCastError#<<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array# as ArrayExtrust-analyzer cargo palette 0.7.6 ArrayExt#>::Itemrust-analyzer cargo palette 0.7.6 ArrayExt#Item#>>
1387 where
1388 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
1389 {
1390 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(), corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>());
1391 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
1392 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(),
1393 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T>()
1394 );
1395
1396 if values.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().%rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#::LENGTHrust-analyzer cargo palette 0.7.6 ArrayExt#LENGTH. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().0 {
1397 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(VecCastErrorrust-analyzer cargo palette 0.7.6 cast/array/VecCastError# {
1398 valuesrust-analyzer cargo palette 0.7.6 cast/array/VecCastError#values.,
1399 kindrust-analyzer cargo palette 0.7.6 cast/array/VecCastError#kind.: VecCastErrorKindrust-analyzer cargo palette 0.7.6 cast/array/VecCastErrorKind#::LengthMismatchrust-analyzer cargo palette 0.7.6 cast/array/VecCastErrorKind#LengthMismatch#,
1400 });
1401 }
1402
1403 if values.capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]capacity().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().%rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#::LENGTHrust-analyzer cargo palette 0.7.6 ArrayExt#LENGTH. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().0 {
1404 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(VecCastErrorrust-analyzer cargo palette 0.7.6 cast/array/VecCastError# {
1405 valuesrust-analyzer cargo palette 0.7.6 cast/array/VecCastError#values.,
1406 kindrust-analyzer cargo palette 0.7.6 cast/array/VecCastError#kind.: VecCastErrorKindrust-analyzer cargo palette 0.7.6 cast/array/VecCastErrorKind#::CapacityMismatchrust-analyzer cargo palette 0.7.6 cast/array/VecCastErrorKind#CapacityMismatch#,
1407 });
1408 }
1409
1410 let mut values = ManuallyDroprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/ManuallyDrop#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/impl#[`ManuallyDrop<T>`]new().(values);
1411
1412 let raw = values.as_mut_ptrrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_mut_ptr().();
1413 let length = values.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div().T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#::LENGTHrust-analyzer cargo palette 0.7.6 ArrayExt#LENGTH.;
1414 let capacity = values.capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]capacity().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div().T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#::LENGTHrust-analyzer cargo palette 0.7.6 ArrayExt#LENGTH.;
1415
1416 // Safety: The requirements of implementing `ArrayCast`, as well as the size
1417 // and alignment asserts, ensures that reading `T` as `T::Array` is safe.
1418 // The length and capacity are multiplies of the array length.
1419 unsafe { Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::from_raw_partsrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]from_raw_parts().(raw.castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]cast().::<T>(), length, capacity)) }
1420 }
1421
1422 /// Map values of color A to values of color B without creating a new `Vec`.
1423 ///
1424 /// This uses the guarantees of [`ArrayCast`] to reuse the allocation.
1425 #[cfg(feature = "alloc")]
1426 #[inline]
1427 pub fn map_vec_in_placerust-analyzer cargo palette 0.7.6 cast/array/map_vec_in_place().<A, B, F>(values: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<A>, mut map: F) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<B>
1428 where
1429 A: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
1430 B: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#<Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array# = A::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>,
1431 F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(A) -> B,
1432 {
1433 // We are checking `B` in advance, to stop the program before any work is
1434 // done. `A` is checked when converting to arrays.
1435 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<B::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(), corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<B>());
1436 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
1437 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<B::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(),
1438 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<B>()
1439 );
1440
1441 let mut values = ManuallyDroprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/ManuallyDrop#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/impl#[`ManuallyDrop<T>`]new().(into_array_vecrust-analyzer cargo palette 0.7.6 cast/array/into_array_vec().(values));
1442
1443 for item in &mut *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/impl#[`ManuallyDrop<T>`][DerefMut]deref_mut().values {
1444 // Safety: We will put a new value back below, and `values` will not be dropped on panic.
1445 let input = unsafe { corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::readrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/read().(item) };
1446
1447 let output = into_arrayrust-analyzer cargo palette 0.7.6 cast/array/into_array().::<B>(map(from_arrayrust-analyzer cargo palette 0.7.6 cast/array/from_array().::<A>(input)));
1448
1449 // Safety: `output` is derived from the original value, so this is putting it back into place.
1450 unsafe { corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/write().(item, output) };
1451 }
1452
1453 from_array_vecrust-analyzer cargo palette 0.7.6 cast/array/from_array_vec().(ManuallyDroprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/ManuallyDrop#::into_innerrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/impl#[`ManuallyDrop<T>`]into_inner().(values))
1454 }
1455
1456 /// Map values of color A to values of color B without creating a new `Box<[B]>`.
1457 ///
1458 /// This uses the guarantees of [`ArrayCast`] to reuse the allocation.
1459 #[cfg(feature = "alloc")]
1460 #[inline]
1461 pub fn map_slice_box_in_placerust-analyzer cargo palette 0.7.6 cast/array/map_slice_box_in_place().<A, B, F>(values: Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<[A]>, mut map: F) -> Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<[B]>
1462 where
1463 A: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
1464 B: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#<Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array# = A::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>,
1465 F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(A) -> B,
1466 {
1467 // We are checking `B` in advance, to stop the program before any work is
1468 // done. `A` is checked when converting to arrays.
1469 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<B::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(), corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<B>());
1470 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
1471 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<B::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#>(),
1472 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<B>()
1473 );
1474
1475 let mut values = ManuallyDroprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/ManuallyDrop#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/impl#[`ManuallyDrop<T>`]new().(into_array_slice_boxrust-analyzer cargo palette 0.7.6 cast/array/into_array_slice_box().(values));
1476
1477 for item in &mut *rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T, A>`][Deref]deref().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/impl#[`ManuallyDrop<T>`][DerefMut]deref_mut().values {
1478 // Safety: We will put a new value back below, and `values` will not be dropped on panic.
1479 let input = unsafe { corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::readrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/read().(item) };
1480
1481 let output = into_arrayrust-analyzer cargo palette 0.7.6 cast/array/into_array().::<B>(map(from_arrayrust-analyzer cargo palette 0.7.6 cast/array/from_array().::<A>(input)));
1482
1483 // Safety: `output` is derived from the original value, so this is putting it back into place.
1484 unsafe { corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/write().(item, output) };
1485 }
1486
1487 from_array_slice_boxrust-analyzer cargo palette 0.7.6 cast/array/from_array_slice_box().(ManuallyDroprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/ManuallyDrop#::into_innerrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/impl#[`ManuallyDrop<T>`]into_inner().(values))
1488 }
1489
1490 /// The error type returned when casting a slice of components fails.
1491 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:)]
1492 pub struct SliceCastErrorrust-analyzer cargo palette 0.7.6 cast/array/SliceCastError#;
1493
1494 impl corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for SliceCastErrorrust-analyzer cargo palette 0.7.6 cast/array/SliceCastError# {
1495 fn fmtrust-analyzer cargo palette 0.7.6 cast/array/impl#[SliceCastError][Display]fmt().(&self, f: &mut corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
1496 f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().("could not convert component slice to colors")
1497 }
1498 }
1499
1500 #[cfg(feature = "std")]
1501 impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# for SliceCastErrorrust-analyzer cargo palette 0.7.6 cast/array/SliceCastError# {}
1502
1503 /// The error type returned when casting a boxed slice of components fails.
1504 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:)]
1505 #[cfg(feature = "alloc")]
1506 pub struct BoxedSliceCastErrorrust-analyzer cargo palette 0.7.6 cast/array/BoxedSliceCastError#<T> {
1507 /// The original values.
1508 pub valuesrust-analyzer cargo palette 0.7.6 cast/array/BoxedSliceCastError#values.: Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<[T]>,
1509 }
1510
1511 #[cfg(feature = "alloc")]
1512 impl<T> corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for BoxedSliceCastErrorrust-analyzer cargo palette 0.7.6 cast/array/BoxedSliceCastError#<T> {
1513 fn fmtrust-analyzer cargo palette 0.7.6 cast/array/impl#[`BoxedSliceCastError<T>`][Debug]fmt().(&self, f: &mut corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
1514 f.debug_structrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]debug_struct().("BoxedSliceCastError").finishrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]finish().()
1515 }
1516 }
1517
1518 #[cfg(feature = "alloc")]
1519 impl<T> corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for BoxedSliceCastErrorrust-analyzer cargo palette 0.7.6 cast/array/BoxedSliceCastError#<T> {
1520 fn fmtrust-analyzer cargo palette 0.7.6 cast/array/impl#[`BoxedSliceCastError<T>`][Display]fmt().(&self, f: &mut corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
1521 f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().("could not convert boxed component slice to colors")
1522 }
1523 }
1524
1525 #[cfg(feature = "std")]
1526 impl<T> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# for BoxedSliceCastErrorrust-analyzer cargo palette 0.7.6 cast/array/BoxedSliceCastError#<T> {}
1527
1528 /// The error type returned when casting a `Vec` of components fails.
1529 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:)]
1530 #[cfg(feature = "alloc")]
1531 pub struct VecCastErrorrust-analyzer cargo palette 0.7.6 cast/array/VecCastError#<T> {
1532 /// The type of error that occurred.
1533 pub kindrust-analyzer cargo palette 0.7.6 cast/array/VecCastError#kind.: VecCastErrorKindrust-analyzer cargo palette 0.7.6 cast/array/VecCastErrorKind#,
1534
1535 /// The original values.
1536 pub valuesrust-analyzer cargo palette 0.7.6 cast/array/VecCastError#values.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T>,
1537 }
1538
1539 #[cfg(feature = "alloc")]
1540 impl<T> corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for VecCastErrorrust-analyzer cargo palette 0.7.6 cast/array/VecCastError#<T> {
1541 fn fmtrust-analyzer cargo palette 0.7.6 cast/array/impl#[`VecCastError<T>`][Debug]fmt().(&self, f: &mut corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
1542 f.debug_structrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]debug_struct().("VecCastError")
1543 .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("kind", &self.kindrust-analyzer cargo palette 0.7.6 cast/array/VecCastError#kind.)
1544 .finishrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]finish().()
1545 }
1546 }
1547
1548 #[cfg(feature = "alloc")]
1549 impl<T> corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for VecCastErrorrust-analyzer cargo palette 0.7.6 cast/array/VecCastError#<T> {
1550 fn fmtrust-analyzer cargo palette 0.7.6 cast/array/impl#[`VecCastError<T>`][Display]fmt().(&self, f: &mut corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
1551 f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().("could not convert component vector to colors")
1552 }
1553 }
1554
1555 #[cfg(feature = "std")]
1556 impl<T> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# for VecCastErrorrust-analyzer cargo palette 0.7.6 cast/array/VecCastError#<T> {}
1557
1558 /// The type of error that is returned when casting a `Vec` of components.
1559 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:)]
1560 #[cfg(feature = "alloc")]
1561 pub enum VecCastErrorKindrust-analyzer cargo palette 0.7.6 cast/array/VecCastErrorKind# {
1562 /// The type of error returned when the length of a `Vec` didn't match the
1563 /// requirements.
1564 LengthMismatchrust-analyzer cargo palette 0.7.6 cast/array/VecCastErrorKind#LengthMismatch#,
1565
1566 /// The type of error returned when the capacity of a `Vec` didn't match the
1567 /// requirements.
1568 CapacityMismatchrust-analyzer cargo palette 0.7.6 cast/array/VecCastErrorKind#CapacityMismatch#,
1569 }
1570
1571 #[cfg(test)]
1572 mod testrust-analyzer cargo palette 0.7.6 cast/array/test/ {
1573 #[cfg(feature = "alloc")]
1574 use craterust-analyzer cargo palette 0.7.6 crate/::{LinSrgbrust-analyzer cargo palette 0.7.6 rgb/LinSrgb#, Srgbrust-analyzer cargo palette 0.7.6 rgb/Srgb#};
1575
1576 #[cfg(feature = "alloc")]
1577 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
1578 fn array_vec_len_caprust-analyzer cargo palette 0.7.6 cast/array/test/array_vec_len_cap().() {
1579 let mut original = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![
1580 Srgbrust-analyzer cargo palette 0.7.6 rgb/Srgb#::newrust-analyzer cargo palette 0.7.6 rgb/rgb/impl#[`Rgb<S, T>`]new().(255u8, 0, 0),
1581 Srgbrust-analyzer cargo palette 0.7.6 rgb/Srgb#::newrust-analyzer cargo palette 0.7.6 rgb/rgb/impl#[`Rgb<S, T>`]new().(0, 255, 0),
1582 Srgbrust-analyzer cargo palette 0.7.6 rgb/Srgb#::newrust-analyzer cargo palette 0.7.6 rgb/rgb/impl#[`Rgb<S, T>`]new().(0, 0, 255),
1583 ];
1584 original.reserve_exactrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]reserve_exact().(5); // Bringing it to 8
1585
1586 let colors_arrays = superrust-analyzer cargo palette 0.7.6 cast/array/::into_array_vecrust-analyzer cargo palette 0.7.6 cast/array/into_array_vec().(original);
1587 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(colors_arrays.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(), 3);
1588 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(colors_arrays.capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]capacity().(), 8);
1589
1590 let colors = superrust-analyzer cargo palette 0.7.6 cast/array/::from_array_vecrust-analyzer cargo palette 0.7.6 cast/array/from_array_vec().::<Srgbrust-analyzer cargo palette 0.7.6 rgb/Srgb#<_>>(colors_arrays);
1591 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(colors.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(), 3);
1592 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(colors.capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]capacity().(), 8);
1593
1594 let colors_components = superrust-analyzer cargo palette 0.7.6 cast/array/::into_component_vecrust-analyzer cargo palette 0.7.6 cast/array/into_component_vec().(colors);
1595 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(colors_components.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(), 9);
1596 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(colors_components.capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]capacity().(), 24);
1597
1598 let colors = superrust-analyzer cargo palette 0.7.6 cast/array/::from_component_vecrust-analyzer cargo palette 0.7.6 cast/array/from_component_vec().::<Srgbrust-analyzer cargo palette 0.7.6 rgb/Srgb#<_>>(colors_components);
1599 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(colors.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(), 3);
1600 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(colors.capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]capacity().(), 8);
1601 }
1602
1603 #[cfg(feature = "alloc")]
1604 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
1605 fn map_vec_in_placerust-analyzer cargo palette 0.7.6 cast/array/test/map_vec_in_place().() {
1606 fn do_thingsrust-analyzer cargo palette 0.7.6 cast/array/test/do_things().(rgb: Srgbrust-analyzer cargo palette 0.7.6 rgb/Srgb#) -> LinSrgbrust-analyzer cargo palette 0.7.6 rgb/LinSrgb# {
1607 let mut linear = rgb.into_linearrust-analyzer cargo palette 0.7.6 rgb/rgb/impl#[`Rgb<S, T>`]into_linear().();
1608 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::swaprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/swap().(&mut linear.redrust-analyzer cargo palette 0.7.6 rgb/rgb/Rgb#red., &mut linear.bluerust-analyzer cargo palette 0.7.6 rgb/rgb/Rgb#blue.);
1609 linear
1610 }
1611
1612 let values = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![Srgbrust-analyzer cargo palette 0.7.6 rgb/Srgb#::newrust-analyzer cargo palette 0.7.6 rgb/rgb/impl#[`Rgb<S, T>`]new().(0.8, 1.0, 0.2), Srgbrust-analyzer cargo palette 0.7.6 rgb/Srgb#::newrust-analyzer cargo palette 0.7.6 rgb/rgb/impl#[`Rgb<S, T>`]new().(0.9, 0.1, 0.3)];
1613 let result = superrust-analyzer cargo palette 0.7.6 cast/array/::map_vec_in_placerust-analyzer cargo palette 0.7.6 cast/array/map_vec_in_place().(values, do_thingsrust-analyzer cargo palette 0.7.6 cast/array/test/do_things().);
1614 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
1615 result,
1616 vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![
1617 do_thingsrust-analyzer cargo palette 0.7.6 cast/array/test/do_things().(Srgbrust-analyzer cargo palette 0.7.6 rgb/Srgb#::newrust-analyzer cargo palette 0.7.6 rgb/rgb/impl#[`Rgb<S, T>`]new().(0.8, 1.0, 0.2)),
1618 do_thingsrust-analyzer cargo palette 0.7.6 cast/array/test/do_things().(Srgbrust-analyzer cargo palette 0.7.6 rgb/Srgb#::newrust-analyzer cargo palette 0.7.6 rgb/rgb/impl#[`Rgb<S, T>`]new().(0.9, 0.1, 0.3))
1619 ]
1620 )
1621 }
1622
1623 #[cfg(feature = "alloc")]
1624 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
1625 fn map_slice_box_in_placerust-analyzer cargo palette 0.7.6 cast/array/test/map_slice_box_in_place().() {
1626 fn do_thingsrust-analyzer cargo palette 0.7.6 cast/array/test/do_things().(rgb: Srgbrust-analyzer cargo palette 0.7.6 rgb/Srgb#) -> LinSrgbrust-analyzer cargo palette 0.7.6 rgb/LinSrgb# {
1627 let mut linear = rgb.into_linearrust-analyzer cargo palette 0.7.6 rgb/rgb/impl#[`Rgb<S, T>`]into_linear().();
1628 corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::swaprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/swap().(&mut linear.redrust-analyzer cargo palette 0.7.6 rgb/rgb/Rgb#red., &mut linear.bluerust-analyzer cargo palette 0.7.6 rgb/rgb/Rgb#blue.);
1629 linear
1630 }
1631
1632 let values = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![Srgbrust-analyzer cargo palette 0.7.6 rgb/Srgb#::newrust-analyzer cargo palette 0.7.6 rgb/rgb/impl#[`Rgb<S, T>`]new().(0.8, 1.0, 0.2), Srgbrust-analyzer cargo palette 0.7.6 rgb/Srgb#::newrust-analyzer cargo palette 0.7.6 rgb/rgb/impl#[`Rgb<S, T>`]new().(0.9, 0.1, 0.3)].into_boxed_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]into_boxed_slice().();
1633 let result = superrust-analyzer cargo palette 0.7.6 cast/array/::map_slice_box_in_placerust-analyzer cargo palette 0.7.6 cast/array/map_slice_box_in_place().(values, do_thingsrust-analyzer cargo palette 0.7.6 cast/array/test/do_things().);
1634 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
1635 result,
1636 vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![
1637 do_thingsrust-analyzer cargo palette 0.7.6 cast/array/test/do_things().(Srgbrust-analyzer cargo palette 0.7.6 rgb/Srgb#::newrust-analyzer cargo palette 0.7.6 rgb/rgb/impl#[`Rgb<S, T>`]new().(0.8, 1.0, 0.2)),
1638 do_thingsrust-analyzer cargo palette 0.7.6 cast/array/test/do_things().(Srgbrust-analyzer cargo palette 0.7.6 rgb/Srgb#::newrust-analyzer cargo palette 0.7.6 rgb/rgb/impl#[`Rgb<S, T>`]new().(0.9, 0.1, 0.3))
1639 ]
1640 .into_boxed_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]into_boxed_slice().()
1641 )
1642 }
1643 }