use crate::lib::*; impl Cast<(A, B)> for (T1, T2) where T1: Cast, T2: Cast, { fn to((a, b): (A, B)) -> Self { (T1::to(a), T2::to(b)) } } impl Cast<(A, B, C)> for (T1, T2, T3) where T1: Cast, T2: Cast, T3: Cast, { fn to((a, b, c): (A, B, C)) -> Self { (T1::to(a), T2::to(b), T3::to(c)) } } impl Cast<(A, B, C, D)> for (T1, T2, T3, T4) where T1: Cast, T2: Cast, T3: Cast, T4: Cast, { fn to((a, b, c, d): (A, B, C, D)) -> Self { (T1::to(a), T2::to(b), T3::to(c), T4::to(d)) } } impl Cast<[A; N]> for [T; N] where T: Cast, { fn to(a: [A; N]) -> Self { a.map(|x| T::to(x)) } } impl Cast> for vec3 { fn to((v1, v2, v3, _): vec4) -> Self { (v1, v2, v3) } } impl Cast> for vec2 { fn to((v1, v2, _, _): vec4) -> Self { (v1, v2) } } impl Cast> for vec2 { fn to((v1, v2, _): vec3) -> Self { (v1, v2) } } impl Cast<&[T]> for vec2 { fn to(v: &[T]) -> Self { ASSERT!(v.len() > 1, "Slice is too short for Vec2"); (*v.at(0), *v.at(1)) } } impl Cast<&[T]> for vec3 { fn to(v: &[T]) -> Self { ASSERT!(v.len() > 2, "Slice is too short for Vec3"); (*v.at(0), *v.at(1), *v.at(2)) } } impl Cast<&[T]> for vec4 { fn to(v: &[T]) -> Self { ASSERT!(v.len() > 3, "Slice is too short for Vec4"); (*v.at(0), *v.at(1), *v.at(2), *v.at(3)) } } macro_rules! array_recast { ($to: ty, $t: ty, $dim: literal) => { impl Cast<$to> for [$t; $dim] { fn to(v: $to) -> Self { v.into() } } impl Cast<[$t; $dim]> for $to { fn to(v: [$t; $dim]) -> Self { v.into() } } }; } macro_rules! impl_transmute { ($($t: ty),+) => { $( array_recast!(vec2<$t>, $t, 2); array_recast!(vec3<$t>, $t, 3); array_recast!(vec4<$t>, $t, 4); )+ }; } impl_transmute!(u8, i8, u16, i16, u32, i32, u64, i64, u128, i128, f16, f32, f64);