newton 0.5.1

A Newtonian physics simulator written in Rust.
Documentation
// Newton - A Newtonian physics simulator written in Rust.
// Copyright (C) 2017 Cooper Paul EdenDay
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// Electronic mail: cedenday@protonmail.com

use std;

use units::{self, Eval};

#[derive(Serialize, Deserialize, PartialEq, Debug, Copy, Clone)]
pub struct Vector<T: Eval>
    where T: Copy
{
    vector: [T; 3],
}

impl<T: Eval> Vector<T>
    where T: Copy
{
    pub fn new(input: [T; 3]) -> Vector<T> {
        Vector { vector: input }
    }

    pub fn get(&self) -> &[T; 3] {
        &self.vector
    }

    pub fn displacement(p0: &Vector<T>, p1: &Vector<T>) -> Vector<T> {
        p0.vector
            .iter()
            .zip(p1.vector.iter())
            .map(|(a, b)| T::new(b.eval() - a.eval()))
            .collect()
    }

    pub fn distance(p0: &Vector<T>, p1: &Vector<T>) -> units::Length {
        units::Length::new(Vector::displacement(p0, p1)
                               .vector
                               .iter()
                               .map(|x| x.eval().powi(2))
                               .sum::<units::Scalar>()
                               .sqrt())
    }

    pub fn magnitude(&self) -> units::Length {
        let origin = Vector::new([T::new(0.0); 3]);
        Vector::distance(&origin, self)
    }

    pub fn normalize(&self) -> Vector<T> {
        let magnitude = self.magnitude();
        self.vector
            .iter()
            .map(|x| x.eval() / magnitude.eval())
            .map(T::new)
            .collect()
    }
}

impl<T: Eval> std::iter::FromIterator<T> for Vector<T>
    where T: Copy
{
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
        let mut x = iter.into_iter();
        Vector::new([x.next().unwrap(), x.next().unwrap(), x.next().unwrap()])
    }
}

impl<T: Eval> std::ops::Add for Vector<T>
    where T: Copy
{
    type Output = Vector<T>;

    fn add(self, other: Vector<T>) -> Vector<T> {
        self.vector
            .iter()
            .zip(other.vector.iter())
            .map(|(a, b)| T::new(a.eval() + b.eval()))
            .collect()
    }
}