From 22bb1056126cee98dcf747eb48fc5fb5736fe7d7 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Mon, 21 Feb 2022 15:04:39 -0500 Subject: [PATCH] also implement Read and Write for &PtyMaster --- CHANGELOG.md | 2 ++ src/pty.rs | 15 +++++++++++++++ test/test_pty.rs | 10 ++++++++++ 3 files changed, 27 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d85b42d066..6568167749 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 diff --git a/src/pty.rs b/src/pty.rs index ae304d83d3..bb266a65bb 100644 --- a/src/pty.rs +++ b/src/pty.rs @@ -95,6 +95,21 @@ impl io::Write for PtyMaster { } } +impl io::Read for &PtyMaster { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + unistd::read(self.0, buf).map_err(io::Error::from) + } +} + +impl io::Write for &PtyMaster { + fn write(&mut self, buf: &[u8]) -> io::Result { + 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 71932f2d6e..1a7cab81a0 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]