cosmic::cosmic_theme::palette::cast

Trait TryFromComponents

source
pub trait TryFromComponents<C>: Sized {
    type Error;

    // Required method
    fn try_from_components(components: C) -> Result<Self, Self::Error>;
}
Expand description

Trait for trying to cast a collection of colors from a collection of color components 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::TryFromComponents, Srgb};

let array: [_; 6] = [64, 139, 10, 93, 18, 214];
let slice: &[_] = &[64, 139, 10, 93, 18, 214];
let slice_mut: &mut [_] = &mut [64, 139, 10, 93, 18, 214];
let vec: Vec<_> = vec![64, 139, 10, 93, 18, 214];

assert_eq!(
    <[Srgb<u8>; 2]>::try_from_components(array),
    Ok([Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)])
);

assert_eq!(
    <&[Srgb<u8>]>::try_from_components(slice),
    Ok([Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)].as_ref())
);

assert_eq!(
    <&mut [Srgb<u8>]>::try_from_components(slice_mut),
    Ok([Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)].as_mut())
);

assert_eq!(
    Vec::<Srgb<u8>>::try_from_components(vec),
    Ok(vec![Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)])
);

Owning types can be cast as slices, too:

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

let array: [_; 6] = [64, 139, 10, 93, 18, 214];
let mut vec: Vec<_> = vec![64, 139, 10, 93, 18, 214];

assert_eq!(
    <&[Srgb<u8>]>::try_from_components(&array),
    Ok([Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)].as_ref())
);

assert_eq!(
    <&mut [Srgb<u8>]>::try_from_components(&mut vec),
    Ok([Srgb::new(64u8, 139, 10), Srgb::new(93, 18, 214)].as_mut())
);

This produces an error:

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

let components = &[64, 139, 10, 93, 18]; // Not a multiple of 3
assert!(<&[Srgb<u8>]>::try_from_components(components).is_err());

Required Associated Types§

source

type Error

The error for when try_from_components fails to cast.

Required Methods§

source

fn try_from_components(components: C) -> Result<Self, Self::Error>

Try to cast a collection of color components into an collection of colors.

Return an error if the conversion can’t be done, such as when the number of items in components isn’t a multiple of the number of components in the color type.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<'a, T, C> TryFromComponents<&'a [T]> for &'a [C]
where T: 'a, C: ArrayCast, <C as ArrayCast>::Array: ArrayExt<Item = T>,

source§

impl<'a, T, C> TryFromComponents<&'a Box<[T]>> for &'a [C]
where T: 'a, C: ArrayCast, <C as ArrayCast>::Array: ArrayExt<Item = T>,

source§

impl<'a, T, C> TryFromComponents<&'a Vec<T>> for &'a [C]
where T: 'a, C: ArrayCast, <C as ArrayCast>::Array: ArrayExt<Item = T>,

source§

type Error = SliceCastError

source§

fn try_from_components( components: &'a Vec<T>, ) -> Result<&'a [C], <&'a [C] as TryFromComponents<&'a Vec<T>>>::Error>

source§

impl<'a, T, C> TryFromComponents<&'a mut [T]> for &'a mut [C]
where T: 'a, C: ArrayCast, <C as ArrayCast>::Array: ArrayExt<Item = T>,

source§

type Error = SliceCastError

source§

fn try_from_components( components: &'a mut [T], ) -> Result<&'a mut [C], <&'a mut [C] as TryFromComponents<&'a mut [T]>>::Error>

source§

impl<'a, T, C> TryFromComponents<&'a mut Box<[T]>> for &'a mut [C]
where T: 'a, C: ArrayCast, <C as ArrayCast>::Array: ArrayExt<Item = T>,

source§

type Error = SliceCastError

source§

fn try_from_components( components: &'a mut Box<[T]>, ) -> Result<&'a mut [C], <&'a mut [C] as TryFromComponents<&'a mut Box<[T]>>>::Error>

source§

impl<'a, T, C> TryFromComponents<&'a mut Vec<T>> for &'a mut [C]
where T: 'a, C: ArrayCast, <C as ArrayCast>::Array: ArrayExt<Item = T>,

source§

type Error = SliceCastError

source§

fn try_from_components( components: &'a mut Vec<T>, ) -> Result<&'a mut [C], <&'a mut [C] as TryFromComponents<&'a mut Vec<T>>>::Error>

source§

impl<'a, T, C, const N: usize> TryFromComponents<&'a [T; N]> for &'a [C]
where T: 'a, C: ArrayCast, <C as ArrayCast>::Array: ArrayExt<Item = T>,

source§

impl<'a, T, C, const N: usize> TryFromComponents<&'a mut [T; N]> for &'a mut [C]
where T: 'a, C: ArrayCast, <C as ArrayCast>::Array: ArrayExt<Item = T>,

source§

type Error = SliceCastError

source§

fn try_from_components( components: &'a mut [T; N], ) -> Result<&'a mut [C], <&'a mut [C] as TryFromComponents<&'a mut [T; N]>>::Error>

source§

impl<T, C> TryFromComponents<Box<[T]>> for Box<[C]>
where C: ArrayCast, <C as ArrayCast>::Array: ArrayExt<Item = T>,

source§

impl<T, C> TryFromComponents<Vec<T>> for Vec<C>
where C: ArrayCast, <C as ArrayCast>::Array: ArrayExt<Item = T>,

source§

impl<T, C, const N: usize, const M: usize> TryFromComponents<[T; N]> for [C; M]
where C: ArrayCast, <C as ArrayCast>::Array: ArrayExt<Item = T>,

Implementors§