Skip to content

Commit abce8fc

Browse files
committed
feat(process): use TryFrom to convert to Stdio
1 parent 9543032 commit abce8fc

File tree

3 files changed

+51
-37
lines changed

3 files changed

+51
-37
lines changed

compio-process/src/lib.rs

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -171,21 +171,21 @@ impl Command {
171171
}
172172

173173
/// Configuration for the child process’s standard input (stdin) handle.
174-
pub fn stdin(&mut self, cfg: impl Into<process::Stdio>) -> &mut Self {
175-
self.0.stdin(cfg);
176-
self
174+
pub fn stdin<S: TryInto<process::Stdio>>(&mut self, cfg: S) -> Result<&mut Self, S::Error> {
175+
self.0.stdin(cfg.try_into()?);
176+
Ok(self)
177177
}
178178

179179
/// Configuration for the child process’s standard output (stdout) handle.
180-
pub fn stdout(&mut self, cfg: impl Into<process::Stdio>) -> &mut Self {
181-
self.0.stdout(cfg);
182-
self
180+
pub fn stdout<S: TryInto<process::Stdio>>(&mut self, cfg: S) -> Result<&mut Self, S::Error> {
181+
self.0.stdout(cfg.try_into()?);
182+
Ok(self)
183183
}
184184

185185
/// Configuration for the child process’s standard error (stderr) handle.
186-
pub fn stderr(&mut self, cfg: impl Into<process::Stdio>) -> &mut Self {
187-
self.0.stderr(cfg);
188-
self
186+
pub fn stderr<S: TryInto<process::Stdio>>(&mut self, cfg: S) -> Result<&mut Self, S::Error> {
187+
self.0.stderr(cfg.try_into()?);
188+
Ok(self)
189189
}
190190

191191
/// Returns the path to the program.
@@ -409,15 +409,16 @@ impl ChildStdout {
409409
}
410410
}
411411

412-
impl From<ChildStdout> for process::Stdio {
413-
fn from(value: ChildStdout) -> Self {
414-
Self::from(
415-
value
416-
.0
417-
.into_inner()
418-
.try_unwrap()
419-
.expect("the handle is being used"),
420-
)
412+
impl TryFrom<ChildStdout> for process::Stdio {
413+
type Error = ChildStdout;
414+
415+
fn try_from(value: ChildStdout) -> Result<Self, ChildStdout> {
416+
value
417+
.0
418+
.into_inner()
419+
.try_unwrap()
420+
.map(Self::from)
421+
.map_err(|fd| ChildStdout(unsafe { Attacher::from_shared_fd_unchecked(fd) }))
421422
}
422423
}
423424

@@ -430,15 +431,16 @@ impl ChildStderr {
430431
}
431432
}
432433

433-
impl From<ChildStderr> for process::Stdio {
434-
fn from(value: ChildStderr) -> Self {
435-
Self::from(
436-
value
437-
.0
438-
.into_inner()
439-
.try_unwrap()
440-
.expect("the handle is being used"),
441-
)
434+
impl TryFrom<ChildStderr> for process::Stdio {
435+
type Error = ChildStderr;
436+
437+
fn try_from(value: ChildStderr) -> Result<Self, ChildStderr> {
438+
value
439+
.0
440+
.into_inner()
441+
.try_unwrap()
442+
.map(Self::from)
443+
.map_err(|fd| ChildStderr(unsafe { Attacher::from_shared_fd_unchecked(fd) }))
442444
}
443445
}
444446

@@ -452,14 +454,15 @@ impl ChildStdin {
452454
}
453455
}
454456

455-
impl From<ChildStdin> for process::Stdio {
456-
fn from(value: ChildStdin) -> Self {
457-
Self::from(
458-
value
459-
.0
460-
.into_inner()
461-
.try_unwrap()
462-
.expect("the handle is being used"),
463-
)
457+
impl TryFrom<ChildStdin> for process::Stdio {
458+
type Error = ChildStdin;
459+
460+
fn try_from(value: ChildStdin) -> Result<Self, ChildStdin> {
461+
value
462+
.0
463+
.into_inner()
464+
.try_unwrap()
465+
.map(Self::from)
466+
.map_err(|fd| ChildStdin(unsafe { Attacher::from_shared_fd_unchecked(fd) }))
464467
}
465468
}

compio-process/tests/process.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ async fn echo() {
3838
let child = cmd
3939
.arg("echo hello world")
4040
.stdout(Stdio::piped())
41+
.unwrap()
4142
.spawn()
4243
.unwrap();
4344

@@ -58,7 +59,8 @@ async fn arg0() {
5859
cmd.arg0("test_string")
5960
.arg("-c")
6061
.arg("echo $0")
61-
.stdout(Stdio::piped());
62+
.stdout(Stdio::piped())
63+
.unwrap();
6264

6365
let output = cmd.output().await.unwrap();
6466
assert_eq!(output.stdout, b"test_string\n");

compio-runtime/src/attacher.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ impl<S> Attacher<S> {
3030
source: SharedFd::new(source),
3131
}
3232
}
33+
34+
/// Create [`Attacher`] without trying to attach the source.
35+
///
36+
/// # Safety
37+
///
38+
/// See [`Attacher::new_unchecked`].
39+
pub unsafe fn from_shared_fd_unchecked(source: SharedFd<S>) -> Self {
40+
Self { source }
41+
}
3342
}
3443

3544
impl<S: AsRawFd> Attacher<S> {

0 commit comments

Comments
 (0)