From dc7c5906fbaac5c0822795079109370f4cabbd5f Mon Sep 17 00:00:00 2001 From: Kyle Scheuing Date: Sat, 5 Aug 2023 03:48:46 -0400 Subject: [PATCH] perf: replace unnecessary Arcs with Rc Now that DeviceRegistry and Device are no longer Send + Sync, there is no reason for them to use Arc over Rc. Also replaces tokio spawn_local usage with spawn to avoid having to deal with LocalSets. --- cli/src/main.rs | 4 ++-- .../actions/create_custom_equalizer_profile.rs | 2 +- .../actions/delete_custom_equalizer_profile.rs | 2 +- gui/src/actions/refresh_devices.rs | 2 +- .../select_custom_equalizer_configuration.rs | 2 +- gui/src/actions/set_ambient_sound_mode.rs | 6 +++--- gui/src/actions/set_custom_noise_canceling.rs | 6 +++--- gui/src/actions/set_device.rs | 8 ++++---- gui/src/actions/set_equalizer_configuration.rs | 10 +++++----- gui/src/actions/set_noise_canceling_mode.rs | 6 +++--- gui/src/actions/set_transpareny_mode.rs | 6 +++--- gui/src/actions/state.rs | 9 ++++----- gui/src/gtk_openscq30_lib/gtk_device.rs | 8 ++++---- .../gtk_openscq30_lib/gtk_device_registry.rs | 18 +++++++++--------- gui/src/main.rs | 4 ++-- gui/src/mock/device_registry.rs | 6 +++--- lib/Cargo.toml | 2 +- lib/src/api/connection/connection_registry.rs | 4 ++-- lib/src/api/device/device_registry.rs | 4 ++-- lib/src/demo/device/demo_device_registry.rs | 6 +++--- lib/src/futures.rs | 4 ++-- lib/src/futures/futures_tokio.rs | 4 ++-- .../btleplug/btleplug_connection_registry.rs | 6 +++--- .../connection/windows/windows_connection.rs | 2 +- .../windows/windows_connection_registry.rs | 6 +++--- lib/src/q30/device/q30_device.rs | 18 +++++++++--------- lib/src/q30/device/q30_device_registry.rs | 14 +++++++------- .../connection/stub_connection_registry.rs | 8 ++++---- web/wasm/src/device.rs | 4 ++-- 29 files changed, 90 insertions(+), 91 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index c502027e..3716db6f 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1,4 +1,4 @@ -use std::{error::Error, sync::Arc}; +use std::{error::Error, rc::Rc}; use clap::Parser; use cli::{Cli, Command}; @@ -59,7 +59,7 @@ async fn do_cli_command(args: Cli, registry: impl DeviceRegistry) -> Result<(), async fn get_device_or_err( registry: &T, descriptor: &T::DescriptorType, -) -> Result, String> +) -> Result, String> where T: DeviceRegistry, { diff --git a/gui/src/actions/create_custom_equalizer_profile.rs b/gui/src/actions/create_custom_equalizer_profile.rs index c3e83807..e6a0e729 100644 --- a/gui/src/actions/create_custom_equalizer_profile.rs +++ b/gui/src/actions/create_custom_equalizer_profile.rs @@ -13,7 +13,7 @@ pub fn create_custom_equalizer_profile( custom_profile: &CustomEqualizerProfileObject, ) -> anyhow::Result<()> where - T: DeviceRegistry + Send + Sync + 'static, + T: DeviceRegistry + 'static, { settings_file.edit(|settings| { settings.set_custom_profile( diff --git a/gui/src/actions/delete_custom_equalizer_profile.rs b/gui/src/actions/delete_custom_equalizer_profile.rs index d217e901..cd508615 100644 --- a/gui/src/actions/delete_custom_equalizer_profile.rs +++ b/gui/src/actions/delete_custom_equalizer_profile.rs @@ -13,7 +13,7 @@ pub fn delete_custom_equalizer_profile( custom_profile: &CustomEqualizerProfileObject, ) -> anyhow::Result<()> where - T: DeviceRegistry + Send + Sync + 'static, + T: DeviceRegistry + 'static, { settings_file.edit(|settings| { settings.remove_custom_profile(&custom_profile.name()); diff --git a/gui/src/actions/refresh_devices.rs b/gui/src/actions/refresh_devices.rs index 067f102b..4a48023a 100644 --- a/gui/src/actions/refresh_devices.rs +++ b/gui/src/actions/refresh_devices.rs @@ -8,7 +8,7 @@ use super::{State, StateUpdate}; pub async fn refresh_devices(state: &Rc>) -> anyhow::Result<()> where - T: DeviceRegistry + Send + Sync + 'static, + T: DeviceRegistry + 'static, { if !state.is_refresh_in_progress.get() { let descriptors = { diff --git a/gui/src/actions/select_custom_equalizer_configuration.rs b/gui/src/actions/select_custom_equalizer_configuration.rs index 26399e7f..114b60de 100644 --- a/gui/src/actions/select_custom_equalizer_configuration.rs +++ b/gui/src/actions/select_custom_equalizer_configuration.rs @@ -17,7 +17,7 @@ pub async fn select_custom_equalizer_configuration( custom_profile: &CustomEqualizerProfileObject, ) -> anyhow::Result<()> where - T: DeviceRegistry + Send + Sync + 'static, + T: DeviceRegistry + 'static, { settings_file .get(|settings| { diff --git a/gui/src/actions/set_ambient_sound_mode.rs b/gui/src/actions/set_ambient_sound_mode.rs index 102930e6..97ae45de 100644 --- a/gui/src/actions/set_ambient_sound_mode.rs +++ b/gui/src/actions/set_ambient_sound_mode.rs @@ -13,7 +13,7 @@ pub async fn set_ambient_sound_mode( ambient_sound_mode: AmbientSoundMode, ) -> anyhow::Result<()> where - T: DeviceRegistry + Send + Sync + 'static, + T: DeviceRegistry + 'static, { let device = state .selected_device() @@ -34,7 +34,7 @@ where #[cfg(test)] mod tests { - use std::sync::Arc; + use std::rc::Rc; use mockall::predicate; use openscq30_lib::{ @@ -72,7 +72,7 @@ mod tests { sound_modes.ambient_sound_mode == AmbientSoundMode::NoiseCanceling })) .return_once(|_ambient_sound_mode| Ok(())); - *state.selected_device.borrow_mut() = Some(Arc::new(selected_device)); + *state.selected_device.borrow_mut() = Some(Rc::new(selected_device)); set_ambient_sound_mode(&state, AmbientSoundMode::NoiseCanceling) .await diff --git a/gui/src/actions/set_custom_noise_canceling.rs b/gui/src/actions/set_custom_noise_canceling.rs index d292210f..126a93d7 100644 --- a/gui/src/actions/set_custom_noise_canceling.rs +++ b/gui/src/actions/set_custom_noise_canceling.rs @@ -14,7 +14,7 @@ pub async fn set_custom_noise_canceling( custom_noise_canceling: CustomNoiseCanceling, ) -> anyhow::Result<()> where - T: DeviceRegistry + Send + Sync + 'static, + T: DeviceRegistry + 'static, { if let Some(handle) = state.set_custom_noise_canceling_handle.take() { handle.abort(); @@ -61,7 +61,7 @@ where #[cfg(test)] mod tests { - use std::sync::Arc; + use std::rc::Rc; use mockall::predicate; use openscq30_lib::{ @@ -99,7 +99,7 @@ mod tests { sound_modes.custom_noise_canceling == CustomNoiseCanceling::new(2) })) .return_once(|_custom_noise_canceling| Ok(())); - *state.selected_device.borrow_mut() = Some(Arc::new(selected_device)); + *state.selected_device.borrow_mut() = Some(Rc::new(selected_device)); set_custom_noise_canceling(&state, CustomNoiseCanceling::new(2)) .await diff --git a/gui/src/actions/set_device.rs b/gui/src/actions/set_device.rs index b344f525..8366b4e3 100644 --- a/gui/src/actions/set_device.rs +++ b/gui/src/actions/set_device.rs @@ -18,7 +18,7 @@ pub async fn set_device( mac_address: Option, ) -> anyhow::Result<()> where - T: DeviceRegistry + Send + Sync + 'static, + T: DeviceRegistry + 'static, { // Clean up any existing devices if let Some(handle) = &*state.connect_to_device_handle.borrow_mut() { @@ -113,7 +113,7 @@ where #[cfg(test)] mod tests { - use std::{collections::VecDeque, sync::Arc}; + use std::{collections::VecDeque, rc::Rc}; use macaddr::MacAddr6; use mockall::predicate; @@ -179,7 +179,7 @@ mod tests { .return_const(receiver); device.expect_state().once().return_const(device_state_2); - Ok(Some(Arc::new(device))) + Ok(Some(Rc::new(device))) }); let (state, mut receiver) = State::new(registry); @@ -268,7 +268,7 @@ mod tests { ..Default::default() }); - Ok(Some(Arc::new(device))) + Ok(Some(Rc::new(device))) }); let (state, _receiver) = State::new(registry); diff --git a/gui/src/actions/set_equalizer_configuration.rs b/gui/src/actions/set_equalizer_configuration.rs index 36e57e47..455ee869 100644 --- a/gui/src/actions/set_equalizer_configuration.rs +++ b/gui/src/actions/set_equalizer_configuration.rs @@ -14,7 +14,7 @@ pub async fn set_equalizer_configuration( equalizer_configuration: impl Into, ) -> anyhow::Result<()> where - T: DeviceRegistry + Send + Sync + 'static, + T: DeviceRegistry + 'static, { set_equalizer_configuration_impl(state, equalizer_configuration.into()).await } @@ -25,7 +25,7 @@ async fn set_equalizer_configuration_impl( equalizer_configuration: EqualizerConfiguration, ) -> anyhow::Result<()> where - T: DeviceRegistry + Send + Sync + 'static, + T: DeviceRegistry + 'static, { if let Some(handle) = state.set_equalizer_configuration_handle.take() { handle.abort(); @@ -64,7 +64,7 @@ where #[cfg(test)] mod tests { - use std::{sync::Arc, time::Duration}; + use std::{rc::Rc, time::Duration}; use gtk::glib::{clone, timeout_future, MainContext}; use mockall::predicate; @@ -90,7 +90,7 @@ mod tests { EqualizerConfiguration::new_from_preset_profile(PresetEqualizerProfile::Acoustic), ))) .return_once(|_ambient_sound_mode| Ok(())); - *state.selected_device.borrow_mut() = Some(Arc::new(selected_device)); + *state.selected_device.borrow_mut() = Some(Rc::new(selected_device)); set_equalizer_configuration( &state, @@ -113,7 +113,7 @@ mod tests { EqualizerConfiguration::new_from_preset_profile(PresetEqualizerProfile::Acoustic), ))) .return_once(|_ambient_sound_mode| Ok(())); - *state.selected_device.borrow_mut() = Some(Arc::new(selected_device)); + *state.selected_device.borrow_mut() = Some(Rc::new(selected_device)); MainContext::default().spawn_local(clone!(@strong state => async move { set_equalizer_configuration( diff --git a/gui/src/actions/set_noise_canceling_mode.rs b/gui/src/actions/set_noise_canceling_mode.rs index db7759b3..79198d4a 100644 --- a/gui/src/actions/set_noise_canceling_mode.rs +++ b/gui/src/actions/set_noise_canceling_mode.rs @@ -13,7 +13,7 @@ pub async fn set_noise_canceling_mode( noise_canceling_mode: NoiseCancelingMode, ) -> anyhow::Result<()> where - T: DeviceRegistry + Send + Sync + 'static, + T: DeviceRegistry + 'static, { let device = state .selected_device() @@ -34,7 +34,7 @@ where #[cfg(test)] mod tests { - use std::sync::Arc; + use std::rc::Rc; use mockall::predicate; use openscq30_lib::{ @@ -72,7 +72,7 @@ mod tests { sound_modes.noise_canceling_mode == NoiseCancelingMode::Transport })) .return_once(|_noise_canceling_mode| Ok(())); - *state.selected_device.borrow_mut() = Some(Arc::new(selected_device)); + *state.selected_device.borrow_mut() = Some(Rc::new(selected_device)); set_noise_canceling_mode(&state, NoiseCancelingMode::Transport) .await diff --git a/gui/src/actions/set_transpareny_mode.rs b/gui/src/actions/set_transpareny_mode.rs index 0cedde29..3a5861e6 100644 --- a/gui/src/actions/set_transpareny_mode.rs +++ b/gui/src/actions/set_transpareny_mode.rs @@ -13,7 +13,7 @@ pub async fn set_transparency_mode( transparency_mode: TransparencyMode, ) -> anyhow::Result<()> where - T: DeviceRegistry + Send + Sync + 'static, + T: DeviceRegistry + 'static, { let device = state .selected_device() @@ -34,7 +34,7 @@ where #[cfg(test)] mod tests { - use std::sync::Arc; + use std::rc::Rc; use mockall::predicate; use openscq30_lib::{ @@ -72,7 +72,7 @@ mod tests { sound_modes.transparency_mode == TransparencyMode::VocalMode })) .return_once(|_transparency_mode| Ok(())); - *state.selected_device.borrow_mut() = Some(Arc::new(selected_device)); + *state.selected_device.borrow_mut() = Some(Rc::new(selected_device)); set_transparency_mode(&state, TransparencyMode::VocalMode) .await diff --git a/gui/src/actions/state.rs b/gui/src/actions/state.rs index eb2399e6..ccb4b425 100644 --- a/gui/src/actions/state.rs +++ b/gui/src/actions/state.rs @@ -1,7 +1,6 @@ use std::{ cell::{Cell, RefCell}, rc::Rc, - sync::Arc, }; use gtk::glib::JoinHandle; @@ -14,11 +13,11 @@ use super::StateUpdate; pub struct State where - T: DeviceRegistry + Send + Sync + 'static, + T: DeviceRegistry + 'static, { pub state_update_sender: UnboundedSender, pub registry: T, - pub selected_device: RefCell>>, + pub selected_device: RefCell>>, pub connect_to_device_handle: RefCell>>, pub set_equalizer_configuration_handle: RefCell>>, pub set_custom_noise_canceling_handle: RefCell>>, @@ -28,7 +27,7 @@ where impl State where - T: DeviceRegistry + Send + Sync + 'static, + T: DeviceRegistry + 'static, { pub fn new(registry: T) -> (Rc, UnboundedReceiver) { let (sender, receiver) = mpsc::unbounded_channel::(); @@ -47,7 +46,7 @@ where ) } - pub fn selected_device(&self) -> Option> { + pub fn selected_device(&self) -> Option> { self.selected_device.borrow().clone() } } diff --git a/gui/src/gtk_openscq30_lib/gtk_device.rs b/gui/src/gtk_openscq30_lib/gtk_device.rs index c2cb3bfa..cf4b1626 100644 --- a/gui/src/gtk_openscq30_lib/gtk_device.rs +++ b/gui/src/gtk_openscq30_lib/gtk_device.rs @@ -1,4 +1,4 @@ -use std::{fmt::Debug, sync::Arc}; +use std::{fmt::Debug, rc::Rc}; use async_trait::async_trait; use macaddr::MacAddr6; @@ -18,15 +18,15 @@ pub struct GtkDevice where InnerDeviceType: Device, { - tokio_runtime: Arc, - soundcore_device: Arc, + tokio_runtime: Rc, + soundcore_device: Rc, } impl GtkDevice where InnerDeviceType: Device, { - pub fn new(device: Arc, tokio_runtime: Arc) -> Self { + pub fn new(device: Rc, tokio_runtime: Rc) -> Self { Self { tokio_runtime, soundcore_device: device, diff --git a/gui/src/gtk_openscq30_lib/gtk_device_registry.rs b/gui/src/gtk_openscq30_lib/gtk_device_registry.rs index 6706d76d..b39228a1 100644 --- a/gui/src/gtk_openscq30_lib/gtk_device_registry.rs +++ b/gui/src/gtk_openscq30_lib/gtk_device_registry.rs @@ -1,4 +1,4 @@ -use std::sync::Arc; +use std::rc::Rc; use macaddr::MacAddr6; use openscq30_lib::api::device::DeviceRegistry; @@ -11,8 +11,8 @@ pub struct GtkDeviceRegistry where InnerRegistryType: DeviceRegistry, { - tokio_runtime: Arc, - soundcore_device_registry: Arc, + tokio_runtime: Rc, + soundcore_device_registry: Rc, } #[async_trait(?Send)] @@ -26,7 +26,7 @@ where async fn device( &self, mac_address: MacAddr6, - ) -> openscq30_lib::Result>> { + ) -> openscq30_lib::Result>> { let mac_address = mac_address.to_owned(); let device_registry = self.soundcore_device_registry.to_owned(); let maybe_device = self @@ -56,15 +56,15 @@ where { pub fn new(registry: InnerRegistryType, tokio_runtime: Runtime) -> Self { Self { - soundcore_device_registry: Arc::new(registry), - tokio_runtime: Arc::new(tokio_runtime), + soundcore_device_registry: Rc::new(registry), + tokio_runtime: Rc::new(tokio_runtime), } } fn to_gtk_device( &self, - device: Arc, - ) -> Arc> { - Arc::new(GtkDevice::new(device, self.tokio_runtime.to_owned())) + device: Rc, + ) -> Rc> { + Rc::new(GtkDevice::new(device, self.tokio_runtime.to_owned())) } } diff --git a/gui/src/main.rs b/gui/src/main.rs index 0cce3fb1..3948d0ed 100644 --- a/gui/src/main.rs +++ b/gui/src/main.rs @@ -187,7 +187,7 @@ fn build_ui(application: &adw::Application) { fn build_ui_2( application: &adw::Application, - gtk_registry: GtkDeviceRegistry, + gtk_registry: GtkDeviceRegistry, ) { #[cfg(target_os = "windows")] if let Err(err) = set_ui_theme(application) { @@ -235,7 +235,7 @@ fn build_ui_2( fn handle_error(err: anyhow::Error, state: &State) where - T: DeviceRegistry + Send + Sync, + T: DeviceRegistry, { let deselect_device = || { state diff --git a/gui/src/mock/device_registry.rs b/gui/src/mock/device_registry.rs index 5a3ec981..9273864b 100644 --- a/gui/src/mock/device_registry.rs +++ b/gui/src/mock/device_registry.rs @@ -1,4 +1,4 @@ -use std::{sync::Arc, time::Duration}; +use std::{rc::Rc, time::Duration}; use async_trait::async_trait; use gtk::glib::timeout_future; @@ -11,7 +11,7 @@ use super::MockDevice; mock! { pub DeviceRegistry { pub fn device_descriptors(&self) -> openscq30_lib::Result>; - pub fn device(&self, mac_address: MacAddr6) -> openscq30_lib::Result>>; + pub fn device(&self, mac_address: MacAddr6) -> openscq30_lib::Result>>; } } @@ -28,7 +28,7 @@ impl DeviceRegistry for MockDeviceRegistry { async fn device( &self, mac_address: MacAddr6, - ) -> openscq30_lib::Result>> { + ) -> openscq30_lib::Result>> { timeout_future(Duration::from_millis(10)).await; self.device(mac_address) } diff --git a/lib/Cargo.toml b/lib/Cargo.toml index b3070a85..938e7721 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -12,6 +12,7 @@ demo = [] crate-type = ["lib"] [dependencies] +web-sys = { version = "0.3", optional = true, features = ["Window"] } tokio = { version = "1", features = ["macros", "sync", "time", "rt"] } futures = { version = "0.3" } uuid = "1.3" @@ -25,7 +26,6 @@ nom = "7" bitflags = { version = "2", features = ["serde"] } serde = { version = "1.0", features = ["derive"] } js-sys = { version = "0.3", optional = true } -web-sys = { version = "0.3", optional = true, features = ["Window"] } wasm-bindgen-futures = { version = "0.4", optional = true } [target.'cfg(any(target_os = "linux", target_os = "macos"))'.dependencies] diff --git a/lib/src/api/connection/connection_registry.rs b/lib/src/api/connection/connection_registry.rs index 26ea3ef7..70c295d5 100644 --- a/lib/src/api/connection/connection_registry.rs +++ b/lib/src/api/connection/connection_registry.rs @@ -1,4 +1,4 @@ -use std::{collections::HashSet, fmt::Debug, sync::Arc}; +use std::{collections::HashSet, fmt::Debug, rc::Rc}; use async_trait::async_trait; use macaddr::MacAddr6; @@ -15,5 +15,5 @@ pub trait ConnectionRegistry { async fn connection( &self, mac_address: MacAddr6, - ) -> crate::Result>>; + ) -> crate::Result>>; } diff --git a/lib/src/api/device/device_registry.rs b/lib/src/api/device/device_registry.rs index 8a249212..d5cc68c1 100644 --- a/lib/src/api/device/device_registry.rs +++ b/lib/src/api/device/device_registry.rs @@ -1,4 +1,4 @@ -use std::{fmt::Debug, sync::Arc}; +use std::{fmt::Debug, rc::Rc}; use async_trait::async_trait; use macaddr::MacAddr6; @@ -11,5 +11,5 @@ pub trait DeviceRegistry { type DescriptorType: DeviceDescriptor + Send + Sync + Debug; async fn device_descriptors(&self) -> crate::Result>; - async fn device(&self, mac_address: MacAddr6) -> crate::Result>>; + async fn device(&self, mac_address: MacAddr6) -> crate::Result>>; } diff --git a/lib/src/demo/device/demo_device_registry.rs b/lib/src/demo/device/demo_device_registry.rs index 30ed21b7..8fd051ab 100644 --- a/lib/src/demo/device/demo_device_registry.rs +++ b/lib/src/demo/device/demo_device_registry.rs @@ -1,4 +1,4 @@ -use std::sync::Arc; +use std::rc::Rc; use async_trait::async_trait; use macaddr::MacAddr6; @@ -31,9 +31,9 @@ impl DeviceRegistry for DemoDeviceRegistry { )]) } - async fn device(&self, mac_address: MacAddr6) -> crate::Result>> { + async fn device(&self, mac_address: MacAddr6) -> crate::Result>> { if mac_address == Self::DEVICE_MAC_ADDRESS { - Ok(Some(Arc::new( + Ok(Some(Rc::new( DemoDevice::new(Self::DEVICE_NAME, Self::DEVICE_MAC_ADDRESS).await, ))) } else { diff --git a/lib/src/futures.rs b/lib/src/futures.rs index cd55b6f0..003f8b0d 100644 --- a/lib/src/futures.rs +++ b/lib/src/futures.rs @@ -13,10 +13,10 @@ pub trait JoinHandle { // tokio's spawn_local returns a JoinHandle, but wasm_bindgen_futures does not, so we can't return // one here. -pub fn spawn_local(future: impl Future + 'static) -> impl JoinHandle { +pub fn spawn(future: impl Future + Send + 'static) -> impl JoinHandle { #[cfg(not(target_arch = "wasm32"))] { - futures_tokio::spawn_local(future) + futures_tokio::spawn(future) } #[cfg(target_arch = "wasm32")] { diff --git a/lib/src/futures/futures_tokio.rs b/lib/src/futures/futures_tokio.rs index a21f7497..d5b485a6 100644 --- a/lib/src/futures/futures_tokio.rs +++ b/lib/src/futures/futures_tokio.rs @@ -15,8 +15,8 @@ impl JoinHandle for TokioJoinHandle { // tokio's spawn_local returns a JoinHandle, but wasm_bindgen_futures does not, so we can't return // one here. -pub fn spawn_local(future: impl Future + 'static) -> TokioJoinHandle { - let join_handle = tokio::task::spawn_local(async move { +pub fn spawn(future: impl Future + Send + 'static) -> TokioJoinHandle { + let join_handle = tokio::task::spawn(async move { future.await; }); TokioJoinHandle(join_handle) diff --git a/lib/src/q30/connection/btleplug/btleplug_connection_registry.rs b/lib/src/q30/connection/btleplug/btleplug_connection_registry.rs index f3ed784a..e5070296 100644 --- a/lib/src/q30/connection/btleplug/btleplug_connection_registry.rs +++ b/lib/src/q30/connection/btleplug/btleplug_connection_registry.rs @@ -1,5 +1,5 @@ use std::collections::HashSet; -use std::sync::{Arc, Weak}; +use std::rc::{Rc, Weak}; use std::time::Duration; use std::vec; @@ -159,12 +159,12 @@ impl ConnectionRegistry for BtlePlugConnectionRegistry { async fn connection( &self, mac_address: MacAddr6, - ) -> crate::Result>> { + ) -> crate::Result>> { match self.connections.lock().await.entry(mac_address.to_owned()) { Entry::Occupied(entry) => Ok(Some(entry.get().to_owned())), Entry::Vacant(entry) => { if let Some(connection) = self.new_connection(mac_address.into_bd_addr()).await? { - let connection = Arc::new(connection); + let connection = Rc::new(connection); entry.insert(connection.to_owned()); Ok(Some(connection)) } else { diff --git a/lib/src/q30/connection/windows/windows_connection.rs b/lib/src/q30/connection/windows/windows_connection.rs index 8a92ef76..b2fc8293 100644 --- a/lib/src/q30/connection/windows/windows_connection.rs +++ b/lib/src/q30/connection/windows/windows_connection.rs @@ -76,7 +76,7 @@ impl WindowsConnection { device, read_characteristic, write_characteristic, - value_changed_token: Arc::new(RwLock::new(None)), + value_changed_token: Default::default(), connection_status_receiver: receiver, connection_status_changed_token, })) diff --git a/lib/src/q30/connection/windows/windows_connection_registry.rs b/lib/src/q30/connection/windows/windows_connection_registry.rs index 138e56cb..feac1a95 100644 --- a/lib/src/q30/connection/windows/windows_connection_registry.rs +++ b/lib/src/q30/connection/windows/windows_connection_registry.rs @@ -1,4 +1,4 @@ -use std::{collections::HashSet, sync::Arc, time::Duration}; +use std::{collections::HashSet, rc::Rc, time::Duration}; use async_trait::async_trait; use macaddr::MacAddr6; @@ -115,7 +115,7 @@ impl ConnectionRegistry for WindowsConnectionRegistry { async fn connection( &self, mac_address: MacAddr6, - ) -> crate::Result>> { - Ok(WindowsConnection::new(mac_address).await?.map(Arc::new)) + ) -> crate::Result>> { + Ok(WindowsConnection::new(mac_address).await?.map(Rc::new)) } } diff --git a/lib/src/q30/device/q30_device.rs b/lib/src/q30/device/q30_device.rs index 2632d7ee..2a5b836c 100644 --- a/lib/src/q30/device/q30_device.rs +++ b/lib/src/q30/device/q30_device.rs @@ -1,4 +1,4 @@ -use std::{sync::Arc, time::Duration}; +use std::{rc::Rc, sync::Arc, time::Duration}; use async_trait::async_trait; use futures::FutureExt; @@ -8,7 +8,7 @@ use tracing::{trace, warn}; use crate::{ api::connection::{Connection, ConnectionStatus}, - futures::{sleep, spawn_local, JoinHandle}, + futures::{sleep, spawn, JoinHandle}, packets::{ outbound::{SetEqualizerPacket, SetSoundModePacket}, structures::{AmbientSoundMode, DeviceFeatureFlags, EqualizerConfiguration, SoundModes}, @@ -27,7 +27,7 @@ pub struct Q30Device where ConnectionType: Connection, { - connection: Arc, + connection: Rc, state: Arc>, join_handle: Box, state_update_sender: broadcast::Sender, @@ -37,7 +37,7 @@ impl Q30Device where ConnectionType: Connection, { - pub async fn new(connection: Arc) -> crate::Result { + pub async fn new(connection: Rc) -> crate::Result { let mut inbound_receiver = connection.inbound_packets_channel().await?; let initial_state = Self::fetch_initial_state(&connection, &mut inbound_receiver).await?; @@ -47,7 +47,7 @@ where let (sender, _) = broadcast::channel(1); let sender_copy = sender.to_owned(); - let join_handle = spawn_local(async move { + let join_handle = spawn(async move { while let Some(packet_bytes) = inbound_receiver.recv().await { match InboundPacket::new(&packet_bytes) { Ok(packet) => { @@ -79,7 +79,7 @@ where } async fn fetch_initial_state( - connection: &Arc, + connection: &ConnectionType, inbound_receiver: &mut Receiver>, ) -> crate::Result { for i in 0..3 { @@ -250,7 +250,7 @@ where #[cfg(test)] mod tests { - use std::{sync::Arc, time::Duration}; + use std::{rc::Rc, time::Duration}; use macaddr::MacAddr6; use tokio::sync::mpsc; @@ -274,8 +274,8 @@ mod tests { ] } - async fn create_test_connection() -> (Arc, mpsc::Sender>) { - let connection = Arc::new(StubConnection::new()); + async fn create_test_connection() -> (Rc, mpsc::Sender>) { + let connection = Rc::new(StubConnection::new()); connection .set_name_return(Ok("Soundcore Q30".to_string())) .await; diff --git a/lib/src/q30/device/q30_device_registry.rs b/lib/src/q30/device/q30_device_registry.rs index c1b79be1..62c37bf9 100644 --- a/lib/src/q30/device/q30_device_registry.rs +++ b/lib/src/q30/device/q30_device_registry.rs @@ -1,4 +1,4 @@ -use std::sync::{Arc, Weak}; +use std::rc::{Rc, Weak}; use async_trait::async_trait; use macaddr::MacAddr6; @@ -68,7 +68,7 @@ where Ok(descriptors) } - async fn device(&self, mac_address: MacAddr6) -> crate::Result>> { + async fn device(&self, mac_address: MacAddr6) -> crate::Result>> { match self.devices.lock().await.entry(mac_address.to_owned()) { Entry::Occupied(entry) => { tracing::debug!("{mac_address} is cached"); @@ -77,7 +77,7 @@ where Entry::Vacant(entry) => { tracing::debug!("{mac_address} is not cached"); if let Some(device) = self.new_device(mac_address).await? { - let device = Arc::new(device); + let device = Rc::new(device); entry.insert(device.to_owned()); Ok(Some(device)) } else { @@ -90,7 +90,7 @@ where #[cfg(test)] mod tests { - use std::{collections::HashMap, sync::Arc}; + use std::{collections::HashMap, rc::Rc}; use macaddr::MacAddr6; use tokio::sync::mpsc; @@ -112,7 +112,7 @@ mod tests { // Must start with soundcore prefix MacAddr6::new(0xAC, 0x12, 0x2F, 0x01, 0x02, 0x03), ); - let device = Arc::new(StubConnection::new()); + let device = Rc::new(StubConnection::new()); let devices = HashMap::from([(descriptor, device)]); let connection_registry = StubConnectionRegistry::new(devices.to_owned()); let device_registry = Q30DeviceRegistry::new(connection_registry).await.unwrap(); @@ -138,7 +138,7 @@ mod tests { "Stub Device", MacAddr6::new(0x00, 0x11, 0x22, 0x33, 0x44, 0x55), ); - let device = Arc::new(StubConnection::new()); + let device = Rc::new(StubConnection::new()); let (sender, receiver) = mpsc::channel(1); sender .send(vec![ @@ -183,7 +183,7 @@ mod tests { "Stub Device", MacAddr6::new(0x00, 0x11, 0x22, 0x33, 0x44, 0x55), ); - let device = Arc::new(StubConnection::new()); + let device = Rc::new(StubConnection::new()); let devices = HashMap::from([(descriptor, device)]); let connection_registry = StubConnectionRegistry::new(devices.to_owned()); diff --git a/lib/src/stub/connection/stub_connection_registry.rs b/lib/src/stub/connection/stub_connection_registry.rs index 9c48a8db..4cf4d64b 100644 --- a/lib/src/stub/connection/stub_connection_registry.rs +++ b/lib/src/stub/connection/stub_connection_registry.rs @@ -1,6 +1,6 @@ use std::{ collections::{HashMap, HashSet}, - sync::Arc, + rc::Rc, }; use async_trait::async_trait; @@ -15,14 +15,14 @@ use super::StubConnection; #[derive(Debug)] pub struct StubConnectionRegistry { connections: - HashMap::ConnectionType>>, + HashMap::ConnectionType>>, } impl StubConnectionRegistry { pub fn new( connections: HashMap< GenericConnectionDescriptor, - Arc<::ConnectionType>, + Rc<::ConnectionType>, >, ) -> Self { Self { connections } @@ -41,7 +41,7 @@ impl ConnectionRegistry for StubConnectionRegistry { async fn connection( &self, mac_address: MacAddr6, - ) -> crate::Result>> { + ) -> crate::Result>> { Ok(self .connections .iter() diff --git a/web/wasm/src/device.rs b/web/wasm/src/device.rs index e6330496..d66b786a 100644 --- a/web/wasm/src/device.rs +++ b/web/wasm/src/device.rs @@ -1,4 +1,4 @@ -use std::sync::Arc; +use std::rc::Rc; use js_sys::Function; use macaddr::MacAddr6; @@ -22,7 +22,7 @@ impl Device { #[wasm_bindgen] pub async fn new(device: BluetoothDevice) -> Result { let connection = WebBluetoothConnection::new(device).await?; - let device = Q30Device::new(Arc::new(connection)) + let device = Q30Device::new(Rc::new(connection)) .await .map_err(|err| format!("{err:?}"))?; Ok(Self {