pty: add MasterPty::try_clone_writer
This commit is contained in:
1 parent
c54c64f395
commit
e2d044bb14
5 files changed
+25
-1
No files matched your search
@@ -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<Box<dyn std::io::Read + Send>, 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<Box<dyn std::io::Write + Send>, Error>;
|
||||
}
|
||||
|
||||
/// Represents a child process spawned into the pty.
|
||||
|
||||
@@ -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<Box<dyn std::io::Write + Send>> {
|
||||
let port = Arc::clone(&self.port);
|
||||
Ok(Box::new(Master { port }))
|
||||
}
|
||||
}
|
||||
|
||||
struct Reader {
|
||||
|
||||
@@ -177,6 +177,12 @@ impl MasterPty for SshMaster {
|
||||
pty: self.pty.clone(),
|
||||
}))
|
||||
}
|
||||
|
||||
fn try_clone_writer(&self) -> anyhow::Result<Box<dyn std::io::Write + Send>> {
|
||||
Ok(Box::new(SshMaster {
|
||||
pty: self.pty.clone(),
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
struct SshSlave {
|
||||
|
||||
+6
-1
@@ -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<Box<dyn Write + Send>, Error> {
|
||||
let fd = PtyFd(self.fd.try_clone()?);
|
||||
Ok(Box::new(UnixMasterPty { fd }))
|
||||
}
|
||||
}
|
||||
|
||||
impl Write for UnixMasterPty {
|
||||
|
||||
@@ -96,6 +96,10 @@ impl MasterPty for ConPtyMasterPty {
|
||||
fn try_clone_reader(&self) -> anyhow::Result<Box<dyn std::io::Read + Send>> {
|
||||
Ok(Box::new(self.inner.lock().unwrap().readable.try_clone()?))
|
||||
}
|
||||
|
||||
fn try_clone_writer(&self) -> anyhow::Result<Box<dyn std::io::Write + Send>> {
|
||||
Ok(Box::new(self.inner.lock().unwrap().writable.try_clone()?))
|
||||
}
|
||||
}
|
||||
|
||||
impl io::Write for ConPtyMasterPty {
|
||||
|
||||
Reference in new issue
Block a user