From 902aeb5dbe4076c1c3deb94a5adbf77a91947b04 Mon Sep 17 00:00:00 2001 From: Jason Travis Smith Date: Thu, 31 Dec 2015 02:58:21 -0500 Subject: [PATCH] This adds get_size() to all vector implementations. get_size() was added so that vectors could all be converted to Vector4 when being turned into binary values. from_str_radix() for floating point numbers was deprecated from the standard library. Until there is time to write our own implementation of this floating point numbers will panic on non base 10 string conversion. Base 10 string conversion will use the from_str function. --- src/lib.rs | 1 - src/number.rs | 9 ++++++++- src/vector.rs | 19 +++++++++++++++---- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index bc83066..7af9538 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,6 @@ //! //! #![feature(zero_one)] -#![feature(float_from_str_radix)] #![feature(float_extras)] #![feature(associated_consts)] diff --git a/src/number.rs b/src/number.rs index ce5129f..6f13328 100644 --- a/src/number.rs +++ b/src/number.rs @@ -2,6 +2,7 @@ use std::cmp::PartialEq; use std::mem::size_of; use std::num::{Zero, One}; use std::ops::{Add, Sub, Mul, Div, Rem}; +use std::str::FromStr; use super::bounded::Bounded; @@ -770,7 +771,13 @@ macro_rules! float_number_trait_impl fn from_str_radix(src: &str, radix: u32) -> Result { - <$varType>::from_str_radix(src, radix) + // TODO: Currently this will panic on a non base 10 radix. + // This is because the std library deprecated the + // from_str_radix function. Until a function can be + // written this will use the from_str function and + // panic for non base 10 requests. + assert!(radix == 10); + <$varType>::from_str(src) } } )*) diff --git a/src/vector.rs b/src/vector.rs index 943c2f6..a47c322 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -47,6 +47,12 @@ pub trait Vector : Clone where T: Number } + // Property functions. + /// Returns the size, how many components the Vector + /// has, of this Vector. + fn get_size(&self) -> u8; + + // Scalar operations that change this Vector. /// Add a scalar value to this Vector. /// @@ -511,7 +517,7 @@ macro_rules! binary_operator_impl /// that implements the Vector trait. macro_rules! define_vector { - ($structName: ident <$T: ident> {$($field: ident),+}) => + ($structName: ident <$T: ident> {$($field: ident),+}, $field_count: expr) => { // Do not add a where clause here or implementing // generic traits were the generic is different @@ -558,6 +564,11 @@ macro_rules! define_vector $structName {$($field: val),+} } + fn get_size(&self) -> u8 + { + $field_count + } + fn add_scalar(&mut self, scalar: T) { *self = &*self + scalar; @@ -665,9 +676,9 @@ macro_rules! define_vector // Define the Vector2, Vector3, and Vector4 structures. -define_vector!(Vector2 {x, y}); -define_vector!(Vector3 {x, y, z}); -define_vector!(Vector4 {x, y, z, w}); +define_vector!(Vector2 {x, y}, 2u8); +define_vector!(Vector3 {x, y, z}, 3u8); +define_vector!(Vector4 {x, y, z, w}, 4u8); // Implements operations specific to the different