Skip to content

Commit

Permalink
feat(process): use TryFrom to convert to Stdio
Browse files Browse the repository at this point in the history
  • Loading branch information
Berrysoft committed May 27, 2024
1 parent 34e3f19 commit 7a6a0e6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 37 deletions.
75 changes: 39 additions & 36 deletions compio-process/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,21 +171,21 @@ impl Command {
}

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

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

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

/// Returns the path to the program.
Expand Down Expand Up @@ -409,15 +409,16 @@ impl ChildStdout {
}
}

impl From<ChildStdout> for process::Stdio {
fn from(value: ChildStdout) -> Self {
Self::from(
value
.0
.into_inner()
.try_unwrap()
.expect("the handle is being used"),
)
impl TryFrom<ChildStdout> for process::Stdio {
type Error = ChildStdout;

fn try_from(value: ChildStdout) -> Result<Self, ChildStdout> {
value
.0
.into_inner()
.try_unwrap()
.map(Self::from)
.map_err(|fd| ChildStdout(unsafe { Attacher::from_shared_fd_unchecked(fd) }))
}
}

Expand All @@ -430,15 +431,16 @@ impl ChildStderr {
}
}

impl From<ChildStderr> for process::Stdio {
fn from(value: ChildStderr) -> Self {
Self::from(
value
.0
.into_inner()
.try_unwrap()
.expect("the handle is being used"),
)
impl TryFrom<ChildStderr> for process::Stdio {
type Error = ChildStderr;

fn try_from(value: ChildStderr) -> Result<Self, ChildStderr> {
value
.0
.into_inner()
.try_unwrap()
.map(Self::from)
.map_err(|fd| ChildStderr(unsafe { Attacher::from_shared_fd_unchecked(fd) }))
}
}

Expand All @@ -452,14 +454,15 @@ impl ChildStdin {
}
}

impl From<ChildStdin> for process::Stdio {
fn from(value: ChildStdin) -> Self {
Self::from(
value
.0
.into_inner()
.try_unwrap()
.expect("the handle is being used"),
)
impl TryFrom<ChildStdin> for process::Stdio {
type Error = ChildStdin;

fn try_from(value: ChildStdin) -> Result<Self, ChildStdin> {
value
.0
.into_inner()
.try_unwrap()
.map(Self::from)
.map_err(|fd| ChildStdin(unsafe { Attacher::from_shared_fd_unchecked(fd) }))
}
}
4 changes: 3 additions & 1 deletion compio-process/tests/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ async fn echo() {
let child = cmd
.arg("echo hello world")
.stdout(Stdio::piped())
.unwrap()
.spawn()
.unwrap();

Expand All @@ -58,7 +59,8 @@ async fn arg0() {
cmd.arg0("test_string")
.arg("-c")
.arg("echo $0")
.stdout(Stdio::piped());
.stdout(Stdio::piped())
.unwrap();

let output = cmd.output().await.unwrap();
assert_eq!(output.stdout, b"test_string\n");
Expand Down
9 changes: 9 additions & 0 deletions compio-runtime/src/attacher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ impl<S> Attacher<S> {
source: SharedFd::new(source),
}
}

/// Create [`Attacher`] without trying to attach the source.
///
/// # Safety
///
/// See [`Attacher::new_unchecked`].
pub unsafe fn from_shared_fd_unchecked(source: SharedFd<S>) -> Self {
Self { source }
}
}

impl<S: AsRawFd> Attacher<S> {
Expand Down

0 comments on commit 7a6a0e6

Please sign in to comment.