blob: a94226b4468424fb9bbfe3ba738a56b6aa0fdfe8 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
pub struct Pty {
pt: std::fs::File,
pts: std::fs::File,
}
impl Pty {
pub fn new() -> crate::Result<Self> {
let (pt, ptsname) = crate::sys::create_pt()?;
let pts = std::fs::OpenOptions::new()
.read(true)
.write(true)
.open(&ptsname)?;
Ok(Self { pt, pts })
}
pub fn resize(&self, size: crate::Size) -> crate::Result<()> {
Ok(crate::sys::set_term_size(self, size)?)
}
#[must_use]
pub fn pts(&self) -> &std::fs::File {
&self.pts
}
}
impl std::ops::Deref for Pty {
type Target = std::fs::File;
fn deref(&self) -> &Self::Target {
&self.pt
}
}
impl std::ops::DerefMut for Pty {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.pt
}
}
impl std::os::unix::io::AsRawFd for Pty {
fn as_raw_fd(&self) -> std::os::unix::io::RawFd {
self.pt.as_raw_fd()
}
}
// there is a Read impl for &std::fs::File, but without this explicit impl,
// rust finds the Read impl for std::fs::File first, and then complains that
// it requires &mut self, because method resolution/autoderef doesn't take
// mutability into account
impl std::io::Read for &Pty {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
(&self.pt).read(buf)
}
}
// same as above
impl std::io::Write for &Pty {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
(&self.pt).write(buf)
}
fn flush(&mut self) -> std::io::Result<()> {
(&self.pt).flush()
}
}
|