cgmath/src/num.rs

118 lines
2.8 KiB
Rust
Raw Normal View History

2014-05-26 17:10:04 +00:00
// Copyright 2013-2014 The CGMath Developers. For a full listing of the authors,
2015-03-14 02:49:46 +00:00
// refer to the Cargo.toml file at the top-level directory of this distribution.
2014-05-26 17:10:04 +00:00
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use approx::ApproxEq;
use std::cmp;
use std::fmt;
2015-04-05 01:32:12 +00:00
use std::num::NumCast;
2014-05-26 17:10:04 +00:00
2015-04-05 01:32:12 +00:00
use rust_num::{Float, Num};
2015-04-05 01:19:11 +00:00
/// A trait providing a [partial ordering](http://mathworld.wolfram.com/PartialOrder.html).
2014-05-26 17:10:04 +00:00
pub trait PartialOrd {
fn partial_min(self, other: Self) -> Self;
fn partial_max(self, other: Self) -> Self;
}
macro_rules! partial_ord_int (
($T:ident) => (
impl PartialOrd for $T {
fn partial_min(self, other: $T) -> $T { cmp::min(self, other) }
fn partial_max(self, other: $T) -> $T { cmp::max(self, other) }
}
)
2014-12-19 17:48:59 +00:00
);
2015-01-09 23:16:39 +00:00
partial_ord_int!(isize);
2014-12-19 17:48:59 +00:00
partial_ord_int!(i8);
partial_ord_int!(i16);
partial_ord_int!(i32);
partial_ord_int!(i64);
2015-01-09 23:16:39 +00:00
partial_ord_int!(usize);
2014-12-19 17:48:59 +00:00
partial_ord_int!(u8);
partial_ord_int!(u16);
partial_ord_int!(u32);
partial_ord_int!(u64);
2014-05-26 17:10:04 +00:00
macro_rules! partial_ord_float (
($T:ident) => (
impl PartialOrd for $T {
fn partial_min(self, other: $T) -> $T { self.min(other) }
fn partial_max(self, other: $T) -> $T { self.max(other) }
}
)
2014-12-19 17:48:59 +00:00
);
2014-05-26 17:10:04 +00:00
2014-12-19 17:48:59 +00:00
partial_ord_float!(f32);
partial_ord_float!(f64);
2014-05-26 17:10:04 +00:00
2014-05-26 17:10:04 +00:00
/// Base numeric types with partial ordering
pub trait BaseNum:
2015-04-05 01:19:11 +00:00
Copy + NumCast + Clone + Num
+ PartialOrd + cmp::PartialOrd + fmt::Debug
+ 'static
{}
macro_rules! impl_basenum_int (
($T: ident) => (
impl BaseNum for $T {}
)
2014-12-19 17:48:59 +00:00
);
2014-12-19 17:48:59 +00:00
impl_basenum_int!(i8);
impl_basenum_int!(i16);
impl_basenum_int!(i32);
impl_basenum_int!(i64);
impl_basenum_int!(u8);
impl_basenum_int!(u16);
impl_basenum_int!(u32);
impl_basenum_int!(u64);
2015-01-09 23:16:39 +00:00
impl_basenum_int!(isize);
impl_basenum_int!(usize);
macro_rules! impl_basenum_float (
($T: ident) => (
impl BaseNum for $T {}
)
2014-12-19 17:48:59 +00:00
);
2014-12-19 17:48:59 +00:00
impl_basenum_float!(f32);
impl_basenum_float!(f64);
2014-05-26 17:10:04 +00:00
/// Base integer types
2015-04-05 01:19:11 +00:00
pub trait BaseInt : BaseNum {}
2014-05-26 17:10:04 +00:00
impl BaseInt for i8 {}
impl BaseInt for i16 {}
impl BaseInt for i32 {}
impl BaseInt for i64 {}
2015-01-09 23:16:39 +00:00
impl BaseInt for isize {}
2014-05-26 17:10:04 +00:00
impl BaseInt for u8 {}
impl BaseInt for u16 {}
impl BaseInt for u32 {}
impl BaseInt for u64 {}
2015-01-09 23:16:39 +00:00
impl BaseInt for usize {}
2014-05-26 17:10:04 +00:00
/// Base floating point types
2015-01-07 22:34:42 +00:00
pub trait BaseFloat : BaseNum + Float + ApproxEq<Self> {}
2014-05-26 17:10:04 +00:00
impl BaseFloat for f32 {}
impl BaseFloat for f64 {}