From e2d044bb14a5c32e4a33dba5f6f0a0ac4bd2c25c Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 9 May 2020 09:36:58 -0700 Subject: [PATCH] pty: add MasterPty::try_clone_writer --- pty/src/lib.rs | 4 ++++ pty/src/serial.rs | 5 +++++ pty/src/ssh.rs | 6 ++++++ pty/src/unix.rs | 7 ++++++- pty/src/win/conpty.rs | 4 ++++ 5 files changed, 25 insertions(+), 1 deletion(-) diff --git a/pty/src/lib.rs b/pty/src/lib.rs index a5157fb5d..95dedae8f 100644 --- a/pty/src/lib.rs +++ b/pty/src/lib.rs @@ -99,6 +99,10 @@ pub trait MasterPty: std::io::Write { /// Obtain a readable handle; output from the slave(s) is readable /// via this stream. fn try_clone_reader(&self) -> Result, Error>; + /// Obtain a writable handle; writing to it will send data to the + /// slave end. This is equivalent to the Write impl on MasterPty + /// itself, but allows splitting it off into a separate object. + fn try_clone_writer(&self) -> Result, Error>; } /// Represents a child process spawned into the pty. diff --git a/pty/src/serial.rs b/pty/src/serial.rs index bd87c29c9..1dec239a6 100644 --- a/pty/src/serial.rs +++ b/pty/src/serial.rs @@ -172,6 +172,11 @@ impl MasterPty for Master { let fd = FileDescriptor::dup(&*self.port.lock().unwrap())?; Ok(Box::new(Reader { fd })) } + + fn try_clone_writer(&self) -> anyhow::Result> { + let port = Arc::clone(&self.port); + Ok(Box::new(Master { port })) + } } struct Reader { diff --git a/pty/src/ssh.rs b/pty/src/ssh.rs index 0b61005c9..ffdbdbd1f 100644 --- a/pty/src/ssh.rs +++ b/pty/src/ssh.rs @@ -177,6 +177,12 @@ impl MasterPty for SshMaster { pty: self.pty.clone(), })) } + + fn try_clone_writer(&self) -> anyhow::Result> { + Ok(Box::new(SshMaster { + pty: self.pty.clone(), + })) + } } struct SshSlave { diff --git a/pty/src/unix.rs b/pty/src/unix.rs index 8299e3adc..c3055a0a0 100644 --- a/pty/src/unix.rs +++ b/pty/src/unix.rs @@ -1,7 +1,7 @@ //! Working with pseudo-terminals use crate::{Child, CommandBuilder, MasterPty, PtyPair, PtySize, PtySystem, SlavePty}; -use anyhow::{bail, Error}; +use anyhow::{bail, Error}; use filedescriptor::FileDescriptor; use libc::{self, winsize}; use std::io; @@ -243,6 +243,11 @@ impl MasterPty for UnixMasterPty { let fd = PtyFd(self.fd.try_clone()?); Ok(Box::new(fd)) } + + fn try_clone_writer(&self) -> Result, Error> { + let fd = PtyFd(self.fd.try_clone()?); + Ok(Box::new(UnixMasterPty { fd })) + } } impl Write for UnixMasterPty { diff --git a/pty/src/win/conpty.rs b/pty/src/win/conpty.rs index 62e1579d7..1099c8082 100644 --- a/pty/src/win/conpty.rs +++ b/pty/src/win/conpty.rs @@ -96,6 +96,10 @@ impl MasterPty for ConPtyMasterPty { fn try_clone_reader(&self) -> anyhow::Result> { Ok(Box::new(self.inner.lock().unwrap().readable.try_clone()?)) } + + fn try_clone_writer(&self) -> anyhow::Result> { + Ok(Box::new(self.inner.lock().unwrap().writable.try_clone()?)) + } } impl io::Write for ConPtyMasterPty {