diff --git a/src/matrix.rs b/src/matrix.rs index b1a4dba..4a9d07a 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -1142,30 +1142,42 @@ impl ApproxEq for Matrix4 { } macro_rules! index_operators { - ($MatrixN:ident <$S:ident>, $VectorN: ident, $n:expr) => { - impl<$S> Index for $MatrixN<$S> { - type Output = $VectorN<$S>; + ($MatrixN:ident<$S:ident>, $n:expr, $Output:ty, $I:ty) => { + impl<$S> Index<$I> for $MatrixN<$S> { + type Output = $Output; #[inline] - fn index<'a>(&'a self, i: usize) -> &'a $VectorN<$S> { + fn index<'a>(&'a self, i: $I) -> &'a $Output { let v: &[[$S; $n]; $n] = self.as_ref(); From::from(&v[i]) } } - impl<$S> IndexMut for $MatrixN<$S> { + impl<$S> IndexMut<$I> for $MatrixN<$S> { #[inline] - fn index_mut<'a>(&'a mut self, i: usize) -> &'a mut $VectorN<$S> { + fn index_mut<'a>(&'a mut self, i: $I) -> &'a mut $Output { let v: &mut [[$S; $n]; $n] = self.as_mut(); From::from(&mut v[i]) } } - }; + } } -index_operators!(Matrix2, Vector2, 2); -index_operators!(Matrix3, Vector3, 3); -index_operators!(Matrix4, Vector4, 4); +index_operators!(Matrix2, 2, Vector2, usize); +index_operators!(Matrix3, 3, Vector3, usize); +index_operators!(Matrix4, 4, Vector4, usize); +// index_operators!(Matrix2, 2, [Vector2], Range); +// index_operators!(Matrix3, 3, [Vector3], Range); +// index_operators!(Matrix4, 4, [Vector4], Range); +// index_operators!(Matrix2, 2, [Vector2], RangeTo); +// index_operators!(Matrix3, 3, [Vector3], RangeTo); +// index_operators!(Matrix4, 4, [Vector4], RangeTo); +// index_operators!(Matrix2, 2, [Vector2], RangeFrom); +// index_operators!(Matrix3, 3, [Vector3], RangeFrom); +// index_operators!(Matrix4, 4, [Vector4], RangeFrom); +// index_operators!(Matrix2, 2, [Vector2], RangeFull); +// index_operators!(Matrix3, 3, [Vector3], RangeFull); +// index_operators!(Matrix4, 4, [Vector4], RangeFull); macro_rules! fixed_array_conversions { ($MatrixN:ident <$S:ident> { $($field:ident : $index:expr),+ }, $n:expr) => { diff --git a/src/point.rs b/src/point.rs index dd09f76..55b2c5d 100644 --- a/src/point.rs +++ b/src/point.rs @@ -443,27 +443,35 @@ tuple_conversions!(Point2 { x, y }, (S, S)); tuple_conversions!(Point3 { x, y, z }, (S, S, S)); macro_rules! index_operators { - ($PointN:ident <$S:ident>, $n:expr) => { - impl<$S> Index for $PointN<$S> { - type Output = $S; + ($PointN:ident<$S:ident>, $n:expr, $Output:ty, $I:ty) => { + impl<$S> Index<$I> for $PointN<$S> { + type Output = $Output; #[inline] - fn index<'a>(&'a self, i: usize) -> &'a $S { + fn index<'a>(&'a self, i: $I) -> &'a $Output { let v: &[$S; $n] = self.as_ref(); &v[i] } } - impl<$S> IndexMut for $PointN<$S> { + impl<$S> IndexMut<$I> for $PointN<$S> { #[inline] - fn index_mut<'a>(&'a mut self, i: usize) -> &'a mut $S { + fn index_mut<'a>(&'a mut self, i: $I) -> &'a mut $Output { let v: &mut [$S; $n] = self.as_mut(); &mut v[i] } } } } -index_operators!(Point2, 2); -index_operators!(Point3, 3); +index_operators!(Point2, 2, S, usize); +index_operators!(Point3, 3, S, usize); +index_operators!(Point2, 2, [S], Range); +index_operators!(Point3, 3, [S], Range); +index_operators!(Point2, 2, [S], RangeTo); +index_operators!(Point3, 3, [S], RangeTo); +index_operators!(Point2, 2, [S], RangeFrom); +index_operators!(Point3, 3, [S], RangeFrom); +index_operators!(Point2, 2, [S], RangeFull); +index_operators!(Point3, 3, [S], RangeFull); impl fmt::Debug for Point2 { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { diff --git a/src/quaternion.rs b/src/quaternion.rs index 9f95d71..9db02e6 100644 --- a/src/quaternion.rs +++ b/src/quaternion.rs @@ -503,21 +503,31 @@ impl<'a, S: BaseFloat> From<&'a mut (S, S, S, S)> for &'a mut Quaternion { } } -impl Index for Quaternion { - type Output = S; +macro_rules! index_operators { + ($S:ident, $Output:ty, $I:ty) => { + impl<$S: BaseFloat> Index<$I> for Quaternion<$S> { + type Output = $Output; - #[inline] - fn index<'a>(&'a self, i: usize) -> &'a S { - let v: &[S; 4] = self.as_ref(); &v[i] + #[inline] + fn index<'a>(&'a self, i: $I) -> &'a $Output { + let v: &[$S; 4] = self.as_ref(); &v[i] + } + } + + impl<$S: BaseFloat> IndexMut<$I> for Quaternion<$S> { + #[inline] + fn index_mut<'a>(&'a mut self, i: $I) -> &'a mut $Output { + let v: &mut [$S; 4] = self.as_mut(); &mut v[i] + } + } } } -impl IndexMut for Quaternion { - #[inline] - fn index_mut<'a>(&'a mut self, i: usize) -> &'a mut S { - let v: &mut [S; 4] = self.as_mut(); &mut v[i] - } -} +index_operators!(S, S, usize); +index_operators!(S, [S], Range); +index_operators!(S, [S], RangeTo); +index_operators!(S, [S], RangeFrom); +index_operators!(S, [S], RangeFull); impl Rand for Quaternion { #[inline] diff --git a/src/vector.rs b/src/vector.rs index 8d3708c..8015ab5 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -449,28 +449,40 @@ tuple_conversions!(Vector3 { x, y, z }, (S, S, S)); tuple_conversions!(Vector4 { x, y, z, w }, (S, S, S, S)); macro_rules! index_operators { - ($Self_:ident <$S:ident>, $n:expr) => { - impl<$S> Index for $Self_<$S> { - type Output = $S; + ($VectorN:ident<$S:ident>, $n:expr, $Output:ty, $I:ty) => { + impl<$S> Index<$I> for $VectorN<$S> { + type Output = $Output; #[inline] - fn index<'a>(&'a self, i: usize) -> &'a $S { + fn index<'a>(&'a self, i: $I) -> &'a $Output { let v: &[$S; $n] = self.as_ref(); &v[i] } } - impl<$S> IndexMut for $Self_<$S> { + impl<$S> IndexMut<$I> for $VectorN<$S> { #[inline] - fn index_mut<'a>(&'a mut self, i: usize) -> &'a mut $S { + fn index_mut<'a>(&'a mut self, i: $I) -> &'a mut $Output { let v: &mut [$S; $n] = self.as_mut(); &mut v[i] } } } } -index_operators!(Vector2, 2); -index_operators!(Vector3, 3); -index_operators!(Vector4, 4); +index_operators!(Vector2, 2, S, usize); +index_operators!(Vector3, 3, S, usize); +index_operators!(Vector4, 4, S, usize); +index_operators!(Vector2, 2, [S], Range); +index_operators!(Vector3, 3, [S], Range); +index_operators!(Vector4, 4, [S], Range); +index_operators!(Vector2, 2, [S], RangeTo); +index_operators!(Vector3, 3, [S], RangeTo); +index_operators!(Vector4, 4, [S], RangeTo); +index_operators!(Vector2, 2, [S], RangeFrom); +index_operators!(Vector3, 3, [S], RangeFrom); +index_operators!(Vector4, 4, [S], RangeFrom); +index_operators!(Vector2, 2, [S], RangeFull); +index_operators!(Vector3, 3, [S], RangeFull); +index_operators!(Vector4, 4, [S], RangeFull); /// Operations specific to numeric two-dimensional vectors. impl Vector2 {