Mercurial > hg-stable
changeset 44693:61fda2dbc522
rust-chg: leverage impl trait at argument position
Differential Revision: https://phab.mercurial-scm.org/D8401
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sat, 11 Apr 2020 00:21:37 +0900 |
parents | 48b99af7b4b3 |
children | 5ac5c25ea97b |
files | rust/chg/src/clientext.rs rust/chg/src/locator.rs rust/chg/src/message.rs |
diffstat | 3 files changed, 38 insertions(+), 65 deletions(-) [+] |
line wrap: on
line diff
--- a/rust/chg/src/clientext.rs Mon Apr 13 01:19:09 2020 -0400 +++ b/rust/chg/src/clientext.rs Sat Apr 11 00:21:37 2020 +0900 @@ -32,29 +32,27 @@ E: AsRawFd; /// Changes the working directory of the server. - fn set_current_dir<P>(self, dir: P) -> OneShotRequest<C> - where - P: AsRef<Path>; + fn set_current_dir(self, dir: impl AsRef<Path>) -> OneShotRequest<C>; /// Updates the environment variables of the server. - fn set_env_vars_os<I, P>(self, vars: I) -> OneShotRequest<C> - where - I: IntoIterator<Item = (P, P)>, - P: AsRef<OsStr>; + fn set_env_vars_os( + self, + vars: impl IntoIterator<Item = (impl AsRef<OsStr>, impl AsRef<OsStr>)>, + ) -> OneShotRequest<C>; /// Changes the process title of the server. - fn set_process_name<P>(self, name: P) -> OneShotRequest<C> - where - P: AsRef<OsStr>; + fn set_process_name(self, name: impl AsRef<OsStr>) -> OneShotRequest<C>; /// Changes the umask of the server process. fn set_umask(self, mask: u32) -> OneShotRequest<C>; /// Runs the specified Mercurial command with cHg extension. - fn run_command_chg<I, P, H>(self, handler: H, args: I) -> ChgRunCommand<C, H> + fn run_command_chg<H>( + self, + handler: H, + args: impl IntoIterator<Item = impl AsRef<OsStr>>, + ) -> ChgRunCommand<C, H> where - I: IntoIterator<Item = P>, - P: AsRef<OsStr>, H: SystemHandler; /// Validates if the server can run Mercurial commands with the expected @@ -65,10 +63,10 @@ /// /// Client-side environment must be sent prior to this request, by /// `set_current_dir()` and `set_env_vars_os()`. - fn validate<I, P>(self, args: I) -> OneShotQuery<C, fn(Bytes) -> io::Result<Vec<Instruction>>> - where - I: IntoIterator<Item = P>, - P: AsRef<OsStr>; + fn validate( + self, + args: impl IntoIterator<Item = impl AsRef<OsStr>>, + ) -> OneShotQuery<C, fn(Bytes) -> io::Result<Vec<Instruction>>>; } impl<C> ChgClientExt<C> for Client<C> @@ -84,25 +82,18 @@ AttachIo::with_client(self, stdin, stdout, Some(stderr)) } - fn set_current_dir<P>(self, dir: P) -> OneShotRequest<C> - where - P: AsRef<Path>, - { + fn set_current_dir(self, dir: impl AsRef<Path>) -> OneShotRequest<C> { OneShotRequest::start_with_args(self, b"chdir", dir.as_ref().as_os_str().as_bytes()) } - fn set_env_vars_os<I, P>(self, vars: I) -> OneShotRequest<C> - where - I: IntoIterator<Item = (P, P)>, - P: AsRef<OsStr>, - { + fn set_env_vars_os( + self, + vars: impl IntoIterator<Item = (impl AsRef<OsStr>, impl AsRef<OsStr>)>, + ) -> OneShotRequest<C> { OneShotRequest::start_with_args(self, b"setenv", message::pack_env_vars_os(vars)) } - fn set_process_name<P>(self, name: P) -> OneShotRequest<C> - where - P: AsRef<OsStr>, - { + fn set_process_name(self, name: impl AsRef<OsStr>) -> OneShotRequest<C> { OneShotRequest::start_with_args(self, b"setprocname", name.as_ref().as_bytes()) } @@ -112,20 +103,21 @@ OneShotRequest::start_with_args(self, b"setumask2", args) } - fn run_command_chg<I, P, H>(self, handler: H, args: I) -> ChgRunCommand<C, H> + fn run_command_chg<H>( + self, + handler: H, + args: impl IntoIterator<Item = impl AsRef<OsStr>>, + ) -> ChgRunCommand<C, H> where - I: IntoIterator<Item = P>, - P: AsRef<OsStr>, H: SystemHandler, { ChgRunCommand::with_client(self, handler, message::pack_args_os(args)) } - fn validate<I, P>(self, args: I) -> OneShotQuery<C, fn(Bytes) -> io::Result<Vec<Instruction>>> - where - I: IntoIterator<Item = P>, - P: AsRef<OsStr>, - { + fn validate( + self, + args: impl IntoIterator<Item = impl AsRef<OsStr>>, + ) -> OneShotQuery<C, fn(Bytes) -> io::Result<Vec<Instruction>>> { OneShotQuery::start_with_args( self, b"validate",
--- a/rust/chg/src/locator.rs Mon Apr 13 01:19:09 2020 -0400 +++ b/rust/chg/src/locator.rs Sat Apr 11 00:21:37 2020 +0900 @@ -75,11 +75,7 @@ } /// Specifies the arguments to be passed to the server at start. - pub fn set_early_args<I, P>(&mut self, args: I) - where - I: IntoIterator<Item = P>, - P: AsRef<OsStr>, - { + pub fn set_early_args(&mut self, args: impl IntoIterator<Item = impl AsRef<OsStr>>) { self.hg_early_args = args.into_iter().map(|a| a.as_ref().to_owned()).collect(); } @@ -358,10 +354,7 @@ /// Creates a directory which the other users cannot access to. /// /// If the directory already exists, tests its permission. -fn create_secure_dir<P>(path: P) -> io::Result<()> -where - P: AsRef<Path>, -{ +fn create_secure_dir(path: impl AsRef<Path>) -> io::Result<()> { DirBuilder::new() .mode(0o700) .create(path.as_ref()) @@ -404,11 +397,7 @@ } /// Collects arguments which need to be passed to the server at start. -pub fn collect_early_args<I, P>(args: I) -> Vec<OsString> -where - I: IntoIterator<Item = P>, - P: AsRef<OsStr>, -{ +pub fn collect_early_args(args: impl IntoIterator<Item = impl AsRef<OsStr>>) -> Vec<OsString> { let mut args_iter = args.into_iter(); let mut early_args = Vec::new(); while let Some(arg) = args_iter.next() {
--- a/rust/chg/src/message.rs Mon Apr 13 01:19:09 2020 -0400 +++ b/rust/chg/src/message.rs Sat Apr 11 00:21:37 2020 +0900 @@ -113,11 +113,9 @@ /// /// Panics if key or value contains `\0` character, or key contains '=' /// character. -pub fn pack_env_vars_os<I, P>(vars: I) -> Bytes -where - I: IntoIterator<Item = (P, P)>, - P: AsRef<OsStr>, -{ +pub fn pack_env_vars_os( + vars: impl IntoIterator<Item = (impl AsRef<OsStr>, impl AsRef<OsStr>)>, +) -> Bytes { let mut vars_iter = vars.into_iter(); if let Some((k, v)) = vars_iter.next() { let mut dst = BytesMut::with_capacity(INITIAL_PACKED_ENV_VARS_CAPACITY); @@ -143,17 +141,11 @@ dst.put_slice(v.as_bytes()); } -fn decode_latin1<S>(s: S) -> String -where - S: AsRef<[u8]>, -{ +fn decode_latin1(s: impl AsRef<[u8]>) -> String { s.as_ref().iter().map(|&c| c as char).collect() } -fn new_parse_error<E>(error: E) -> io::Error -where - E: Into<Box<dyn error::Error + Send + Sync>>, -{ +fn new_parse_error(error: impl Into<Box<dyn error::Error + Send + Sync>>) -> io::Error { io::Error::new(io::ErrorKind::InvalidData, error) }