From 2301fda3070b282c0d298414d27ad11cb21a9a05 Mon Sep 17 00:00:00 2001 From: Sprite Date: Fri, 20 Jan 2023 21:14:20 +0800 Subject: [PATCH] Fix errors caused by sync locks crossing `await` in `cli::authenticate` --- src/helpers/cli.rs | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/src/helpers/cli.rs b/src/helpers/cli.rs index 5f4bbf0..b48d88f 100644 --- a/src/helpers/cli.rs +++ b/src/helpers/cli.rs @@ -7,18 +7,38 @@ use crate::{errors::Result, registration::Registered, Mastodon}; pub async fn authenticate(registration: Registered) -> Result { let url = registration.authorize_url()?; - let stdout = io::stdout(); - let stdin = io::stdin(); + let code = { + let stdout = io::stdout(); + let stdin = io::stdin(); - let mut stdout = stdout.lock(); - let mut stdin = stdin.lock(); + let mut stdout = stdout.lock(); + let mut stdin = stdin.lock(); - writeln!(&mut stdout, "Click this link to authorize: {}", url)?; - write!(&mut stdout, "Paste the returned authorization code: ")?; - stdout.flush()?; + writeln!(&mut stdout, "Click this link to authorize: {}", url)?; + write!(&mut stdout, "Paste the returned authorization code: ")?; + stdout.flush()?; + + let mut input = String::new(); + stdin.read_line(&mut input)?; + input + }; + let code = code.trim(); - let mut input = String::new(); - stdin.read_line(&mut input)?; - let code = input.trim(); registration.complete(code).await } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn send_sync() { + fn assert_send_sync(_: impl Send + Sync) {} + + let mock_reg = || -> Registered { unimplemented!() }; + let no_run = || async move { + let _ = authenticate(mock_reg()).await; + }; + assert_send_sync(no_run()); + } +}