feat(core): add more fine grain casting operations for signals
This commit is contained in:
parent
4431b79bcd
commit
aa952c3151
3 changed files with 77 additions and 41 deletions
|
|
@ -1,64 +1,91 @@
|
|||
use core::iter::zip;
|
||||
|
||||
use crate::signals::traits::SignalNumCast;
|
||||
use crate::signals::traits::{SignalNumCast, TrySignalCast};
|
||||
use crate::signals::Signal;
|
||||
use crate::{ArgusError, ArgusResult};
|
||||
|
||||
macro_rules! impl_cast {
|
||||
(bool => $to:ty) => {
|
||||
macro_rules! impl_cast_bool {
|
||||
([$( $to:ty ),*]) => {
|
||||
paste::paste! {
|
||||
#[inline]
|
||||
fn [<to_ $to>](&self) -> Option<Signal<$to>> {
|
||||
match self {
|
||||
Signal::Empty => Some(Signal::Empty),
|
||||
Signal::Constant { value } => num_traits::cast::<_, $to>(*value as i64).map(Signal::constant),
|
||||
Signal::Sampled { values, time_points } => {
|
||||
zip(time_points, values)
|
||||
.map(|(&t, &v)| {
|
||||
let val = num_traits::cast::<_, $to>(v as i64)?;
|
||||
Some((t, val))
|
||||
})
|
||||
.collect()
|
||||
impl SignalNumCast for Signal<bool> {
|
||||
$(
|
||||
#[inline]
|
||||
fn [<to_ $to>](&self) -> Option<Signal<$to>> {
|
||||
match self {
|
||||
Signal::Empty => Some(Signal::Empty),
|
||||
Signal::Constant { value } => num_traits::cast::<_, $to>(*value as i64).map(Signal::constant),
|
||||
Signal::Sampled { values, time_points } => {
|
||||
zip(time_points, values)
|
||||
.map(|(&t, &v)| {
|
||||
let val = num_traits::cast::<_, $to>(v as i64)?;
|
||||
Some((t, val))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
}
|
||||
)*
|
||||
}
|
||||
|
||||
$(
|
||||
impl TrySignalCast<Signal<$to>> for Signal<bool> {
|
||||
fn try_cast(&self) -> ArgusResult<Signal<$to>> {
|
||||
self.[<to_ $to>]().ok_or(ArgusError::InvalidCast {
|
||||
from: std::any::type_name::<bool>(),
|
||||
to: std::any::type_name::<$to>(),
|
||||
})
|
||||
}
|
||||
}
|
||||
)*
|
||||
}
|
||||
};
|
||||
($from:ty => $to:ty) => {
|
||||
() => {
|
||||
impl_cast_bool!([i8, i16, i32, i64, u8, u16, u32, u64, f32, f64]);
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! impl_cast {
|
||||
($from:ty, [$( $to:ty ),*]) => {
|
||||
paste::paste! {
|
||||
#[inline]
|
||||
fn [<to_ $to>](&self) -> Option<Signal<$to>> {
|
||||
match self {
|
||||
Signal::Empty => Some(Signal::Empty),
|
||||
Signal::Constant { value } => num_traits::cast::<_, $to>(*value).map(Signal::constant),
|
||||
Signal::Sampled { values, time_points } => {
|
||||
zip(time_points, values)
|
||||
.map(|(&t, &v)| {
|
||||
let val = num_traits::cast::<_, $to>(v)?;
|
||||
Some((t, val))
|
||||
})
|
||||
.collect()
|
||||
impl SignalNumCast for Signal<$from> {
|
||||
$(
|
||||
#[inline]
|
||||
fn [<to_ $to>](&self) -> Option<Signal<$to>> {
|
||||
match self {
|
||||
Signal::Empty => Some(Signal::Empty),
|
||||
Signal::Constant { value } => num_traits::cast::<_, $to>(*value).map(Signal::constant),
|
||||
Signal::Sampled { values, time_points } => {
|
||||
zip(time_points, values)
|
||||
.map(|(&t, &v)| {
|
||||
let val = num_traits::cast::<_, $to>(v)?;
|
||||
Some((t, val))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
}
|
||||
)*
|
||||
}
|
||||
|
||||
$(
|
||||
impl TrySignalCast<Signal<$to>> for Signal<$from> {
|
||||
fn try_cast(&self) -> ArgusResult<Signal<$to>> {
|
||||
self.[<to_ $to>]().ok_or(ArgusError::InvalidCast {
|
||||
from: std::any::type_name::<$from>(),
|
||||
to: std::any::type_name::<$to>(),
|
||||
})
|
||||
}
|
||||
}
|
||||
)*
|
||||
}
|
||||
};
|
||||
|
||||
($from:ty) => {
|
||||
impl SignalNumCast for Signal<$from> {
|
||||
impl_cast!($from => i8);
|
||||
impl_cast!($from => i16);
|
||||
impl_cast!($from => i32);
|
||||
impl_cast!($from => i64);
|
||||
impl_cast!($from => u8);
|
||||
impl_cast!($from => u16);
|
||||
impl_cast!($from => u32);
|
||||
impl_cast!($from => u64);
|
||||
impl_cast!($from => f32);
|
||||
impl_cast!($from => f64);
|
||||
}
|
||||
impl_cast!($from, [i8, i16, i32, i64, u8, u16, u32, u64, f32, f64]);
|
||||
};
|
||||
}
|
||||
|
||||
impl_cast_bool!();
|
||||
impl_cast!(i8);
|
||||
impl_cast!(i16);
|
||||
impl_cast!(i32);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue