From 262fe5630ff8f7124da1a1f70d54367f09a1454e Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Tue, 6 Dec 2022 18:56:58 +0100 Subject: [PATCH] feat(sdk): Implement IntoFuture for LoginBuilder and SsoLoginBuilder --- crates/matrix-sdk/src/client/login_builder.rs | 37 ++++++++++++++++++- crates/matrix-sdk/src/client/mod.rs | 3 -- crates/matrix-sdk/tests/integration/client.rs | 1 - .../tests/integration/refresh_token.rs | 1 - examples/autojoin/src/main.rs | 6 +-- examples/command_bot/src/main.rs | 6 +-- examples/cross_signing_bootstrap/src/main.rs | 7 +--- examples/custom_events/src/main.rs | 1 - examples/emoji_verification/src/main.rs | 1 - examples/get_profiles/src/main.rs | 6 +-- examples/getting_started/src/main.rs | 1 - examples/image_bot/src/main.rs | 6 +-- examples/login/src/main.rs | 6 +-- examples/timeline/src/main.rs | 1 - examples/wasm_command_bot/src/lib.rs | 1 - labs/jack-in/src/main.rs | 2 +- .../src/helpers.rs | 2 +- 17 files changed, 44 insertions(+), 44 deletions(-) diff --git a/crates/matrix-sdk/src/client/login_builder.rs b/crates/matrix-sdk/src/client/login_builder.rs index 6e0d856c2..b520001e5 100644 --- a/crates/matrix-sdk/src/client/login_builder.rs +++ b/crates/matrix-sdk/src/client/login_builder.rs @@ -14,8 +14,10 @@ // limitations under the License. #![cfg_attr(not(target_arch = "wasm32"), deny(clippy::future_not_send))] -#[cfg(feature = "sso-login")] -use std::future::Future; +use std::{ + future::{Future, IntoFuture}, + pin::Pin, +}; use ruma::{ api::client::{session::login, uiaa::UserIdentifier}, @@ -136,6 +138,9 @@ impl LoginBuilder { } /// Send the login request. + /// + /// Instead of calling this function and `.await`ing its return value, you + /// can also `.await` the `LoginBuilder` directly. #[instrument( target = "matrix_sdk::client", name = "login", @@ -159,6 +164,16 @@ impl LoginBuilder { } } +impl IntoFuture for LoginBuilder { + type Output = Result; + // TODO: Use impl Trait once allowed in this position on stable + type IntoFuture = Pin>>; + + fn into_future(self) -> Self::IntoFuture { + Box::pin(self.send()) + } +} + /// Builder type used to configure optional settings for logging in via SSO. /// /// Created with [`Client::login_sso`]. @@ -260,6 +275,9 @@ where } /// Send the login request. + /// + /// Instead of calling this function and `.await`ing its return value, you + /// can also `.await` the `SsoLoginBuilder` directly. #[instrument(target = "matrix_sdk::client", name = "login", skip_all, fields(method = "sso"))] pub async fn send(self) -> Result { use std::{ @@ -399,3 +417,18 @@ where login_builder.send().await } } + +#[cfg(feature = "sso-login")] +impl IntoFuture for SsoLoginBuilder +where + F: FnOnce(String) -> Fut + Send + 'static, + Fut: Future> + Send + 'static, +{ + type Output = Result; + // TODO: Use impl Trait once allowed in this position on stable + type IntoFuture = Pin>>; + + fn into_future(self) -> Self::IntoFuture { + Box::pin(self.send()) + } +} diff --git a/crates/matrix-sdk/src/client/mod.rs b/crates/matrix-sdk/src/client/mod.rs index 3524cb9a2..86d1eec3d 100644 --- a/crates/matrix-sdk/src/client/mod.rs +++ b/crates/matrix-sdk/src/client/mod.rs @@ -983,7 +983,6 @@ impl Client { /// let response = client /// .login_username(user, "wordpass") /// .initial_device_display_name("My bot") - /// .send() /// .await?; /// /// println!( @@ -1046,7 +1045,6 @@ impl Client { /// let response = client /// .login_token(login_token) /// .initial_device_display_name("My app") - /// .send() /// .await /// .unwrap(); /// @@ -1107,7 +1105,6 @@ impl Client { /// Ok(()) /// }) /// .initial_device_display_name("My app") - /// .send() /// .await /// .unwrap(); /// diff --git a/crates/matrix-sdk/tests/integration/client.rs b/crates/matrix-sdk/tests/integration/client.rs index 27f7fa5aa..74f56ed48 100644 --- a/crates/matrix-sdk/tests/integration/client.rs +++ b/crates/matrix-sdk/tests/integration/client.rs @@ -132,7 +132,6 @@ async fn login_with_sso() { Ok(()) }) .identity_provider_id(&idp.id) - .send() .await .unwrap(); diff --git a/crates/matrix-sdk/tests/integration/refresh_token.rs b/crates/matrix-sdk/tests/integration/refresh_token.rs index dc69375ed..469bec272 100644 --- a/crates/matrix-sdk/tests/integration/refresh_token.rs +++ b/crates/matrix-sdk/tests/integration/refresh_token.rs @@ -82,7 +82,6 @@ async fn login_sso_refresh_token() { }) .identity_provider_id(&idp.id) .request_refresh_token() - .send() .await .unwrap(); diff --git a/examples/autojoin/src/main.rs b/examples/autojoin/src/main.rs index 54bca70b5..bfb9b3b83 100644 --- a/examples/autojoin/src/main.rs +++ b/examples/autojoin/src/main.rs @@ -60,11 +60,7 @@ async fn login_and_sync( let client = client_builder.build().await?; - client - .login_username(username, password) - .initial_device_display_name("autojoin bot") - .send() - .await?; + client.login_username(username, password).initial_device_display_name("autojoin bot").await?; println!("logged in as {username}"); diff --git a/examples/command_bot/src/main.rs b/examples/command_bot/src/main.rs index 676dd7293..52030d336 100644 --- a/examples/command_bot/src/main.rs +++ b/examples/command_bot/src/main.rs @@ -51,11 +51,7 @@ async fn login_and_sync( } let client = client_builder.build().await.unwrap(); - client - .login_username(&username, &password) - .initial_device_display_name("command bot") - .send() - .await?; + client.login_username(&username, &password).initial_device_display_name("command bot").await?; println!("logged in as {username}"); diff --git a/examples/cross_signing_bootstrap/src/main.rs b/examples/cross_signing_bootstrap/src/main.rs index a6b27112e..522eb8be9 100644 --- a/examples/cross_signing_bootstrap/src/main.rs +++ b/examples/cross_signing_bootstrap/src/main.rs @@ -36,11 +36,8 @@ async fn login(homeserver_url: String, username: &str, password: &str) -> matrix let homeserver_url = Url::parse(&homeserver_url).expect("Couldn't parse the homeserver URL"); let client = Client::new(homeserver_url).await.unwrap(); - let response = client - .login_username(username, password) - .initial_device_display_name("rust-sdk") - .send() - .await?; + let response = + client.login_username(username, password).initial_device_display_name("rust-sdk").await?; let user_id = &response.user_id; let client_ref = &client; diff --git a/examples/custom_events/src/main.rs b/examples/custom_events/src/main.rs index d3745aebb..a7000da4d 100644 --- a/examples/custom_events/src/main.rs +++ b/examples/custom_events/src/main.rs @@ -109,7 +109,6 @@ async fn login_and_sync( client .login_username(username, password) .initial_device_display_name("getting started bot") - .send() .await?; // it worked! diff --git a/examples/emoji_verification/src/main.rs b/examples/emoji_verification/src/main.rs index 496c4d18d..7e8a8d644 100644 --- a/examples/emoji_verification/src/main.rs +++ b/examples/emoji_verification/src/main.rs @@ -175,7 +175,6 @@ async fn login(cli: Cli) -> Result { client .login_username(&cli.user_name, &cli.password) .initial_device_display_name("rust-sdk") - .send() .await?; Ok(client) diff --git a/examples/get_profiles/src/main.rs b/examples/get_profiles/src/main.rs index 90a69f78d..f2e7b6c2b 100644 --- a/examples/get_profiles/src/main.rs +++ b/examples/get_profiles/src/main.rs @@ -39,11 +39,7 @@ async fn login( let homeserver_url = Url::parse(&homeserver_url).expect("Couldn't parse the homeserver URL"); let client = Client::new(homeserver_url).await.unwrap(); - client - .login_username(username, password) - .initial_device_display_name("rust-sdk") - .send() - .await?; + client.login_username(username, password).initial_device_display_name("rust-sdk").await?; Ok(client) } diff --git a/examples/getting_started/src/main.rs b/examples/getting_started/src/main.rs index 475569a3c..f4b8412c4 100644 --- a/examples/getting_started/src/main.rs +++ b/examples/getting_started/src/main.rs @@ -75,7 +75,6 @@ async fn login_and_sync( client .login_username(username, password) .initial_device_display_name("getting started bot") - .send() .await?; // It worked! diff --git a/examples/image_bot/src/main.rs b/examples/image_bot/src/main.rs index 908e4019a..ef509b54b 100644 --- a/examples/image_bot/src/main.rs +++ b/examples/image_bot/src/main.rs @@ -33,11 +33,7 @@ async fn login_and_sync( let homeserver_url = Url::parse(&homeserver_url).expect("Couldn't parse the homeserver URL"); let client = Client::new(homeserver_url).await.unwrap(); - client - .login_username(&username, &password) - .initial_device_display_name("command bot") - .send() - .await?; + client.login_username(&username, &password).initial_device_display_name("command bot").await?; let response = client.sync_once(SyncSettings::default()).await.unwrap(); diff --git a/examples/login/src/main.rs b/examples/login/src/main.rs index 3998203b0..e75ab69f9 100644 --- a/examples/login/src/main.rs +++ b/examples/login/src/main.rs @@ -36,11 +36,7 @@ async fn login(homeserver_url: String, username: &str, password: &str) -> matrix client.add_event_handler(on_room_message); - client - .login_username(username, password) - .initial_device_display_name("rust-sdk") - .send() - .await?; + client.login_username(username, password).initial_device_display_name("rust-sdk").await?; client.sync(SyncSettings::new()).await?; Ok(()) diff --git a/examples/timeline/src/main.rs b/examples/timeline/src/main.rs index 8bf228161..297d73071 100644 --- a/examples/timeline/src/main.rs +++ b/examples/timeline/src/main.rs @@ -45,7 +45,6 @@ async fn login(cli: Cli) -> Result { client .login_username(&cli.user_name, &cli.password) .initial_device_display_name("rust-sdk") - .send() .await?; Ok(client) diff --git a/examples/wasm_command_bot/src/lib.rs b/examples/wasm_command_bot/src/lib.rs index 14a687a5e..30a64b9e3 100644 --- a/examples/wasm_command_bot/src/lib.rs +++ b/examples/wasm_command_bot/src/lib.rs @@ -73,7 +73,6 @@ pub async fn run() -> Result { client .login_username(username, password) .initial_device_display_name("rust-sdk-wasm") - .send() .await .unwrap(); diff --git a/labs/jack-in/src/main.rs b/labs/jack-in/src/main.rs index 9bad51eed..25361fe7f 100644 --- a/labs/jack-in/src/main.rs +++ b/labs/jack-in/src/main.rs @@ -253,7 +253,7 @@ async fn main() -> Result<()> { .with_prompt(format!("Password for {user_id:} :")) .interact()?, }; - client.login_username(&user_id, &password).send().await?; + client.login_username(&user_id, &password).await?; } if let Some(session) = client.session() { diff --git a/testing/matrix-sdk-integration-testing/src/helpers.rs b/testing/matrix-sdk-integration-testing/src/helpers.rs index 3f7b9ea1c..dd534e339 100644 --- a/testing/matrix-sdk-integration-testing/src/helpers.rs +++ b/testing/matrix-sdk-integration-testing/src/helpers.rs @@ -71,7 +71,7 @@ pub async fn get_client_for_user(username: String, use_sled_store: bool) -> Resu let _ = client.register(request).await; } } - client.login_username(&username, &username).send().await?; + client.login_username(&username, &username).await?; users.insert(username, (client.clone(), tmp_dir)); // keeping temp dir around so it doesn't get destroyed yet Ok(client)