From a423aaa27a2b3047e380afde29cca917eb47573e Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Tue, 10 Jan 2023 17:29:19 -0500 Subject: [PATCH] refactor: Vec::with_capacity where possible --- cosmic-applet-network/src/app.rs | 68 ++++++++----------- .../src/network_manager/current_networks.rs | 62 +++++------------ 2 files changed, 46 insertions(+), 84 deletions(-) diff --git a/cosmic-applet-network/src/app.rs b/cosmic-applet-network/src/app.rs index 4c7d5f58..e1f38a35 100644 --- a/cosmic-applet-network/src/app.rs +++ b/cosmic-applet-network/src/app.rs @@ -7,7 +7,7 @@ use cosmic::{ popup::{destroy_popup, get_popup}, SurfaceIdWrapper, }, - widget::{column, container, row, scrollable, text, text_input}, + widget::{column, container, row, scrollable, text, text_input, Column}, Alignment, Application, Color, Command, Length, Subscription, }, iced_native::{ @@ -328,20 +328,16 @@ impl Application for CosmicNetworkApplet { for conn in &self.nm_state.active_conns { match conn { ActiveConnectionInfo::Vpn { name, ip_addresses } => { - let mut ipv4 = column![]; + let mut ipv4 = Vec::with_capacity(ip_addresses.len()); for addr in ip_addresses { - match addr { - std::net::IpAddr::V4(a) => { - ipv4 = ipv4.push( - text(format!("{}: {}", fl!("ipv4"), a.to_string())) - .size(12), - ); - } - std::net::IpAddr::V6(_) => {} - } + ipv4.push( + text(format!("{}: {}", fl!("ipv4"), addr.to_string())) + .size(12) + .into(), + ); } - vpn_ethernet_col = - vpn_ethernet_col.push(column![text(name), ipv4].spacing(4)); + vpn_ethernet_col = vpn_ethernet_col + .push(column![text(name), Column::with_children(ipv4)].spacing(4)); } ActiveConnectionInfo::Wired { name, @@ -349,17 +345,13 @@ impl Application for CosmicNetworkApplet { speed, ip_addresses, } => { - let mut ipv4 = column![]; + let mut ipv4 = Vec::with_capacity(ip_addresses.len()); for addr in ip_addresses { - match addr { - std::net::IpAddr::V4(a) => { - ipv4 = ipv4.push( - text(format!("{}: {}", fl!("ipv4"), a.to_string())) - .size(12), - ); - } - std::net::IpAddr::V6(a) => {} - } + ipv4.push( + text(format!("{}: {}", fl!("ipv4"), addr.to_string())) + .size(12) + .into(), + ); } vpn_ethernet_col = vpn_ethernet_col.push( column![ @@ -368,7 +360,7 @@ impl Application for CosmicNetworkApplet { text(format!("{speed} {}", fl!("megabits-per-second"))) ] .spacing(16), - ipv4, + Column::with_children(ipv4), ] .spacing(4), ); @@ -376,17 +368,13 @@ impl Application for CosmicNetworkApplet { ActiveConnectionInfo::WiFi { name, ip_addresses, .. } => { - let mut ipv4 = column![]; + let mut ipv4 = Vec::with_capacity(ip_addresses.len()); for addr in ip_addresses { - match addr { - std::net::IpAddr::V4(a) => { - ipv4 = ipv4.push( - text(format!("{}: {}", fl!("ipv4"), a.to_string())) - .size(12), - ); - } - std::net::IpAddr::V6(_) => {} - } + ipv4.push( + text(format!("{}: {}", fl!("ipv4"), addr.to_string())) + .size(12) + .into(), + ); } known_wifi = known_wifi.push(column![button(Button::Secondary) .custom(vec![ @@ -397,7 +385,8 @@ impl Application for CosmicNetworkApplet { .width(Length::Units(24)) .height(Length::Units(24)) .into(), - column![text(name).size(14), ipv4,].into(), + column![text(name).size(14), Column::with_children(ipv4)] + .into(), text(format!("{}", fl!("connected"))) .size(14) .width(Length::Fill) @@ -615,7 +604,8 @@ impl Application for CosmicNetworkApplet { } } } else if self.nm_state.wifi_enabled { - let mut list_col = column![]; + let mut list_col = + Vec::with_capacity(self.nm_state.wireless_access_points.len()); for ap in &self.nm_state.wireless_access_points { if self .nm_state @@ -644,9 +634,11 @@ impl Application for CosmicNetworkApplet { .on_press(Message::SelectWirelessAccessPoint(ap.clone())) .width(Length::Fill) .padding([8, 24]); - list_col = list_col.push(button); + list_col.push(button.into()); } - content = content.push(scrollable(list_col).height(Length::Units(300))); + content = content.push( + scrollable(Column::with_children(list_col)).height(Length::Units(300)), + ); } } self.applet_helper.popup_container(content).into() diff --git a/cosmic-applet-network/src/network_manager/current_networks.rs b/cosmic-applet-network/src/network_manager/current_networks.rs index a32bf354..9024770f 100644 --- a/cosmic-applet-network/src/network_manager/current_networks.rs +++ b/cosmic-applet-network/src/network_manager/current_networks.rs @@ -5,59 +5,29 @@ use cosmic_dbus_networkmanager::{ device::SpecificDevice, interface::enums::{ApFlags, ApSecurityFlags}, }; -use std::net::IpAddr; +use std::net::Ipv4Addr; pub async fn active_connections( active_connections: Vec>, ) -> zbus::Result> { let mut info = Vec::::with_capacity(active_connections.len()); for connection in active_connections { + let ipv4 = connection + .ip4_config() + .await? + .address_data() + .await + .unwrap_or_default(); + let addresses: Vec<_> = ipv4.iter().map(|d| d.address).collect(); + if connection.vpn().await.unwrap_or_default() { - let mut ip_addresses = Vec::new(); - for address_data in connection - .ip4_config() - .await? - .address_data() - .await - .unwrap_or_default() - { - ip_addresses.push(IpAddr::V4(address_data.address)); - } - for address_data in connection - .ip6_config() - .await? - .address_data() - .await - .unwrap_or_default() - { - ip_addresses.push(IpAddr::V6(address_data.address)); - } info.push(ActiveConnectionInfo::Vpn { name: connection.id().await?, - ip_addresses, + ip_addresses: addresses.clone(), }); continue; } for device in connection.devices().await.unwrap_or_default() { - let mut ip_addresses = Vec::new(); - for address_data in connection - .ip4_config() - .await? - .address_data() - .await - .unwrap_or_default() - { - ip_addresses.push(IpAddr::V4(address_data.address)); - } - for address_data in connection - .ip6_config() - .await? - .address_data() - .await - .unwrap_or_default() - { - ip_addresses.push(IpAddr::V6(address_data.address)); - } match device .downcast_to_device() .await @@ -69,14 +39,14 @@ pub async fn active_connections( name: connection.id().await?, hw_address: wired_device.hw_address().await?, speed: wired_device.speed().await?, - ip_addresses, + ip_addresses: addresses.clone(), }); } Some(SpecificDevice::Wireless(wireless_device)) => { if let Ok(access_point) = wireless_device.active_access_point().await { info.push(ActiveConnectionInfo::WiFi { name: String::from_utf8_lossy(&access_point.ssid().await?).into_owned(), - ip_addresses, + ip_addresses: addresses.clone(), hw_address: wireless_device.hw_address().await?, flags: access_point.flags().await?, rsn_flags: access_point.rsn_flags().await?, @@ -87,7 +57,7 @@ pub async fn active_connections( Some(SpecificDevice::WireGuard(_)) => { info.push(ActiveConnectionInfo::Vpn { name: connection.id().await?, - ip_addresses, + ip_addresses: addresses.clone(), }); } _ => {} @@ -113,11 +83,11 @@ pub enum ActiveConnectionInfo { name: String, hw_address: String, speed: u32, - ip_addresses: Vec, + ip_addresses: Vec, }, WiFi { name: String, - ip_addresses: Vec, + ip_addresses: Vec, hw_address: String, flags: ApFlags, rsn_flags: ApSecurityFlags, @@ -125,7 +95,7 @@ pub enum ActiveConnectionInfo { }, Vpn { name: String, - ip_addresses: Vec, + ip_addresses: Vec, }, }