diff --git a/benches/apply.rs b/benches/apply.rs index 19fd41940..590dbf2db 100644 --- a/benches/apply.rs +++ b/benches/apply.rs @@ -19,7 +19,7 @@ pub fn apply(c: &mut Criterion) { group.bench_function(format!("mock-u32-plus-1-{n_samp}"), |b| { b.iter(|| { - let block = Apply::new_typed(|x: &u32| x + 1); + let block = Apply::new(|x: &u32| x + 1); let mut mocker = Mocker::new(block); mocker.input(0, input.clone()); diff --git a/crates/macros/src/lib.rs b/crates/macros/src/lib.rs index 991671e85..fcb0496e8 100644 --- a/crates/macros/src/lib.rs +++ b/crates/macros/src/lib.rs @@ -165,6 +165,8 @@ pub fn connect(attr: proc_macro::TokenStream) -> proc_macro::TokenStream { use futuresdr::runtime::Block; use futuresdr::runtime::Error; use futuresdr::runtime::Flowgraph; + use futuresdr::runtime::Kernel; + use futuresdr::runtime::TypedBlock; use std::result::Result; struct FgOp; @@ -181,6 +183,11 @@ pub fn connect(attr: proc_macro::TokenStream) -> proc_macro::TokenStream { fg.add_block(b) } } + impl Add> for FgOp { + fn add(fg: &mut Flowgraph, b: TypedBlock) -> Result { + fg.add_block(b) + } + } }); // Add the blocks to the flowgraph diff --git a/examples/egui/src/lib.rs b/examples/egui/src/lib.rs index 0b713ea34..083005bf4 100644 --- a/examples/egui/src/lib.rs +++ b/examples/egui/src/lib.rs @@ -10,5 +10,5 @@ use futuresdr::num_complex::Complex32; use futuresdr::runtime::Block; pub fn power_block() -> Block { - Apply::new(|x: &Complex32| x.norm_sqr()) + Apply::new(|x: &Complex32| x.norm_sqr()).into() } diff --git a/examples/m17/src/moving_average.rs b/examples/m17/src/moving_average.rs index a51199367..73dbe12cd 100644 --- a/examples/m17/src/moving_average.rs +++ b/examples/m17/src/moving_average.rs @@ -19,11 +19,7 @@ pub struct MovingAverage { } impl MovingAverage { - pub fn new(len: usize) -> Block { - Block::from_typed(Self::new_typed(len)) - } - - pub fn new_typed(len: usize) -> TypedBlock { + pub fn new(len: usize) -> TypedBlock { assert!(len > 0); TypedBlock::new( BlockMetaBuilder::new("MovingAverage").build(), diff --git a/examples/mock.rs b/examples/mock.rs index 7063dbfc4..4ad7222b2 100644 --- a/examples/mock.rs +++ b/examples/mock.rs @@ -9,7 +9,7 @@ fn main() { .take(64) .collect(); - let block = Apply::new_typed(|x: &u32| x + 1); + let block = Apply::new(|x: &u32| x + 1); let mut mocker = Mocker::new(block); mocker.input(0, input.clone()); diff --git a/examples/spectrum/src/lib.rs b/examples/spectrum/src/lib.rs index 2e8d78ea2..2c462a504 100644 --- a/examples/spectrum/src/lib.rs +++ b/examples/spectrum/src/lib.rs @@ -11,13 +11,13 @@ use futuresdr::num_complex::Complex32; use futuresdr::runtime::Block; pub fn lin2db_block() -> Block { - Apply::new(|x: &f32| 10.0 * x.log10()) + Apply::new(|x: &f32| 10.0 * x.log10()).into() } pub fn power_block() -> Block { - Apply::new(|x: &Complex32| x.norm_sqr()) + Apply::new(|x: &Complex32| x.norm_sqr()).into() } pub fn lin2power_db() -> Block { - Apply::new(|x: &Complex32| 10.0 * x.norm_sqr().log10()) + Apply::new(|x: &Complex32| 10.0 * x.norm_sqr().log10()).into() } diff --git a/examples/wlan/src/moving_average.rs b/examples/wlan/src/moving_average.rs index af5760d29..054689514 100644 --- a/examples/wlan/src/moving_average.rs +++ b/examples/wlan/src/moving_average.rs @@ -45,11 +45,7 @@ pub struct MovingAverage { } impl MovingAverage { - pub fn new(len: usize) -> Block { - Block::from_typed(Self::new_typed(len)) - } - - pub fn new_typed(len: usize) -> TypedBlock { + pub fn new(len: usize) -> TypedBlock { assert!(len > 0); TypedBlock::new( BlockMetaBuilder::new("MovingAverage").build(), diff --git a/src/blocks/apply.rs b/src/blocks/apply.rs index 194f21230..982722f0b 100644 --- a/src/blocks/apply.rs +++ b/src/blocks/apply.rs @@ -1,5 +1,4 @@ use crate::anyhow::Result; -use crate::runtime::Block; use crate::runtime::BlockMeta; use crate::runtime::BlockMetaBuilder; use crate::runtime::Kernel; @@ -68,15 +67,7 @@ where /// /// ## Parameter /// - `f`: Function to apply on each sample - pub fn new(f: F) -> Block { - Block::from_typed(Self::new_typed(f)) - } - - /// Create typed [`Apply`] block - /// - /// ## Parameter - /// - `f`: Function to apply on each sample - pub fn new_typed(f: F) -> TypedBlock { + pub fn new(f: F) -> TypedBlock { TypedBlock::new( BlockMetaBuilder::new("Apply").build(), StreamIoBuilder::new() diff --git a/src/blocks/combine.rs b/src/blocks/combine.rs index 81743159d..ea956781d 100644 --- a/src/blocks/combine.rs +++ b/src/blocks/combine.rs @@ -24,13 +24,10 @@ use crate::runtime::WorkIo; /// # Usage /// ``` /// use futuresdr::blocks::Combine; -/// use futuresdr::runtime::Flowgraph; /// -/// let mut fg = Flowgraph::new(); -/// -/// let adder = fg.add_block(Combine::new(|a: &f32, b: &f32| { +/// let adder = Combine::new(|a: &f32, b: &f32| { /// a + b -/// }))?; +/// }); /// ``` #[allow(clippy::type_complexity)] pub struct Combine diff --git a/src/blocks/message_copy.rs b/src/blocks/message_copy.rs index 607c83f03..eb4331c7b 100644 --- a/src/blocks/message_copy.rs +++ b/src/blocks/message_copy.rs @@ -1,4 +1,3 @@ -use crate::runtime::Block; use crate::runtime::BlockMeta; use crate::runtime::BlockMetaBuilder; use crate::runtime::Kernel; @@ -14,12 +13,7 @@ pub struct MessageCopy {} impl MessageCopy { /// Create MessageCopy block - pub fn new() -> Block { - Self::new_typed().into() - } - - /// Create MessageCopy block - pub fn new_typed() -> TypedBlock { + pub fn new() -> TypedBlock { TypedBlock::new( BlockMetaBuilder::new("MessageCopy").build(), StreamIoBuilder::new().build(), diff --git a/src/blocks/moving_avg.rs b/src/blocks/moving_avg.rs index 63217609d..a62b1313c 100644 --- a/src/blocks/moving_avg.rs +++ b/src/blocks/moving_avg.rs @@ -1,5 +1,4 @@ use crate::anyhow::Result; -use crate::runtime::Block; use crate::runtime::BlockMeta; use crate::runtime::BlockMetaBuilder; use crate::runtime::Kernel; @@ -25,22 +24,7 @@ pub struct MovingAvg { } impl MovingAvg { - /// Instantiate moving average as a [`Block`]. - /// - /// # Arguments - /// - /// * `decay_factor`: amount current value should contribute to the rolling average. - /// Must be in `[0.0, 1.0]`. - /// * `history_size`: number of chunks to average over - /// - /// Typical parameter values might be `decay_factor=0.1` and `history_size=3` - /// # Panics - /// Function will panic if `decay_factor` is not in `[0.0, 1.0]` - pub fn new(decay_factor: f32, history_size: usize) -> Block { - Block::from_typed(Self::new_typed(decay_factor, history_size)) - } - - /// Instantiate moving average as a [`TypedBlock`]. + /// Instantiate moving average. /// /// # Arguments /// @@ -52,7 +36,7 @@ impl MovingAvg { /// /// # Panics /// Function will panic if `decay_factor` is not in `[0.0, 1.0]` - pub fn new_typed(decay_factor: f32, history_size: usize) -> TypedBlock { + pub fn new(decay_factor: f32, history_size: usize) -> TypedBlock { assert!( (0.0..=1.0).contains(&decay_factor), "decay_factor must be in [0, 1]" diff --git a/src/blocks/seify/builder.rs b/src/blocks/seify/builder.rs index 1a57ab01d..fb96fb480 100644 --- a/src/blocks/seify/builder.rs +++ b/src/blocks/seify/builder.rs @@ -115,7 +115,7 @@ impl Builder { match (self.dev.take(), self.builder_type) { (Some(dev), BuilderType::Source) => { self.config.apply(&dev, &self.channels, Direction::Rx)?; - Ok(Source::new_typed(dev, self.channels, self.start_time)) + Ok(Source::new(dev, self.channels, self.start_time)) } _ => Err(Error::InvalidParameter), } @@ -125,7 +125,7 @@ impl Builder { match (self.dev.take(), self.builder_type) { (Some(dev), BuilderType::Sink) => { self.config.apply(&dev, &self.channels, Direction::Tx)?; - Ok(Sink::new_typed(dev, self.channels, self.start_time)) + Ok(Sink::new(dev, self.channels, self.start_time)) } _ => Err(Error::InvalidParameter), } @@ -136,11 +136,11 @@ impl Builder { Some(dev) => match self.builder_type { BuilderType::Sink => { self.config.apply(&dev, &self.channels, Direction::Tx)?; - Ok(Sink::new(dev, self.channels, self.start_time)) + Ok(Sink::new(dev, self.channels, self.start_time).into()) } BuilderType::Source => { self.config.apply(&dev, &self.channels, Direction::Rx)?; - Ok(Source::new(dev, self.channels, self.start_time)) + Ok(Source::new(dev, self.channels, self.start_time).into()) } }, None => { @@ -148,11 +148,11 @@ impl Builder { match self.builder_type { BuilderType::Sink => { self.config.apply(&dev, &self.channels, Direction::Tx)?; - Ok(Sink::new(dev, self.channels, self.start_time)) + Ok(Sink::new(dev, self.channels, self.start_time).into()) } BuilderType::Source => { self.config.apply(&dev, &self.channels, Direction::Rx)?; - Ok(Source::new(dev, self.channels, self.start_time)) + Ok(Source::new(dev, self.channels, self.start_time).into()) } } } diff --git a/src/blocks/seify/sink.rs b/src/blocks/seify/sink.rs index 81bb30b7a..c4f571af6 100644 --- a/src/blocks/seify/sink.rs +++ b/src/blocks/seify/sink.rs @@ -10,7 +10,6 @@ use crate::anyhow::Result; use crate::blocks::seify::Builder; use crate::blocks::seify::Config; use crate::num_complex::Complex32; -use crate::runtime::Block; use crate::runtime::BlockMeta; use crate::runtime::BlockMetaBuilder; use crate::runtime::ItemTag; @@ -50,11 +49,7 @@ pub struct Sink { } impl Sink { - pub(super) fn new(dev: Device, channels: Vec, start_time: Option) -> Block { - Self::new_typed(dev, channels, start_time).into() - } - - pub(super) fn new_typed( + pub(super) fn new( dev: Device, channels: Vec, start_time: Option, diff --git a/src/blocks/seify/source.rs b/src/blocks/seify/source.rs index c9dd18230..f2137a1b2 100644 --- a/src/blocks/seify/source.rs +++ b/src/blocks/seify/source.rs @@ -11,7 +11,6 @@ use crate::blocks::seify::builder::BuilderType; use crate::blocks::seify::Builder; use crate::blocks::seify::Config; use crate::num_complex::Complex32; -use crate::runtime::Block; use crate::runtime::BlockMeta; use crate::runtime::BlockMetaBuilder; use crate::runtime::Kernel; @@ -47,11 +46,7 @@ pub struct Source { } impl Source { - pub(super) fn new(dev: Device, channels: Vec, start_time: Option) -> Block { - Self::new_typed(dev, channels, start_time).into() - } - - pub(super) fn new_typed( + pub(super) fn new( dev: Device, channels: Vec, start_time: Option, diff --git a/src/runtime/flowgraph.rs b/src/runtime/flowgraph.rs index 4bbe469dc..a60f7432c 100644 --- a/src/runtime/flowgraph.rs +++ b/src/runtime/flowgraph.rs @@ -39,8 +39,8 @@ impl Flowgraph { } /// Add [`Block`] to flowgraph - pub fn add_block(&mut self, block: Block) -> Result { - self.topology.as_mut().unwrap().add_block(block) + pub fn add_block(&mut self, block: impl Into) -> Result { + self.topology.as_mut().unwrap().add_block(block.into()) } /// Make stream connection diff --git a/src/runtime/topology.rs b/src/runtime/topology.rs index 8877be5ae..1dce3e88b 100644 --- a/src/runtime/topology.rs +++ b/src/runtime/topology.rs @@ -1,5 +1,11 @@ use futures::channel::mpsc::Sender; +use slab::Slab; +use std::any::Any; +use std::any::TypeId; use std::collections::HashMap; +use std::fmt::Debug; +use std::hash::Hash; +use std::hash::Hasher; use crate::runtime::buffer::BufferBuilder; use crate::runtime::buffer::BufferWriter; @@ -8,12 +14,6 @@ use crate::runtime::BlockMessage; use crate::runtime::ConnectCtx; use crate::runtime::Error; use crate::runtime::PortId; -use slab::Slab; -use std::any::Any; -use std::any::TypeId; -use std::fmt::Debug; -use std::hash::Hash; -use std::hash::Hasher; pub trait BufferBuilderKey: Debug + Send + Sync { fn eq(&self, other: &dyn BufferBuilderKey) -> bool; diff --git a/tests/mocker.rs b/tests/mocker.rs index a71a3d847..b1b8ee951 100644 --- a/tests/mocker.rs +++ b/tests/mocker.rs @@ -16,7 +16,7 @@ fn multi_input_mock() { .take(128) .collect(); - let block = Apply::new_typed(|x: &u32| x + 1); + let block = Apply::new(|x: &u32| x + 1); let mut mocker = Mocker::new(block); mocker.input(0, input[..64].to_vec()); @@ -34,7 +34,7 @@ fn multi_input_mock() { #[test] fn tags_through_mock() -> Result<()> { - let mut noop = Apply::<_, f32, f32>::new_typed(|x| *x); + let mut noop = Apply::<_, f32, f32>::new(|x| *x); noop.sio.set_tag_propagation(Box::new(copy_tag_propagation)); let mut mock = Mocker::new(noop); @@ -91,7 +91,7 @@ fn tags_through_mock() -> Result<()> { #[test] fn mock_pmts() -> Result<()> { - let copy = MessageCopy::new_typed(); + let copy = MessageCopy::new(); let mut mock = Mocker::new(copy); mock.init(); diff --git a/tests/moving_avg.rs b/tests/moving_avg.rs index 1954c652f..b55f91827 100644 --- a/tests/moving_avg.rs +++ b/tests/moving_avg.rs @@ -3,7 +3,7 @@ use futuresdr::runtime::Mocker; #[test] fn moving_avg_correct_output() { - let block = MovingAvg::<3>::new_typed(0.1, 3); + let block = MovingAvg::<3>::new(0.1, 3); let mut mocker = Mocker::new(block); mocker.input::(0, vec![1.0, 2.0, 3.0, 1.0, 2.0, 3.0, 1.0, 2.0, 3.0]); @@ -15,7 +15,7 @@ fn moving_avg_correct_output() { #[test] fn moving_avg_handles_non_finite_values() { - let block = MovingAvg::<3>::new_typed(0.1, 3); + let block = MovingAvg::<3>::new(0.1, 3); let mut mocker = Mocker::new(block); mocker.input::( 0,