Fix compilation

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2024-11-14 00:17:37 +11:00 committed by Jiahao XU
parent 6770f34ec4
commit a30d61e5c4

View file

@ -4,7 +4,7 @@ use std::{
str, str,
}; };
use tokio::process::Command; use tokio::{io::AsyncWriteExt, process::Command};
use zeroize::Zeroizing; use zeroize::Zeroizing;
pub(super) async fn get() -> io::Result<Zeroizing<Box<str>>> { pub(super) async fn get() -> io::Result<Zeroizing<Box<str>>> {
@ -56,18 +56,22 @@ pub(super) async fn get() -> io::Result<Zeroizing<Box<str>>> {
Ok(Zeroizing::new(password.into())) Ok(Zeroizing::new(password.into()))
} }
// Helper function to execute a command with input trait CommandExt {
async fn output_async_with_stdin( // Helper function to execute a command with input
cmd: &mut Command, async fn output_async_with_stdin(&mut self, input: &[u8]) -> io::Result<Output>;
input: &[u8], }
) -> io::Result<Output> {
let mut child = cmd.spawn()?; impl CommandExt for Command {
let mut stdin = child.stdin.take().expect("Failed to open stdin"); async fn output_async_with_stdin(&mut self, input: &[u8]) -> io::Result<Output> {
let mut child = self.spawn()?;
tokio::spawn(async move {
use tokio::io::AsyncWriteExt; child
let _ = stdin.write_all(input).await; .stdin
}); .take()
.expect("Failed to open stdin")
child.wait_with_output().await .write_all(input)
.await?;
child.wait_with_output().await
}
} }