diff --git a/benches/callbacks_bench.rs b/benches/callbacks_bench.rs index 73e20863..6fccd446 100644 --- a/benches/callbacks_bench.rs +++ b/benches/callbacks_bench.rs @@ -14,7 +14,7 @@ fn bench_call_all(c: &mut Criterion) { number_of_callbacks, |b, &number_of_callbacks| { let rt = Runtime::new().unwrap(); - let callbacks = Callbacks::::new(); + let callbacks = Callbacks::::default(); for _ in 0..number_of_callbacks { callbacks.add_callback({ diff --git a/src/lib/callbacks.rs b/src/lib/callbacks.rs index 463b77dc..17bdbd4d 100644 --- a/src/lib/callbacks.rs +++ b/src/lib/callbacks.rs @@ -12,13 +12,21 @@ pub struct Callbacks { callbacks: Arc>>>, } -impl Callbacks { - pub fn new() -> Self { +impl Default for Callbacks { + fn default() -> Self { Self { callbacks: Arc::new(Mutex::new(IndexMap::new())), } } +} + +impl std::fmt::Debug for Callbacks { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Callbacks").finish() + } +} +impl Callbacks { pub fn add_callback(&self, callback: F) -> usize where F: Fn(T) -> Fut + Send + Sync + 'static, @@ -93,7 +101,7 @@ mod tests { #[tokio::test] async fn test_callbacks() { - let callbacks = Callbacks::::new(); + let callbacks = Callbacks::::default(); let (tx1, rx1) = oneshot::channel(); let tx1 = Arc::new(Mutex::new(Some(tx1))); diff --git a/src/lib/drivers/fake.rs b/src/lib/drivers/fake.rs index 4ac0937a..e42844f7 100644 --- a/src/lib/drivers/fake.rs +++ b/src/lib/drivers/fake.rs @@ -29,7 +29,7 @@ impl FakeSink { FakeSinkBuilder(Self { name: arc_swap::ArcSwap::new(name.clone()), uuid: Self::generate_uuid(&name), - on_message_input: Callbacks::new(), + on_message_input: Callbacks::default(), print: false, stats: Arc::new(RwLock::new(AccumulatedDriverStats::new( name, @@ -164,7 +164,7 @@ impl FakeSource { name: arc_swap::ArcSwap::new(name.clone()), uuid: Self::generate_uuid(&name), period, - on_message_output: Callbacks::new(), + on_message_output: Callbacks::default(), stats: Arc::new(RwLock::new(AccumulatedDriverStats::new( name, &FakeSourceInfo, diff --git a/src/lib/drivers/mod.rs b/src/lib/drivers/mod.rs index 90b2681a..ec9d4420 100644 --- a/src/lib/drivers/mod.rs +++ b/src/lib/drivers/mod.rs @@ -268,7 +268,7 @@ mod tests { ExampleDriverBuilder(Self { name: arc_swap::ArcSwap::new(name.clone()), uuid: Self::generate_uuid(&name), - on_message_input: Callbacks::new(), + on_message_input: Callbacks::default(), stats: Arc::new(RwLock::new(AccumulatedDriverStats::new( name, &ExampleDriverInfo, diff --git a/src/lib/drivers/rest/mod.rs b/src/lib/drivers/rest/mod.rs index 69b94583..d1c31ccd 100644 --- a/src/lib/drivers/rest/mod.rs +++ b/src/lib/drivers/rest/mod.rs @@ -63,8 +63,8 @@ impl Rest { RestBuilder(Self { name: arc_swap::ArcSwap::new(name.clone()), uuid: Self::generate_uuid(&name), - on_message_input: Callbacks::new(), - on_message_output: Callbacks::new(), + on_message_input: Callbacks::default(), + on_message_output: Callbacks::default(), stats: Arc::new(RwLock::new(AccumulatedDriverStats::new(name, &RestInfo))), }) } diff --git a/src/lib/drivers/serial/mod.rs b/src/lib/drivers/serial/mod.rs index 95293598..530904f9 100644 --- a/src/lib/drivers/serial/mod.rs +++ b/src/lib/drivers/serial/mod.rs @@ -62,8 +62,8 @@ impl Serial { uuid: Self::generate_uuid(&format!("{port_name}:{baud_rate}")), port_name: port_name.to_string(), baud_rate, - on_message_input: Callbacks::new(), - on_message_output: Callbacks::new(), + on_message_input: Callbacks::default(), + on_message_output: Callbacks::default(), stats: Arc::new(RwLock::new(AccumulatedDriverStats::new(name, &SerialInfo))), }) } diff --git a/src/lib/drivers/tcp/client.rs b/src/lib/drivers/tcp/client.rs index 706aaaef..8c702fad 100644 --- a/src/lib/drivers/tcp/client.rs +++ b/src/lib/drivers/tcp/client.rs @@ -62,8 +62,8 @@ impl TcpClient { remote_addr: remote_addr.to_string(), name: arc_swap::ArcSwap::new(name.clone()), uuid: Self::generate_uuid(remote_addr), - on_message_input: Callbacks::new(), - on_message_output: Callbacks::new(), + on_message_input: Callbacks::default(), + on_message_output: Callbacks::default(), stats: Arc::new(RwLock::new(AccumulatedDriverStats::new( name, &TcpClientInfo, diff --git a/src/lib/drivers/tcp/server.rs b/src/lib/drivers/tcp/server.rs index f7791f2b..227aea6c 100644 --- a/src/lib/drivers/tcp/server.rs +++ b/src/lib/drivers/tcp/server.rs @@ -62,8 +62,8 @@ impl TcpServer { local_addr: local_addr.to_string(), name: arc_swap::ArcSwap::new(name.clone()), uuid: Self::generate_uuid(local_addr), - on_message_input: Callbacks::new(), - on_message_output: Callbacks::new(), + on_message_input: Callbacks::default(), + on_message_output: Callbacks::default(), stats: Arc::new(RwLock::new(AccumulatedDriverStats::new( name, &TcpServerInfo, diff --git a/src/lib/drivers/tlog/reader.rs b/src/lib/drivers/tlog/reader.rs index e3c7a84c..eeb78a03 100644 --- a/src/lib/drivers/tlog/reader.rs +++ b/src/lib/drivers/tlog/reader.rs @@ -50,7 +50,7 @@ impl TlogReader { path, name: arc_swap::ArcSwap::new(name.clone()), uuid: Self::generate_uuid(&path_str), - on_message_input: Callbacks::new(), + on_message_input: Callbacks::default(), stats: Arc::new(RwLock::new(AccumulatedDriverStats::new( name, &TlogReaderInfo, diff --git a/src/lib/drivers/tlog/writer.rs b/src/lib/drivers/tlog/writer.rs index 3a61d30a..17ff26f3 100644 --- a/src/lib/drivers/tlog/writer.rs +++ b/src/lib/drivers/tlog/writer.rs @@ -51,7 +51,7 @@ impl TlogWriter { path, name: arc_swap::ArcSwap::new(name.clone()), uuid: Self::generate_uuid(&path_str), - on_message_output: Callbacks::new(), + on_message_output: Callbacks::default(), stats: Arc::new(RwLock::new(AccumulatedDriverStats::new( name, &TlogWriterInfo, diff --git a/src/lib/drivers/udp/client.rs b/src/lib/drivers/udp/client.rs index f3ddd008..d0c9a5ba 100644 --- a/src/lib/drivers/udp/client.rs +++ b/src/lib/drivers/udp/client.rs @@ -59,8 +59,8 @@ impl UdpClient { remote_addr: remote_addr.to_string(), name: arc_swap::ArcSwap::new(name.clone()), uuid: Self::generate_uuid(remote_addr), - on_message_input: Callbacks::new(), - on_message_output: Callbacks::new(), + on_message_input: Callbacks::default(), + on_message_output: Callbacks::default(), stats: Arc::new(RwLock::new(AccumulatedDriverStats::new( name, &UdpClientInfo, diff --git a/src/lib/drivers/udp/server.rs b/src/lib/drivers/udp/server.rs index 649d3a1e..060ea646 100644 --- a/src/lib/drivers/udp/server.rs +++ b/src/lib/drivers/udp/server.rs @@ -60,9 +60,8 @@ impl UdpServer { local_addr: local_addr.to_string(), name: arc_swap::ArcSwap::new(name.clone()), uuid: Self::generate_uuid(local_addr), - clients: Arc::new(RwLock::new(HashMap::new())), - on_message_input: Callbacks::new(), - on_message_output: Callbacks::new(), + on_message_input: Callbacks::default(), + on_message_output: Callbacks::default(), stats: Arc::new(RwLock::new(AccumulatedDriverStats::new( name, &UdpServerInfo,