From 526ca95d62aa0307fab24cf0c3c8d3b5e6d572de Mon Sep 17 00:00:00 2001 From: Joseph Hobbs Date: Tue, 6 Aug 2024 16:24:01 -0400 Subject: [PATCH] Replace Rc with Arc for thread-safety --- src/connection.rs | 18 +++++++++--------- src/device.rs | 14 +++++++------- src/manager.rs | 8 ++++---- src/wifi.rs | 8 ++++---- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/connection.rs b/src/connection.rs index d8f5089..863016e 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -1,6 +1,6 @@ use std::fmt; use std::net::Ipv4Addr; -use std::rc::Rc; +use std::sync::Arc; use dbus_nm::DBusNetworkManager; use errors::*; @@ -11,17 +11,17 @@ use wifi::{AccessPoint, AccessPointCredentials}; #[derive(Clone)] pub struct Connection { - dbus_manager: Rc, + dbus_manager: Arc, path: String, settings: ConnectionSettings, } impl Connection { - fn init(dbus_manager: &Rc, path: &str) -> Result { + fn init(dbus_manager: &Arc, path: &str) -> Result { let settings = dbus_manager.get_connection_settings(path)?; Ok(Connection { - dbus_manager: Rc::clone(dbus_manager), + dbus_manager: Arc::clone(dbus_manager), path: path.to_string(), settings, }) @@ -193,7 +193,7 @@ impl From for ConnectionState { } } -pub fn get_connections(dbus_manager: &Rc) -> Result> { +pub fn get_connections(dbus_manager: &Arc) -> Result> { let paths = dbus_manager.list_connections()?; let mut connections = Vec::with_capacity(paths.len()); @@ -207,7 +207,7 @@ pub fn get_connections(dbus_manager: &Rc) -> Result) -> Result> { +pub fn get_active_connections(dbus_manager: &Arc) -> Result> { let active_paths = dbus_manager.get_active_connections()?; let mut connections = Vec::with_capacity(active_paths.len()); @@ -224,7 +224,7 @@ pub fn get_active_connections(dbus_manager: &Rc) -> Result, + dbus_manager: &Arc, device_path: &str, access_point: &AccessPoint, credentials: &AccessPointCredentials, @@ -243,7 +243,7 @@ pub fn connect_to_access_point( } pub fn create_hotspot( - dbus_manager: &Rc, + dbus_manager: &Arc, device_path: &str, interface: &str, ssid: &S, @@ -267,7 +267,7 @@ where } pub fn create_hotspot_advanced( - dbus_manager: &Rc, + dbus_manager: &Arc, device_path: &str, interface: &str, ssid: &S, diff --git a/src/device.rs b/src/device.rs index 67c9e9c..cb85c2c 100644 --- a/src/device.rs +++ b/src/device.rs @@ -1,5 +1,5 @@ use std::fmt; -use std::rc::Rc; +use std::sync::Arc; use dbus_nm::DBusNetworkManager; use errors::*; @@ -8,20 +8,20 @@ use wifi::{new_wifi_device, WiFiDevice}; #[derive(Clone)] pub struct Device { - dbus_manager: Rc, + dbus_manager: Arc, path: String, interface: String, device_type: DeviceType, } impl Device { - fn init(dbus_manager: &Rc, path: &str) -> Result { + fn init(dbus_manager: &Arc, path: &str) -> Result { let interface = dbus_manager.get_device_interface(path)?; let device_type = dbus_manager.get_device_type(path)?; Ok(Device { - dbus_manager: Rc::clone(dbus_manager), + dbus_manager: Arc::clone(dbus_manager), path: path.to_string(), interface, device_type, @@ -225,7 +225,7 @@ impl From for DeviceState { } } -pub fn get_devices(dbus_manager: &Rc) -> Result> { +pub fn get_devices(dbus_manager: &Arc) -> Result> { let device_paths = dbus_manager.get_devices()?; let mut result = Vec::with_capacity(device_paths.len()); @@ -240,7 +240,7 @@ pub fn get_devices(dbus_manager: &Rc) -> Result> } pub fn get_device_by_interface( - dbus_manager: &Rc, + dbus_manager: &Arc, interface: &str, ) -> Result { let path = dbus_manager.get_device_by_interface(interface)?; @@ -249,7 +249,7 @@ pub fn get_device_by_interface( } pub fn get_active_connection_devices( - dbus_manager: &Rc, + dbus_manager: &Arc, active_path: &str, ) -> Result> { let device_paths = dbus_manager.get_active_connection_devices(active_path)?; diff --git a/src/manager.rs b/src/manager.rs index f17ab2d..7e5c05f 100644 --- a/src/manager.rs +++ b/src/manager.rs @@ -1,4 +1,4 @@ -use std::rc::Rc; +use std::sync::Arc; use dbus_nm::DBusNetworkManager; use errors::*; @@ -8,19 +8,19 @@ use device::{get_device_by_interface, get_devices, Device}; use service::{get_service_state, start_service, stop_service, ServiceState}; pub struct NetworkManager { - dbus_manager: Rc, + dbus_manager: Arc, } impl NetworkManager { pub fn new() -> Self { NetworkManager { - dbus_manager: Rc::new(DBusNetworkManager::new(None)), + dbus_manager: Arc::new(DBusNetworkManager::new(None)), } } pub fn with_method_timeout(timeout: u64) -> Self { NetworkManager { - dbus_manager: Rc::new(DBusNetworkManager::new(Some(timeout))), + dbus_manager: Arc::new(DBusNetworkManager::new(Some(timeout))), } } diff --git a/src/wifi.rs b/src/wifi.rs index 0580de0..ffd3cd1 100644 --- a/src/wifi.rs +++ b/src/wifi.rs @@ -1,5 +1,5 @@ use std::net::Ipv4Addr; -use std::rc::Rc; +use std::sync::Arc; use dbus_nm::DBusNetworkManager; use errors::*; @@ -9,7 +9,7 @@ use device::{Device, PathGetter}; use ssid::{AsSsidSlice, Ssid, SsidSlice}; pub struct WiFiDevice<'a> { - dbus_manager: Rc, + dbus_manager: Arc, device: &'a Device, } @@ -186,11 +186,11 @@ bitflags! { } pub fn new_wifi_device<'a>( - dbus_manager: &Rc, + dbus_manager: &Arc, device: &'a Device, ) -> WiFiDevice<'a> { WiFiDevice { - dbus_manager: Rc::clone(dbus_manager), + dbus_manager: Arc::clone(dbus_manager), device, } }