diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2022-02-24 01:40:57 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-24 01:40:57 +0000 |
commit | 6123083a4702002e1958229735a4fd70cb326257 (patch) | |
tree | f208402f7ebddfbc4c33ea952de01b39fe0c141a | |
parent | fc3a77b7fcf7da0d198c3c9ab9957c8c889a6fe0 (diff) | |
parent | 22bb1056126cee98dcf747eb48fc5fb5736fe7d7 (diff) | |
download | nix-6123083a4702002e1958229735a4fd70cb326257.zip |
Merge #1664
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>
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | src/pty.rs | 15 | ||||
-rw-r--r-- | test/test_pty.rs | 10 |
3 files changed, 27 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index d85b42d0..65681677 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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)) /// diff --git a/test/test_pty.rs b/test/test_pty.rs index 71932f2d..1a7cab81 100644 --- a/test/test_pty.rs +++ b/test/test_pty.rs @@ -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 @@ -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] |