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:
parent
04e744fb0f
commit
9306a73f9a
@ -22,7 +22,7 @@ use super::number::Number;
|
||||
/// [10]: https://doc.rust-lang.org/std/primitive.i64.html
|
||||
/// [11]: https://doc.rust-lang.org/std/primitive.isize.html
|
||||
/// [12]: https://doc.rust-lang.org/std/index.html
|
||||
pub trait Integer : Number
|
||||
pub trait Integer: Number
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -2,11 +2,12 @@
|
||||
//! License: Proprietary
|
||||
//!
|
||||
//!
|
||||
#![feature(zero_one)]
|
||||
#![feature(float_extras)]
|
||||
#![feature(associated_consts)]
|
||||
|
||||
|
||||
mod zero;
|
||||
mod one;
|
||||
mod bounded;
|
||||
mod number;
|
||||
mod whole;
|
||||
@ -20,6 +21,8 @@ pub mod matrix;
|
||||
pub mod quaternion;
|
||||
|
||||
|
||||
pub use self::zero::Zero;
|
||||
pub use self::one::One;
|
||||
pub use self::number::Number;
|
||||
pub use self::whole::Whole;
|
||||
pub use self::integer::Integer;
|
||||
|
@ -1,9 +1,10 @@
|
||||
//! 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 super::number::Number;
|
||||
use super::vector::{Vector, Vector2, Vector3, Vector4};
|
||||
use ::zero::Zero;
|
||||
use ::one::One;
|
||||
use ::number::Number;
|
||||
use ::vector::{Vector, Vector2, Vector3, Vector4};
|
||||
|
||||
|
||||
/// A trait that defines the minimum set of
|
||||
|
@ -1,17 +1,18 @@
|
||||
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;
|
||||
use ::zero::Zero;
|
||||
use ::one::One;
|
||||
use ::bounded::Bounded;
|
||||
|
||||
|
||||
/// A trait that defines what is required to be considered
|
||||
/// a number.
|
||||
pub trait Number : Zero + One + Add<Output=Self> + Sub<Output=Self> +
|
||||
Mul<Output=Self> + Div<Output=Self> + Rem<Output=Self> +
|
||||
PartialEq + Copy + Clone
|
||||
pub trait Number: Zero + One + Add<Output=Self> + Sub<Output=Self> +
|
||||
Mul<Output=Self> + Div<Output=Self> + Rem<Output=Self> +
|
||||
PartialEq + Copy + Clone
|
||||
{
|
||||
type StrRadixError;
|
||||
|
||||
|
54
src/one.rs
Normal file
54
src/one.rs
Normal 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);
|
@ -3,11 +3,12 @@
|
||||
//!
|
||||
//! [1]: https://en.wikipedia.org/wiki/Quaternion
|
||||
//! [2]: https://en.wikipedia.org/wiki/Dual_quaternion
|
||||
use std::num::{Zero, One};
|
||||
use std::ops::{Add, Sub, Mul, Div, Neg};
|
||||
|
||||
use super::real::Real;
|
||||
use super::vector::{Vector, EuclideanVector, Vector3};
|
||||
use ::zero::Zero;
|
||||
use ::one::One;
|
||||
use ::real::Real;
|
||||
use ::vector::{Vector, EuclideanVector, Vector3};
|
||||
|
||||
|
||||
/// A Quaternion is a combination of a scalar and a vector
|
||||
|
@ -1,12 +1,13 @@
|
||||
//! This module defines 2, 3, and 4 component [Vector][1] structures.
|
||||
//!
|
||||
//! [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 super::number::Number;
|
||||
use super::real::Real;
|
||||
use super::trig::{acos, atan2};
|
||||
use ::zero::Zero;
|
||||
use ::one::One;
|
||||
use ::number::Number;
|
||||
use ::real::Real;
|
||||
use ::trig::{acos, atan2};
|
||||
|
||||
|
||||
/// A trait that defines the minimum set of
|
||||
|
@ -16,7 +16,7 @@ use super::number::Number;
|
||||
/// [5]: https://doc.rust-lang.org/std/primitive.u64.html
|
||||
/// [6]: https://doc.rust-lang.org/std/primitive.usize.html
|
||||
/// [7]: https://doc.rust-lang.org/std/index.html
|
||||
pub trait Whole : Number
|
||||
pub trait Whole: Number
|
||||
{
|
||||
}
|
||||
|
||||
|
54
src/zero.rs
Normal file
54
src/zero.rs
Normal 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);
|
Loading…
x
Reference in New Issue
Block a user