Skip to content

Commit ff34127

Browse files
authored
Fix child io types: Avoid exposing impl details (#58)
* Fix `ChildStd*` types: Avoid exposing impl details Implementing `TryFrom<tokio::process::ChildStd*>` for `ChildStd*` exposes the implementation details, and permits the caller to construct a `ChildStd*`. Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Simplify `impl_from_impl_child_io!` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
1 parent ee50b05 commit ff34127

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/command.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
use super::stdio::TryFromChildIo;
12
use super::RemoteChild;
23
use super::Stdio;
34
use super::{Error, Session};
45

56
use std::borrow::Cow;
6-
use std::convert::TryFrom;
77
use std::ffi::OsStr;
88
use std::process;
99

@@ -235,9 +235,9 @@ impl<'s> Command<'s> {
235235
let (imp, stdin, stdout, stderr) = imp.spawn().await?;
236236
(
237237
imp.into(),
238-
stdin.map(TryFrom::try_from).transpose()?,
239-
stdout.map(TryFrom::try_from).transpose()?,
240-
stderr.map(TryFrom::try_from).transpose()?,
238+
stdin.map(TryFromChildIo::try_from).transpose()?,
239+
stdout.map(TryFromChildIo::try_from).transpose()?,
240+
stderr.map(TryFromChildIo::try_from).transpose()?,
241241
)
242242
}),
243243
))

src/stdio.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ macro_rules! impl_try_from_tokio_process_child_for_stdio {
113113
type Error = Error;
114114

115115
fn try_from(arg: tokio::process::$type) -> Result<Self, Self::Error> {
116-
let wrapper: $type = arg.try_into()?;
116+
let wrapper: $type = TryFromChildIo::try_from(arg)?;
117117
Ok(wrapper.0.into())
118118
}
119119
}
@@ -136,26 +136,32 @@ pub struct ChildStdout(tokio_pipe::PipeRead);
136136
#[derive(Debug)]
137137
pub struct ChildStderr(tokio_pipe::PipeRead);
138138

139+
pub(crate) trait TryFromChildIo<T>: Sized {
140+
type Error;
141+
142+
fn try_from(arg: T) -> Result<Self, Self::Error>;
143+
}
144+
139145
macro_rules! impl_from_impl_child_io {
140146
(process, $type:ident, $inner:ty) => {
141-
impl TryFrom<tokio::process::$type> for $type {
147+
impl TryFromChildIo<tokio::process::$type> for $type {
142148
type Error = Error;
143149

144150
fn try_from(arg: tokio::process::$type) -> Result<Self, Self::Error> {
145151
let fd = arg.as_raw_fd();
146152

147153
// safety: arg.as_raw_fd() is guaranteed to return a valid fd.
148154
let fd = unsafe { dup(fd) }?.into_raw_fd();
149-
Ok(Self(
150-
<$inner>::from_raw_fd_checked(fd).map_err(Error::ChildIo)?,
151-
))
155+
<$inner>::from_raw_fd_checked(fd)
156+
.map(Self)
157+
.map_err(Error::ChildIo)
152158
}
153159
}
154160
};
155161

156162
(native_mux, $type:ident) => {
157163
#[cfg(feature = "native-mux")]
158-
impl TryFrom<native_mux_impl::$type> for $type {
164+
impl TryFromChildIo<native_mux_impl::$type> for $type {
159165
type Error = Error;
160166

161167
fn try_from(arg: native_mux_impl::$type) -> Result<Self, Self::Error> {

0 commit comments

Comments
 (0)