-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe76540
commit b2731a7
Showing
5 changed files
with
79 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use std::{ | ||
io, | ||
pin::Pin, | ||
sync::{Arc, Mutex}, | ||
task::{Context, Poll, Waker}, | ||
}; | ||
|
||
struct State { | ||
waker: Option<Waker>, | ||
data: Option<io::Result<std::process::Output>>, | ||
} | ||
|
||
pub struct AsyncCommand { | ||
state: Arc<Mutex<State>>, | ||
} | ||
|
||
impl AsyncCommand { | ||
pub fn spawn(mut command: std::process::Command) -> Self { | ||
let state = Arc::new(Mutex::new(State { | ||
waker: None, | ||
data: None, | ||
})); | ||
|
||
std::thread::spawn({ | ||
let state = state.clone(); | ||
move || { | ||
let output = command.output(); | ||
|
||
let mut state = state.lock().unwrap(); | ||
state.data = Some(output); | ||
|
||
if let Some(waker) = state.waker.take() { | ||
waker.wake(); | ||
} | ||
} | ||
}); | ||
|
||
Self { state } | ||
} | ||
} | ||
|
||
impl std::future::Future for AsyncCommand { | ||
type Output = io::Result<std::process::Output>; | ||
|
||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
let mut state = self.state.lock().unwrap(); | ||
|
||
if state.data.is_some() { | ||
Poll::Ready(state.data.take().unwrap()) | ||
} else { | ||
state.waker = Some(cx.waker().clone()); | ||
Poll::Pending | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
mod child_stdout; | ||
mod async_command; | ||
pub(crate) mod zenity; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters