oh my god use tabs

This commit is contained in:
Quaternions 2024-08-28 10:47:30 -07:00
parent 67ac4cf7ff
commit f4ab9403a4
4 changed files with 273 additions and 273 deletions

View File

@ -5,8 +5,8 @@ use fixed_wide_traits::wide::WideDot;
// mat4x3.wide_dot(vec3.extend(1)) // mat4x3.wide_dot(vec3.extend(1))
pub struct Affine<M,T>{ pub struct Affine<M,T>{
pub matrix:M, pub matrix:M,
pub offset:T, pub offset:T,
} }
impl<M:Copy,T:Copy> Affine<M,T>{ impl<M:Copy,T:Copy> Affine<M,T>{

View File

@ -5,277 +5,277 @@ pub mod wide;
#[doc(hidden)] #[doc(hidden)]
#[macro_export(local_inner_macros)] #[macro_export(local_inner_macros)]
macro_rules! impl_vector { macro_rules! impl_vector {
( $struct: ident { $($field: ident), + }, ( $($generic: ident), + ), $size: expr ) => { ( $struct: ident { $($field: ident), + }, ( $($generic: ident), + ), $size: expr ) => {
impl<T> $struct<T> { impl<T> $struct<T> {
/// Constructs a new vector with the specified values for each field. /// Constructs a new vector with the specified values for each field.
/// ///
/// # Example /// # Example
/// ///
/// ``` /// ```
/// use fixed_wide_vectors::Vector2; /// use fixed_wide_vectors::Vector2;
/// ///
/// let vec2 = Vector2::new(0, 0); /// let vec2 = Vector2::new(0, 0);
/// ///
/// assert_eq!(vec2.x, 0); /// assert_eq!(vec2.x, 0);
/// assert_eq!(vec2.y, 0); /// assert_eq!(vec2.y, 0);
/// ``` /// ```
#[inline(always)] #[inline(always)]
pub const fn new( $($field: T), + ) -> Self { pub const fn new( $($field: T), + ) -> Self {
Self { Self {
$( $field ), + $( $field ), +
} }
} }
/// Consumes the vector and returns its values as an array. /// Consumes the vector and returns its values as an array.
/// ///
/// # Example /// # Example
/// ///
/// ``` /// ```
/// use fixed_wide_vectors::Vector2; /// use fixed_wide_vectors::Vector2;
/// ///
/// let vec2 = Vector2::new(0, 0); /// let vec2 = Vector2::new(0, 0);
/// let array = vec2.to_array(); /// let array = vec2.to_array();
/// ///
/// assert_eq!(array, [0, 0]); /// assert_eq!(array, [0, 0]);
/// ``` /// ```
#[inline(always)] #[inline(always)]
pub fn to_array(self) -> [T; $size] { pub fn to_array(self) -> [T; $size] {
[ $(self.$field), + ] [ $(self.$field), + ]
} }
/// Consumes the vector and returns its values as a tuple. /// Consumes the vector and returns its values as a tuple.
/// ///
/// # Example /// # Example
/// ///
/// ``` /// ```
/// use fixed_wide_vectors::Vector2; /// use fixed_wide_vectors::Vector2;
/// ///
/// let vec2 = Vector2::new(0, 0); /// let vec2 = Vector2::new(0, 0);
/// let tuple = vec2.to_tuple(); /// let tuple = vec2.to_tuple();
/// ///
/// assert_eq!(tuple, (0, 0)); /// assert_eq!(tuple, (0, 0));
/// ``` /// ```
#[inline(always)] #[inline(always)]
pub fn to_tuple(self) -> ( $($generic), + ) { pub fn to_tuple(self) -> ( $($generic), + ) {
( $(self.$field), + ) ( $(self.$field), + )
} }
/// Consumes the vector and returns a new vector with the given function applied on each field. /// Consumes the vector and returns a new vector with the given function applied on each field.
/// ///
/// # Example /// # Example
/// ///
/// ``` /// ```
/// use fixed_wide_vectors::Vector2; /// use fixed_wide_vectors::Vector2;
/// ///
/// let vec2 = Vector2::new(1, 2) /// let vec2 = Vector2::new(1, 2)
/// .map(|i| i * 2); /// .map(|i| i * 2);
/// ///
/// assert_eq!(vec2, Vector2::new(2, 4)); /// assert_eq!(vec2, Vector2::new(2, 4));
/// ``` /// ```
#[inline] #[inline]
pub fn map<F, U>(self, f: F) -> $struct<U> pub fn map<F, U>(self, f: F) -> $struct<U>
where where
F: Fn(T) -> U F: Fn(T) -> U
{ {
$struct { $struct {
$( $field: f(self.$field) ), + $( $field: f(self.$field) ), +
} }
} }
} }
impl<T: Copy> $struct<T> { impl<T: Copy> $struct<T> {
/// Constructs a vector using the given `value` as the value for all of its fields. /// Constructs a vector using the given `value` as the value for all of its fields.
/// ///
/// # Example /// # Example
/// ///
/// ``` /// ```
/// use fixed_wide_vectors::Vector2; /// use fixed_wide_vectors::Vector2;
/// ///
/// let vec2 = Vector2::from_value(0); /// let vec2 = Vector2::from_value(0);
/// ///
/// assert_eq!(vec2, Vector2::new(0, 0)); /// assert_eq!(vec2, Vector2::new(0, 0));
/// ``` /// ```
#[inline(always)] #[inline(always)]
pub const fn from_value(value: T) -> Self { pub const fn from_value(value: T) -> Self {
Self { Self {
$( $field: value ), + $( $field: value ), +
} }
} }
} }
impl<T> From<[T; $size]> for $struct<T> { impl<T> From<[T; $size]> for $struct<T> {
fn from(from: [T; $size]) -> Self { fn from(from: [T; $size]) -> Self {
let mut iterator = from.into_iter(); let mut iterator = from.into_iter();
Self { Self {
// SAFETY: We know the size of `from` so `iterator.next()` is always `Some(..)` // SAFETY: We know the size of `from` so `iterator.next()` is always `Some(..)`
$( $field: unsafe { iterator.next().unwrap_unchecked() } ), + $( $field: unsafe { iterator.next().unwrap_unchecked() } ), +
} }
} }
} }
impl<T> From<($($generic), +)> for $struct<T> { impl<T> From<($($generic), +)> for $struct<T> {
fn from(from: ($($generic), +)) -> Self { fn from(from: ($($generic), +)) -> Self {
let ( $($field), + ) = from; let ( $($field), + ) = from;
Self { Self {
$( $field ), + $( $field ), +
} }
} }
} }
impl<T: core::fmt::Debug> core::fmt::Debug for $struct<T> { impl<T: core::fmt::Debug> core::fmt::Debug for $struct<T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let identifier = core::stringify!($struct); let identifier = core::stringify!($struct);
f.debug_struct(identifier) f.debug_struct(identifier)
$( .field( core::stringify!($field), &self.$field ) ) + $( .field( core::stringify!($field), &self.$field ) ) +
.finish() .finish()
} }
} }
impl<T: PartialEq> PartialEq for $struct<T> { impl<T: PartialEq> PartialEq for $struct<T> {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
$( self.$field == other.$field ) && + $( self.$field == other.$field ) && +
} }
} }
impl<T: Eq> Eq for $struct<T> { } impl<T: Eq> Eq for $struct<T> { }
impl<T: core::hash::Hash> core::hash::Hash for $struct<T> { impl<T: core::hash::Hash> core::hash::Hash for $struct<T> {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) { fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
$( self.$field.hash(state); ) + $( self.$field.hash(state); ) +
} }
} }
impl<T: Clone> Clone for $struct<T> { impl<T: Clone> Clone for $struct<T> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self {
$( $field: self.$field.clone() ), + $( $field: self.$field.clone() ), +
} }
} }
} }
impl<T: Copy> Copy for $struct<T> { } impl<T: Copy> Copy for $struct<T> { }
impl<T: Default> Default for $struct<T> { impl<T: Default> Default for $struct<T> {
fn default() -> Self { fn default() -> Self {
Self { Self {
$( $field: T::default() ), + $( $field: T::default() ), +
} }
} }
} }
impl<T: Ord> $struct<T> { impl<T: Ord> $struct<T> {
pub fn min(self, rhs: Self) -> $struct<T> { pub fn min(self, rhs: Self) -> $struct<T> {
$struct{ $struct{
$( $field: self.$field.min(rhs.$field) ), + $( $field: self.$field.min(rhs.$field) ), +
} }
} }
pub fn max(self, rhs: Self) -> $struct<T> { pub fn max(self, rhs: Self) -> $struct<T> {
$struct{ $struct{
$( $field: self.$field.max(rhs.$field) ), + $( $field: self.$field.max(rhs.$field) ), +
} }
} }
pub fn cmp(self, rhs: Self) -> $struct<core::cmp::Ordering> { pub fn cmp(self, rhs: Self) -> $struct<core::cmp::Ordering> {
$struct{ $struct{
$( $field: self.$field.cmp(&rhs.$field) ), + $( $field: self.$field.cmp(&rhs.$field) ), +
} }
} }
pub fn lt(self, rhs: Self) -> $struct<bool> { pub fn lt(self, rhs: Self) -> $struct<bool> {
$struct{ $struct{
$( $field: self.$field.lt(&rhs.$field) ), + $( $field: self.$field.lt(&rhs.$field) ), +
} }
} }
pub fn gt(self, rhs: Self) -> $struct<bool> { pub fn gt(self, rhs: Self) -> $struct<bool> {
$struct{ $struct{
$( $field: self.$field.gt(&rhs.$field) ), + $( $field: self.$field.gt(&rhs.$field) ), +
} }
} }
pub fn ge(self, rhs: Self) -> $struct<bool> { pub fn ge(self, rhs: Self) -> $struct<bool> {
$struct{ $struct{
$( $field: self.$field.ge(&rhs.$field) ), + $( $field: self.$field.ge(&rhs.$field) ), +
} }
} }
pub fn le(self, rhs: Self) -> $struct<bool> { pub fn le(self, rhs: Self) -> $struct<bool> {
$struct{ $struct{
$( $field: self.$field.le(&rhs.$field) ), + $( $field: self.$field.le(&rhs.$field) ), +
} }
} }
} }
impl<T: core::ops::Neg<Output = T>> core::ops::Neg for $struct<T> { impl<T: core::ops::Neg<Output = T>> core::ops::Neg for $struct<T> {
type Output = Self; type Output = Self;
fn neg(self) -> Self::Output { fn neg(self) -> Self::Output {
Self { Self {
$( $field: -self.$field ), + $( $field: -self.$field ), +
} }
} }
} }
// Impl arithmetic pperators // Impl arithmetic pperators
$crate::impl_operator!( $struct { $($field), + }, AddAssign, add_assign ); $crate::impl_operator!( $struct { $($field), + }, AddAssign, add_assign );
$crate::impl_operator!( $struct { $($field), + }, Add, add, Self ); $crate::impl_operator!( $struct { $($field), + }, Add, add, Self );
$crate::impl_operator!( $struct { $($field), + }, SubAssign, sub_assign ); $crate::impl_operator!( $struct { $($field), + }, SubAssign, sub_assign );
$crate::impl_operator!( $struct { $($field), + }, Sub, sub, Self ); $crate::impl_operator!( $struct { $($field), + }, Sub, sub, Self );
$crate::impl_operator!( $struct { $($field), + }, MulAssign, mul_assign ); $crate::impl_operator!( $struct { $($field), + }, MulAssign, mul_assign );
$crate::impl_operator!( $struct { $($field), + }, Mul, mul, Self ); $crate::impl_operator!( $struct { $($field), + }, Mul, mul, Self );
$crate::impl_operator!( $struct { $($field), + }, DivAssign, div_assign ); $crate::impl_operator!( $struct { $($field), + }, DivAssign, div_assign );
$crate::impl_operator!( $struct { $($field), + }, Div, div, Self ); $crate::impl_operator!( $struct { $($field), + }, Div, div, Self );
$crate::impl_operator!( $struct { $($field), + }, RemAssign, rem_assign ); $crate::impl_operator!( $struct { $($field), + }, RemAssign, rem_assign );
$crate::impl_operator!( $struct { $($field), + }, Rem, rem, Self ); $crate::impl_operator!( $struct { $($field), + }, Rem, rem, Self );
// Impl bitwise operators // Impl bitwise operators
$crate::impl_operator!( $struct { $($field), + }, BitAndAssign, bitand_assign ); $crate::impl_operator!( $struct { $($field), + }, BitAndAssign, bitand_assign );
$crate::impl_operator!( $struct { $($field), + }, BitAnd, bitand, Self ); $crate::impl_operator!( $struct { $($field), + }, BitAnd, bitand, Self );
$crate::impl_operator!( $struct { $($field), + }, BitOrAssign, bitor_assign ); $crate::impl_operator!( $struct { $($field), + }, BitOrAssign, bitor_assign );
$crate::impl_operator!( $struct { $($field), + }, BitOr, bitor, Self ); $crate::impl_operator!( $struct { $($field), + }, BitOr, bitor, Self );
$crate::impl_operator!( $struct { $($field), + }, BitXorAssign, bitxor_assign ); $crate::impl_operator!( $struct { $($field), + }, BitXorAssign, bitxor_assign );
$crate::impl_operator!( $struct { $($field), + }, BitXor, bitxor, Self ); $crate::impl_operator!( $struct { $($field), + }, BitXor, bitxor, Self );
// Impl floating-point based methods // Impl floating-point based methods
$crate::impl_wide_operations!( $struct { $($field), + }, $size ); $crate::impl_wide_operations!( $struct { $($field), + }, $size );
}; };
} }
#[doc(hidden)] #[doc(hidden)]
#[macro_export(local_inner_macros)] #[macro_export(local_inner_macros)]
macro_rules! impl_operator { macro_rules! impl_operator {
( $struct: ident { $($field: ident), + }, $trait: ident, $method: ident, $output: ty ) => { ( $struct: ident { $($field: ident), + }, $trait: ident, $method: ident, $output: ty ) => {
impl<T: core::ops::$trait<Output = T>> core::ops::$trait<Self> for $struct<T> { impl<T: core::ops::$trait<Output = T>> core::ops::$trait<Self> for $struct<T> {
type Output = $output; type Output = $output;
fn $method(self, other: Self) -> Self::Output { fn $method(self, other: Self) -> Self::Output {
Self { Self {
$( $field: self.$field.$method(other.$field) ), + $( $field: self.$field.$method(other.$field) ), +
} }
} }
} }
impl<T: core::ops::$trait<Output = T> + Copy> core::ops::$trait<T> for $struct<T> { impl<T: core::ops::$trait<Output = T> + Copy> core::ops::$trait<T> for $struct<T> {
type Output = $output; type Output = $output;
fn $method(self, other: T) -> Self::Output { fn $method(self, other: T) -> Self::Output {
Self { Self {
$( $field: self.$field.$method(other) ), + $( $field: self.$field.$method(other) ), +
} }
} }
} }
}; };
( $struct: ident { $($field: ident), + }, $trait: ident, $method: ident ) => { ( $struct: ident { $($field: ident), + }, $trait: ident, $method: ident ) => {
impl<T: core::ops::$trait> core::ops::$trait for $struct<T> { impl<T: core::ops::$trait> core::ops::$trait for $struct<T> {
fn $method(&mut self, other: Self) { fn $method(&mut self, other: Self) {
$( self.$field.$method(other.$field) ); + $( self.$field.$method(other.$field) ); +
} }
} }
impl<T: core::ops::$trait + Copy> core::ops::$trait<T> for $struct<T> { impl<T: core::ops::$trait + Copy> core::ops::$trait<T> for $struct<T> {
fn $method(&mut self, other: T) { fn $method(&mut self, other: T) {
$( self.$field.$method(other) ); + $( self.$field.$method(other) ); +
} }
} }
}; };
} }

