Merge pull request #304 from bjz/repr-c

Mark some types as #[repr(C, packed)]
This commit is contained in:
Brendan Zabarauskas 2016-03-25 12:52:29 +11:00
commit 4200b75c6d
6 changed files with 84 additions and 10 deletions

View file

@ -15,6 +15,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Improves the `fmt::Debug` impls for `Vector`, `Matrix`, `Point`, `Decomposed`, - Improves the `fmt::Debug` impls for `Vector`, `Matrix`, `Point`, `Decomposed`,
`Quaternion` and `Angle` to make them easier to derive, and have clearer `Quaternion` and `Angle` to make them easier to derive, and have clearer
formatting. formatting.
- Marks vectors, points, matrices, and angles as `#[repr(C, packed)]`.
## [v0.7.0] - 2015-12-23 ## [v0.7.0] - 2015-12-23

View file

@ -28,10 +28,16 @@ use rust_num::traits::cast;
use approx::ApproxEq; use approx::ApproxEq;
use num::BaseFloat; 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)] #[derive(Copy, Clone, PartialEq, PartialOrd, RustcEncodable, RustcDecodable)]
pub struct Rad<S> { pub s: S } 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)] #[derive(Copy, Clone, PartialEq, PartialOrd, RustcEncodable, RustcDecodable)]
pub struct Deg<S> { pub s: S } pub struct Deg<S> { pub s: S }

View file

@ -35,16 +35,37 @@ use vector::{Vector, EuclideanVector};
use vector::{Vector2, Vector3, Vector4}; use vector::{Vector2, Vector3, Vector4};
/// A 2 x 2, column major matrix /// A 2 x 2, column major matrix
///
/// This type is marked as `#[repr(C, packed)]`.
#[repr(C, packed)]
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)] #[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 /// A 3 x 3, column major matrix
///
/// This type is marked as `#[repr(C, packed)]`.
#[repr(C, packed)]
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)] #[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 /// A 4 x 4, column major matrix
///
/// This type is marked as `#[repr(C, packed)]`.
#[repr(C, packed)]
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)] #[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> { impl<S: BaseFloat> Matrix2<S> {

View file

@ -30,12 +30,25 @@ use num::{BaseNum, BaseFloat};
use vector::*; use vector::*;
/// A point in 2-dimensional space. /// 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)] #[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. /// 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)] #[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> { 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 /// A [quaternion](https://en.wikipedia.org/wiki/Quaternion) in scalar/vector
/// form. /// form.
///
/// This type is marked as `#[repr(C, packed)]`.
#[repr(C, packed)]
#[derive(Copy, Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)] #[derive(Copy, Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
pub struct Quaternion<S> { pub struct Quaternion<S> {
pub s: S, pub s: S,

View file

@ -136,12 +136,42 @@ pub trait Vector: Copy + Clone where
/// Dot product of two vectors. /// Dot product of two vectors.
#[inline] pub fn dot<V: Vector>(a: V, b: V) -> V::Scalar { a.dot(b) } #[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 // Utility macro for generating associated functions for the vectors
macro_rules! impl_vector { macro_rules! impl_vector {
($VectorN:ident <$S:ident> { $($field:ident),+ }, $n:expr, $constructor:ident) => { ($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> { impl<$S> $VectorN<$S> {
/// Construct a new vector, using the provided values. /// Construct a new vector, using the provided values.
#[inline] #[inline]