#![allow(unused)]
use crate::{Point3, Vector3};
use std::fmt::{Display, Formatter, Result};
use std::ops::{Index, IndexMut};
struct MyType<T>(T);
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Axis {
X = 0,
Y = 1,
Z = 2,
}
impl Display for Axis {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(
f,
"{}",
match *self {
Axis::X => "x",
Axis::Y => "y",
Axis::Z => "z",
}
)
}
}
impl Index<Axis> for [f32] {
type Output = f32;
fn index(&self, axis: Axis) -> &f32 {
&self[axis as usize]
}
}
impl Index<Axis> for Point3 {
type Output = f32;
fn index(&self, axis: Axis) -> &f32 {
match axis {
Axis::X => &self.x,
Axis::Y => &self.y,
Axis::Z => &self.z,
}
}
}
impl Index<Axis> for MyType<Vector3> {
type Output = f32;
fn index(&self, axis: Axis) -> &f32 {
match axis {
Axis::X => &self.0.x,
Axis::Y => &self.0.y,
Axis::Z => &self.0.z,
}
}
}
impl IndexMut<Axis> for [f32] {
fn index_mut(&mut self, axis: Axis) -> &mut f32 {
&mut self[axis as usize]
}
}
impl IndexMut<Axis> for Point3 {
fn index_mut(&mut self, axis: Axis) -> &mut f32 {
match axis {
Axis::X => &mut self.x,
Axis::Y => &mut self.y,
Axis::Z => &mut self.z,
}
}
}
impl IndexMut<Axis> for MyType<Vector3> {
fn index_mut(&mut self, axis: Axis) -> &mut f32 {
match axis {
Axis::X => &mut self.0.x,
Axis::Y => &mut self.0.y,
Axis::Z => &mut self.0.z,
}
}
}
#[cfg(test)]
mod test {
use crate::axis::Axis;
use proptest::prelude::*;
proptest! {
#[test]
fn test_index_by_axis(tpl: (f32, f32, f32)) {
let a = [tpl.0, tpl.1, tpl.2];
assert!((a[0] - a[Axis::X]).abs() < f32::EPSILON && (a[1] - a[Axis::Y]).abs() < f32::EPSILON && (a[2] - a[Axis::Z]).abs() < f32::EPSILON);
}
#[test]
fn test_set_by_axis(tpl: (f32, f32, f32)) {
let mut a = [0.0, 0.0, 0.0];
a[Axis::X] = tpl.0;
a[Axis::Y] = tpl.1;
a[Axis::Z] = tpl.2;
assert!((a[0] - tpl.0).abs() < f32::EPSILON && (a[1] - tpl.1).abs() < f32::EPSILON && (a[2] - tpl.2).abs() < f32::EPSILON);
}
}
}