refactor: Vec::with_capacity where possible
This commit is contained in:
parent
cbbd350fc6
commit
a423aaa27a
2 changed files with 46 additions and 84 deletions
|
|
@ -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