Added the Zero and One traits to sigils.

It looks like these may be removed from the standard library soon.
This commit is contained in:
Jason Travis Smith 2016-01-06 05:59:38 -05:00
parent 04e744fb0f
commit 9306a73f9a
9 changed files with 133 additions and 18 deletions

View File

@ -22,7 +22,7 @@ use super::number::Number;
/// [10]: https://doc.rust-lang.org/std/primitive.i64.html /// [10]: https://doc.rust-lang.org/std/primitive.i64.html
/// [11]: https://doc.rust-lang.org/std/primitive.isize.html /// [11]: https://doc.rust-lang.org/std/primitive.isize.html
/// [12]: https://doc.rust-lang.org/std/index.html /// [12]: https://doc.rust-lang.org/std/index.html
pub trait Integer : Number pub trait Integer: Number
{ {
} }

View File

@ -2,11 +2,12 @@
//! License: Proprietary //! License: Proprietary
//! //!
//! //!
#![feature(zero_one)]
#![feature(float_extras)] #![feature(float_extras)]
#![feature(associated_consts)] #![feature(associated_consts)]
mod zero;
mod one;
mod bounded; mod bounded;
mod number; mod number;
mod whole; mod whole;
@ -20,6 +21,8 @@ pub mod matrix;
pub mod quaternion; pub mod quaternion;
pub use self::zero::Zero;
pub use self::one::One;
pub use self::number::Number; pub use self::number::Number;
pub use self::whole::Whole; pub use self::whole::Whole;
pub use self::integer::Integer; pub use self::integer::Integer;

View File

@ -1,9 +1,10 @@
//! This module defines the 2x2, 3x3, and 4x4 Matrix structures. //! This module defines the 2x2, 3x3, and 4x4 Matrix structures.
use std::num::{Zero, One};
use std::ops::{Add, Sub, Mul, Div, Rem, Neg}; use std::ops::{Add, Sub, Mul, Div, Rem, Neg};
use super::number::Number; use ::zero::Zero;
use super::vector::{Vector, Vector2, Vector3, Vector4}; use ::one::One;
use ::number::Number;
use ::vector::{Vector, Vector2, Vector3, Vector4};
/// A trait that defines the minimum set of /// A trait that defines the minimum set of

View File

