Skip to content

Commit 6123083

Browse files
bors[bot]doy
andauthored
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 <[email protected]>
2 parents fc3a77b + 22bb105 commit 6123083

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
@@ -52,6 +52,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
5252
(#[1553](https://github.com/nix-rust/nix/pull/1553))
5353
- Added `ENOTRECOVERABLE` and `EOWNERDEAD` error codes on DragonFly.
5454
(#[1665](https://github.com/nix-rust/nix/pull/1665))
55+
- Implemented `Read` and `Write` for `&PtyMaster`
56+
(#[1664](https://github.com/nix-rust/nix/pull/1664))
5557

5658
### Changed
5759

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)