1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
//! Blocks for converting from one type to another.
use anyhow::Result;
use crate::stream::{new_streamp, Streamp};
use crate::{map_block_convert_macro, Float};
/// Convert floats to unsigned 32bit int, scaled if needed.
///
/// `u32 = Float * scale`
pub struct FloatToU32 {
scale: Float,
src: Streamp<Float>,
dst: Streamp<u32>,
}
impl FloatToU32 {
/// Create new FloatToU32, scaled.
///
/// Return value is the input multiplied by the scale. E.g. with a
/// scale of 100.0, the input 0.123 becomes 12.
pub fn new(src: Streamp<Float>, scale: Float) -> Self {
Self {
scale,
src,
dst: new_streamp(),
}
}
fn process_one(&mut self, s: Float) -> u32 {
(s * self.scale) as u32
}
}
map_block_convert_macro![FloatToU32];
/*
struct Convert<From, To> {
scale_from: From,
scale_to: To,
}
impl std::convert::Into<u32> for Float {
fn into(t: Float) -> u32 {
t as u32
}
}
impl<From, To> Convert<From, To>
where From: std::ops::Mul<Output=From> + std::convert::TryInto<To>,
To: std::ops::Mul<Output=To>
{
fn new(scale_from: From, scale_to: To) -> Self {
Self{
scale_from,
scale_to,
}
}
pub fn work(&mut self, r: &mut Stream<From>, w: &mut Stream<To>) -> Result<()>
where <From as TryInto<To>>::Error: std::fmt::Debug
{
let v = r.data.iter().map(|e| {
//From::into(*e * self.scale_from) * self.scale_to
(*e * self.scale_from).try_into().unwrap() * self.scale_to
});
Ok(())
}
}
*/