#![macro_use]
#[cfg(feature = "simd")]
macro_rules! default_fn {
{ $($tt:tt)* } => { default fn $( $tt )* };
}
#[cfg(not(feature = "simd"))]
macro_rules! default_fn {
{ $($tt:tt)* } => { fn $( $tt )* };
}
macro_rules! impl_operator {
(<$S:ident: $Constraint:ident> $Op:ident for $Lhs:ty {
fn $op:ident($x:ident) -> $Output:ty { $body:expr }
}) => {
impl<$S: $Constraint> $Op for $Lhs {
type Output = $Output;
#[inline]
default_fn!($op(self) -> $Output {
let $x = self; $body
});
}
impl<'a, $S: $Constraint> $Op for &'a $Lhs {
type Output = $Output;
#[inline]
default_fn!($op(self) -> $Output {
let $x = self; $body
});
}
};
(<$S:ident: $Constraint:ident> $Op:ident<$Rhs:ident> for $Lhs:ty {
fn $op:ident($lhs:ident, $rhs:ident) -> $Output:ty { $body:expr }
}) => {
impl<$S: $Constraint> $Op<$Rhs> for $Lhs {
type Output = $Output;
#[inline]
default_fn!($op(self, other: $Rhs) -> $Output {
let ($lhs, $rhs) = (self, other); $body
});
}
impl<'a, $S: $Constraint> $Op<$Rhs> for &'a $Lhs {
type Output = $Output;
#[inline]
default_fn!($op(self, other: $Rhs) -> $Output {
let ($lhs, $rhs) = (self, other); $body
});
}
};
(<$S:ident: $Constraint:ident> $Op:ident<$Rhs:ty> for $Lhs:ty {
fn $op:ident($lhs:ident, $rhs:ident) -> $Output:ty { $body:expr }
}) => {
impl<$S: $Constraint> $Op<$Rhs> for $Lhs {
type Output = $Output;
#[inline]
default_fn!( $op(self, other: $Rhs) -> $Output {
let ($lhs, $rhs) = (self, other); $body
});
}
impl<'a, $S: $Constraint> $Op<&'a $Rhs> for $Lhs {
type Output = $Output;
#[inline]
default_fn!( $op(self, other: &'a $Rhs) -> $Output {
let ($lhs, $rhs) = (self, other); $body
});
}
impl<'a, $S: $Constraint> $Op<$Rhs> for &'a $Lhs {
type Output = $Output;
#[inline]
default_fn!( $op(self, other: $Rhs) -> $Output {
let ($lhs, $rhs) = (self, other); $body
});
}
impl<'a, 'b, $S: $Constraint> $Op<&'a $Rhs> for &'b $Lhs {
type Output = $Output;
#[inline]
default_fn!( $op(self, other: &'a $Rhs) -> $Output {
let ($lhs, $rhs) = (self, other); $body
});
}
};
($Op:ident<$Rhs:ident<$S:ident>> for $Lhs:ty {
fn $op:ident($lhs:ident, $rhs:ident) -> $Output:ty { $body:expr }
}) => {
impl $Op<$Rhs<$S>> for $Lhs {
type Output = $Output;
#[inline]
default_fn!( $op(self, other: $Rhs<$S>) -> $Output {
let ($lhs, $rhs) = (self, other); $body
});
}
impl<'a> $Op<&'a $Rhs<$S>> for $Lhs {
type Output = $Output;
#[inline]
default_fn!( $op(self, other: &'a $Rhs<$S>) -> $Output {
let ($lhs, $rhs) = (self, other); $body
});
}
};
}
macro_rules! impl_assignment_operator {
(<$S:ident: $Constraint:ident> $Op:ident<$Rhs:ty> for $Lhs:ty {
fn $op:ident(&mut $lhs:ident, $rhs:ident) $body:block
}) => {
impl<$S: $Constraint + $Op<$S>> $Op<$Rhs> for $Lhs {
#[inline]
default_fn!( $op(&mut $lhs, $rhs: $Rhs) $body );
}
};
}
macro_rules! fold_array {
(&$method:ident, { $x:expr }) => {
*$x
};
(&$method:ident, { $x:expr, $y:expr }) => {
$x.$method(&$y)
};
(&$method:ident, { $x:expr, $y:expr, $z:expr }) => {
$x.$method(&$y).$method(&$z)
};
(&$method:ident, { $x:expr, $y:expr, $z:expr, $w:expr }) => {
$x.$method(&$y).$method(&$z).$method(&$w)
};
($method:ident, { $x:expr }) => {
$x
};
($method:ident, { $x:expr, $y:expr }) => {
$x.$method($y)
};
($method:ident, { $x:expr, $y:expr, $z:expr }) => {
$x.$method($y).$method($z)
};
($method:ident, { $x:expr, $y:expr, $z:expr, $w:expr }) => {
$x.$method($y).$method($z).$method($w)
};
}
macro_rules! impl_fixed_array_conversions {
($ArrayN:ident <$S:ident> { $($field:ident : $index:expr),+ }, $n:expr) => {
impl<$S> Into<[$S; $n]> for $ArrayN<$S> {
#[inline]
fn into(self) -> [$S; $n] {
match self { $ArrayN { $($field),+ } => [$($field),+] }
}
}
impl<$S> AsRef<[$S; $n]> for $ArrayN<$S> {
#[inline]
fn as_ref(&self) -> &[$S; $n] {
unsafe { mem::transmute(self) }
}
}
impl<$S> AsMut<[$S; $n]> for $ArrayN<$S> {
#[inline]
fn as_mut(&mut self) -> &mut [$S; $n] {
unsafe { mem::transmute(self) }
}
}
impl<$S: Clone> From<[$S; $n]> for $ArrayN<$S> {
#[inline]
fn from(v: [$S; $n]) -> $ArrayN<$S> {
$ArrayN { $($field: v[$index].clone()),+ }
}
}
impl<'a, $S> From<&'a [$S; $n]> for &'a $ArrayN<$S> {
#[inline]
fn from(v: &'a [$S; $n]) -> &'a $ArrayN<$S> {
unsafe { mem::transmute(v) }
}
}
impl<'a, $S> From<&'a mut [$S; $n]> for &'a mut $ArrayN<$S> {
#[inline]
fn from(v: &'a mut [$S; $n]) -> &'a mut $ArrayN<$S> {
unsafe { mem::transmute(v) }
}
}
}
}
macro_rules! tuple_layout_matches {
($ArrayN:ident <$S:ident> { $($field:ident : $index:tt),+ }, $Tuple:ty) => {{
use std::mem::{align_of, size_of, MaybeUninit};
use std::ptr::addr_of;
size_of::<$ArrayN<$S>>() == size_of::<$Tuple>()
&& align_of::<$ArrayN<$S>>() == align_of::<$Tuple>()
&& {
let s = MaybeUninit::<$ArrayN<$S>>::uninit();
let s_base = s.as_ptr() as usize;
let t = MaybeUninit::<$Tuple>::uninit();
let t_base = t.as_ptr() as usize;
unsafe {
$(
(addr_of!((*s.as_ptr()).$field) as usize - s_base)
== (addr_of!((*t.as_ptr()).$index) as usize - t_base)
)&&+
}
}
}};
}
macro_rules! impl_tuple_conversions {
($ArrayN:ident <$S:ident> { $($field:ident : $index:tt),+ }, $Tuple:ty) => {
impl<$S> Into<$Tuple> for $ArrayN<$S> {
#[inline]
fn into(self) -> $Tuple {
match self { $ArrayN { $($field),+ } => ($($field),+,) }
}
}
impl<$S> AsRef<$Tuple> for $ArrayN<$S> {
#[inline]
fn as_ref(&self) -> &$Tuple {
assert!(
tuple_layout_matches!($ArrayN<$S> { $($field: $index),+ }, $Tuple),
"cgmath-next: internal invariant violated -- {} and {} have \
diverged in memory layout on this platform; refusing to \
transmute (see docs/unsafe-audit.md UNSAFE-002)",
stringify!($ArrayN<$S>), stringify!($Tuple)
);
unsafe { mem::transmute(self) }
}
}
impl<$S> AsMut<$Tuple> for $ArrayN<$S> {
#[inline]
fn as_mut(&mut self) -> &mut $Tuple {
assert!(
tuple_layout_matches!($ArrayN<$S> { $($field: $index),+ }, $Tuple),
"cgmath-next: internal invariant violated -- {} and {} have \
diverged in memory layout on this platform; refusing to \
transmute (see docs/unsafe-audit.md UNSAFE-002)",
stringify!($ArrayN<$S>), stringify!($Tuple)
);
unsafe { mem::transmute(self) }
}
}
impl<$S> From<$Tuple> for $ArrayN<$S> {
#[inline]
fn from(v: $Tuple) -> $ArrayN<$S> {
match v { ($($field),+,) => $ArrayN { $($field: $field),+ } }
}
}
impl<'a, $S> From<&'a $Tuple> for &'a $ArrayN<$S> {
#[inline]
fn from(v: &'a $Tuple) -> &'a $ArrayN<$S> {
assert!(
tuple_layout_matches!($ArrayN<$S> { $($field: $index),+ }, $Tuple),
"cgmath-next: internal invariant violated -- {} and {} have \
diverged in memory layout on this platform; refusing to \
transmute (see docs/unsafe-audit.md UNSAFE-002)",
stringify!($ArrayN<$S>), stringify!($Tuple)
);
unsafe { mem::transmute(v) }
}
}
impl<'a, $S> From<&'a mut $Tuple> for &'a mut $ArrayN<$S> {
#[inline]
fn from(v: &'a mut $Tuple) -> &'a mut $ArrayN<$S> {
assert!(
tuple_layout_matches!($ArrayN<$S> { $($field: $index),+ }, $Tuple),
"cgmath-next: internal invariant violated -- {} and {} have \
diverged in memory layout on this platform; refusing to \
transmute (see docs/unsafe-audit.md UNSAFE-002)",
stringify!($ArrayN<$S>), stringify!($Tuple)
);
unsafe { mem::transmute(v) }
}
}
}
}
macro_rules! impl_index_operators {
($VectorN:ident<$S:ident>, $n:expr, $Output:ty, $I:ty) => {
impl<$S> Index<$I> for $VectorN<$S> {
type Output = $Output;
#[inline]
fn index<'a>(&'a self, i: $I) -> &'a $Output {
let v: &[$S; $n] = self.as_ref();
&v[i]
}
}
impl<$S> IndexMut<$I> for $VectorN<$S> {
#[inline]
fn index_mut<'a>(&'a mut self, i: $I) -> &'a mut $Output {
let v: &mut [$S; $n] = self.as_mut();
&mut v[i]
}
}
};
}
#[cfg(feature = "mint")]
macro_rules! impl_mint_conversions {
($ArrayN:ident { $($field:ident),+ }, $Mint:ident) => {
impl<S: Clone> Into<mint::$Mint<S>> for $ArrayN<S> {
#[inline]
fn into(self) -> mint::$Mint<S> {
mint::$Mint::from([$(self.$field),+])
}
}
impl<S> From<mint::$Mint<S>> for $ArrayN<S> {
#[inline]
fn from(v: mint::$Mint<S>) -> Self {
$ArrayN { $( $field: v.$field, )+ }
}
}
}
}
include!(concat!(env!("OUT_DIR"), "/swizzle_operator_macro.rs"));
#[cfg(test)]
mod tuple_layout_guard_tests {
use vector::Vector4;
#[test]
fn accepts_the_real_correct_mapping() {
assert!(tuple_layout_matches!(
Vector4<f32> { x: 0, y: 1, z: 2, w: 3 },
(f32, f32, f32, f32)
));
assert!(tuple_layout_matches!(
Vector4<f64> { x: 0, y: 1, z: 2, w: 3 },
(f64, f64, f64, f64)
));
}
#[test]
fn rejects_a_scrambled_field_to_index_mapping() {
assert!(!tuple_layout_matches!(
Vector4<f32> { x: 3, y: 2, z: 1, w: 0 },
(f32, f32, f32, f32)
));
}
#[test]
fn rejects_a_size_mismatch() {
assert!(!tuple_layout_matches!(
Vector4<f32> { x: 0, y: 1, z: 2, w: 3 },
(f64, f64, f64, f64)
));
}
}