Mark some types as #[repr(C, packed)]

Closes #296
This commit is contained in:
Brendan Zabarauskas 2016-03-25 12:34:12 +11:00
parent 49cac4d894
commit 3d55187a7d
5 changed files with 83 additions and 10 deletions

View file

@ -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<S> { 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<S> { pub s: S }

View file

@ -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<S> { pub x: Vector2<S>, pub y: Vector2<S> }
pub struct Matrix2<S> {
pub x: Vector2<S>,
pub y: Vector2<S>,
}
/// 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<S> { pub x: Vector3<S>, pub y: Vector3<S>, pub z: Vector3<S> }
pub struct Matrix3<S> {
pub x: Vector3<S>,
pub y: Vector3<S>,
pub z: Vector3<S>,
}
/// 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<S> { pub x: Vector4<S>, pub y: Vector4<S>, pub z: Vector4<S>, pub w: Vector4<S> }
pub struct Matrix4<S> {
pub x: Vector4<S>,
pub y: Vector4<S>,
pub z: Vector4<S>,
pub w: Vector4<S>,
}
impl<S: BaseFloat> Matrix2<S> {

View file

@ -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<S> { pub x: S, pub y: S }
pub struct Point2<S> {
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<S> { pub x: S, pub y: S, pub z: S }
pub struct Point3<S> {
pub x: S,
pub y: S,
pub z: S,
}
impl<S: BaseNum> Point2<S> {

View file

@ -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<S> {
pub s: S,

View file

@ -136,12 +136,42 @@ pub trait Vector: Copy + Clone where
/// Dot product of two vectors.
#[inline] pub fn dot<V: Vector>(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<S> {
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<S> {
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<S> {
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<S> { $(pub $field: S),+ }
impl<$S> $VectorN<$S> {
/// Construct a new vector, using the provided values.
#[inline]