feat!(core): update casting from bool to float to match quantitative semantics

This commit is contained in:
Anand Balakrishnan 2023-04-26 12:58:14 -07:00
parent bcddb7a1a2
commit d3b30deaa3
No known key found for this signature in database

View file

@ -4,11 +4,9 @@ use crate::signals::traits::{SignalNumCast, TrySignalCast};
use crate::signals::Signal; use crate::signals::Signal;
use crate::{ArgusError, ArgusResult}; use crate::{ArgusError, ArgusResult};
macro_rules! impl_cast_bool { macro_rules! impl_bool_to_num {
([$( $to:ty ),*]) => { ($to:ty) => {
paste::paste! { paste::paste! {
impl SignalNumCast for Signal<bool> {
$(
#[inline] #[inline]
fn [<to_ $to>](&self) -> Option<Signal<$to>> { fn [<to_ $to>](&self) -> Option<Signal<$to>> {
match self { match self {
@ -24,24 +22,53 @@ macro_rules! impl_cast_bool {
} }
} }
} }
)* }
};
}
impl SignalNumCast for Signal<bool> {
impl_bool_to_num!(i8);
impl_bool_to_num!(i16);
impl_bool_to_num!(i32);
impl_bool_to_num!(i64);
impl_bool_to_num!(u8);
impl_bool_to_num!(u16);
impl_bool_to_num!(u32);
impl_bool_to_num!(u64);
#[inline]
fn to_f32(&self) -> Option<Signal<f32>> {
match self {
Signal::Empty => Some(Signal::Empty),
Signal::Constant { value } => {
let value: f32 = if *value { f32::INFINITY } else { f32::NEG_INFINITY };
Some(Signal::Constant { value })
}
Signal::Sampled { values, time_points } => zip(time_points, values)
.map(|(&t, &v)| {
let val = num_traits::cast::<_, f32>(v as i64)?;
Some((t, val))
})
.collect(),
}
} }
$( #[inline]
impl TrySignalCast<Signal<$to>> for Signal<bool> { fn to_f64(&self) -> Option<Signal<f64>> {
fn try_cast(&self) -> ArgusResult<Signal<$to>> { match self {
self.[<to_ $to>]().ok_or(ArgusError::InvalidCast { Signal::Empty => Some(Signal::Empty),
from: std::any::type_name::<bool>(), Signal::Constant { value } => {
to: std::any::type_name::<$to>(), let value: f64 = if *value { f64::INFINITY } else { f64::NEG_INFINITY };
Some(Signal::Constant { value })
}
Signal::Sampled { values, time_points } => zip(time_points, values)
.map(|(&t, &v)| {
let val = num_traits::cast::<_, f64>(v as i64)?;
Some((t, val))
}) })
.collect(),
} }
} }
)*
}
};
() => {
impl_cast_bool!([i8, i16, i32, i64, u8, u16, u32, u64, f32, f64]);
};
} }
macro_rules! impl_cast { macro_rules! impl_cast {
@ -85,7 +112,6 @@ macro_rules! impl_cast {
}; };
} }
impl_cast_bool!();
impl_cast!(i8); impl_cast!(i8);
impl_cast!(i16); impl_cast!(i16);
impl_cast!(i32); impl_cast!(i32);