diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d171e4..b85f91e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - Improves the `fmt::Debug` impls for `Vector`, `Matrix`, `Point`, `Decomposed`, `Quaternion` and `Angle` to make them easier to derive, and have clearer formatting. +- Marks vectors, points, matrices, and angles as `#[repr(C, packed)]`. ## [v0.7.0] - 2015-12-23 diff --git a/src/angle.rs b/src/angle.rs index 71c7bec..403a887 100644 --- a/src/angle.rs +++ b/src/angle.rs @@ -28,10 +28,16 @@ use rust_num::traits::cast; use approx::ApproxEq; use num::BaseFloat; -/// An angle, in radians +/// An angle, in radians. +/// +/// This type is marked as `#[repr(C, packed)]`. +#[repr(C, packed)] #[derive(Copy, Clone, PartialEq, PartialOrd, RustcEncodable, RustcDecodable)] pub struct Rad { pub s: S } -/// An angle, in degrees +/// An angle, in degrees. +/// +/// This type is marked as `#[repr(C, packed)]`. +#[repr(C, packed)] #[derive(Copy, Clone, PartialEq, PartialOrd, RustcEncodable, RustcDecodable)] pub struct Deg { pub s: S } diff --git a/src/matrix.rs b/src/matrix.rs index ca908f2..f364072 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -35,16 +35,37 @@ use vector::{Vector, EuclideanVector}; use vector::{Vector2, Vector3, Vector4}; /// A 2 x 2, column major matrix +/// +/// This type is marked as `#[repr(C, packed)]`. +#[repr(C, packed)] #[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)] -pub struct Matrix2 { pub x: Vector2, pub y: Vector2 } +pub struct Matrix2 { + pub x: Vector2, + pub y: Vector2, +} /// A 3 x 3, column major matrix +/// +/// This type is marked as `#[repr(C, packed)]`. +#[repr(C, packed)] #[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)] -pub struct Matrix3 { pub x: Vector3, pub y: Vector3, pub z: Vector3 } +pub struct Matrix3 { + pub x: Vector3, + pub y: Vector3, + pub z: Vector3, +} /// A 4 x 4, column major matrix +/// +/// This type is marked as `#[repr(C, packed)]`. +#[repr(C, packed)] #[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)] -pub struct Matrix4 { pub x: Vector4, pub y: Vector4, pub z: Vector4, pub w: Vector4 } +pub struct Matrix4 { + pub x: Vector4, + pub y: Vector4, + pub z: Vector4, + pub w: Vector4, +} impl Matrix2 { diff --git a/src/point.rs b/src/point.rs index b5a2dd2..fbf21fd 100644 --- a/src/point.rs +++ b/src/point.rs @@ -30,12 +30,25 @@ use num::{BaseNum, BaseFloat}; use vector::*; /// A point in 2-dimensional space. +/// +/// This type is marked as `#[repr(C, packed)]`. +#[repr(C, packed)] #[derive(PartialEq, Eq, Copy, Clone, Hash, RustcEncodable, RustcDecodable)] -pub struct Point2 { pub x: S, pub y: S } +pub struct Point2 { + pub x: S, + pub y: S, +} /// A point in 3-dimensional space. +/// +/// This type is marked as `#[repr(C, packed)]`. +#[repr(C, packed)] #[derive(PartialEq, Eq, Copy, Clone, Hash, RustcEncodable, RustcDecodable)] -pub struct Point3 { pub x: S, pub y: S, pub z: S } +pub struct Point3 { + pub x: S, + pub y: S, + pub z: S, +} impl Point2 { diff --git a/src/quaternion.rs b/src/quaternion.rs index b249960..9151003 100644 --- a/src/quaternion.rs +++ b/src/quaternion.rs @@ -31,6 +31,9 @@ use vector::{Vector3, Vector, EuclideanVector}; /// A [quaternion](https://en.wikipedia.org/wiki/Quaternion) in scalar/vector /// form. +/// +/// This type is marked as `#[repr(C, packed)]`. +#[repr(C, packed)] #[derive(Copy, Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)] pub struct Quaternion { pub s: S, diff --git a/src/vector.rs b/src/vector.rs index b9dc1aa..b7b9ed8 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -136,12 +136,42 @@ pub trait Vector: Copy + Clone where /// Dot product of two vectors. #[inline] pub fn dot(a: V, b: V) -> V::Scalar { a.dot(b) } +/// A 2-dimensional vector. +/// +/// This type is marked as `#[repr(C, packed)]`. +#[repr(C, packed)] +#[derive(PartialEq, Eq, Copy, Clone, Hash, RustcEncodable, RustcDecodable)] +pub struct Vector2 { + pub x: S, + pub y: S, +} + +/// A 3-dimensional vector. +/// +/// This type is marked as `#[repr(C, packed)]`. +#[repr(C, packed)] +#[derive(PartialEq, Eq, Copy, Clone, Hash, RustcEncodable, RustcDecodable)] +pub struct Vector3 { + pub x: S, + pub y: S, + pub z: S, +} + +/// A 4-dimensional vector. +/// +/// This type is marked as `#[repr(C, packed)]`. +#[repr(C, packed)] +#[derive(PartialEq, Eq, Copy, Clone, Hash, RustcEncodable, RustcDecodable)] +pub struct Vector4 { + pub x: S, + pub y: S, + pub z: S, + pub w: S, +} + // Utility macro for generating associated functions for the vectors macro_rules! impl_vector { ($VectorN:ident <$S:ident> { $($field:ident),+ }, $n:expr, $constructor:ident) => { - #[derive(PartialEq, Eq, Copy, Clone, Hash, RustcEncodable, RustcDecodable)] - pub struct $VectorN { $(pub $field: S),+ } - impl<$S> $VectorN<$S> { /// Construct a new vector, using the provided values. #[inline]