pub const MAX_LANES: usize = 4;
pub type Lanes = [f32; MAX_LANES];
pub trait Animatable: Copy {
const LANES: usize;
fn to_lanes(self) -> Lanes;
fn from_lanes(lanes: Lanes) -> Self;
}
impl Animatable for f32 {
const LANES: usize = 1;
fn to_lanes(self) -> Lanes {
[self, 0.0, 0.0, 0.0]
}
fn from_lanes([x, ..]: Lanes) -> Self {
x
}
}
impl Animatable for [f32; 2] {
const LANES: usize = 2;
fn to_lanes(self) -> Lanes {
let [x, y] = self;
[x, y, 0.0, 0.0]
}
fn from_lanes([x, y, ..]: Lanes) -> Self {
[x, y]
}
}
impl Animatable for [f32; 3] {
const LANES: usize = 3;
fn to_lanes(self) -> Lanes {
let [x, y, z] = self;
[x, y, z, 0.0]
}
fn from_lanes([x, y, z, _]: Lanes) -> Self {
[x, y, z]
}
}
impl Animatable for [f32; 4] {
const LANES: usize = 4;
fn to_lanes(self) -> Lanes {
self
}
fn from_lanes(lanes: Lanes) -> Self {
lanes
}
}
#[derive(Clone, Copy, Debug, PartialEq, Default)]
pub struct Edges {
pub left: f32,
pub top: f32,
pub right: f32,
pub bottom: f32,
}
impl Edges {
pub const fn new(left: f32, top: f32, right: f32, bottom: f32) -> Edges {
Edges { left, top, right, bottom }
}
}
impl Animatable for Edges {
const LANES: usize = 4;
fn to_lanes(self) -> Lanes {
[self.left, self.top, self.right, self.bottom]
}
fn from_lanes([left, top, right, bottom]: Lanes) -> Self {
Edges { left, top, right, bottom }
}
}
pub(crate) const fn lane_count<T: Animatable>() -> usize {
if T::LANES < MAX_LANES { T::LANES } else { MAX_LANES }
}