Skip to content

Commit

Permalink
Merge #1664
Browse files Browse the repository at this point in the history
1664: also implement Read and Write for &PtyMaster r=rtzoeller a=doy

align with std::fs::File which also does this because the underlying calls are just syscalls which are safe to run concurrently

Co-authored-by: Jesse Luehrs <doy@tozt.net>
  • Loading branch information
bors[bot] and doy committed Feb 24, 2022
2 parents fc3a77b + 22bb105 commit 6123083
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 @@ -52,6 +52,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
(#[1553](https://github.com/nix-rust/nix/pull/1553))
- Added `ENOTRECOVERABLE` and `EOWNERDEAD` error codes on DragonFly.
(#[1665](https://github.com/nix-rust/nix/pull/1665))
- 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 6123083

Please sign in to comment.