Skip to content

Commit

Permalink
also implement Read and Write for &PtyMaster
Browse files Browse the repository at this point in the history
  • Loading branch information
doy committed Feb 21, 2022
1 parent b0a39cb commit 0a3c63e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/pty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@ impl io::Write for PtyMaster {
}
}

impl io::Read for &PtyMaster {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
unistd::read(self.0, buf).map_err(io::Error::from)
}
}

impl io::Write for &PtyMaster {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
unistd::write(self.0, buf).map_err(io::Error::from)
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}

/// Grant access to a slave pseudoterminal (see
/// [`grantpt(3)`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/grantpt.html))
///
Expand Down
10 changes: 10 additions & 0 deletions test/test_pty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ fn test_read_ptty_pair() {
slave.write_all(b"hello").unwrap();
master.read_exact(&mut buf).unwrap();
assert_eq!(&buf, b"hello");

let mut master = &master;
slave.write_all(b"hello").unwrap();
master.read_exact(&mut buf).unwrap();
assert_eq!(&buf, b"hello");
}

/// Test `io::Write` on the PTTY master
Expand All @@ -182,6 +187,11 @@ fn test_write_ptty_pair() {
master.write_all(b"adios").unwrap();
slave.read_exact(&mut buf).unwrap();
assert_eq!(&buf, b"adios");

let mut master = &master;
master.write_all(b"adios").unwrap();
slave.read_exact(&mut buf).unwrap();
assert_eq!(&buf, b"adios");
}

#[test]
Expand Down

0 comments on commit 0a3c63e

Please sign in to comment.