[Updated] D8444: rust-chg: reimplement uihandler by using async-trait and tokio-0.2
yuja (Yuya Nishihara)
phabricator at mercurial-scm.org
Thu Apr 23 18:03:01 UTC 2020
Herald added a subscriber: mercurial-patches.
Closed by commit rHGc794d0da5fb2: rust-chg: reimplement uihandler by using async-trait and tokio-0.2 (authored by yuja).
This revision was automatically updated to reflect the committed changes.
This revision was not accepted when it landed; it landed in state "Needs Review".
REPOSITORY
rHG Mercurial
CHANGES SINCE LAST UPDATE
https://phab.mercurial-scm.org/D8444?vs=21121&id=21188
CHANGES SINCE LAST ACTION
https://phab.mercurial-scm.org/D8444/new/
REVISION DETAIL
https://phab.mercurial-scm.org/D8444
AFFECTED FILES
rust/chg/src/lib.rs
rust/chg/src/uihandler.rs
CHANGE DETAILS
diff --git a/rust/chg/src/uihandler.rs b/rust/chg/src/uihandler.rs
--- a/rust/chg/src/uihandler.rs
+++ b/rust/chg/src/uihandler.rs
@@ -3,8 +3,7 @@
// This software may be used and distributed according to the terms of the
// GNU General Public License version 2 or any later version.
-use futures::future::IntoFuture;
-use futures::Future;
+use async_trait::async_trait;
use std::io;
use std::os::unix::io::AsRawFd;
use std::os::unix::process::ExitStatusExt;
@@ -16,20 +15,19 @@
use crate::procutil;
/// Callback to process shell command requests received from server.
-pub trait SystemHandler: Sized {
+#[async_trait]
+pub trait SystemHandler {
type PagerStdin: AsRawFd;
- type SpawnPagerResult: IntoFuture<Item = (Self, Self::PagerStdin), Error = io::Error>;
- type RunSystemResult: IntoFuture<Item = (Self, i32), Error = io::Error>;
/// Handles pager command request.
///
/// Returns the pipe to be attached to the server if the pager is spawned.
- fn spawn_pager(self, spec: CommandSpec) -> Self::SpawnPagerResult;
+ async fn spawn_pager(&mut self, spec: &CommandSpec) -> io::Result<Self::PagerStdin>;
/// Handles system command request.
///
/// Returns command exit code (positive) or signal number (negative).
- fn run_system(self, spec: CommandSpec) -> Self::RunSystemResult;
+ async fn run_system(&mut self, spec: &CommandSpec) -> io::Result<i32>;
}
/// Default cHg implementation to process requests received from server.
@@ -41,12 +39,11 @@
}
}
+#[async_trait]
impl SystemHandler for ChgUiHandler {
type PagerStdin = ChildStdin;
- type SpawnPagerResult = io::Result<(Self, Self::PagerStdin)>;
- type RunSystemResult = Box<dyn Future<Item = (Self, i32), Error = io::Error> + Send>;
- fn spawn_pager(self, spec: CommandSpec) -> Self::SpawnPagerResult {
+ async fn spawn_pager(&mut self, spec: &CommandSpec) -> io::Result<Self::PagerStdin> {
let mut pager = new_shell_command(&spec).stdin(Stdio::piped()).spawn()?;
let pin = pager.stdin.take().unwrap();
procutil::set_blocking_fd(pin.as_raw_fd())?;
@@ -54,23 +51,22 @@
// otherwise the server won't get SIGPIPE if it does not write
// anything. (issue5278)
// kill(peerpid, SIGPIPE);
- tokio::spawn(pager.map(|_| ()).map_err(|_| ())); // just ignore errors
- Ok((self, pin))
+ tokio::spawn(async { pager.await }); // just ignore errors
+ Ok(pin)
}
- fn run_system(self, spec: CommandSpec) -> Self::RunSystemResult {
- let fut = new_shell_command(&spec)
- .spawn()
- .into_future()
- .flatten()
- .map(|status| {
+ async fn run_system(&mut self, spec: &CommandSpec) -> io::Result<i32> {
+ let status = new_shell_command(&spec).spawn()?.await?;
+ // TODO: unindent
+ {
+ {
let code = status
.code()
.or_else(|| status.signal().map(|n| -n))
.expect("either exit code or signal should be set");
- (self, code)
- });
- Box::new(fut)
+ Ok(code)
+ }
+ }
}
}
diff --git a/rust/chg/src/lib.rs b/rust/chg/src/lib.rs
--- a/rust/chg/src/lib.rs
+++ b/rust/chg/src/lib.rs
@@ -9,7 +9,7 @@
pub mod message;
pub mod procutil;
//mod runcommand;
-//mod uihandler;
+mod uihandler;
//pub use clientext::ChgClientExt;
-//pub use uihandler::{ChgUiHandler, SystemHandler};
+pub use uihandler::{ChgUiHandler, SystemHandler};
To: yuja, #hg-reviewers, Alphare
Cc: mercurial-patches, mercurial-devel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mercurial-scm.org/pipermail/mercurial-patches/attachments/20200423/5ab65361/attachment-0001.html>
More information about the Mercurial-patches
mailing list