Trait particular::particle::Particle
source · pub trait Particle {
type Array: Array;
// Required methods
fn position(&self) -> Self::Array;
fn mu(&self) -> <Self::Array as Array>::Item;
}
Expand description
Trait to describe a particle which consists of a position and a gravitational parameter mu.
§Deriving:
Used when the type has fields named position
and mu
:
#[derive(Particle)]
#[dim(3)]
struct Body {
position: Vec3,
mu: f32,
// ...
}
§Manual implementation:
Used when the type cannot directly provide a position and a gravitational parameter.
struct Body {
position: Vec3,
mass: f32,
// ...
}
impl Particle for Body {
type Array = [f32; 3];
fn position(&self) -> [f32; 3] {
self.position.into()
}
fn mu(&self) -> f32 {
self.mass * G
}
}
If you can’t implement Particle
on a type, you can use the fact that it is implemented for
tuples of an array and its scalar type instead of creating an intermediate type.
let particle = ([1.0, 1.0, 0.0], 5.0);
assert_eq!(particle.position(), [1.0, 1.0, 0.0]);
assert_eq!(particle.mu(), 5.0);