Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

also implement Read and Write for &PtyMaster #1664

Merged
merged 1 commit into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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