From 92b42785b30f0c0415a121e6681ac83ddda478e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Mon, 20 Aug 2018 10:28:48 +0200 Subject: [PATCH 1/2] Public export of `Ssid`, `SsidSlice` and `WifiDevice` Also removes unneeded trait `IntoSsid`. --- src/lib.rs | 3 ++- src/ssid.rs | 4 ---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a8eeb9b..b1aedaf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -30,5 +30,6 @@ mod ssid; pub use manager::{Connectivity, NetworkManager}; pub use connection::{Connection, ConnectionSettings, ConnectionState}; pub use device::{Device, DeviceState, DeviceType}; -pub use wifi::{AccessPoint, AccessPointCredentials, Security}; +pub use wifi::{AccessPoint, AccessPointCredentials, Security, WiFiDevice}; pub use service::ServiceState; +pub use ssid::{Ssid, SsidSlice}; diff --git a/src/ssid.rs b/src/ssid.rs index ed7f09b..3eb4a0c 100644 --- a/src/ssid.rs +++ b/src/ssid.rs @@ -35,10 +35,6 @@ impl Ssid { } } -pub trait IntoSsid: Sized { - fn into_ssid(self) -> Result; -} - impl Deref for Ssid { type Target = SsidSlice; From ec5890be8c1f6f15241b85381e3723ee122dfef8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Mon, 20 Aug 2018 10:38:46 +0200 Subject: [PATCH 2/2] Make the `WiFiDevice` own the `Device` instance To not change the public interface `as_wifi_device` does not uses `self` as parameter and sticks to `&self` and uses clone internally. --- src/device.rs | 4 ++-- src/wifi.rs | 35 ++++++++++++++++++++++------------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/device.rs b/src/device.rs index bd7f22b..9466edf 100644 --- a/src/device.rs +++ b/src/device.rs @@ -4,7 +4,7 @@ use std::fmt; use errors::*; use dbus_nm::DBusNetworkManager; -use wifi::{new_wifi_device, WiFiDevice}; +use wifi::WiFiDevice; #[derive(Clone)] pub struct Device { @@ -42,7 +42,7 @@ impl Device { pub fn as_wifi_device(&self) -> Option { if self.device_type == DeviceType::WiFi { - Some(new_wifi_device(&self.dbus_manager, self)) + Some(WiFiDevice::new(self.dbus_manager.clone(), self.clone())) } else { None } diff --git a/src/wifi.rs b/src/wifi.rs index 0cf5e42..e1b347f 100644 --- a/src/wifi.rs +++ b/src/wifi.rs @@ -1,5 +1,6 @@ use std::rc::Rc; use std::net::Ipv4Addr; +use std::ops::Deref; use errors::*; use dbus_nm::DBusNetworkManager; @@ -8,12 +9,22 @@ use connection::{connect_to_access_point, create_hotspot, Connection, Connection use device::{Device, PathGetter}; use ssid::{AsSsidSlice, Ssid, SsidSlice}; -pub struct WiFiDevice<'a> { +pub struct WiFiDevice { dbus_manager: Rc, - device: &'a Device, + device: Device, } -impl<'a> WiFiDevice<'a> { +impl WiFiDevice { + pub(crate) fn new( + dbus_manager: Rc, + device: Device, + ) -> WiFiDevice { + WiFiDevice { + dbus_manager, + device, + } + } + /// Get the list of access points visible to this device. /// /// # Examples @@ -85,6 +96,14 @@ impl<'a> WiFiDevice<'a> { } } +impl Deref for WiFiDevice { + type Target = Device; + + fn deref(&self) -> &Self::Target { + &self.device + } +} + #[derive(Debug)] pub struct AccessPoint { pub path: String, @@ -166,16 +185,6 @@ bitflags! { } } -pub fn new_wifi_device<'a>( - dbus_manager: &Rc, - device: &'a Device, -) -> WiFiDevice<'a> { - WiFiDevice { - dbus_manager: Rc::clone(dbus_manager), - device: device, - } -} - fn get_access_point(manager: &DBusNetworkManager, path: &str) -> Result> { if let Some(ssid) = manager.get_access_point_ssid(path) { let strength = manager.get_access_point_strength(path)?;