pub trait CastFrom<T> {
// Required method
fn cast_from(value: T) -> Self;
}Expand description
Same semantics as the as
keyword: 4f32 as u64, and the From trait, but generic friendly.
One should always prefer implementing CastFrom over CastInto because implementing CastFrom automatically provides one with an implementation of CastInto thanks to the blanket implementation in the hexga_math library.
Like the as keyword, the result might lose some precision.
use hexga_math::prelude::*;
assert_eq!(i32::cast_from(12.3f32), 12);
let casted : i32 = 12.3f32.cast_into();
assert_eq!(casted, 12i32);Also work with composite type
ⓘ
use hexga_math::prelude::*;
let x = [1, 2i32];
let y : [f32; 2] = x.cast_into(),
assert_eq!(y, [1f32, 2f32]);
let a = point2(1, 2);
let b : Vec2 = a.cast_into(),
assert_eq!(b, vec2(1., 2.));Required Methods§
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.