Using the enum Axes
to confine Coordinate
and Quaternion
:
#[derive(Clone)]
pub enum Axes {
Coordinate {
x: f64,
y: f64,
z: f64,
reserve: Vec<f64>,
},
Quaternion {
x: f64,
y: f64,
z: f64,
},
}
impl Axes {
pub fn shift(&mut self, Sample: &Axes) -> () {
let Dup: Axes = self.clone();
match Dup {
Axes::Coordinate { x, y, z, reserve } => match &Sample {
Axes::Coordinate { x, y, z, reserve } => {
*self = Axes::Coordinate {
x: *x,
y: *y,
z: *z,
reserve: reserve.to_vec(),
};
}
_ => panic!(),
},
Axes::Quaternion { x, y, z } => match &Sample {
Axes::Quaternion { x, y, z } => {
*self = Axes::Quaternion {
x: *x,
y: *y,
z: *z,
};
}
_ => panic!(),
},
}
}
}
Using the trait Axes
to link the Coordinate
and Quaternion
structs:
pub trait Axes {
fn shift(&mut self, Sample: &Axes) -> ();
fn fold(&mut self, Sample: &Axes) -> ();
}
pub struct Coordinate {
pub x: f64,
pub y: f64,
pub z: f64,
pub reserve: Vec<f64>,
}
pub struct Quaternion {
pub x: f64,
pub y: f64,
pub z: f64,
}
impl Axes for Coordinate {
fn shift(&mut self, Sample: &Axes) -> () {}
fn fold(&mut self, Sample: &Axes) -> () {}
}
impl Axes for Quaternion {
fn shift(&mut self, Sample: &Axes) -> () {}
fn fold(&mut self, Sample: &Axes) -> () {}
}
Is a trait implemented on structs more accessible and more efficient in this case? I am confused of which to use and in what cases.
See Question&Answers more detail:
os