Skip to content

Commit 4727f03

Browse files
authored
Rollup merge of rust-lang#73495 - Lucretiel:wasi-io-impls, r=sfackler
Converted all platform-specific stdin/stdout/stderr implementations to use io:: traits Currently, some of the platform-specific standard streams (`src/libstd/sys/*/stdio.rs`) manually implement parts of the `io::Write` interface directly as methods on the struct, rather than by actually implementing the trait. There doesn't seem to be any reason for this, other than an unused advantage of `fn write(&self, ...)` instead of `fn write(&mut self, ...)`. Unfortunately, this means that those implementations don't have the default-implemented io methods, like `read_exact` and `write_all`. This caused rust-lang#72705, which adds forwarding methods to the user-facing standard stream implementations, to fail to compile on those platforms. This change converts *all* such standard stream structs to use the standard library traits. This change should not cause any breakages, because the changed types are not publicly exported, and in fact are only ever used in `src/libstd/io/stdio.rs`.
2 parents 35dac31 + 0094f44 commit 4727f03

File tree

2 files changed

+49
-56
lines changed

2 files changed

+49
-56
lines changed

src/libstd/sys/hermit/stdio.rs

+17-22
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ impl Stdin {
1010
pub fn new() -> io::Result<Stdin> {
1111
Ok(Stdin)
1212
}
13+
}
1314

