From 5c6419c85c8bb505739a879b09121f2b36c63411 Mon Sep 17 00:00:00 2001 From: cyqsimon <28627918+cyqsimon@users.noreply.github.com> Date: Tue, 17 Oct 2023 16:40:18 +0800 Subject: [PATCH] Make `write_to_stdout` take a `&str` instead of a `String` --- src/display/ui.rs | 20 ++++++++++---------- src/main.rs | 4 ++-- src/os/shared.rs | 4 ++-- src/tests/cases/test_utils.rs | 6 +++--- src/tests/cases/ui.rs | 8 ++++---- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/display/ui.rs b/src/display/ui.rs index b2ce8f4..1ccd2c5 100644 --- a/src/display/ui.rs +++ b/src/display/ui.rs @@ -45,17 +45,17 @@ where opts: opts.render_opts, } } - pub fn output_text(&mut self, write_to_stdout: &mut (dyn FnMut(String) + Send)) { + pub fn output_text(&mut self, write_to_stdout: &mut (dyn FnMut(&str) + Send)) { let state = &self.state; let ip_to_host = &self.ip_to_host; let local_time: DateTime = Local::now(); let timestamp = local_time.timestamp(); let mut no_traffic = true; - let output_process_data = |write_to_stdout: &mut (dyn FnMut(String) + Send), + let output_process_data = |write_to_stdout: &mut (dyn FnMut(&str) + Send), no_traffic: &mut bool| { for (proc_info, process_network_data) in &state.processes { - write_to_stdout(format!( + write_to_stdout(&format!( "process: <{timestamp}> \"{}\" up/down Bps: {}/{} connections: {}", proc_info.name, process_network_data.total_bytes_uploaded, @@ -67,9 +67,9 @@ where }; let output_connections_data = - |write_to_stdout: &mut (dyn FnMut(String) + Send), no_traffic: &mut bool| { + |write_to_stdout: &mut (dyn FnMut(&str) + Send), no_traffic: &mut bool| { for (connection, connection_network_data) in &state.connections { - write_to_stdout(format!( + write_to_stdout(&format!( "connection: <{timestamp}> {} up/down Bps: {}/{} process: \"{}\"", display_connection_string( connection, @@ -84,10 +84,10 @@ where } }; - let output_adressess_data = |write_to_stdout: &mut (dyn FnMut(String) + Send), + let output_adressess_data = |write_to_stdout: &mut (dyn FnMut(&str) + Send), no_traffic: &mut bool| { for (remote_address, remote_address_network_data) in &state.remote_addresses { - write_to_stdout(format!( + write_to_stdout(&format!( "remote_address: <{timestamp}> {} up/down Bps: {}/{} connections: {}", display_ip_or_host(*remote_address, ip_to_host), remote_address_network_data.total_bytes_uploaded, @@ -99,7 +99,7 @@ where }; // header - write_to_stdout("Refreshing:".into()); + write_to_stdout("Refreshing:"); // body1 if self.opts.processes { @@ -119,11 +119,11 @@ where // body2: In case no traffic is detected if no_traffic { - write_to_stdout("".into()); + write_to_stdout(""); } // footer - write_to_stdout("".into()); + write_to_stdout(""); } pub fn draw(&mut self, paused: bool, show_dns: bool, elapsed_time: Duration, ui_offset: usize) { diff --git a/src/main.rs b/src/main.rs index 3434adb..3178da7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -82,7 +82,7 @@ pub struct OsInputOutput { pub get_open_sockets: fn() -> OpenSockets, pub terminal_events: Box + Send>, pub dns_client: Option, - pub write_to_stdout: Box, + pub write_to_stdout: Box, } pub fn start(terminal_backend: B, os_input: OsInputOutput, opts: Opt) @@ -123,7 +123,7 @@ where move || { while running.load(Ordering::Acquire) { let render_start_time = Instant::now(); - let utilization = { network_utilization.lock().unwrap().clone_and_reset() }; + let utilization = network_utilization.lock().unwrap().clone_and_reset(); let OpenSockets { sockets_to_procs } = get_open_sockets(); let mut ip_to_host = IpTable::new(); if let Some(dns_client) = dns_client.as_mut() { diff --git a/src/os/shared.rs b/src/os/shared.rs index e1fbcdb..e57b6f7 100644 --- a/src/os/shared.rs +++ b/src/os/shared.rs @@ -80,10 +80,10 @@ fn get_interface(interface_name: &str) -> Option { .find(|iface| iface.name == interface_name) } -fn create_write_to_stdout() -> Box { +fn create_write_to_stdout() -> Box { let mut stdout = io::stdout(); Box::new({ - move |output: String| match writeln!(stdout, "{}", output) { + move |output| match writeln!(stdout, "{}", output) { Ok(_) => (), Err(e) if e.kind() == ErrorKind::BrokenPipe => { // A process that was listening to bandwhich stdout has exited diff --git a/src/tests/cases/test_utils.rs b/src/tests/cases/test_utils.rs index 7735632..c2224ef 100644 --- a/src/tests/cases/test_utils.rs +++ b/src/tests/cases/test_utils.rs @@ -257,14 +257,14 @@ pub fn os_input_output_factory( ) -> OsInputOutput { let interfaces_with_frames = get_interfaces_with_frames(network_frames); - let write_to_stdout: Box = match stdout { + let write_to_stdout: Box = match stdout { Some(stdout) => Box::new({ - move |output: String| { + move |output| { let mut stdout = stdout.lock().unwrap(); writeln!(&mut stdout, "{}", output).unwrap(); } }), - None => Box::new(move |_output: String| {}), + None => Box::new(|_output| {}), }; OsInputOutput { diff --git a/src/tests/cases/ui.rs b/src/tests/cases/ui.rs index 70d7e78..e4730e6 100644 --- a/src/tests/cases/ui.rs +++ b/src/tests/cases/ui.rs @@ -657,7 +657,7 @@ fn traffic_with_host_names(network_frames: Vec>) { String::from("i-like-cheese.com"), ); let dns_client = create_fake_dns_client(ips_to_hostnames); - let write_to_stdout = Box::new(move |_output: String| {}); + let write_to_stdout = Box::new(|_output: &_| {}); let os_input = OsInputOutput { interfaces_with_frames, @@ -696,7 +696,7 @@ fn truncate_long_hostnames(network_frames: Vec>) { String::from("i-like-cheese.com"), ); let dns_client = create_fake_dns_client(ips_to_hostnames); - let write_to_stdout = Box::new(move |_output: String| {}); + let write_to_stdout = Box::new(|_output: &_| {}); let os_input = OsInputOutput { interfaces_with_frames, @@ -735,7 +735,7 @@ fn no_resolve_mode(network_frames: Vec>) { String::from("i-like-cheese.com"), ); let dns_client = None; - let write_to_stdout = Box::new(move |_output: String| {}); + let write_to_stdout = Box::new(|_output: &_| {}); let os_input = OsInputOutput { interfaces_with_frames, @@ -768,7 +768,7 @@ fn traffic_with_winch_event() { let (terminal_events, terminal_draw_events, backend) = test_backend_factory(190, 50); let dns_client = create_fake_dns_client(HashMap::new()); - let write_to_stdout = Box::new(move |_output: String| {}); + let write_to_stdout = Box::new(|_output: &_| {}); let os_input = OsInputOutput { interfaces_with_frames,