View File

@ -1,34 +1,34 @@
#[doc(hidden)] #[doc(hidden)]
#[macro_export(local_inner_macros)] #[macro_export(local_inner_macros)]
macro_rules! impl_wide_operations { macro_rules! impl_wide_operations {
( $struct: ident { $($field: ident), + }, $size: expr ) => { ( $struct: ident { $($field: ident), + }, $size: expr ) => {
impl<U,T:Copy+fixed_wide_traits::wide::WideMul<Output=U>> fixed_wide_traits::wide::WideMul for $struct<T> { impl<U,T:Copy+fixed_wide_traits::wide::WideMul<Output=U>> fixed_wide_traits::wide::WideMul for $struct<T> {
type Output=$struct<U>; type Output=$struct<U>;
#[inline] #[inline]
fn wide_mul(self, rhs: Self) -> Self::Output { fn wide_mul(self, rhs: Self) -> Self::Output {
$struct{ $struct{
$( $field: self.$field.wide_mul(rhs.$field) ), + $( $field: self.$field.wide_mul(rhs.$field) ), +
} }
} }
} }
impl<V:core::ops::Add<Output=V>,U,T:fixed_wide_traits::wide::WideMul<U,Output=V>> fixed_wide_traits::wide::WideDot<$struct<U>> for $struct<T> { impl<V:core::ops::Add<Output=V>,U,T:fixed_wide_traits::wide::WideMul<U,Output=V>> fixed_wide_traits::wide::WideDot<$struct<U>> for $struct<T> {
type Output=V; type Output=V;
#[inline] #[inline]
fn wide_dot(self, rhs: $struct<U>) -> Self::Output { fn wide_dot(self, rhs: $struct<U>) -> Self::Output {
$crate::sum_repeating!( $crate::sum_repeating!(
$( + (self.$field.wide_mul(rhs.$field)) ) + $( + (self.$field.wide_mul(rhs.$field)) ) +
) )
} }
} }
impl<U:std::ops::Add<Output=U>,T:Copy+fixed_wide_traits::wide::WideMul<Output=U>> $struct<T> { impl<U:std::ops::Add<Output=U>,T:Copy+fixed_wide_traits::wide::WideMul<Output=U>> $struct<T> {
#[inline] #[inline]
pub fn wide_length_squared(&self) -> U { pub fn wide_length_squared(&self) -> U {
$crate::sum_repeating!( $crate::sum_repeating!(
$( + self.$field.wide_mul(self.$field) ) + $( + self.$field.wide_mul(self.$field) ) +
) )
} }
} }
}; };
} }
@ -37,7 +37,7 @@ macro_rules! impl_wide_operations {
#[doc(hidden)] #[doc(hidden)]
#[macro_export(local_inner_macros)] #[macro_export(local_inner_macros)]
macro_rules! sum_repeating { macro_rules! sum_repeating {
( + $($item: tt) * ) => { ( + $($item: tt) * ) => {
$($item) * $($item) *
}; };
} }

View File

@ -14,8 +14,8 @@
/// assert_eq!(vec2.y, 4); /// assert_eq!(vec2.y, 4);
/// ``` /// ```
pub struct Vector2<T> { pub struct Vector2<T> {
pub x: T, pub x: T,
pub y: T, pub y: T,
} }
@ -34,9 +34,9 @@ pub struct Vector2<T> {
/// assert_eq!(vec3.z, 6); /// assert_eq!(vec3.z, 6);
/// ``` /// ```
pub struct Vector3<T> { pub struct Vector3<T> {
pub x: T, pub x: T,
pub y: T, pub y: T,
pub z: T, pub z: T,
} }
@ -56,10 +56,10 @@ pub struct Vector3<T> {
/// assert_eq!(vec4.w, 8); /// assert_eq!(vec4.w, 8);
/// ``` /// ```
pub struct Vector4<T> { pub struct Vector4<T> {
pub x: T, pub x: T,
pub y: T, pub y: T,
pub z: T, pub z: T,
pub w: T, pub w: T,
} }