From df3ce2935c5135b6253fe6caeb85a8f1f7a1daa8 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Thu, 25 Jul 2013 12:52:25 +1000 Subject: [PATCH] Add general `Dimensioned` and `SwapComponents` impls --- src/math/macros.rs | 25 ++++++++++++++++ src/math/math.rs | 72 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/src/math/macros.rs b/src/math/macros.rs index 7794107..149044c 100644 --- a/src/math/macros.rs +++ b/src/math/macros.rs @@ -40,6 +40,31 @@ macro_rules! impl_dimensioned( unsafe { transmute(self) } } } + ); + ($Self:ident) => ( + impl Dimensioned<$Self,[$Self,..1]> for $Self { + #[inline] + pub fn i<'a>(&'a self, i: uint) -> &'a $Self { + &'a self.as_slice()[i] + } + + #[inline] + pub fn mut_i<'a>(&'a mut self, i: uint) -> &'a mut $Self { + &'a mut self.as_mut_slice()[i] + } + + #[inline] + pub fn as_slice<'a>(&'a self) -> &'a [$Self,..1] { + use std::cast::transmute; + unsafe { transmute(self) } + } + + #[inline] + pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [$Self,..1] { + use std::cast::transmute; + unsafe { transmute(self) } + } + } ) ) diff --git a/src/math/math.rs b/src/math/math.rs index 2be88fa..6a5c18c 100644 --- a/src/math/math.rs +++ b/src/math/math.rs @@ -50,3 +50,75 @@ pub trait Dimensioned { pub trait SwapComponents { pub fn swap(&mut self, a: uint, b: uint); } + +// `Dimensioned` impls for primitive numeric types + +impl_dimensioned!(u8) +impl_dimensioned!(u16) +impl_dimensioned!(u32) +impl_dimensioned!(u64) +impl_dimensioned!(uint) +impl_dimensioned!(i8) +impl_dimensioned!(i16) +impl_dimensioned!(i32) +impl_dimensioned!(i64) +impl_dimensioned!(int) +impl_dimensioned!(f32) +impl_dimensioned!(f64) +impl_dimensioned!(float) + +// Helper type aliases for implementing `Dimendioned` and `SwapComponents` +// for tuples. + +pub type Tuple1 = (T,); +pub type Tuple2 = (T,T); +pub type Tuple3 = (T,T,T); +pub type Tuple4 = (T,T,T,T); +pub type Tuple5 = (T,T,T,T,T); +pub type Tuple6 = (T,T,T,T,T,T); + +// `Dimensioned` impls for tuples + +impl_dimensioned!(Tuple1, T, 1) +impl_dimensioned!(Tuple2, T, 2) +impl_dimensioned!(Tuple3, T, 3) +impl_dimensioned!(Tuple4, T, 4) +impl_dimensioned!(Tuple5, T, 5) +impl_dimensioned!(Tuple6, T, 6) + +// `SwapComponents` impls for tuples + +impl_swap_components!(Tuple1) +impl_swap_components!(Tuple2) +impl_swap_components!(Tuple3) +impl_swap_components!(Tuple4) +impl_swap_components!(Tuple5) +impl_swap_components!(Tuple6) + +// Helper type aliases for implementing `Dimendioned` and `SwapComponents` +// for fixed length vectors. + +pub type Fixed1 = [T, ..1]; +pub type Fixed2 = [T, ..2]; +pub type Fixed3 = [T, ..3]; +pub type Fixed4 = [T, ..4]; +pub type Fixed5 = [T, ..5]; +pub type Fixed6 = [T, ..6]; + +// `Dimensioned` impls for fixed length vectors + +impl_dimensioned!(Fixed1, T, 1) +impl_dimensioned!(Fixed2, T, 2) +impl_dimensioned!(Fixed3, T, 3) +impl_dimensioned!(Fixed4, T, 4) +impl_dimensioned!(Fixed5, T, 5) +impl_dimensioned!(Fixed6, T, 6) + +// `SwapComponents` impls for fixed length vectors + +impl_swap_components!(Fixed1) +impl_swap_components!(Fixed2) +impl_swap_components!(Fixed3) +impl_swap_components!(Fixed4) +impl_swap_components!(Fixed5) +impl_swap_components!(Fixed6)