use nami::{Signal, SignalExt};
use num_traits::ToPrimitive;
pub trait IntoSignalF32 {
type Signal: Signal<Output = f32>;
fn into_signal_f32(self) -> Self::Signal;
}
impl<S> IntoSignalF32 for S
where
S: Signal + 'static,
S::Output: IntoF32,
{
type Signal = nami::map::Map<S, fn(S::Output) -> f32, f32>;
fn into_signal_f32(self) -> Self::Signal {
self.map(IntoF32::into_f32)
}
}
pub trait IntoF32: 'static {
fn into_f32(self) -> f32;
}
macro_rules! impl_into_f32 {
($($t:ty),*) => {
$(
impl IntoF32 for $t {
#[inline]
fn into_f32(self) -> f32 {
self.to_f32().unwrap_or_else(|| {
panic!("failed to convert `{}` into f32", stringify!($t))
})
}
}
)*
};
}
impl_into_f32!(f32, f64, i8, i16, i32, i64, isize, u8, u16, u32, u64, usize);