refactor: Vec::with_capacity where possible
This commit is contained in:
parent
cbbd350fc6
commit
a423aaa27a
2 changed files with 46 additions and 84 deletions
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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<ActiveConnection<'_>>,
|
||||
) -> zbus::Result<Vec<ActiveConnectionInfo>> {
|
||||
let mut info = Vec::<ActiveConnectionInfo>::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<IpAddr>,
|
||||
ip_addresses: Vec<Ipv4Addr>,
|
||||
},
|
||||
WiFi {
|
||||
name: String,
|
||||
ip_addresses: Vec<IpAddr>,
|
||||
ip_addresses: Vec<Ipv4Addr>,
|
||||
hw_address: String,
|
||||
flags: ApFlags,
|
||||
rsn_flags: ApSecurityFlags,
|
||||
|
|
@ -125,7 +95,7 @@ pub enum ActiveConnectionInfo {
|
|||
},
|
||||
Vpn {
|
||||
name: String,
|
||||
ip_addresses: Vec<IpAddr>,
|
||||
ip_addresses: Vec<Ipv4Addr>,
|
||||
},
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue