Commit bab450f8 authored by Luke Jones's avatar Luke Jones

remove float/numtrait

parent c52e4e27
Pipeline #62786215 passed with stages
in 3 minutes and 15 seconds
[package]
name = "vec2d"
version = "0.1.1"
version = "0.1.2"
authors = ["Luke Jones <luke@sphereidentity.com>"]
edition = "2018"
[dependencies]
num-traits = "0.2"
extern crate num_traits;
/// Representation of a mathematical vector e.g. a position or velocity
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Default)]
pub struct Vec2d<T> {
......@@ -7,7 +5,6 @@ pub struct Vec2d<T> {
pub y: T,
}
use num_traits::float::Float;
use std::convert::From;
use std::ops::{
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign,
......@@ -63,7 +60,7 @@ impl_vec_ops_for!(f32 f64);
Standard Vector operators
******************************************/
impl<T: Float> Vec2d<T> {
impl<T> Vec2d<T> {
pub fn new(x: T, y: T) -> Vec2d<T>
where
T: Mul,
......@@ -79,8 +76,8 @@ impl<T: Float> Vec2d<T> {
}
}
/// Add scalar to `Vec2d`
impl<T: Float + Add + Copy> Add<T> for Vec2d<T> {
type Output = Vec2d<T>;
impl<T: Add + Copy> Add<T> for Vec2d<T> {
type Output = Vec2d<T::Output>;
fn add(self, rhs: T) -> Self::Output {
Vec2d {
x: self.x + rhs,
......@@ -89,8 +86,8 @@ impl<T: Float + Add + Copy> Add<T> for Vec2d<T> {
}
}
/// Add two `Vec2d` together
impl<T: Float + Add> Add for Vec2d<T> {
type Output = Vec2d<T>;
impl<T: Add> Add for Vec2d<T> {
type Output = Vec2d<T::Output>;
fn add(self, rhs: Self) -> Self::Output {
Vec2d {
x: self.x + rhs.x,
......@@ -99,8 +96,8 @@ impl<T: Float + Add> Add for Vec2d<T> {
}
}
/// Subtract a scalar from this `Vec2d`
impl<T: Float + Sub + Copy> Sub<T> for Vec2d<T> {
type Output = Vec2d<T>;
impl<T: Sub + Copy> Sub<T> for Vec2d<T> {
type Output = Vec2d<T::Output>;
fn sub(self, rhs: T) -> Self::Output {
Vec2d {
x: self.x - rhs,
......@@ -109,8 +106,8 @@ impl<T: Float + Sub + Copy> Sub<T> for Vec2d<T> {
}
}
/// Subtract a `Vec2d` from this `Vec2d`
impl<T: Float + Sub> Sub for Vec2d<T> {
type Output = Vec2d<T>;
impl<T: Sub> Sub for Vec2d<T> {
type Output = Vec2d<T::Output>;
fn sub(self, rhs: Self) -> Self::Output {
Vec2d {
x: self.x - rhs.x,
......@@ -119,8 +116,8 @@ impl<T: Float + Sub> Sub for Vec2d<T> {
}
}
/// Multiply `Vec2d` by scalar
impl<T: Float + Mul + Copy> Mul<T> for Vec2d<T> {
type Output = Vec2d<T>;
impl<T: Mul + Copy> Mul<T> for Vec2d<T> {
type Output = Vec2d<T::Output>;
fn mul(self, rhs: T) -> Self::Output {
Vec2d {
x: self.x * rhs,
......@@ -129,7 +126,7 @@ impl<T: Float + Mul + Copy> Mul<T> for Vec2d<T> {
}
}
/// Calculate and return the scalar product of this `Vec2d` by `Vec2d`
impl<T: Float + Mul + Add> Mul for Vec2d<T>
impl<T: Mul + Add> Mul for Vec2d<T>
where
<T as Mul>::Output: Add {
type Output = <<T as Mul>::Output as Add>::Output;
......@@ -138,8 +135,8 @@ impl<T: Float + Mul + Add> Mul for Vec2d<T>
}
}
/// Divide `Vec2d` by scalar
impl<T: Float + Div + Copy> Div<T> for Vec2d<T> {
type Output = Vec2d<T>;
impl<T: Div + Copy> Div<T> for Vec2d<T> {
type Output = Vec2d<T::Output>;
fn div(self, rhs: T) -> Self::Output {
Vec2d {
x: self.x / rhs,
......@@ -148,8 +145,8 @@ impl<T: Float + Div + Copy> Div<T> for Vec2d<T> {
}
}
/// Divide `Vec2d` by scalar
impl<T: Float + Div> Div for Vec2d<T> {
type Output = Vec2d<T>;
impl<T: Div> Div for Vec2d<T> {
type Output = Vec2d<T::Output>;
fn div(self, rhs: Self) -> Self::Output {
Vec2d {
x: self.x / rhs.x,
......@@ -158,64 +155,64 @@ impl<T: Float + Div> Div for Vec2d<T> {
}
}
impl<T: Float + AddAssign + Copy> AddAssign<T> for Vec2d<T> {
impl<T: AddAssign + Copy> AddAssign<T> for Vec2d<T> {
fn add_assign(&mut self, rhs: T) {
self.x += rhs;
self.y += rhs;
}
}
impl<T: Float + AddAssign> AddAssign<Vec2d<T>> for Vec2d<T> {
impl<T: AddAssign> AddAssign<Vec2d<T>> for Vec2d<T> {
fn add_assign(&mut self, rhs: Self) {
self.x += rhs.x;
self.y += rhs.y;
}
}
impl<T: Float + SubAssign + Copy> SubAssign<T> for Vec2d<T> {
impl<T: SubAssign + Copy> SubAssign<T> for Vec2d<T> {
fn sub_assign(&mut self, rhs: T) {
self.x -= rhs;
self.y -= rhs;
}
}
impl<T: Float + SubAssign> SubAssign<Vec2d<T>> for Vec2d<T> {
impl<T: SubAssign> SubAssign<Vec2d<T>> for Vec2d<T> {
fn sub_assign(&mut self, rhs: Self) {
self.x -= rhs.x;
self.y -= rhs.y;
}
}
impl<T: Float + MulAssign + Copy> MulAssign<T> for Vec2d<T> {
impl<T: MulAssign + Copy> MulAssign<T> for Vec2d<T> {
fn mul_assign(&mut self, rhs: T) {
self.x *= rhs;
self.y *= rhs;
}
}
impl<T: Float + MulAssign> MulAssign<Vec2d<T>> for Vec2d<T> {
impl<T: MulAssign> MulAssign<Vec2d<T>> for Vec2d<T> {
fn mul_assign(&mut self, rhs: Self) {
self.x *= rhs.x;
self.y *= rhs.y;
}
}
impl<T: Float + DivAssign + Copy> DivAssign<T> for Vec2d<T> {
impl<T: DivAssign + Copy> DivAssign<T> for Vec2d<T> {
fn div_assign(&mut self, rhs: T) {
self.x /= rhs;
self.y /= rhs;
}
}
impl<T: Float + DivAssign> DivAssign<Vec2d<T>> for Vec2d<T> {
impl<T: DivAssign> DivAssign<Vec2d<T>> for Vec2d<T> {
fn div_assign(&mut self, rhs: Self) {
self.x /= rhs.x;
self.y /= rhs.y;
}
}
impl<T: Float + Neg> Neg for Vec2d<T> {
type Output = Vec2d<T>;
impl<T: Neg> Neg for Vec2d<T> {
type Output = Vec2d<T::Output>;
fn neg(self) -> Self::Output {
Vec2d {
......@@ -232,7 +229,7 @@ impl<T> Into<[T; 2]> for Vec2d<T> {
}
}
impl<T: Float + Copy> From<[T; 2]> for Vec2d<T> {
impl<T: Copy> From<[T; 2]> for Vec2d<T> {
#[inline]
fn from(array: [T; 2]) -> Self {
Vec2d {
......@@ -326,16 +323,4 @@ mod tests {
let point: Vec2d<f32> = Vec2d::new(4.0, 3.0);
assert_eq!(point.magnitude(), 5.0);
}
#[test]
fn normalise_f64() {
let point: Vec2d<f64> = Vec2d::new(4.0, 3.0);
assert_eq!(point.normalise(), Vec2d::new(0.8, 0.6000000000000001));
}
#[test]
fn magnitude_f64() {
let point: Vec2d<f64> = Vec2d::new(4.0, 3.0);
assert_eq!(point.magnitude(), 5.0);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment