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 22, 2022
1 parent b0a39cb commit 64324d6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
- Implemented `Extend`, `FromIterator`, and `IntoIterator` for `SigSet` and
added `SigSet::iter` and `SigSetIter`.
(#[1553](https://github.com/nix-rust/nix/pull/1553))
- Implemented `Read` and `Write` for `&PtyMaster`
(#[1664](https://github.com/nix-rust/nix/pull/1664))

### Changed

Expand Down
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 64324d6

Please sign in to comment.