Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View range_type.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
pub trait RangedNum<T>: Num {
pub fn min() -> Self;
pub fn max() -> Self;
pub fn new(x: T) -> Self;
pub fn set(&mut self, x: T);
pub fn get(&self) -> T;
pub fn normalize(&self) -> Self;
pub fn normalize_self(&mut self);
}
 
macro_rules! range_type(
($Type:ident ($T:ty) = $mn:expr .. $mx:expr) => (
// required because macros can't expand into multiple items :(
pub mod wrapper {
use super::RangedNum;
 
pub fn $Type(x: $T) -> $Type { $Type::new(x) }
 
pub struct $Type {
priv _priv: $T
}
 
impl $Type {
#[inline] pub fn min() -> $Type { $Type { _priv: $mn } }
#[inline] pub fn max() -> $Type { $Type { _priv: $mx } }
#[inline] pub fn new(x: $T) -> $Type { $Type { _priv: x }.normalize() }
}
 
impl RangedNum<$T> for $Type {
#[inline] pub fn min() -> $Type { $Type::min() }
#[inline] pub fn max() -> $Type { $Type::max() }
#[inline] pub fn new(x: $T) -> $Type { $Type::new(x) }
 
#[inline]
pub fn set(&mut self, x: $T) { *self = $Type::new(x) }
 
#[inline]
pub fn get(&self) -> $T { self._priv }
 
#[inline]
pub fn normalize(&self) -> $Type {
$Type {
_priv: self.get().clamp(&$Type::min().get(),
&$Type::max().get())
}
}
 
#[inline]
pub fn normalize_self(&mut self) {
*self = self.normalize()
}
}
 
impl Eq for $Type {
#[inline]
fn eq(&self, other: &$Type) -> bool { self.get() == other.get() }
#[inline]
fn ne(&self, other: &$Type) -> bool { self.get() != other.get() }
}
 
impl Ord for $Type {
#[inline]
fn lt(&self, other: &$Type) -> bool { self.get() < other.get() }
#[inline]
fn le(&self, other: &$Type) -> bool { self.get() <= other.get() }
#[inline]
fn ge(&self, other: &$Type) -> bool { self.get() >= other.get() }
#[inline]
fn gt(&self, other: &$Type) -> bool { self.get() > other.get() }
}
 
impl Clone for $Type {
#[inline]
fn clone(&self) -> $Type { $Type { _priv: self.get().clone() } }
}
 
impl Add<$Type, $Type> for $Type {
#[inline]
fn add(&self, other: &$Type) -> $Type {
$Type::new(self.get() + other.get())
}
}
 
impl Sub<$Type, $Type> for $Type {
#[inline]
fn sub(&self, other: &$Type) -> $Type {
$Type::new(self.get() - other.get())
}
}
 
impl Mul<$Type, $Type> for $Type {
#[inline]
fn mul(&self, other: &$Type) -> $Type {
$Type::new(self.get() * other.get())
}
}
 
impl Div<$Type, $Type> for $Type {
#[inline]
fn div(&self, other: &$Type) -> $Type {
$Type::new(self.get() + other.get())
}
}
 
impl Rem<$Type, $Type> for $Type {
#[inline]
fn rem(&self, other: &$Type) -> $Type {
$Type::new(self.get() % other.get())
}
}
 
impl ToStr for $Type {
fn to_str(&self) -> ~str {
fmt!("%?", self.get())
}
}
}
)
)
 
range_type!(Channelf64(float) = 0.0 .. 1.0)
 
fn main() {
use wrapper::*;
 
let x = Channelf64(-0.1);
let y = Channelf64(2.0);
let z = Channelf64(0.5);
 
println(x.to_str());
println(y.to_str());
println(z.to_str());
println((x + y).to_str());
println((x * y).to_str());
}

Can you try and get this into std? Perhaps in std::util? This seems useful for all sorts of stuff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.