From 969e57591d5a59f89d4c8d54912f2db1674be0fc Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Tue, 4 Dec 2012 17:50:15 +1000 Subject: [PATCH] Fix `to_ptr` method impls and use in the `index` methods where appropriate --- src/mat.rs | 18 +++++++++++++++--- src/quat.rs | 11 ++++++----- src/vec.rs | 33 ++++++++++++++++++--------------- 3 files changed, 39 insertions(+), 23 deletions(-) diff --git a/src/mat.rs b/src/mat.rs index 65e3cea..5681ea0 100644 --- a/src/mat.rs +++ b/src/mat.rs @@ -425,7 +425,11 @@ pub impl Mat2: Index> { pub impl Mat2: ToPtr { #[inline(always)] pure fn to_ptr(&self) -> *T { - self[0].to_ptr() + unsafe { + transmute::<*Mat2, *T>( + to_unsafe_ptr(&*self) + ) + } } } @@ -818,7 +822,11 @@ pub impl Mat3: Index> { pub impl Mat3: ToPtr { #[inline(always)] pure fn to_ptr(&self) -> *T { - self[0].to_ptr() + unsafe { + transmute::<*Mat3, *T>( + to_unsafe_ptr(&*self) + ) + } } } @@ -1270,7 +1278,11 @@ pub impl Mat4: Index> { pub impl Mat4: ToPtr { #[inline(always)] pure fn to_ptr(&self) -> *T { - self[0].to_ptr() + unsafe { + transmute::<*Mat4, *T>( + to_unsafe_ptr(&*self) + ) + } } } diff --git a/src/quat.rs b/src/quat.rs index c56dcbc..f14f7af 100644 --- a/src/quat.rs +++ b/src/quat.rs @@ -142,17 +142,18 @@ pub impl Quat: Dimensional { pub impl Quat: Index { #[inline(always)] pure fn index(i: uint) -> T { - unsafe { do buf_as_slice( - transmute::<*Quat, *T>( - to_unsafe_ptr(&self)), 4) |slice| { slice[i] } - } + unsafe { do buf_as_slice(self.to_ptr(), 4) |slice| { slice[i] } } } } pub impl Quat: ToPtr { #[inline(always)] pure fn to_ptr(&self) -> *T { - to_unsafe_ptr(&self[0]) + unsafe { + transmute::<*Quat, *T>( + to_unsafe_ptr(&*self) + ) + } } } diff --git a/src/vec.rs b/src/vec.rs index d12df1b..773d0b6 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -180,17 +180,18 @@ pub impl Vec2: Dimensional { pub impl Vec2: Index { #[inline(always)] pure fn index(i: uint) -> T { - unsafe { do buf_as_slice( - transmute::<*Vec2, *T>( - to_unsafe_ptr(&self)), 2) |slice| { slice[i] } - } + unsafe { do buf_as_slice(self.to_ptr(), 2) |slice| { slice[i] } } } } pub impl Vec2: ToPtr { #[inline(always)] pure fn to_ptr(&self) -> *T { - ptr::to_unsafe_ptr(&self[0]) + unsafe { + transmute::<*Vec2, *T>( + to_unsafe_ptr(&*self) + ) + } } } @@ -342,17 +343,18 @@ pub impl Vec3: Dimensional { pub impl Vec3: Index { #[inline(always)] pure fn index(i: uint) -> T { - unsafe { do buf_as_slice( - transmute::<*Vec3, *T>( - to_unsafe_ptr(&self)), 3) |slice| { slice[i] } - } + unsafe { do buf_as_slice(self.to_ptr(), 3) |slice| { slice[i] } } } } pub impl Vec3: ToPtr { #[inline(always)] pure fn to_ptr(&self) -> *T { - ptr::to_unsafe_ptr(&self[0]) + unsafe { + transmute::<*Vec3, *T>( + to_unsafe_ptr(&*self) + ) + } } } @@ -522,17 +524,18 @@ pub impl Vec4: Dimensional { pub impl Vec4: Index { #[inline(always)] pure fn index(i: uint) -> T { - unsafe { do buf_as_slice( - transmute::<*Vec4, *T>( - to_unsafe_ptr(&self)), 4) |slice| { slice[i] } - } + unsafe { do buf_as_slice(self.to_ptr(), 4) |slice| { slice[i] } } } } pub impl Vec4: ToPtr { #[inline(always)] pure fn to_ptr(&self) -> *T { - ptr::to_unsafe_ptr(&self[0]) + unsafe { + transmute::<*Vec4, *T>( + to_unsafe_ptr(&*self) + ) + } } }