Associated constants are now in the language proper.
The feature gate is no longer required and the constants can be used.
This commit is contained in:
parent
3725a5d9d0
commit
1250e46066
2
.gitignore
vendored
2
.gitignore
vendored
@ -14,4 +14,4 @@
|
||||
# Remove Cargo.lock from gitignore if creating an executable,
|
||||
# leave it for libraries.
|
||||
# More information here: http://doc.crates.io/guide.html#cargotoml-vs-cargolock
|
||||
#Cargo.lock
|
||||
Cargo.lock
|
||||
|
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -9,7 +9,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "binding"
|
||||
version = "0.1.0"
|
||||
source = "git+ssh://git@gitlab.com/CyberMages/Core/binding#8b5d5a9c57a7f87b26bb741067babb7a0b765ae7"
|
||||
source = "git+ssh://git@gitlab.com/CyberMages/Core/binding#d408d4b239e5da791507f1624b9e98b74eaba75e"
|
||||
dependencies = [
|
||||
"scribe 0.5.0 (git+ssh://git@gitlab.com/CyberMages/Core/scribe.git)",
|
||||
]
|
||||
|
105
src/bounded.rs
105
src/bounded.rs
@ -4,19 +4,11 @@ use std::{u8, u16, u32, u64, usize, i8, i16, i32, i64, isize, f32, f64};
|
||||
/// Primitive types that have upper and lower bounds.
|
||||
pub trait Bounded
|
||||
{
|
||||
// TODO: I would love for this to be associated consts, but
|
||||
// they are not currently working inside macros.
|
||||
// It causes rust's stack to explode.
|
||||
// Check this on later versions of rustc.
|
||||
//
|
||||
// Last version checked: rustc v1.3.0
|
||||
/// The minimum value for this type.
|
||||
//const MIN: Self;
|
||||
fn min_value() -> Self;
|
||||
const MIN: Self;
|
||||
|
||||
/// The maximum value for this type.
|
||||
//const MAX: Self;
|
||||
fn max_value() -> Self;
|
||||
const MAX: Self;
|
||||
}
|
||||
|
||||
|
||||
@ -28,34 +20,83 @@ macro_rules! bounded_trait_impl
|
||||
{
|
||||
impl Bounded for $T
|
||||
{
|
||||
//const MIN: $T = $minVal;
|
||||
fn min_value() -> $T
|
||||
{
|
||||
$minVal
|
||||
}
|
||||
const MIN: $T = $minVal;
|
||||
|
||||
//const MAX: $T = $maxVal;
|
||||
fn max_value() -> $T
|
||||
{
|
||||
$maxVal
|
||||
}
|
||||
const MAX: $T = $maxVal;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Implement the Bounded for all the primitive types.
|
||||
bounded_trait_impl!(u8, u8::MIN, u8::MAX);
|
||||
bounded_trait_impl!(u16, u16::MIN, u16::MAX);
|
||||
bounded_trait_impl!(u32, u32::MIN, u32::MAX);
|
||||
bounded_trait_impl!(u64, u64::MIN, u64::MAX);
|
||||
bounded_trait_impl!(usize, usize::MIN, usize::MAX);
|
||||
bounded_trait_impl!(u8, 0u8, !0u8);
|
||||
bounded_trait_impl!(u16, 0u16, !0u16);
|
||||
bounded_trait_impl!(u32, 0u32, !0u32);
|
||||
bounded_trait_impl!(u64, 0u64, !0u64);
|
||||
bounded_trait_impl!(usize, 0usize, !0usize);
|
||||
|
||||
bounded_trait_impl!(i8, i8::MIN, i8::MAX);
|
||||
bounded_trait_impl!(i16, i16::MIN, i16::MAX);
|
||||
bounded_trait_impl!(i32, i32::MIN, i32::MAX);
|
||||
bounded_trait_impl!(i64, i64::MIN, i64::MAX);
|
||||
bounded_trait_impl!(isize, isize::MIN, isize::MAX);
|
||||
bounded_trait_impl!(i8, (!0i8 ^ (!0u8 >> 1u8) as i8),
|
||||
!(!0i8 ^ (!0u8 >> 1u8) as i8));
|
||||
bounded_trait_impl!(i16, (!0i16 ^ (!0u16 >> 1u16) as i16),
|
||||
!(!0i16 ^ (!0u16 >> 1u16) as i16));
|
||||
bounded_trait_impl!(i32, (!0i32 ^ (!0u32 >> 1u32) as i32),
|
||||
!(!0i32 ^ (!0u32 >> 1u32) as i32));
|
||||
bounded_trait_impl!(i64, (!0i64 ^ (!0u64 >> 1u64) as i64),
|
||||
!(!0i64 ^ (!0u64 >> 1u64) as i64));
|
||||
bounded_trait_impl!(isize, (!0isize ^ (!0usize >> 1usize) as isize),
|
||||
!(!0isize ^ (!0usize >> 1usize) as isize));
|
||||
|
||||
bounded_trait_impl!(f32, f32::MIN, f32::MAX);
|
||||
bounded_trait_impl!(f64, f64::MIN, f64::MAX);
|
||||
bounded_trait_impl!(f32, -3.40282347e+38f32, 3.40282347e+38f32);
|
||||
bounded_trait_impl!(f64, -1.7976931348623157e+308f64,
|
||||
1.7976931348623157e+308f64);
|
||||
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests
|
||||
{
|
||||
macro_rules! bounds_test
|
||||
{
|
||||
($T: ident, $func_name: ident, $minVal: expr, $maxVal: expr) =>
|
||||
{
|
||||
#[test]
|
||||
fn $func_name()
|
||||
{
|
||||
use bounded::Bounded;
|
||||
|
||||
|
||||
|
||||
assert_eq!($T::MIN, $minVal);
|
||||
assert_eq!($T::MAX, $maxVal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bounds_test!(u8, min_max_u8, 0u8, !0u8);
|
||||
bounds_test!(u16, min_max_u16, 0u16, !0u16);
|
||||
bounds_test!(u32, min_max_u32, 0u32, !0u32);
|
||||
bounds_test!(u64, min_max_u64, 0u64, !0u64);
|
||||
bounds_test!(usize, min_max_usize, 0usize, !0usize);
|
||||
|
||||
bounds_test!(i8, min_max_i8,
|
||||
-1i8 << (((::std::mem::size_of::<i8>() as i8)*8i8)-1i8),
|
||||
!(-1i8 << (((::std::mem::size_of::<i8>() as i8)*8i8)-1i8)));
|
||||
bounds_test!(i16, min_max_i16,
|
||||
-1i16 << (((::std::mem::size_of::<i16>() as i16)*8i16)-1i16),
|
||||
!(-1i16 << (((::std::mem::size_of::<i16>() as i16)*8i16)-1i16)));
|
||||
bounds_test!(i32, min_max_i32,
|
||||
-1i32 << (((::std::mem::size_of::<i32>() as i32)*8i32)-1i32),
|
||||
!(-1i32 << (((::std::mem::size_of::<i32>() as i32)*8i32)-1i32)));
|
||||
bounds_test!(i64, min_max_i64,
|
||||
-1i64 << (((::std::mem::size_of::<i64>() as i64)*8i64)-1i64),
|
||||
!(-1i64 << (((::std::mem::size_of::<i64>() as i64)*8i64)-1i64)));
|
||||
bounds_test!(isize, min_max_isize,
|
||||
-1isize << (((::std::mem::size_of::<isize>() as isize)*8isize)-1isize),
|
||||
!(-1isize << (((::std::mem::size_of::<isize>() as isize)*8isize)-1isize)));
|
||||
|
||||
bounds_test!(f32, min_max_f32,
|
||||
-3.40282347e+38f32, 3.40282347e+38f32);
|
||||
bounds_test!(f64, min_max_f64,
|
||||
-1.7976931348623157e+308f64,
|
||||
1.7976931348623157e+308f64);
|
||||
}
|
||||
|
@ -2,8 +2,8 @@
|
||||
//! License: Proprietary
|
||||
//!
|
||||
//!
|
||||
#![feature(float_extras)]
|
||||
#![feature(associated_consts)]
|
||||
//#![feature(float_extras)]
|
||||
//#![feature(associated_consts)]
|
||||
#![no_std]
|
||||
extern crate core as std;
|
||||
|
||||
@ -39,4 +39,5 @@ pub use self::whole::Whole;
|
||||
pub use self::integer::Integer;
|
||||
pub use self::real::Real;
|
||||
pub use self::constants::Constants;
|
||||
pub use self::bounded::Bounded;
|
||||
pub use self::trig::{Degree, Radian, Trig};
|
||||
|
@ -12,25 +12,26 @@ 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> +
|
||||
AddAssign<Self> + SubAssign<Self> + MulAssign<Self> +
|
||||
DivAssign<Self> + RemAssign<Self> + PartialEq + PartialOrd +
|
||||
Copy + Clone + Debug + Display
|
||||
pub trait Number: Zero + One + Add<Output=Self> + Sub<Output=Self> +
|
||||
Mul<Output=Self> + Div<Output=Self> + Rem<Output=Self> +
|
||||
AddAssign<Self> + SubAssign<Self> + MulAssign<Self> +
|
||||
DivAssign<Self> + RemAssign<Self> + PartialEq + PartialOrd +
|
||||
Copy + Clone + Debug + Display
|
||||
{
|
||||
type StrRadixError;
|
||||
|
||||
|
||||
|
||||
/// The smallest number that can be stored in this type.
|
||||
const MIN: Self;
|
||||
//const MIN: Self;
|
||||
|
||||
/// The largest number that can be stored in this type.
|
||||
const MAX: Self;
|
||||
//const MAX: Self;
|
||||
|
||||
|
||||
|
||||
///
|
||||
/*
|
||||
fn max(self, other: Self) -> Self
|
||||
{
|
||||
if self >= other
|
||||
@ -55,6 +56,7 @@ pub trait Number : Zero + One + Add<Output=Self> + Sub<Output=Self> +
|
||||
other
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
/// Create a number from a given string and base radix.
|
||||
///
|
||||
@ -94,7 +96,7 @@ pub trait ToNumber
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert this to an u16.
|
||||
/// Convert this to a u16.
|
||||
/// None is returned if the conversion is not possible.
|
||||
fn to_u16(&self) -> Option<u16>
|
||||
{
|
||||
@ -390,8 +392,8 @@ macro_rules! convert_int_to_int
|
||||
let max_value: $dstType;
|
||||
|
||||
num = $val as i64;
|
||||
min_value = Bounded::min_value();
|
||||
max_value = Bounded::max_value();
|
||||
min_value = Bounded::MIN;
|
||||
max_value = Bounded::MAX;
|
||||
|
||||
if (min_value as i64) <= num && num <= (max_value as i64)
|
||||
{
|
||||
@ -414,7 +416,7 @@ macro_rules! convert_int_to_uint
|
||||
let max_value: $dstType;
|
||||
|
||||
zero = Zero::zero();
|
||||
max_value = Bounded::max_value();
|
||||
max_value = Bounded::MAX;
|
||||
|
||||
if zero <= $val && ($val as u64) <= (max_value as u64)
|
||||
{
|
||||
@ -435,7 +437,7 @@ macro_rules! convert_uint_to_int
|
||||
{
|
||||
let max_value: $dstType;
|
||||
|
||||
max_value = Bounded::max_value();
|
||||
max_value = Bounded::MAX;
|
||||
if ($val as u64) <= (max_value as u64)
|
||||
{
|
||||
Some($val as $dstType)
|
||||
@ -462,7 +464,7 @@ macro_rules! convert_uint_to_uint
|
||||
let max_value: $dstType;
|
||||
|
||||
zero = Zero::zero();
|
||||
max_value = Bounded::max_value();
|
||||
max_value = Bounded::MAX;
|
||||
|
||||
if zero <= $val && ($val as u64) <= (max_value as u64)
|
||||
{
|
||||
@ -491,8 +493,8 @@ macro_rules! convert_float_to_float
|
||||
let max_value: $srcType;
|
||||
|
||||
num = $val as f64;
|
||||
min_value = Bounded::min_value();
|
||||
max_value = Bounded::max_value();
|
||||
min_value = Bounded::MIN;
|
||||
max_value = Bounded::MAX;
|
||||
|
||||
if (min_value as f64) <= num && num <= (max_value as f64)
|
||||
{
|
||||
@ -792,8 +794,8 @@ macro_rules! int_number_trait_impl
|
||||
|
||||
|
||||
|
||||
const MIN: Self = $min;
|
||||
const MAX: Self = $max;
|
||||
//const MIN: Self = $min;
|
||||
//const MAX: Self = $max;
|
||||
|
||||
|
||||
|
||||
@ -818,8 +820,8 @@ macro_rules! float_number_trait_impl
|
||||
|
||||
|
||||
|
||||
const MIN: Self = $min;
|
||||
const MAX: Self = $max;
|
||||
//const MIN: Self = $min;
|
||||
//const MAX: Self = $max;
|
||||
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user