src/types.rs

branch
dev
changeset 81
d2acaaddd9af
parent 32
e3cfc4917ee7
equal deleted inserted replaced
49:edb95d2b83cc 81:d2acaaddd9af
55 + std::fmt::Debug + std::fmt::Display + serde::Serialize 55 + std::fmt::Debug + std::fmt::Display + serde::Serialize
56 + CastFrom<u8> + CastFrom<u16> + CastFrom<u32> + CastFrom<u64> 56 + CastFrom<u8> + CastFrom<u16> + CastFrom<u32> + CastFrom<u64>
57 + CastFrom<u128> + CastFrom<usize> 57 + CastFrom<u128> + CastFrom<usize>
58 + CastFrom<i8> + CastFrom<i16> + CastFrom<i32> + CastFrom<i64> 58 + CastFrom<i8> + CastFrom<i16> + CastFrom<i32> + CastFrom<i64>
59 + CastFrom<i128> + CastFrom<isize> 59 + CastFrom<i128> + CastFrom<isize>
60 + CastFrom<f32> + CastFrom<f64> { 60 + CastFrom<f32> + CastFrom<f64>
61 + HasScalarField<Field = Self> {
61 62
62 const ZERO : Self; 63 const ZERO : Self;
63 const ONE : Self; 64 const ONE : Self;
64 const TWO : Self; 65 const TWO : Self;
65 /// Generic version of `Self::MAX` 66 /// Generic version of `Self::MAX`
104 const ONE : Self = 1 as $type; 105 const ONE : Self = 1 as $type;
105 const TWO : Self = 2 as $type; 106 const TWO : Self = 2 as $type;
106 const RANGE_MAX : Self = <$type>::MAX; 107 const RANGE_MAX : Self = <$type>::MAX;
107 const RANGE_MIN : Self = <$type>::MIN; 108 const RANGE_MIN : Self = <$type>::MIN;
108 } 109 }
110 impl HasScalarField for $type {
111 type Field = $type;
112 }
109 )* } 113 )* }
110 } 114 }
111 115
112 macro_rules! impl_integers { 116 macro_rules! impl_integers {
113 ($signed:ty : $($type:ty)*) => { $( 117 ($signed:ty : $($type:ty)*) => { $(
161 pub trait CompatibleUnsigned<F : Float> = Unsigned + Into<F>; 165 pub trait CompatibleUnsigned<F : Float> = Unsigned + Into<F>;
162 pub trait CompatibleSigned<F : Float> = Signed + Into<F>; 166 pub trait CompatibleSigned<F : Float> = Signed + Into<F>;
163 } 167 }
164 */ 168 */
165 169
170
171 /// Trait for on-demand cloning as of `Self` as `Target`.
172 ///
173 /// Blanket implementations clone references, but do nothing if `Self` equals `Target`.
174 ///
175 /// The idea is to allow other traits to be implemented generically, similar to now one
176 /// can work with references and owned values through [`std::borrow::Borrow`], but, in
177 /// contrast to the latter, this trait will provide an owned value instead of reference.
178 pub trait CloneIfNeeded<Target> {
179 /// Clone `self` if needed as `Target`.
180 fn clone_if_needed(self) -> Target;
181 }
182
183 impl<'a, T : Clone> CloneIfNeeded<T> for &'a T {
184 #[inline]
185 /// Clone
186 fn clone_if_needed(self) -> T {
187 self.clone()
188 }
189 }
190
191 impl<'a, T : Clone> CloneIfNeeded<T> for &'a mut T {
192 #[inline]
193 /// Clone
194 fn clone_if_needed(self) -> T {
195 self.clone()
196 }
197 }
198
199 impl<'a, T> CloneIfNeeded<T> for T {
200 #[inline]
201 /// No clone needed
202 fn clone_if_needed(self) -> T {
203 self
204 }
205 }
206
207 /// Trait for objects that have an associated scalar field.
208 pub trait HasScalarField {
209 type Field : Num;
210 }
211
212 /// Trait for objects that have an associated real field.
213 pub trait HasRealField : HasScalarField<Field = Self::RealField> {
214 type RealField : Float;
215 }
216
217 impl<T> HasRealField for T where T : HasScalarField, T::Field : Float {
218 type RealField = T::Field;
219 }
220

mercurial