From 2313c099fa423bda69d73e6771775b8e42ffd951 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Tue, 23 Aug 2022 11:16:59 +0200 Subject: [PATCH] chore(bindings): Replace parking_lot RwLock by std RwLock --- Cargo.lock | 1 - bindings/matrix-sdk-ffi/Cargo.toml | 1 - .../src/authentication_service.rs | 13 ++++---- bindings/matrix-sdk-ffi/src/client.rs | 27 ++++++++------- bindings/matrix-sdk-ffi/src/room.rs | 15 ++++----- .../src/session_verification.rs | 33 +++++++++---------- 6 files changed, 42 insertions(+), 48 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f6e968e9a..65f1e9dca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2402,7 +2402,6 @@ dependencies = [ "futures-util", "matrix-sdk", "once_cell", - "parking_lot 0.12.1", "sanitize-filename-reader-friendly", "serde", "serde_json", diff --git a/bindings/matrix-sdk-ffi/Cargo.toml b/bindings/matrix-sdk-ffi/Cargo.toml index 6f19497af..5c5ca73f9 100644 --- a/bindings/matrix-sdk-ffi/Cargo.toml +++ b/bindings/matrix-sdk-ffi/Cargo.toml @@ -23,7 +23,6 @@ futures-core = "0.3.17" futures-util = { version = "0.3.17", default-features = false } matrix-sdk = { path = "../../crates/matrix-sdk", features = ["experimental-timeline", "markdown"] } once_cell = "1.10.0" -parking_lot = "0.12.0" sanitize-filename-reader-friendly = "2.2.1" serde = { version = "1", features = ["derive"] } serde_json = { version = "1" } diff --git a/bindings/matrix-sdk-ffi/src/authentication_service.rs b/bindings/matrix-sdk-ffi/src/authentication_service.rs index 7adc787c2..7f11bc570 100644 --- a/bindings/matrix-sdk-ffi/src/authentication_service.rs +++ b/bindings/matrix-sdk-ffi/src/authentication_service.rs @@ -1,11 +1,10 @@ -use std::sync::Arc; +use std::sync::{Arc, RwLock}; use futures_util::future::join3; use matrix_sdk::{ ruma::{OwnedDeviceId, UserId}, Session, }; -use parking_lot::RwLock; use super::{client::Client, client_builder::ClientBuilder, RUNTIME}; @@ -66,7 +65,7 @@ impl AuthenticationService { } pub fn homeserver_details(&self) -> Option> { - self.homeserver_details.read().clone() + self.homeserver_details.read().unwrap().clone() } /// Updates the service to authenticate with the homeserver for the @@ -85,8 +84,8 @@ impl AuthenticationService { RUNTIME.block_on(async move { let details = Arc::new(self.details_from_client(&client).await?); - *self.client.write() = Some(client); - *self.homeserver_details.write() = Some(details); + *self.client.write().unwrap() = Some(client); + *self.homeserver_details.write().unwrap() = Some(details); Ok(()) }) @@ -98,7 +97,7 @@ impl AuthenticationService { username: String, password: String, ) -> Result, AuthenticationError> { - match self.client.read().as_ref() { + match self.client.read().unwrap().as_ref() { Some(client) => { // Login and ask the server for the full user ID as this could be different from // the username that was entered. @@ -137,7 +136,7 @@ impl AuthenticationService { token: String, device_id: String, ) -> Result, AuthenticationError> { - match self.client.read().as_ref() { + match self.client.read().unwrap().as_ref() { Some(client) => { // Restore the client and ask the server for the full user ID as this // could be different from the username that was entered. diff --git a/bindings/matrix-sdk-ffi/src/client.rs b/bindings/matrix-sdk-ffi/src/client.rs index 8e478f1d4..3dff33eb7 100644 --- a/bindings/matrix-sdk-ffi/src/client.rs +++ b/bindings/matrix-sdk-ffi/src/client.rs @@ -1,4 +1,4 @@ -use std::sync::Arc; +use std::sync::{Arc, RwLock}; use anyhow::anyhow; use matrix_sdk::{ @@ -16,7 +16,6 @@ use matrix_sdk::{ }, Client as MatrixClient, LoopCtrl, Session, }; -use parking_lot::RwLock; use super::{ room::Room, session_verification::SessionVerificationController, ClientState, RestoreToken, @@ -78,7 +77,7 @@ impl Client { } pub fn set_delegate(&self, delegate: Option>) { - *self.delegate.write() = delegate; + *self.delegate.write().unwrap() = delegate; } /// The homeserver this client is configured to use. @@ -137,18 +136,18 @@ impl Client { client .sync_with_callback(sync_settings, |sync_response| async { - if !state.read().has_first_synced { - state.write().has_first_synced = true + if !state.read().unwrap().has_first_synced { + state.write().unwrap().has_first_synced = true; } - if state.read().should_stop_syncing { - state.write().is_syncing = false; + if state.read().unwrap().should_stop_syncing { + state.write().unwrap().is_syncing = false; return LoopCtrl::Break; - } else if !state.read().is_syncing { - state.write().is_syncing = true; + } else if !state.read().unwrap().is_syncing { + state.write().unwrap().is_syncing = true; } - if let Some(delegate) = &*delegate.read() { + if let Some(delegate) = &*delegate.read().unwrap() { delegate.did_receive_sync_update() } @@ -169,17 +168,17 @@ impl Client { /// Indication whether we've received a first sync response since /// establishing the client (in memory) pub fn has_first_synced(&self) -> bool { - self.state.read().has_first_synced + self.state.read().unwrap().has_first_synced } /// Indication whether we are currently syncing pub fn is_syncing(&self) -> bool { - self.state.read().has_first_synced + self.state.read().unwrap().has_first_synced } /// Is this a guest account? pub fn is_guest(&self) -> bool { - self.state.read().is_guest + self.state.read().unwrap().is_guest } pub fn restore_token(&self) -> anyhow::Result { @@ -189,7 +188,7 @@ impl Client { Ok(serde_json::to_string(&RestoreToken { session, homeurl, - is_guest: self.state.read().is_guest, + is_guest: self.state.read().unwrap().is_guest, })?) }) } diff --git a/bindings/matrix-sdk-ffi/src/room.rs b/bindings/matrix-sdk-ffi/src/room.rs index f3718f2e5..38ce71c6d 100644 --- a/bindings/matrix-sdk-ffi/src/room.rs +++ b/bindings/matrix-sdk-ffi/src/room.rs @@ -1,4 +1,4 @@ -use std::sync::Arc; +use std::sync::{Arc, RwLock}; use anyhow::{bail, Result}; use futures_util::{pin_mut, StreamExt}; @@ -6,7 +6,6 @@ use matrix_sdk::{ room::Room as MatrixRoom, ruma::{events::room::message::RoomMessageEventContent, UserId}, }; -use parking_lot::RwLock; use super::{ backward_stream::BackwardsStream, @@ -40,7 +39,7 @@ impl Room { } pub fn set_delegate(&self, delegate: Option>) { - *self.delegate.write() = delegate; + *self.delegate.write().unwrap() = delegate; } pub fn id(&self) -> String { @@ -115,11 +114,11 @@ impl Room { } pub fn start_live_event_listener(&self) -> Option> { - if *self.is_listening_to_live_events.read() { + if *self.is_listening_to_live_events.read().unwrap() { return None; } - *self.is_listening_to_live_events.write() = true; + *self.is_listening_to_live_events.write().unwrap() = true; let room = self.room.clone(); let delegate = self.delegate.clone(); @@ -133,11 +132,11 @@ impl Room { pin_mut!(forward_stream); while let Some(sync_event) = forward_stream.next().await { - if !(*is_listening_to_live_events.read()) { + if !(*is_listening_to_live_events.read().unwrap()) { return; } - if let Some(delegate) = &*delegate.read() { + if let Some(delegate) = &*delegate.read().unwrap() { if let Some(message) = sync_event_to_message(sync_event) { delegate.did_receive_message(message) } @@ -148,7 +147,7 @@ impl Room { } pub fn stop_live_event_listener(&self) { - *self.is_listening_to_live_events.write() = false; + *self.is_listening_to_live_events.write().unwrap() = false; } pub fn send(&self, msg: Arc, txn_id: Option) -> Result<()> { diff --git a/bindings/matrix-sdk-ffi/src/session_verification.rs b/bindings/matrix-sdk-ffi/src/session_verification.rs index ea3997567..88b39e836 100644 --- a/bindings/matrix-sdk-ffi/src/session_verification.rs +++ b/bindings/matrix-sdk-ffi/src/session_verification.rs @@ -1,4 +1,4 @@ -use std::sync::Arc; +use std::sync::{Arc, RwLock}; use matrix_sdk::{ encryption::{ @@ -10,7 +10,6 @@ use matrix_sdk::{ events::{key::verification::VerificationMethod, AnyToDeviceEvent}, }, }; -use parking_lot::RwLock; use super::RUNTIME; @@ -55,7 +54,7 @@ impl SessionVerificationController { } pub fn set_delegate(&self, delegate: Option>) { - *self.delegate.write() = delegate; + *self.delegate.write().unwrap() = delegate; } pub fn is_verified(&self) -> bool { @@ -67,7 +66,7 @@ impl SessionVerificationController { let methods = vec![VerificationMethod::SasV1]; let verification_request = self.user_identity.request_verification_with_methods(methods).await?; - *self.verification_request.write() = Some(verification_request); + *self.verification_request.write().unwrap() = Some(verification_request); Ok(()) }) @@ -75,7 +74,7 @@ impl SessionVerificationController { pub fn approve_verification(&self) -> anyhow::Result<()> { RUNTIME.block_on(async move { - let sas_verification = self.sas_verification.read().clone(); + let sas_verification = self.sas_verification.read().unwrap().clone(); if let Some(sas_verification) = sas_verification { sas_verification.confirm().await?; } @@ -86,7 +85,7 @@ impl SessionVerificationController { pub fn decline_verification(&self) -> anyhow::Result<()> { RUNTIME.block_on(async move { - let sas_verification = self.sas_verification.read().clone(); + let sas_verification = self.sas_verification.read().unwrap().clone(); if let Some(sas_verification) = sas_verification { sas_verification.mismatch().await?; } @@ -97,7 +96,7 @@ impl SessionVerificationController { pub fn cancel_verification(&self) -> anyhow::Result<()> { RUNTIME.block_on(async move { - let verification_request = self.verification_request.read().clone(); + let verification_request = self.verification_request.read().unwrap().clone(); if let Some(verification) = verification_request { verification.cancel().await?; } @@ -122,7 +121,7 @@ impl SessionVerificationController { return; } - if let Some(delegate) = &*self.delegate.read() { + if let Some(delegate) = &*self.delegate.read().unwrap() { delegate.did_cancel() } } @@ -131,9 +130,9 @@ impl SessionVerificationController { return; } - if let Some(sas_verification) = &*sas_verification.read() { + if let Some(sas_verification) = &*sas_verification.read().unwrap() { if let Some(emojis) = sas_verification.emoji() { - if let Some(delegate) = &*self.delegate.read() { + if let Some(delegate) = &*self.delegate.read().unwrap() { let emojis = emojis .iter() .map(|e| { @@ -146,10 +145,10 @@ impl SessionVerificationController { delegate.did_receive_verification_data(emojis); } - } else if let Some(delegate) = &*self.delegate.read() { + } else if let Some(delegate) = &*self.delegate.read().unwrap() { delegate.did_fail() } - } else if let Some(delegate) = &*self.delegate.read() { + } else if let Some(delegate) = &*self.delegate.read().unwrap() { delegate.did_fail() } } @@ -158,7 +157,7 @@ impl SessionVerificationController { return; } - if let Some(delegate) = &*self.delegate.read() { + if let Some(delegate) = &*self.delegate.read().unwrap() { delegate.did_finish() } } @@ -168,7 +167,7 @@ impl SessionVerificationController { } fn is_transaction_id_valid(&self, transaction_id: String) -> bool { - if let Some(verification) = &*self.verification_request.read() { + if let Some(verification) = &*self.verification_request.read().unwrap() { return verification.flow_id() == transaction_id; } @@ -176,14 +175,14 @@ impl SessionVerificationController { } async fn start_sas_verification(&self) { - let verification_request = self.verification_request.read().clone(); + let verification_request = self.verification_request.read().unwrap().clone(); if let Some(verification) = verification_request { match verification.start_sas().await { Ok(verification) => { - *self.sas_verification.write() = verification; + *self.sas_verification.write().unwrap() = verification; } Err(_) => { - if let Some(delegate) = &*self.delegate.read() { + if let Some(delegate) = &*self.delegate.read().unwrap() { delegate.did_fail() } }