Skip to content

Commit 64324d6

Browse files
committed
also implement Read and Write for &PtyMaster
1 parent b0a39cb commit 64324d6

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
5050
- Implemented `Extend`, `FromIterator`, and `IntoIterator` for `SigSet` and
5151
added `SigSet::iter` and `SigSetIter`.
5252
(#[1553](https://github.com/nix-rust/nix/pull/1553))
53+
- Implemented `Read` and `Write` for `&PtyMaster`
54+
(#[1664](https://github.com/nix-rust/nix/pull/1664))
5355

5456
### Changed
5557

src/pty.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,21 @@ impl io::Write for PtyMaster {
9595
}
9696
}
9797

98+
impl io::Read for &PtyMaster {
99+
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
100+
unistd::read(self.0, buf).map_err(io::Error::from)
101+
}
102+
}
103+
104+
impl io::Write for &PtyMaster {
105+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
106+
unistd::write(self.0, buf).map_err(io::Error::from)
107+
}
108+
fn flush(&mut self) -> io::Result<()> {
109+
Ok(())
110+
}
111+
}
112+
98113
/// Grant access to a slave pseudoterminal (see
99114
/// [`grantpt(3)`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/grantpt.html))
100115
///

test/test_pty.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ fn test_read_ptty_pair() {
170170
slave.write_all(b"hello").unwrap();
171171
master.read_exact(&mut buf).unwrap();
172172
assert_eq!(&buf, b"hello");
173+
174+
let mut master = &master;
175+
slave.write_all(b"hello").unwrap();
176+
master.read_exact(&mut buf).unwrap();
177+
assert_eq!(&buf, b"hello");
173178
}
174179

175180
/// Test `io::Write` on the PTTY master
@@ -182,6 +187,11 @@ fn test_write_ptty_pair() {
182187
master.write_all(b"adios").unwrap();
183188
slave.read_exact(&mut buf).unwrap();
184189
assert_eq!(&buf, b"adios");
190+
191+
let mut master = &master;
192+
master.write_all(b"adios").unwrap();
193+
slave.read_exact(&mut buf).unwrap();
194+
assert_eq!(&buf, b"adios");
185195
}
186196

187197
#[test]

0 commit comments

Comments
 (0)