From 8249f57a3a7eca322aba55c447e4a4fee8bfde0b Mon Sep 17 00:00:00 2001 From: Mamadou Babaei Date: Fri, 10 Nov 2023 22:30:52 +0100 Subject: [PATCH] eliminate cargo clippy warnings --- src/api/bdaddr.rs | 2 +- src/api/mod.rs | 9 ++++----- src/bluez/peripheral.rs | 4 +--- src/common/adapter_manager.rs | 11 ++++------- src/common/util.rs | 4 ++-- src/winrtble/ble/characteristic.rs | 6 +++--- src/winrtble/peripheral.rs | 15 +++++++-------- src/winrtble/utils.rs | 2 +- 8 files changed, 23 insertions(+), 30 deletions(-) diff --git a/src/api/bdaddr.rs b/src/api/bdaddr.rs index a0887a5a..94b839c6 100644 --- a/src/api/bdaddr.rs +++ b/src/api/bdaddr.rs @@ -106,7 +106,7 @@ impl TryFrom for BDAddr { impl From for u64 { fn from(addr: BDAddr) -> Self { let mut slice = [0; 8]; - (&mut slice[2..]).copy_from_slice(&addr.into_inner()); + slice[2..].copy_from_slice(&addr.into_inner()); u64::from_be_bytes(slice) } } diff --git a/src/api/mod.rs b/src/api/mod.rs index 156fe704..df039c45 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -49,18 +49,17 @@ use crate::platform::PeripheralId; serde(crate = "serde_cr") )] #[derive(Debug, Clone, Copy, Eq, PartialEq)] +#[derive(Default)] pub enum AddressType { Random, + #[default] Public, } -impl Default for AddressType { - fn default() -> Self { - AddressType::Public - } -} + impl AddressType { + #[allow(clippy::should_implement_trait)] pub fn from_str(v: &str) -> Option { match v { "public" => Some(AddressType::Public), diff --git a/src/bluez/peripheral.rs b/src/bluez/peripheral.rs index 0054acec..d7edd768 100644 --- a/src/bluez/peripheral.rs +++ b/src/bluez/peripheral.rs @@ -367,9 +367,7 @@ impl From<&ServiceInternal> for Service { uuid: service.info.uuid, primary: service.info.primary, characteristics: service - .characteristics - .iter() - .map(|(_, characteristic)| make_characteristic(characteristic, service.info.uuid)) + .characteristics.values().map(|characteristic| make_characteristic(characteristic, service.info.uuid)) .collect(), } } diff --git a/src/common/adapter_manager.rs b/src/common/adapter_manager.rs index bf0c280b..47fc1a68 100644 --- a/src/common/adapter_manager.rs +++ b/src/common/adapter_manager.rs @@ -44,11 +44,8 @@ where PeripheralType: Peripheral + 'static, { pub fn emit(&self, event: CentralEvent) { - match event { - CentralEvent::DeviceDisconnected(ref id) => { - self.peripherals.remove(id); - } - _ => {} + if let CentralEvent::DeviceDisconnected(ref id) = event { + self.peripherals.remove(id); } if let Err(lost) = self.events_channel.send(event) { @@ -59,8 +56,8 @@ where pub fn event_stream(&self) -> Pin + Send>> { let receiver = self.events_channel.subscribe(); Box::pin(BroadcastStream::new(receiver).filter_map(|x| async move { - if x.is_ok() { - Some(x.unwrap()) + if let Ok(e) = x { + Some(e) } else { None } diff --git a/src/common/util.rs b/src/common/util.rs index 4ffebe7c..a69697cf 100644 --- a/src/common/util.rs +++ b/src/common/util.rs @@ -15,8 +15,8 @@ pub fn notifications_stream_from_broadcast_receiver( receiver: Receiver, ) -> Pin + Send>> { Box::pin(BroadcastStream::new(receiver).filter_map(|x| async move { - if x.is_ok() { - Some(x.unwrap()) + if let Ok(n) = x { + Some(n) } else { None } diff --git a/src/winrtble/ble/characteristic.rs b/src/winrtble/ble/characteristic.rs index f21d3220..d99d7c49 100644 --- a/src/winrtble/ble/characteristic.rs +++ b/src/winrtble/ble/characteristic.rs @@ -35,9 +35,9 @@ use windows::{ pub type NotifiyEventHandler = Box) + Send>; -impl Into for WriteType { - fn into(self) -> GattWriteOption { - match self { +impl From for GattWriteOption { + fn from(val: WriteType) -> Self { + match val { WriteType::WithoutResponse => GattWriteOption::WriteWithoutResponse, WriteType::WithResponse => GattWriteOption::WriteWithResponse, } diff --git a/src/winrtble/peripheral.rs b/src/winrtble/peripheral.rs index 725e78fb..c5d391d8 100644 --- a/src/winrtble/peripheral.rs +++ b/src/winrtble/peripheral.rs @@ -90,9 +90,9 @@ impl Peripheral { let (broadcast_sender, _) = broadcast::channel(16); Peripheral { shared: Arc::new(Shared { - adapter: adapter, + adapter, device: tokio::sync::Mutex::new(None), - address: address, + address, connected: AtomicBool::new(false), ble_services: DashMap::new(), notifications_channel: broadcast_sender, @@ -124,10 +124,9 @@ impl Peripheral { .services .read() .unwrap() - .iter() - .map(|uuid| *uuid) + .iter().copied() .collect(), - class: self.shared.class.read().unwrap().clone(), + class: *self.shared.class.read().unwrap(), } } @@ -251,7 +250,7 @@ impl Peripheral { self.emit_event(CentralEvent::ServicesAdvertisement { id: self.shared.address.into(), - services: services_guard.iter().map(|uuid| *uuid).collect(), + services: services_guard.iter().copied().collect(), }); } } @@ -409,7 +408,7 @@ impl ApiPeripheral for Peripheral { for service in gatt_services { let uuid = utils::to_uuid(&service.Uuid().unwrap()); if !self.shared.ble_services.contains_key(&uuid) { - match BLEDevice::get_characteristics(&service).await { + match BLEDevice::get_characteristics(service).await { Ok(characteristics) => { let characteristics = characteristics.into_iter().map(|characteristic| async { @@ -499,7 +498,7 @@ impl ApiPeripheral for Peripheral { let uuid = characteristic.uuid; ble_characteristic .subscribe(Box::new(move |value| { - let notification = ValueNotification { uuid: uuid, value }; + let notification = ValueNotification { uuid, value }; // Note: we ignore send errors here which may happen while there are no // receivers... let _ = notifications_sender.send(notification); diff --git a/src/winrtble/utils.rs b/src/winrtble/utils.rs index 29964b33..24282925 100644 --- a/src/winrtble/utils.rs +++ b/src/winrtble/utils.rs @@ -33,7 +33,7 @@ pub fn to_error(status: GattCommunicationStatus) -> Result<()> { } else if status == GattCommunicationStatus::ProtocolError { Err(Error::NotSupported("ProtocolError".to_string())) } else { - Err(Error::Other(format!("Communication Error:").into())) + Err(Error::Other("Communication Error:".to_string().into())) } }