14-
pub fn read(&self, data: &mut [u8]) -> io::Result<usize> {
15+
impl io::Read for Stdin {
16+
fn read(&mut self, data: &mut [u8]) -> io::Result<usize> {
1517
self.read_vectored(&mut [IoSliceMut::new(data)])
1618
}
1719

18-
pub fn read_vectored(&self, _data: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
19-
//ManuallyDrop::new(unsafe { WasiFd::from_raw(libc::STDIN_FILENO as u32) })
20-
// .read(data)
20+
fn read_vectored(&mut self, _data: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
2121
Ok(0)
2222
}
2323

2424
#[inline]
25-
pub fn is_read_vectored(&self) -> bool {
25+
fn is_read_vectored(&self) -> bool {
2626
true
2727
}
2828
}
@@ -31,8 +31,10 @@ impl Stdout {
3131
pub fn new() -> io::Result<Stdout> {
3232
Ok(Stdout)
3333
}
34+
}
3435

35-
pub fn write(&self, data: &[u8]) -> io::Result<usize> {
36+
impl io::Write for Stdout {
37+
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
3638
let len;
3739

3840
unsafe { len = abi::write(1, data.as_ptr() as *const u8, data.len()) }
@@ -44,7 +46,7 @@ impl Stdout {
4446
}
4547
}
4648

47-
pub fn write_vectored(&self, data: &[IoSlice<'_>]) -> io::Result<usize> {
49+
fn write_vectored(&mut self, data: &[IoSlice<'_>]) -> io::Result<usize> {
4850
let len;
4951

5052
unsafe { len = abi::write(1, data.as_ptr() as *const u8, data.len()) }
@@ -57,11 +59,11 @@ impl Stdout {
5759
}
5860

5961
#[inline]
60-
pub fn is_write_vectored(&self) -> bool {
62+
fn is_write_vectored(&self) -> bool {
6163
true
6264
}
6365

64-
pub fn flush(&self) -> io::Result<()> {
66+
fn flush(&mut self) -> io::Result<()> {
6567
Ok(())
6668
}
6769
}
@@ -70,8 +72,10 @@ impl Stderr {
7072
pub fn new() -> io::Result<Stderr> {
7173
Ok(Stderr)
7274
}
75+
}
7376

74-
pub fn write(&self, data: &[u8]) -> io::Result<usize> {
77+
impl io::Write for Stderr {
78+
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
7579
let len;
7680

7781
unsafe { len = abi::write(2, data.as_ptr() as *const u8, data.len()) }
@@ -83,7 +87,7 @@ impl Stderr {
8387
}
8488
}
8589

86-
pub fn write_vectored(&self, data: &[IoSlice<'_>]) -> io::Result<usize> {
90+
fn write_vectored(&mut self, data: &[IoSlice<'_>]) -> io::Result<usize> {
8791
let len;
8892

8993
unsafe { len = abi::write(2, data.as_ptr() as *const u8, data.len()) }
@@ -96,21 +100,12 @@ impl Stderr {
96100
}
97101

98102
#[inline]
99-
pub fn is_write_vectored(&self) -> bool {
103+
fn is_write_vectored(&self) -> bool {
100104
true
101105
}
102106

103-
pub fn flush(&self) -> io::Result<()> {
104-
Ok(())
105-
}
106-
}
107-
108-
impl io::Write for Stderr {
109-
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
110-
(&*self).write(data)
111-
}
112107
fn flush(&mut self) -> io::Result<()> {
113-
(&*self).flush()
108+
Ok(())
114109
}
115110
}
116111

src/libstd/sys/wasi/stdio.rs

+32-34
Original file line numberDiff line numberDiff line change
@@ -11,85 +11,83 @@ impl Stdin {
1111
Ok(Stdin)
1212
}
1313

14-
pub fn read(&self, data: &mut [u8]) -> io::Result<usize> {
14+
#[inline]
15+
pub fn as_raw_fd(&self) -> u32 {
16+
0
17+
}
18+
}
19+
20+
impl io::Read for Stdin {
21+
fn read(&mut self, data: &mut [u8]) -> io::Result<usize> {
1522
self.read_vectored(&mut [IoSliceMut::new(data)])
1623
}
1724

18-
pub fn read_vectored(&self, data: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
25+
fn read_vectored(&mut self, data: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
1926
ManuallyDrop::new(unsafe { WasiFd::from_raw(self.as_raw_fd()) }).read(data)
2027
}
2128

2229
#[inline]
23-
pub fn is_read_vectored(&self) -> bool {
30+
fn is_read_vectored(&self) -> bool {
2431
true
2532
}
26-
27-
pub fn as_raw_fd(&self) -> u32 {
28-
0
29-
}
3033
}
3134

3235
impl Stdout {
3336
pub fn new() -> io::Result<Stdout> {
3437
Ok(Stdout)
3538
}
3639

37-
pub fn write(&self, data: &[u8]) -> io::Result<usize> {
40+
#[inline]
41+
pub fn as_raw_fd(&self) -> u32 {
42+
1
43+
}
44+
}
45+
46+
impl io::Write for Stdout {
47+
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
3848
self.write_vectored(&[IoSlice::new(data)])
3949
}
4050

41-
pub fn write_vectored(&self, data: &[IoSlice<'_>]) -> io::Result<usize> {
51+
fn write_vectored(&mut self, data: &[IoSlice<'_>]) -> io::Result<usize> {
4252
ManuallyDrop::new(unsafe { WasiFd::from_raw(self.as_raw_fd()) }).write(data)
4353
}
4454

4555
#[inline]
46-
pub fn is_write_vectored(&self) -> bool {
56+
fn is_write_vectored(&self) -> bool {
4757
true
4858
}
49-
50-
pub fn flush(&self) -> io::Result<()> {
59+
fn flush(&mut self) -> io::Result<()> {
5160
Ok(())
5261
}
53-
54-
pub fn as_raw_fd(&self) -> u32 {
55-
1
56-
}
5762
}
5863

5964
impl Stderr {
6065
pub fn new() -> io::Result<Stderr> {
6166
Ok(Stderr)
6267
}
6368

64-
pub fn write(&self, data: &[u8]) -> io::Result<usize> {
65-
self.write_vectored(&[IoSlice::new(data)])
66-
}
67-
68-
pub fn write_vectored(&self, data: &[IoSlice<'_>]) -> io::Result<usize> {
69-
ManuallyDrop::new(unsafe { WasiFd::from_raw(self.as_raw_fd()) }).write(data)
70-
}
71-
7269
#[inline]
73-
pub fn is_write_vectored(&self) -> bool {
74-
true
75-
}
76-
77-
pub fn flush(&self) -> io::Result<()> {
78-
Ok(())
79-
}
80-
8170
pub fn as_raw_fd(&self) -> u32 {
8271
2
8372
}
8473
}
8574

8675
impl io::Write for Stderr {
8776
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
88-
(&*self).write(data)
77+
self.write_vectored(&[IoSlice::new(data)])
78+
}
79+
80+
fn write_vectored(&mut self, data: &[IoSlice<'_>]) -> io::Result<usize> {
81+
ManuallyDrop::new(unsafe { WasiFd::from_raw(self.as_raw_fd()) }).write(data)
82+
}
83+
84+
#[inline]
85+
fn is_write_vectored(&self) -> bool {
86+
true
8987
}
9088

9189
fn flush(&mut self) -> io::Result<()> {
92-
(&*self).flush()
90+
Ok(())
9391
}
9492
}
9593

0 commit comments

Comments
 (0)