Skip to content

Commit 4e3794f

Browse files
committed
Add RealConvert impl for float types
1 parent bed058f commit 4e3794f

File tree

1 file changed

+49
-4
lines changed

1 file changed

+49
-4
lines changed

splashsurf_lib/src/traits.rs

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,35 +62,42 @@ pub trait Index:
6262
+ ThreadSafe
6363
+ 'static
6464
{
65+
#[inline]
6566
fn range(start: Self, end: Self) -> IndexRange<Self> {
6667
IndexRange::new(start, end)
6768
}
6869

70+
#[inline(always)]
6971
fn two() -> Self {
7072
Self::one() + Self::one()
7173
}
7274

7375
/// Converts this value to the specified [`Real`] type `T` by converting first to `f64` followed by `T::from_f64`. If the value cannot be represented by the target type, `None` is returned.
76+
#[inline]
7477
fn to_real<R: Real>(self) -> Option<R> {
7578
R::from_f64(self.to_f64()?)
7679
}
7780

7881
/// Converts this value to the specified [`Real`] type, panics if the value cannot be represented by the target type.
82+
#[inline]
7983
fn to_real_unchecked<R: Real>(self) -> R {
8084
R::from_f64(self.to_f64().unwrap()).unwrap()
8185
}
8286

8387
/// Multiplies this value by the specified `i32` coefficient. Panics if the coefficient cannot be converted into the target type.
88+
#[inline]
8489
fn times(self, n: i32) -> Self {
8590
self.mul(Self::from_i32(n).unwrap())
8691
}
8792

8893
/// Returns the squared value of this value.
94+
#[inline]
8995
fn squared(self) -> Self {
9096
self * self
9197
}
9298

9399
/// Returns the cubed value of this value.
100+
#[inline]
94101
fn cubed(self) -> Self {
95102
self * self * self
96103
}
@@ -117,19 +124,21 @@ pub trait Real:
117124
+ ThreadSafe
118125
{
119126
/// Converts the given float value to this Real type
120-
#[inline]
127+
#[inline(always)]
121128
fn from_float<T>(value: T) -> Self
122-
where Self: SupersetOf<T>
129+
where Self: SupersetOf<T>
123130
{
124131
Self::from_subset(&value)
125132
}
126-
133+
127134
/// Converts this value to the specified [`Index`] type. If the value cannot be represented by the target type, `None` is returned.
135+
#[inline]
128136
fn to_index<I: Index>(self) -> Option<I> {
129137
I::from_f64(self.to_f64()?)
130138
}
131139

132140
/// Converts this value to the specified [`Index`] type, panics if the value cannot be represented by the target type.
141+
#[inline]
133142
fn to_index_unchecked<I: Index>(self) -> I {
134143
I::from_f64(self.to_f64().unwrap()).unwrap()
135144
}
@@ -206,6 +215,42 @@ impl<From: Real> RealConvert for &From {
206215
}
207216
}
208217

218+
impl RealConvert for f32 {
219+
type Out<To>
220+
= To
221+
where
222+
To: Real;
223+
224+
/// Converts this `f32` value to the target `Real` type. Never returns `None`.
225+
#[inline(always)]
226+
fn try_convert<To: Real>(self) -> Option<To> {
227+
Some(To::from_float(self))
228+
}
229+
/// Converts this `f32` value to the target `Real` type. Never panics.
230+
#[inline(always)]
231+
fn convert<To: Real>(self) -> To {
232+
To::from_float(self)
233+
}
234+
}
235+
236+
impl RealConvert for f64 {
237+
type Out<To>
238+
= To
239+
where
240+
To: Real;
241+
242+
/// Converts this `f64` value to the target `Real` type. Never returns `None`.
243+
#[inline(always)]
244+
fn try_convert<To: Real>(self) -> Option<To> {
245+
Some(To::from_float(self))
246+
}
247+
/// Converts this `f64` value to the target `Real` type. Never panics.
248+
#[inline(always)]
249+
fn convert<To: Real>(self) -> To {
250+
To::from_float(self)
251+
}
252+
}
253+
209254
impl<From: Real, const R: usize, const C: usize> RealConvert for SMatrix<From, R, C> {
210255
type Out<To>
211256
= SMatrix<To, R, C>
@@ -219,7 +264,7 @@ impl<From: Real, const R: usize, const C: usize> RealConvert for SMatrix<From, R
219264
.iter_mut()
220265
.zip(self.iter())
221266
.try_for_each(|(x_out, x_in)| {
222-
*x_out = <To as NumCast>::from(*x_in)?;
267+
*x_out = (*x_in).try_convert()?;
223268
Some(())
224269
})?;
225270
Some(m_out)

0 commit comments

Comments
 (0)