pub trait TryComponentsAsMut<C>where
C: ?Sized,{
type Error;
// Required method
fn try_components_as_mut(&mut self) -> Result<&mut C, Self::Error>;
}
Expand description
Trait for trying to cast a mutable reference to collection of color components into a mutable reference to collection of colors without copying.
This trait is meant as a more convenient alternative to the free functions
in cast
, to allow method chaining among other things.
§Errors
The cast will return an error if the cast fails, such as when the length of the input is not a multiple of the color’s array length.
§Examples
use palette::{cast::TryComponentsAsMut, Srgb};
let mut array: [_; 6] = [64, 139, 10, 93, 18, 214];
let slice_mut: &mut [_] = &mut [64, 139, 10, 93, 18, 214];
let mut vec: Vec<_> = vec![64, 139, 10, 93, 18, 214];
let colors: Result<&mut [Srgb<u8>], _> = array.try_components_as_mut();
assert_eq!(colors, Ok(&mut [Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)][..]));
let colors: Result<&mut [Srgb<u8>], _> = slice_mut.try_components_as_mut();
assert_eq!(colors, Ok(&mut [Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)][..]));
let colors: Result<&mut [Srgb<u8>], _> = vec.try_components_as_mut();
assert_eq!(colors, Ok(&mut [Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)][..]));
This produces an error:
use palette::{cast::TryComponentsAsMut, Srgb};
let mut components = [64, 139, 10, 93, 18]; // Not a multiple of 3
let colors: Result<&mut [Srgb<u8>], _> = components.try_components_as_mut();
assert!(colors.is_err());
Required Associated Types§
Required Methods§
sourcefn try_components_as_mut(&mut self) -> Result<&mut C, Self::Error>
fn try_components_as_mut(&mut self) -> Result<&mut C, Self::Error>
Try to cast this collection of color components into a mutable reference to a collection of colors.
Return an error if the conversion can’t be done, such as when the number
of items in self
isn’t a multiple of the number of components in the
color type.