Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use clone function over static method #25

Merged
merged 7 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions src/drivers/fake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,9 @@ impl Driver for FakeSink {
let mut hub_receiver = hub_sender.subscribe();

while let Ok(message) = hub_receiver.recv().await {
self.stats
.write()
.await
.update_input(Arc::clone(&message))
.await;

self.stats
.write()
.await
.update_input(Arc::clone(&message))
.await;

for future in self.on_message_input.call_all(Arc::clone(&message)) {
self.stats.write().await.update_input(message.clone()).await;

for future in self.on_message_input.call_all(message.clone()) {
if let Err(error) = future.await {
debug!("Dropping message: on_message_input callback returned error: {error:?}");
continue;
Expand Down Expand Up @@ -219,10 +209,10 @@ impl Driver for FakeSource {
self.stats
.write()
.await
.update_output(Arc::clone(&message))
.update_output(message.clone())
.await;

for future in self.on_message_output.call_all(Arc::clone(&message)) {
for future in self.on_message_output.call_all(message.clone()) {
if let Err(error) = future.await {
debug!(
"Dropping message: on_message_input callback returned error: {error:?}"
Expand Down
8 changes: 2 additions & 6 deletions src/drivers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,9 @@ mod tests {
let mut hub_receiver = hub_sender.subscribe();

while let Ok(message) = hub_receiver.recv().await {
self.stats
.write()
.await
.update_input(Arc::clone(&message))
.await;
self.stats.write().await.update_input(message.clone()).await;

for future in self.on_message_input.call_all(Arc::clone(&message)) {
for future in self.on_message_input.call_all(message.clone()) {
if let Err(error) = future.await {
debug!(
"Dropping message: on_message_input callback returned error: {error:?}"
Expand Down
4 changes: 2 additions & 2 deletions src/drivers/serial/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl Serial {
read_all_messages("serial", &mut buf, |message| async {
let message = Arc::new(message);

for future in on_message_input.call_all(Arc::clone(&message)) {
for future in on_message_input.call_all(message.clone()) {
if let Err(error) = future.await {
debug!("Dropping message: on_message_input callback returned error: {error:?}");
continue;
Expand Down Expand Up @@ -114,7 +114,7 @@ impl Serial {
loop {
match hub_receiver.recv().await {
Ok(message) => {
for future in on_message_output.call_all(Arc::clone(&message)) {
for future in on_message_output.call_all(message.clone()) {
if let Err(error) = future.await {
debug!("Dropping message: on_message_output callback returned error: {error:?}");
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/tcp/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl Driver for TcpClient {
let hub_receiver = hub_sender.subscribe();

tokio::select! {
result = tcp_receive_task(read, server_addr, Arc::clone(&hub_sender), &self.on_message_input, &self.stats) => {
result = tcp_receive_task(read, server_addr, hub_sender.clone(), &self.on_message_input, &self.stats) => {
if let Err(e) = result {
error!("Error in TCP receive task: {e:?}");
}
Expand Down
12 changes: 4 additions & 8 deletions src/drivers/tcp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ async fn tcp_receive_task(
read_all_messages(remote_addr, &mut buf, |message| async {
let message = Arc::new(message);

stats.write().await.update_input(Arc::clone(&message)).await;
stats.write().await.update_input(message.clone()).await;

for future in on_message_input.call_all(Arc::clone(&message)) {
for future in on_message_input.call_all(message.clone()) {
if let Err(error) = future.await {
debug!("Dropping message: on_message_input callback returned error: {error:?}");
continue;
Expand Down Expand Up @@ -86,13 +86,9 @@ async fn tcp_send_task(
continue; // Don't do loopback
}

stats
.write()
.await
.update_output(Arc::clone(&message))
.await;
stats.write().await.update_output(message.clone()).await;

for future in on_message_output.call_all(Arc::clone(&message)) {
for future in on_message_output.call_all(message.clone()) {
if let Err(error) = future.await {
debug!("Dropping message: on_message_output callback returned error: {error:?}");
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/tcp/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl Driver for TcpServer {
match listener.accept().await {
Ok((socket, remote_addr)) => {
let remote_addr = remote_addr.to_string();
let hub_sender = Arc::clone(&hub_sender);
let hub_sender = hub_sender.clone();

tokio::spawn(TcpServer::handle_client(
socket,
Expand Down
8 changes: 2 additions & 6 deletions src/drivers/tlog/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,9 @@ impl TlogReader {

let message = Arc::new(message);

self.stats
.write()
.await
.update_input(Arc::clone(&message))
.await;
self.stats.write().await.update_input(message.clone()).await;

for future in self.on_message_input.call_all(Arc::clone(&message)) {
for future in self.on_message_input.call_all(message.clone()) {
if let Err(error) = future.await {
debug!("Dropping message: on_message_input callback returned error: {error:?}");
continue;
Expand Down
4 changes: 2 additions & 2 deletions src/drivers/tlog/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ impl TlogWriter {
self.stats
.write()
.await
.update_output(Arc::clone(&message))
.update_output(message.clone())
.await;

for future in self.on_message_output.call_all(Arc::clone(&message)) {
for future in self.on_message_output.call_all(message.clone()) {
if let Err(error) = future.await {
debug!(
"Dropping message: on_message_input callback returned error: {error:?}"
Expand Down
8 changes: 4 additions & 4 deletions src/drivers/udp/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ impl UdpClient {
self.stats
.write()
.await
.update_input(Arc::clone(&message))
.update_input(message.clone())
.await;

for future in self.on_message_input.call_all(Arc::clone(&message)) {
for future in self.on_message_input.call_all(message.clone()) {
if let Err(error) = future.await {
debug!("Dropping message: on_message_input callback returned error: {error:?}");
continue;
Expand Down Expand Up @@ -122,10 +122,10 @@ impl UdpClient {
self.stats
.write()
.await
.update_output(Arc::clone(&message))
.update_output(message.clone())
.await;

for future in self.on_message_output.call_all(Arc::clone(&message)) {
for future in self.on_message_output.call_all(message.clone()) {
if let Err(error) = future.await {
debug!(
"Dropping message: on_message_output callback returned error: {error:?}"
Expand Down
8 changes: 4 additions & 4 deletions src/drivers/udp/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ impl UdpServer {
self.stats
.write()
.await
.update_input(Arc::clone(&message))
.update_input(message.clone())
.await;

for future in self.on_message_input.call_all(Arc::clone(&message)) {
for future in self.on_message_input.call_all(message.clone()) {
if let Err(error) = future.await {
debug!("Dropping message: on_message_input callback returned error: {error:?}");
continue;
Expand Down Expand Up @@ -141,10 +141,10 @@ impl UdpServer {
self.stats
.write()
.await
.update_output(Arc::clone(&message))
.update_output(message.clone())
.await;

for future in self.on_message_output.call_all(Arc::clone(&message)) {
for future in self.on_message_output.call_all(message.clone()) {
if let Err(error) = future.await {
debug!("Dropping message: on_message_output callback returned error: {error:?}");
continue;
Expand Down
16 changes: 8 additions & 8 deletions src/stats/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ impl StatsActor {
pub async fn start(mut self, mut receiver: mpsc::Receiver<StatsCommand>) {
let drivers_stats_task = tokio::spawn({
let hub = self.hub.clone();
let update_period = Arc::clone(&self.update_period);
let last_accumulated_drivers_stats = Arc::clone(&self.last_accumulated_drivers_stats);
let drivers_stats = Arc::clone(&self.drivers_stats);
let start_time = Arc::clone(&self.start_time);
let update_period = self.update_period.clone();
let last_accumulated_drivers_stats = self.last_accumulated_drivers_stats.clone();
let drivers_stats = self.drivers_stats.clone();
let start_time = self.start_time.clone();

async move {
loop {
Expand All @@ -51,10 +51,10 @@ impl StatsActor {

let hub_stats_task = tokio::spawn({
let hub = self.hub.clone();
let update_period = Arc::clone(&self.update_period);
let last_accumulated_hub_stats = Arc::clone(&self.last_accumulated_hub_stats);
let hub_stats = Arc::clone(&self.hub_stats);
let start_time = Arc::clone(&self.start_time);
let update_period = self.update_period.clone();
let last_accumulated_hub_stats = self.last_accumulated_hub_stats.clone();
let hub_stats = self.hub_stats.clone();
let start_time = self.start_time.clone();

async move {
loop {
Expand Down
Loading