Skip to content

Commit c9c434d

Browse files
committed
Converted all platform-specific stdin/stdout/stderr implementations to io traits
1 parent 036b5fe commit c9c434d

File tree

2 files changed

+49
-54
lines changed

2 files changed

+49
-54
lines changed

src/libstd/sys/hermit/stdio.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,21 @@ 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> {
20+
fn read_vectored(&self, _data: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
1921
//ManuallyDrop::new(unsafe { WasiFd::from_raw(libc::STDIN_FILENO as u32) })
2022
// .read(data)
2123
Ok(0)
2224
}
2325

2426
#[inline]
25-
pub fn is_read_vectored(&self) -> bool {
27+
fn is_read_vectored(&self) -> bool {
2628
true
2729
}
2830
}
@@ -31,8 +33,10 @@ impl Stdout {
3133
pub fn new() -> io::Result<Stdout> {
3234
Ok(Stdout)
3335
}
36+
}
3437

35-
pub fn write(&self, data: &[u8]) -> io::Result<usize> {
38+
impl io::Write for Stdout {
39+
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
3640
let len;
3741

3842
unsafe { len = abi::write(1, data.as_ptr() as *const u8, data.len()) }
@@ -44,7 +48,7 @@ impl Stdout {
4448
}
4549
}
4650

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

5054
unsafe { len = abi::write(1, data.as_ptr() as *const u8, data.len()) }
@@ -57,11 +61,11 @@ impl Stdout {
5761
}
5862

5963
#[inline]
60-
pub fn is_write_vectored(&self) -> bool {
64+
fn is_write_vectored(&self) -> bool {
6165
true
6266
}
6367

64-
pub fn flush(&self) -> io::Result<()> {
68+
fn flush(&mut self) -> io::Result<()> {
6569
Ok(())
6670
}
6771
}
@@ -70,8 +74,10 @@ impl Stderr {
7074
pub fn new() -> io::Result<Stderr> {
7175
Ok(Stderr)
7276
}
77+
}
7378

74-
pub fn write(&self, data: &[u8]) -> io::Result<usize> {
79+
impl io::Write for Stderr {
80+
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
7581
let len;
7682

7783
unsafe { len = abi::write(2, data.as_ptr() as *const u8, data.len()) }
@@ -83,7 +89,7 @@ impl Stderr {
8389
}
8490
}
8591

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

8995
unsafe { len = abi::write(2, data.as_ptr() as *const u8, data.len()) }
@@ -96,21 +102,12 @@ impl Stderr {
96102
}
97103

98104
#[inline]
99-
pub fn is_write_vectored(&self) -> bool {
105+
fn is_write_vectored(&self) -> bool {
100106
true
101107
}
102108

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-
}
112109
fn flush(&mut self) -> io::Result<()> {
113-
(&*self).flush()
110+
Ok(())
114111
}
115112
}
116113

src/libstd/sys/wasi/stdio.rs

Lines changed: 32 additions & 34 deletions
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)