From c283c9d1e72f88dcc904db4bb4add111fc10320a Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Mon, 21 Oct 2013 11:30:30 +1100 Subject: [PATCH] Add trait for accessing the pointer to the first element of a type --- src/cgmath/lib.rs | 2 ++ src/cgmath/ptr.rs | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 src/cgmath/ptr.rs diff --git a/src/cgmath/lib.rs b/src/cgmath/lib.rs index 7842085..29f05db 100644 --- a/src/cgmath/lib.rs +++ b/src/cgmath/lib.rs @@ -44,3 +44,5 @@ pub mod frustum; pub mod intersect; pub mod obb; pub mod sphere; + +pub mod ptr; diff --git a/src/cgmath/ptr.rs b/src/cgmath/ptr.rs new file mode 100644 index 0000000..2810103 --- /dev/null +++ b/src/cgmath/ptr.rs @@ -0,0 +1,38 @@ +// Copyright 2013 The CGMath Developers. For a full listing of the authors, +// refer to the AUTHORS file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use array::Array; +use matrix::{Mat2, Mat3, Mat4}; +use point::{Point2, Point3}; +use vector::{Vec2, Vec3, Vec4}; + +pub trait Ptr { + fn ptr<'a>(&'a self) -> &'a T; +} + +impl Ptr for Vec2 { #[inline] fn ptr<'a>(&'a self) -> &'a S { self.i(0) } } +impl Ptr for Vec3 { #[inline] fn ptr<'a>(&'a self) -> &'a S { self.i(0) } } +impl Ptr for Vec4 { #[inline] fn ptr<'a>(&'a self) -> &'a S { self.i(0) } } + +impl Ptr for Point2 { #[inline] fn ptr<'a>(&'a self) -> &'a S { self.i(0) } } +impl Ptr for Point3 { #[inline] fn ptr<'a>(&'a self) -> &'a S { self.i(0) } } + +impl Ptr for Mat2 { #[inline] fn ptr<'a>(&'a self) -> &'a S { self.i(0).i(0) } } +impl Ptr for Mat3 { #[inline] fn ptr<'a>(&'a self) -> &'a S { self.i(0).i(0) } } +impl Ptr for Mat4 { #[inline] fn ptr<'a>(&'a self) -> &'a S { self.i(0).i(0) } } + +impl<'self, T, P: Ptr> Ptr for &'self [P] { + #[inline] fn ptr<'a>(&'a self) -> &'a T { self[0].ptr() } +}