pub trait CastRangeFrom<T> {
// Required method
fn cast_range_from(value: T) -> Self;
}Expand description
Remap the value RangeDefault to the RangeDefault of the target type,
in a generic friendly way, and similar to the From trait.
[Self::RANGE_MIN..Self::RANGE_MAX] => [T::RANGE_MIN..T::RANGE_MAX]
One should always prefer implementing CastRangeFrom over CastRangeInto because implementing CastRangeFrom automatically provides one with an implementation of CastRangeInto thanks to the blanket implementation in the hexga_math library.
use hexga_math::prelude::*;
assert_eq!(u8::cast_range_from(1f32), 255u8);
assert_eq!(u8::cast_range_from(0f32), 0u8);
let casted_range : u16 = u8::MAX.cast_range_into();
assert_eq!(casted_range, u16::MAX);Also work with composite like std::array, Vector…
ⓘ
use hexga_math::prelude::*;
let x = [0u8, 127u8, 255u8];
let y : [u16; 3] = x.cast_range_into(),
assert_eq!(y, [0u16, 32639u16, 65535u16]);
let a = vector3(0u8, 127u8, 255u8);
let b : Vector3::<u16> = a.cast_into(),
assert_eq!(b, vector3(0u16, u16::MAX / 2 - u8::RANGE_MAX as u16 / 2 - 1, u16::MAX));Required Methods§
fn cast_range_from(value: T) -> Self
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.