palette::cast

Trait ComponentsAsMut

source
pub trait ComponentsAsMut<C: ?Sized> {
    // Required method
    fn components_as_mut(&mut self) -> &mut C;
}
Expand description

Trait for casting 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.

§Panics

The cast will panic 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::ComponentsAsMut, 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: &mut [Srgb<u8>] = array.components_as_mut();
assert_eq!(colors, &mut [Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)]);

let colors: &mut [Srgb<u8>] = slice_mut.components_as_mut();
assert_eq!(colors, &mut [Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)]);

let colors: &mut [Srgb<u8>] = vec.components_as_mut();
assert_eq!(colors, &mut [Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)]);

This panics:

use palette::{cast::ComponentsAsMut, Srgb};

let mut components = [64, 139, 10, 93, 18, 214, 0, 123]; // Not a multiple of 3
let colors: &mut [Srgb<u8>] = components.components_as_mut();

Required Methods§

source

fn components_as_mut(&mut self) -> &mut C

Cast this collection of color components into a mutable reference to a collection of colors.

§Panics

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.

Implementors§

source§

impl<T, C> ComponentsAsMut<C> for T
where T: TryComponentsAsMut<C> + ?Sized, T::Error: Debug, C: ?Sized,