@ -1,17 +1,18 @@
use std::cmp::PartialEq; use std::cmp::PartialEq;
use std::mem::size_of; use std::mem::size_of;
use std::num::{Zero, One};
use std::ops::{Add, Sub, Mul, Div, Rem}; use std::ops::{Add, Sub, Mul, Div, Rem};
use std::str::FromStr; use std::str::FromStr;
use super::bounded::Bounded; use ::zero::Zero;
use ::one::One;
use ::bounded::Bounded;
/// A trait that defines what is required to be considered /// A trait that defines what is required to be considered
/// a number. /// a number.
pub trait Number : Zero + One + Add<Output=Self> + Sub<Output=Self> + pub trait Number: Zero + One + Add<Output=Self> + Sub<Output=Self> +
Mul<Output=Self> + Div<Output=Self> + Rem<Output=Self> + Mul<Output=Self> + Div<Output=Self> + Rem<Output=Self> +
PartialEq + Copy + Clone PartialEq + Copy + Clone
{ {
type StrRadixError; type StrRadixError;

54
src/one.rs Normal file
View File

@ -0,0 +1,54 @@
use std::ops::Mul;
/// Defines a multiplicative identity element for `Self`.
pub trait One: Sized + Mul<Self, Output=Self>
{
/// Returns the multiplicative identity element of `Self`, `1`.
///
/// # Laws
///
/// ```{.text}
/// a * 1 = a ∀ a ∈ Self
/// 1 * a = a ∀ a ∈ Self
/// ```
///
/// # Purity
///
/// This function should return the same result at all times regardless of
/// external mutable state, for example values stored in TLS or in
/// `static mut`s.
fn one() -> Self;
}
macro_rules! one_impl
{
($varType: ty, $val: expr) =>
{
impl One for $varType
{
fn one() -> $varType
{
$val
}
}
}
}
one_impl!(u8, 1u8);
one_impl!(u16, 1u16);
one_impl!(u32, 1u32);
one_impl!(u64, 1u64);
one_impl!(usize, 1usize);
one_impl!(i8, 1i8);
one_impl!(i16, 1i16);
one_impl!(i32, 1i32);
one_impl!(i64, 1i64);
one_impl!(isize, 1isize);
one_impl!(f32, 1.0f32);
one_impl!(f64, 1.0f64);

View File

@ -3,11 +3,12 @@
//! //!
//! [1]: https://en.wikipedia.org/wiki/Quaternion //! [1]: https://en.wikipedia.org/wiki/Quaternion
//! [2]: https://en.wikipedia.org/wiki/Dual_quaternion //! [2]: https://en.wikipedia.org/wiki/Dual_quaternion
use std::num::{Zero, One};
use std::ops::{Add, Sub, Mul, Div, Neg}; use std::ops::{Add, Sub, Mul, Div, Neg};
use super::real::Real; use ::zero::Zero;
use super::vector::{Vector, EuclideanVector, Vector3}; use ::one::One;
use ::real::Real;
use ::vector::{Vector, EuclideanVector, Vector3};
/// A Quaternion is a combination of a scalar and a vector /// A Quaternion is a combination of a scalar and a vector

View File

@ -1,12 +1,13 @@
//! This module defines 2, 3, and 4 component [Vector][1] structures. //! This module defines 2, 3, and 4 component [Vector][1] structures.
//! //!
//! [1]: https://en.wikipedia.org/wiki/Vector_(mathematics_and_physics) //! [1]: https://en.wikipedia.org/wiki/Vector_(mathematics_and_physics)
use std::num::{Zero, One};
use std::ops::{Add, Sub, Mul, Div, Rem, Neg}; use std::ops::{Add, Sub, Mul, Div, Rem, Neg};
use super::number::Number; use ::zero::Zero;
use super::real::Real; use ::one::One;
use super::trig::{acos, atan2}; use ::number::Number;
use ::real::Real;
use ::trig::{acos, atan2};
/// A trait that defines the minimum set of /// A trait that defines the minimum set of

View File

@ -16,7 +16,7 @@ use super::number::Number;
/// [5]: https://doc.rust-lang.org/std/primitive.u64.html /// [5]: https://doc.rust-lang.org/std/primitive.u64.html
/// [6]: https://doc.rust-lang.org/std/primitive.usize.html /// [6]: https://doc.rust-lang.org/std/primitive.usize.html
/// [7]: https://doc.rust-lang.org/std/index.html /// [7]: https://doc.rust-lang.org/std/index.html
pub trait Whole : Number pub trait Whole: Number
{ {
} }

54
src/zero.rs Normal file
View File

@ -0,0 +1,54 @@
use std::ops::Add;
/// Defines an additive identity element for `Self`.
pub trait Zero: Sized + Add<Self, Output=Self>
{
/// Returns the additive identity element of `Self`, `0`.
///
/// # Laws
///
/// ```{.text}
/// a + 0 = a ∀ a ∈ Self
/// 0 + a = a ∀ a ∈ Self
/// ```
///
/// # Purity
///
/// This function should return the same result at all times regardless of
/// external mutable state, for example values stored in TLS or in
/// `static mut`s.
fn zero() -> Self;
}
macro_rules! zero_impl
{
($varType: ty, $val: expr) =>
{
impl Zero for $varType
{
fn zero() -> $varType
{
$val
}
}
}
}
zero_impl!(u8, 0u8);
zero_impl!(u16, 0u16);
zero_impl!(u32, 0u32);
zero_impl!(u64, 0u64);
zero_impl!(usize, 0usize);
zero_impl!(i8, 0i8);
zero_impl!(i16, 0i16);
zero_impl!(i32, 0i32);
zero_impl!(i64, 0i64);
zero_impl!(isize, 0isize);
zero_impl!(f32, 0.0f32);
zero_impl!(f64, 0.0f64);