From 2ca126930c4ed54b0b7bb79fd6e27f460fefbcb6 Mon Sep 17 00:00:00 2001 From: Matthias Vogelgesang Date: Sun, 10 Dec 2023 23:08:46 +0100 Subject: [PATCH] Drop async-trait --- Cargo.toml | 1 - src/api/mod.rs | 81 +++++++++++++++++++++++---------- src/bluez/adapter.rs | 2 - src/bluez/manager.rs | 2 - src/bluez/peripheral.rs | 2 - src/corebluetooth/adapter.rs | 2 - src/corebluetooth/manager.rs | 2 - src/corebluetooth/peripheral.rs | 2 - src/droidplug/adapter.rs | 2 - src/droidplug/manager.rs | 2 - src/droidplug/peripheral.rs | 2 - src/winrtble/adapter.rs | 2 - src/winrtble/manager.rs | 2 - src/winrtble/peripheral.rs | 2 - 14 files changed, 56 insertions(+), 50 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index acca3d75..5a05019e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,6 @@ path = "src/lib.rs" serde = ["uuid/serde", "serde_cr", "serde_bytes"] [dependencies] -async-trait = "0.1.74" log = "0.4.20" bitflags = "2.4.1" thiserror = "1.0.50" diff --git a/src/api/mod.rs b/src/api/mod.rs index f24bba61..28af346c 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -25,7 +25,6 @@ pub(crate) mod bdaddr; pub mod bleuuid; use crate::Result; -use async_trait::async_trait; use bitflags::bitflags; use futures::stream::Stream; #[cfg(feature = "serde")] @@ -222,7 +221,6 @@ pub enum WriteType { /// Peripheral is the device that you would like to communicate with (the "server" of BLE). This /// struct contains both the current state of the device (its properties, characteristics, etc.) /// as well as functions for communication. -#[async_trait] pub trait Peripheral: Send + Sync + Clone + Debug { /// Returns the unique identifier of the peripheral. fn id(&self) -> PeripheralId; @@ -232,7 +230,9 @@ pub trait Peripheral: Send + Sync + Clone + Debug { /// Returns the set of properties associated with the peripheral. These may be updated over time /// as additional advertising reports are received. - async fn properties(&self) -> Result>; + fn properties( + &self, + ) -> impl std::future::Future>> + Send; /// The set of services we've discovered for this device. This will be empty until /// `discover_services` is called. @@ -248,51 +248,71 @@ pub trait Peripheral: Send + Sync + Clone + Debug { } /// Returns true iff we are currently connected to the device. - async fn is_connected(&self) -> Result; + fn is_connected(&self) -> impl std::future::Future> + Send; /// Creates a connection to the device. If this method returns Ok there has been successful /// connection. Note that peripherals allow only one connection at a time. Operations that /// attempt to communicate with a device will fail until it is connected. - async fn connect(&self) -> Result<()>; + fn connect(&self) -> impl std::future::Future> + Send; /// Terminates a connection to the device. - async fn disconnect(&self) -> Result<()>; + fn disconnect(&self) -> impl std::future::Future> + Send; /// Discovers all services for the device, including their characteristics. - async fn discover_services(&self) -> Result<()>; + fn discover_services(&self) -> impl std::future::Future> + Send; /// Write some data to the characteristic. Returns an error if the write couldn't be sent or (in /// the case of a write-with-response) if the device returns an error. - async fn write( + fn write( &self, characteristic: &Characteristic, data: &[u8], write_type: WriteType, - ) -> Result<()>; + ) -> impl std::future::Future> + Send; /// Sends a read request to the device. Returns either an error if the request was not accepted /// or the response from the device. - async fn read(&self, characteristic: &Characteristic) -> Result>; + fn read( + &self, + characteristic: &Characteristic, + ) -> impl std::future::Future>> + Send; /// Enables either notify or indicate (depending on support) for the specified characteristic. - async fn subscribe(&self, characteristic: &Characteristic) -> Result<()>; + fn subscribe( + &self, + characteristic: &Characteristic, + ) -> impl std::future::Future> + Send; /// Disables either notify or indicate (depending on support) for the specified characteristic. - async fn unsubscribe(&self, characteristic: &Characteristic) -> Result<()>; + fn unsubscribe( + &self, + characteristic: &Characteristic, + ) -> impl std::future::Future> + Send; /// Returns a stream of notifications for characteristic value updates. The stream will receive /// a notification when a value notification or indication is received from the device. /// The stream will remain valid across connections and can be queried before any connection /// is made. - async fn notifications(&self) -> Result + Send>>>; + fn notifications( + &self, + ) -> impl std::future::Future< + Output = Result + Send>>>, + > + Send; /// Write some data to the descriptor. Returns an error if the write couldn't be sent or (in /// the case of a write-with-response) if the device returns an error. - async fn write_descriptor(&self, descriptor: &Descriptor, data: &[u8]) -> Result<()>; + fn write_descriptor( + &self, + descriptor: &Descriptor, + data: &[u8], + ) -> impl std::future::Future> + Send; /// Sends a read descriptor request to the device. Returns either an error if the request /// was not accepted or the response from the device. - async fn read_descriptor(&self, descriptor: &Descriptor) -> Result>; + fn read_descriptor( + &self, + descriptor: &Descriptor, + ) -> impl std::future::Future>> + Send; } #[cfg_attr( @@ -325,13 +345,14 @@ pub enum CentralEvent { /// Central is the "client" of BLE. It's able to scan for and establish connections to peripherals. /// A Central can be obtained from [`Manager::adapters()`]. -#[async_trait] pub trait Central: Send + Sync + Clone { type Peripheral: Peripheral; /// Retrieve a stream of `CentralEvent`s. This stream will receive notifications when events /// occur for this Central module. See [`CentralEvent`] for the full set of possible events. - async fn events(&self) -> Result + Send>>>; + fn events( + &self, + ) -> impl std::future::Future + Send>>>> + Send; /// Starts a scan for BLE devices. This scan will generally continue until explicitly stopped, /// although this may depend on your Bluetooth adapter. Discovered devices will be announced @@ -340,26 +361,37 @@ pub trait Central: Send + Sync + Clone { /// ignore (parts of) the filter and make additional devices available, other implementations /// might require at least one filter for security reasons. Cross-platform code should provide /// a filter, but must be able to handle devices, which do not fit into the filter. - async fn start_scan(&self, filter: ScanFilter) -> Result<()>; + fn start_scan( + &self, + filter: ScanFilter, + ) -> impl std::future::Future> + Send; /// Stops scanning for BLE devices. - async fn stop_scan(&self) -> Result<()>; + fn stop_scan(&self) -> impl std::future::Future> + Send; /// Returns the list of [`Peripheral`]s that have been discovered so far. Note that this list /// may contain peripherals that are no longer available. - async fn peripherals(&self) -> Result>; + fn peripherals( + &self, + ) -> impl std::future::Future>> + Send; /// Returns a particular [`Peripheral`] by its address if it has been discovered. - async fn peripheral(&self, id: &PeripheralId) -> Result; + fn peripheral( + &self, + id: &PeripheralId, + ) -> impl std::future::Future> + Send; /// Add a [`Peripheral`] from a MAC address without a scan result. Not supported on all Bluetooth systems. - async fn add_peripheral(&self, address: &PeripheralId) -> Result; + fn add_peripheral( + &self, + address: &PeripheralId, + ) -> impl std::future::Future> + Send; /// Get information about the Bluetooth adapter being used, such as the model or type. /// /// The details of this are platform-specific andyou should not attempt to parse it, but it may /// be useful for debug logs. - async fn adapter_info(&self) -> Result; + fn adapter_info(&self) -> impl std::future::Future> + Send; } /// The Manager is the entry point to the library, providing access to all the Bluetooth adapters on @@ -380,11 +412,10 @@ pub trait Central: Send + Sync + Clone { /// # Ok(()) /// # } /// ``` -#[async_trait] pub trait Manager { /// The concrete type of the [`Central`] implementation. type Adapter: Central; /// Get a list of all Bluetooth adapters on the system. Each adapter implements [`Central`]. - async fn adapters(&self) -> Result>; + fn adapters(&self) -> impl std::future::Future>> + Send; } diff --git a/src/bluez/adapter.rs b/src/bluez/adapter.rs index dcf393b2..6a6c4365 100644 --- a/src/bluez/adapter.rs +++ b/src/bluez/adapter.rs @@ -1,7 +1,6 @@ use super::peripheral::{Peripheral, PeripheralId}; use crate::api::{Central, CentralEvent, ScanFilter}; use crate::{Error, Result}; -use async_trait::async_trait; use bluez_async::{ AdapterId, BluetoothError, BluetoothEvent, BluetoothSession, DeviceEvent, DiscoveryFilter, Transport, @@ -22,7 +21,6 @@ impl Adapter { } } -#[async_trait] impl Central for Adapter { type Peripheral = Peripheral; diff --git a/src/bluez/manager.rs b/src/bluez/manager.rs index afcb992c..d1d4c10c 100644 --- a/src/bluez/manager.rs +++ b/src/bluez/manager.rs @@ -1,6 +1,5 @@ use super::adapter::Adapter; use crate::{api, Result}; -use async_trait::async_trait; use bluez_async::BluetoothSession; /// Implementation of [api::Manager](crate::api::Manager). @@ -16,7 +15,6 @@ impl Manager { } } -#[async_trait] impl api::Manager for Manager { type Adapter = Adapter; diff --git a/src/bluez/peripheral.rs b/src/bluez/peripheral.rs index 426ff30b..7aafbc60 100644 --- a/src/bluez/peripheral.rs +++ b/src/bluez/peripheral.rs @@ -1,4 +1,3 @@ -use async_trait::async_trait; use bluez_async::{ BluetoothEvent, BluetoothSession, CharacteristicEvent, CharacteristicFlags, CharacteristicId, CharacteristicInfo, DescriptorInfo, DeviceId, DeviceInfo, MacAddress, ServiceInfo, @@ -128,7 +127,6 @@ impl Peripheral { } } -#[async_trait] impl api::Peripheral for Peripheral { fn id(&self) -> PeripheralId { PeripheralId(self.device.to_owned()) diff --git a/src/corebluetooth/adapter.rs b/src/corebluetooth/adapter.rs index c139c43d..9a7a6f2e 100644 --- a/src/corebluetooth/adapter.rs +++ b/src/corebluetooth/adapter.rs @@ -3,7 +3,6 @@ use super::peripheral::{Peripheral, PeripheralId}; use crate::api::{Central, CentralEvent, ScanFilter}; use crate::common::adapter_manager::AdapterManager; use crate::{Error, Result}; -use async_trait::async_trait; use futures::channel::mpsc::{self, Sender}; use futures::sink::SinkExt; use futures::stream::{Stream, StreamExt}; @@ -79,7 +78,6 @@ impl Adapter { } } -#[async_trait] impl Central for Adapter { type Peripheral = Peripheral; diff --git a/src/corebluetooth/manager.rs b/src/corebluetooth/manager.rs index a08d31ad..f6621a6a 100644 --- a/src/corebluetooth/manager.rs +++ b/src/corebluetooth/manager.rs @@ -7,7 +7,6 @@ use super::adapter::Adapter; use crate::{api, Result}; -use async_trait::async_trait; /// Implementation of [api::Manager](crate::api::Manager). #[derive(Clone, Debug)] @@ -19,7 +18,6 @@ impl Manager { } } -#[async_trait] impl api::Manager for Manager { type Adapter = Adapter; diff --git a/src/corebluetooth/peripheral.rs b/src/corebluetooth/peripheral.rs index 117faab7..72501a37 100644 --- a/src/corebluetooth/peripheral.rs +++ b/src/corebluetooth/peripheral.rs @@ -19,7 +19,6 @@ use crate::{ common::{adapter_manager::AdapterManager, util::notifications_stream_from_broadcast_receiver}, Error, Result, }; -use async_trait::async_trait; use futures::channel::mpsc::{Receiver, SendError, Sender}; use futures::sink::SinkExt; use futures::stream::{Stream, StreamExt}; @@ -195,7 +194,6 @@ impl Debug for Peripheral { } } -#[async_trait] impl api::Peripheral for Peripheral { fn id(&self) -> PeripheralId { PeripheralId(self.shared.uuid) diff --git a/src/droidplug/adapter.rs b/src/droidplug/adapter.rs index a1f829c9..a15658b7 100644 --- a/src/droidplug/adapter.rs +++ b/src/droidplug/adapter.rs @@ -10,7 +10,6 @@ use crate::{ common::adapter_manager::AdapterManager, Error, Result, }; -use async_trait::async_trait; use futures::stream::Stream; use jni::{ objects::{GlobalRef, JObject, JString}, @@ -123,7 +122,6 @@ impl Adapter { } } -#[async_trait] impl Central for Adapter { type Peripheral = Peripheral; diff --git a/src/droidplug/manager.rs b/src/droidplug/manager.rs index 83f743d1..d52ff750 100644 --- a/src/droidplug/manager.rs +++ b/src/droidplug/manager.rs @@ -1,6 +1,5 @@ use super::adapter::Adapter; use crate::{api, Result}; -use async_trait::async_trait; #[derive(Clone, Debug)] pub struct Manager; @@ -11,7 +10,6 @@ impl Manager { } } -#[async_trait] impl api::Manager for Manager { type Adapter = Adapter; diff --git a/src/droidplug/peripheral.rs b/src/droidplug/peripheral.rs index 14ccc2f7..2a9209df 100644 --- a/src/droidplug/peripheral.rs +++ b/src/droidplug/peripheral.rs @@ -5,7 +5,6 @@ use crate::{ }, Error, Result, }; -use async_trait::async_trait; use futures::stream::Stream; use jni::{ descriptors, @@ -206,7 +205,6 @@ impl Debug for Peripheral { } } -#[async_trait] impl api::Peripheral for Peripheral { /// Returns the unique identifier of the peripheral. fn id(&self) -> PeripheralId { diff --git a/src/winrtble/adapter.rs b/src/winrtble/adapter.rs index 6d41d24f..39b0c12e 100644 --- a/src/winrtble/adapter.rs +++ b/src/winrtble/adapter.rs @@ -17,7 +17,6 @@ use crate::{ common::adapter_manager::AdapterManager, Error, Result, }; -use async_trait::async_trait; use futures::stream::Stream; use std::convert::TryInto; use std::fmt::{self, Debug, Formatter}; @@ -47,7 +46,6 @@ impl Debug for Adapter { } } -#[async_trait] impl Central for Adapter { type Peripheral = Peripheral; diff --git a/src/winrtble/manager.rs b/src/winrtble/manager.rs index da525843..3249958e 100644 --- a/src/winrtble/manager.rs +++ b/src/winrtble/manager.rs @@ -13,7 +13,6 @@ use super::adapter::Adapter; use crate::{api, Result}; -use async_trait::async_trait; use windows::Devices::Radios::{Radio, RadioKind}; /// Implementation of [api::Manager](crate::api::Manager). @@ -26,7 +25,6 @@ impl Manager { } } -#[async_trait] impl api::Manager for Manager { type Adapter = Adapter; diff --git a/src/winrtble/peripheral.rs b/src/winrtble/peripheral.rs index 56f0e43b..53311367 100644 --- a/src/winrtble/peripheral.rs +++ b/src/winrtble/peripheral.rs @@ -24,7 +24,6 @@ use crate::{ common::{adapter_manager::AdapterManager, util::notifications_stream_from_broadcast_receiver}, Error, Result, }; -use async_trait::async_trait; use dashmap::DashMap; use futures::stream::Stream; use log::{error, trace}; @@ -328,7 +327,6 @@ impl Debug for Peripheral { } } -#[async_trait] impl ApiPeripheral for Peripheral { fn id(&self) -> PeripheralId { PeripheralId(self.shared.address)