Skip to main content

CastInto

Trait CastInto 

Source
pub trait CastInto<T>: Sized + CastIntoComposite<T, Output = T> {
    // Provided method
    fn cast_into(self) -> Self::Output { ... }
}
Expand description

Same semantics as the as keyword: 4f32 as u64, but generic friendly.

Like the as keyword, the result might lose some precision.

The CastIntoComposite trait is the most generic way to use it.

The Output type can be a little bit different for composite type, but it should still be related to the generic type.

use hexga_math::prelude::*;

assert_eq!(i32::cast_from(255u8), 255i32);
assert_eq!(i32::cast_from(12.3f32), 12);
use hexga_math::prelude::*;

let float32 = 2.5f32;
let float64 = 2.5f64;
let float32_to_64 = f64::cast_from(float32);
assert_eq!(float32_to_64, float32_to_64);

let float32_to_64 = <f32 as CastInto<f64>>::cast_into(float32);
assert_eq!(float32_to_64, float32_to_64);

// The most generic way to do it
let float32_to_64 = <f32 as CastIntoComposite<f64>>::cast_into_composite(float32);
assert_eq!(float32_to_64, float32_to_64);

Only the CastIntoComposite will be working with composite :

use hexga_math::prelude::*;

let vec_f32 = Vector2::<f32>::new(0.5, 0.5);
let vec_f64 = Vector2::<f64>::new(0.5, 0.5);
let vec_f32_to_f64 = <Vector2::<f32> as CastIntoComposite<f64>>::cast_into_composite(vec_f32);
assert_eq!(vec_f32_to_f64, vec_f64);

There is no CastFromComposite trait because it is impossible to impl it.

Provided Methods§

Source

fn cast_into(self) -> Self::Output

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.

Implementors§

Source§

impl<T, T2> CastInto<T> for T2
where T2: CastIntoComposite<T, Output = T>,