src/types.rs

Tue, 31 Dec 2024 09:12:43 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Tue, 31 Dec 2024 09:12:43 -0500
branch
dev
changeset 81
d2acaaddd9af
parent 32
e3cfc4917ee7
permissions
-rw-r--r--

Try to have Field as member type in Mappings etc.

5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
1 /*!
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
2 Some useful (numerical) types and traits.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
3
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
4 The traits are based on corresponding ones in [`num_traits`], but try to fill some gaps in the
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
5 super-traits and available constants.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
6
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
7 As [`nalgebra`] unnecessarily provides many of the same methods as [`num_traits`], to avoid having
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
8 to refer to the methods with the full path, it is often necesary to use [`ToNalgebraRealField`][crate::nalgebra_support::ToNalgebraRealField] to hide the nalgebra implementations until
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
9 absolutely necessary to use nalgebra.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
10 */
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
11
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
12 //use trait_set::trait_set;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
13 pub use num_traits::Float as NumTraitsFloat; // needed to re-export functions.
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
14 pub use num_traits::cast::AsPrimitive;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
15
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
16 /// Typical integer type
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
17 #[allow(non_camel_case_types)]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
18 pub type int = i64;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
19
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
20 /// Typical unsigned integer type
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
21 #[allow(non_camel_case_types)]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
22 pub type uint = u64;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
23
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
24 /// Typical floating point number type
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
25 #[allow(non_camel_case_types)]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
26 pub type float = f64;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
27
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
28 /// Casts of abstract numerical types to others via the standard `as` keyword.
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
29 pub trait CastFrom<T : 'static + Copy> : num_traits::cast::AsPrimitive<T> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
30 fn cast_from(other : T) -> Self;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
31 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
32
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
33 macro_rules! impl_casts {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
34 ($($type:ty)*) => { $(
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
35 impl_casts!(@phase2, $type,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
36 u8 u16 u32 u64 u128 usize
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
37 i8 i16 i32 i64 i128 isize
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
38 f32 f64);
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
39 )* };
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
40 (@phase2, $type:ty, $($type2:ty)*) => { $(
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
41 impl CastFrom<$type2> for $type {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
42 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
43 fn cast_from(other : $type2) -> Self { other as $type }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
44 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
45 )* };
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
46 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
47
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
48 impl_casts!(u8 u16 u32 u64 u128 usize
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
49 i8 i16 i32 i64 i128 isize
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
50 f32 f64);
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
51
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
52 /// Trait for general numeric types
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
53 pub trait Num : 'static + Copy + Sync + Send + num::Num + num_traits::NumAssign
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
54 + std::iter::Sum + std::iter::Product
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
55 + std::fmt::Debug + std::fmt::Display + serde::Serialize
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
56 + CastFrom<u8> + CastFrom<u16> + CastFrom<u32> + CastFrom<u64>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
57 + CastFrom<u128> + CastFrom<usize>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
58 + CastFrom<i8> + CastFrom<i16> + CastFrom<i32> + CastFrom<i64>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
59 + CastFrom<i128> + CastFrom<isize>
81
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
60 + CastFrom<f32> + CastFrom<f64>
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
61 + HasScalarField<Field = Self> {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
62
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
63 const ZERO : Self;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
64 const ONE : Self;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
65 const TWO : Self;
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
66 /// Generic version of `Self::MAX`
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
67 const RANGE_MAX : Self;
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
68 /// Generic version of `Self::MIN`
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
69 const RANGE_MIN : Self;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
70 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
71
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
72 /// Trait for signed numeric types
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
73 pub trait SignedNum : Num + num::Signed + std::ops::Neg<Output=Self> {}
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
74 impl<U : Num + num::Signed + std::ops::Neg<Output=Self>> SignedNum for U { }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
75
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
76 /// Trait for floating point numbers
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
77 pub trait Float : SignedNum + num::Float /*+ From<Self::CompatibleSize>*/ {
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
78 // An unsigned integer that can be used for indexing operations and
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
79 // converted to F without loss.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
80 //type CompatibleSize : CompatibleUnsigned<Self>;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
81
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
82 const PI : Self;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
83 const E : Self;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
84 const EPSILON : Self;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
85 const SQRT_2 : Self;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
86 const INFINITY : Self;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
87 const NEG_INFINITY : Self;
32
e3cfc4917ee7 NAN constant
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
88 const NAN : Self;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
89 const FRAC_2_SQRT_PI : Self;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
90 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
91
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
92 /// Trait for integers
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
93 pub trait Integer : Num + num::Integer {}
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
94
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
95 /// Trait for unsigned integers
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
96 pub trait Unsigned : Num + Integer + num::Unsigned {}
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
97
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
98 /// Trait for signed integers
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
99 pub trait Signed : SignedNum + Integer {}
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
100
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
101 macro_rules! impl_num_consts {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
102 ($($type:ty)*) => { $(
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
103 impl Num for $type {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
104 const ZERO : Self = 0 as $type;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
105 const ONE : Self = 1 as $type;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
106 const TWO : Self = 2 as $type;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
107 const RANGE_MAX : Self = <$type>::MAX;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
108 const RANGE_MIN : Self = <$type>::MIN;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
109 }
81
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
110 impl HasScalarField for $type {
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
111 type Field = $type;
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
112 }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
113 )* }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
114 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
115
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
116 macro_rules! impl_integers {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
117 ($signed:ty : $($type:ty)*) => { $(
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
118 impl_num_consts!($type);
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
119 impl Integer for $type {}
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
120 impl $signed for $type {}
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
121 )* }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
122 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
123
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
124 impl_integers!(Signed: i8 i16 i32 i64 i128 isize);
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
125 impl_integers!(Unsigned: u8 u16 u32 u64 u128 usize);
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
126
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
127 impl_num_consts!(f32 f64);
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
128
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
129 impl Float for f64 {
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
130 /*#[cfg(any(target_pointer_width = "128", target_pointer_width = "64"))]
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
131 type CompatibleSize = u32;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
132 #[cfg(any(target_pointer_width = "32", target_pointer_width = "16"))]
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
133 type CompatibleSize = usize;*/
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
134
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
135 const PI : Self = std::f64::consts::PI;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
136 const E : Self = std::f64::consts::E;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
137 const EPSILON : Self = std::f64::EPSILON;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
138 const SQRT_2 : Self = std::f64::consts::SQRT_2;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
139 const INFINITY : Self = std::f64::INFINITY;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
140 const NEG_INFINITY : Self = std::f64::NEG_INFINITY;
32
e3cfc4917ee7 NAN constant
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
141 const NAN : Self = std::f64::NAN;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
142 const FRAC_2_SQRT_PI : Self = std::f64::consts::FRAC_2_SQRT_PI;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
143 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
144
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
145 impl Float for f32 {
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
146 /*
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
147 #[cfg(any(target_pointer_width = "128", target_pointer_width = "64", target_pointer_width = "32"))]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
148 type CompatibleSize = u16;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
149 #[cfg(any(target_pointer_width = "16"))]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
150 type CompatibleSize = usize;
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
151 */
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
152
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
153 const PI : Self = std::f32::consts::PI;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
154 const E : Self = std::f32::consts::E;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
155 const EPSILON : Self = std::f32::EPSILON;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
156 const SQRT_2 : Self = std::f32::consts::SQRT_2;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
157 const INFINITY : Self = std::f32::INFINITY;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
158 const NEG_INFINITY : Self = std::f32::NEG_INFINITY;
32
e3cfc4917ee7 NAN constant
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
159 const NAN : Self = std::f32::NAN;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
160 const FRAC_2_SQRT_PI : Self = std::f32::consts::FRAC_2_SQRT_PI;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
161 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
162
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
163 /*
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
164 trait_set! {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
165 pub trait CompatibleUnsigned<F : Float> = Unsigned + Into<F>;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
166 pub trait CompatibleSigned<F : Float> = Signed + Into<F>;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
167 }
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
168 */
13
465fa2121ccb Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents: 8
diff changeset
169
81
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
170
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
171 /// Trait for on-demand cloning as of `Self` as `Target`.
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
172 ///
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
173 /// Blanket implementations clone references, but do nothing if `Self` equals `Target`.
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
174 ///
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
175 /// The idea is to allow other traits to be implemented generically, similar to now one
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
176 /// can work with references and owned values through [`std::borrow::Borrow`], but, in
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
177 /// contrast to the latter, this trait will provide an owned value instead of reference.
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
178 pub trait CloneIfNeeded<Target> {
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
179 /// Clone `self` if needed as `Target`.
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
180 fn clone_if_needed(self) -> Target;
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
181 }
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
182
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
183 impl<'a, T : Clone> CloneIfNeeded<T> for &'a T {
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
184 #[inline]
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
185 /// Clone
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
186 fn clone_if_needed(self) -> T {
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
187 self.clone()
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
188 }
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
189 }
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
190
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
191 impl<'a, T : Clone> CloneIfNeeded<T> for &'a mut T {
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
192 #[inline]
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
193 /// Clone
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
194 fn clone_if_needed(self) -> T {
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
195 self.clone()
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
196 }
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
197 }
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
198
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
199 impl<'a, T> CloneIfNeeded<T> for T {
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
200 #[inline]
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
201 /// No clone needed
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
202 fn clone_if_needed(self) -> T {
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
203 self
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
204 }
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
205 }
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
206
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
207 /// Trait for objects that have an associated scalar field.
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
208 pub trait HasScalarField {
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
209 type Field : Num;
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
210 }
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
211
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
212 /// Trait for objects that have an associated real field.
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
213 pub trait HasRealField : HasScalarField<Field = Self::RealField> {
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
214 type RealField : Float;
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
215 }
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
216
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
217 impl<T> HasRealField for T where T : HasScalarField, T::Field : Float {
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
218 type RealField = T::Field;
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
219 }
d2acaaddd9af Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 32
diff changeset
220

mercurial