Skip to content

Commit

Permalink
src: lib: Change Callbacks from new to default
Browse files Browse the repository at this point in the history
  • Loading branch information
joaoantoniocardoso committed Oct 22, 2024
1 parent 95e8c0e commit 54757db
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion benches/callbacks_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>::new();
let callbacks = Callbacks::<String>::default();

for _ in 0..number_of_callbacks {
callbacks.add_callback({
Expand Down
16 changes: 9 additions & 7 deletions src/lib/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ pub struct Callbacks<T> {
callbacks: Arc<Mutex<IndexMap<usize, Callback<T>>>>,
}

impl<T> Default for Callbacks<T> {
fn default() -> Self {
Self {
callbacks: Arc::new(Mutex::new(IndexMap::new())),
}
}
}

impl<T> std::fmt::Debug for Callbacks<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Callback").finish()
}
}

impl<T> Callbacks<T> {
pub fn new() -> Self {
Self {
callbacks: Arc::new(Mutex::new(IndexMap::new())),
}
}

pub fn add_callback<F, Fut>(&self, callback: F) -> usize
where
F: Fn(T) -> Fut + Send + Sync + 'static,
Expand Down Expand Up @@ -99,7 +101,7 @@ mod tests {

#[tokio::test]
async fn test_callbacks() {
let callbacks = Callbacks::<String>::new();
let callbacks = Callbacks::<String>::default();

let (tx1, rx1) = oneshot::channel();
let tx1 = Arc::new(Mutex::new(Some(tx1)));
Expand Down
4 changes: 2 additions & 2 deletions src/lib/drivers/fake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,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,
Expand Down Expand Up @@ -166,7 +166,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,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/drivers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,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,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/drivers/rest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,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))),
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/drivers/serial/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,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))),
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/drivers/tcp/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,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,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/drivers/tcp/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,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,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/drivers/tlog/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,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,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/drivers/tlog/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,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,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/drivers/udp/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,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,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/drivers/udp/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ impl UdpServer {
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,
Expand Down

0 comments on commit 54757db

Please sign in to comment.