Skip to content

Commit 6464e32

Browse files
committed
Use standard Read/Write traits in sys::stdio
1 parent b09803e commit 6464e32

File tree

6 files changed

+114
-75
lines changed

6 files changed

+114
-75
lines changed

src/libstd/sys/cloudabi/stdio.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ impl Stdin {
99
pub fn new() -> io::Result<Stdin> {
1010
Ok(Stdin(()))
1111
}
12+
}
1213

13-
pub fn read(&self, _: &mut [u8]) -> io::Result<usize> {
14+
impl io::Read for Stdin {
15+
fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
1416
Ok(0)
1517
}
1618
}
@@ -19,15 +21,17 @@ impl Stdout {
1921
pub fn new() -> io::Result<Stdout> {
2022
Ok(Stdout(()))
2123
}
24+
}
2225

23-
pub fn write(&self, _: &[u8]) -> io::Result<usize> {
26+
impl io::Write for Stdout {
27+
fn write(&mut self, _buf: &[u8]) -> io::Result<usize> {
2428
Err(io::Error::new(
2529
io::ErrorKind::BrokenPipe,
2630
"Stdout is not connected to any output in this environment",
2731
))
2832
}
2933

30-
pub fn flush(&self) -> io::Result<()> {
34+
fn flush(&mut self) -> io::Result<()> {
3135
Ok(())
3236
}
3337
}
@@ -36,15 +40,17 @@ impl Stderr {
3640
pub fn new() -> io::Result<Stderr> {
3741
Ok(Stderr(()))
3842
}
43+
}
3944

40-
pub fn write(&self, _: &[u8]) -> io::Result<usize> {
45+
impl io::Write for Stderr {
46+
fn write(&mut self, _buf: &[u8]) -> io::Result<usize> {
4147
Err(io::Error::new(
4248
io::ErrorKind::BrokenPipe,
4349
"Stderr is not connected to any output in this environment",
4450
))
4551
}
4652

47-
pub fn flush(&self) -> io::Result<()> {
53+
fn flush(&mut self) -> io::Result<()> {
4854
Ok(())
4955
}
5056
}

src/libstd/sys/redox/stdio.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,47 @@ pub struct Stderr(());
88

99
impl Stdin {
1010
pub fn new() -> io::Result<Stdin> { Ok(Stdin(())) }
11+
}
1112

12-
pub fn read(&self, data: &mut [u8]) -> io::Result<usize> {
13+
impl io::Read for Stdin {
14+
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
1315
let fd = FileDesc::new(0);
14-
let ret = fd.read(data);
16+
let ret = fd.read(buf);
1517
fd.into_raw();
1618
ret
1719
}
1820
}
1921

2022
impl Stdout {
2123
pub fn new() -> io::Result<Stdout> { Ok(Stdout(())) }
24+
}
2225

23-
pub fn write(&self, data: &[u8]) -> io::Result<usize> {
26+
impl io::Write for Stdout {
27+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
2428
let fd = FileDesc::new(1);
25-
let ret = fd.write(data);
29+
let ret = fd.write(buf);
2630
fd.into_raw();
2731
ret
2832
}
2933

30-
pub fn flush(&self) -> io::Result<()> {
34+
fn flush(&mut self) -> io::Result<()> {
3135
cvt(syscall::fsync(1)).and(Ok(()))
3236
}
3337
}
3438

3539
impl Stderr {
3640
pub fn new() -> io::Result<Stderr> { Ok(Stderr(())) }
41+
}
3742

38-
pub fn write(&self, data: &[u8]) -> io::Result<usize> {
43+
impl io::Write for Stderr {
44+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
3945
let fd = FileDesc::new(2);
40-
let ret = fd.write(data);
46+
let ret = fd.write(buf);
4147
fd.into_raw();
4248
ret
4349
}
4450

45-
pub fn flush(&self) -> io::Result<()> {
51+
fn flush(&mut self) -> io::Result<()> {
4652
cvt(syscall::fsync(2)).and(Ok(()))
4753
}
4854
}

src/libstd/sys/sgx/stdio.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,38 @@ fn with_std_fd<F: FnOnce(&FileDesc) -> R, R>(fd: abi::Fd, f: F) -> R {
1616

1717
impl Stdin {
1818
pub fn new() -> io::Result<Stdin> { Ok(Stdin(())) }
19+
}
1920

20-
pub fn read(&self, data: &mut [u8]) -> io::Result<usize> {
21-
with_std_fd(abi::FD_STDIN, |fd| fd.read(data))
21+
impl io::Read for Stdin {
22+
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
23+
with_std_fd(abi::FD_STDIN, |fd| fd.read(buf))
2224
}
2325
}
2426

2527
impl Stdout {
2628
pub fn new() -> io::Result<Stdout> { Ok(Stdout(())) }
29+
}
2730

28-
pub fn write(&self, data: &[u8]) -> io::Result<usize> {
29-
with_std_fd(abi::FD_STDOUT, |fd| fd.write(data))
31+
impl io::Write for Stdout {
32+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
33+
with_std_fd(abi::FD_STDOUT, |fd| fd.write(buf))
3034
}
3135

32-
pub fn flush(&self) -> io::Result<()> {
36+
fn flush(&mut self) -> io::Result<()> {
3337
with_std_fd(abi::FD_STDOUT, |fd| fd.flush())
3438
}
3539
}
3640

3741
impl Stderr {
3842
pub fn new() -> io::Result<Stderr> { Ok(Stderr(())) }
43+
}
3944

40-
pub fn write(&self, data: &[u8]) -> io::Result<usize> {
41-
with_std_fd(abi::FD_STDERR, |fd| fd.write(data))
45+
impl io::Write for Stderr {
46+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
47+
with_std_fd(abi::FD_STDERR, |fd| fd.write(buf))
4248
}
4349

44-
pub fn flush(&self) -> io::Result<()> {
50+
fn flush(&mut self) -> io::Result<()> {
4551
with_std_fd(abi::FD_STDERR, |fd| fd.flush())
4652
}
4753
}

src/libstd/sys/unix/stdio.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,47 @@ pub struct Stderr(());
88

99
impl Stdin {
1010
pub fn new() -> io::Result<Stdin> { Ok(Stdin(())) }
11+
}
1112

12-
pub fn read(&self, data: &mut [u8]) -> io::Result<usize> {
13+
impl io::Read for Stdin {
14+
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
1315
let fd = FileDesc::new(libc::STDIN_FILENO);
14-
let ret = fd.read(data);
16+
let ret = fd.read(buf);
1517
fd.into_raw(); // do not close this FD
1618
ret
1719
}
1820
}
1921

2022
impl Stdout {
2123
pub fn new() -> io::Result<Stdout> { Ok(Stdout(())) }
24+
}
2225

23-
pub fn write(&self, data: &[u8]) -> io::Result<usize> {
26+
impl io::Write for Stdout {
27+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
2428
let fd = FileDesc::new(libc::STDOUT_FILENO);
25-
let ret = fd.write(data);
29+
let ret = fd.write(buf);
2630
fd.into_raw(); // do not close this FD
2731
ret
2832
}
2933

30-
pub fn flush(&self) -> io::Result<()> {
34+
fn flush(&mut self) -> io::Result<()> {
3135
Ok(())
3236
}
3337
}
3438

3539
impl Stderr {
3640
pub fn new() -> io::Result<Stderr> { Ok(Stderr(())) }
41+
}
3742

38-
pub fn write(&self, data: &[u8]) -> io::Result<usize> {
43+
impl io::Write for Stderr {
44+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
3945
let fd = FileDesc::new(libc::STDERR_FILENO);
40-
let ret = fd.write(data);
46+
let ret = fd.write(buf);
4147
fd.into_raw(); // do not close this FD
4248
ret
4349
}
4450

45-
pub fn flush(&self) -> io::Result<()> {
51+
fn flush(&mut self) -> io::Result<()> {
4652
Ok(())
4753
}
4854
}

src/libstd/sys/wasm/stdio.rs

+16-10
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,27 @@ impl Stdin {
99
pub fn new() -> io::Result<Stdin> {
1010
Ok(Stdin)
1111
}
12+
}
1213

13-
pub fn read(&self, data: &mut [u8]) -> io::Result<usize> {
14-
Ok(ReadSysCall::perform(0, data))
14+
impl io::Read for Stdin {
15+
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
16+
Ok(ReadSysCall::perform(0, buf))
1517
}
1618
}
1719

1820
impl Stdout {
1921
pub fn new() -> io::Result<Stdout> {
2022
Ok(Stdout)
2123
}
24+
}
2225

23-
pub fn write(&self, data: &[u8]) -> io::Result<usize> {
24-
WriteSysCall::perform(1, data);
25-
Ok(data.len())
26+
impl io::Write for Stdout {
27+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
28+
WriteSysCall::perform(1, buf);
29+
Ok(buf.len())
2630
}
2731

28-
pub fn flush(&self) -> io::Result<()> {
32+
fn flush(&mut self) -> io::Result<()> {
2933
Ok(())
3034
}
3135
}
@@ -34,13 +38,15 @@ impl Stderr {
3438
pub fn new() -> io::Result<Stderr> {
3539
Ok(Stderr)
3640
}
41+
}
3742

38-
pub fn write(&self, data: &[u8]) -> io::Result<usize> {
39-
WriteSysCall::perform(2, data);
40-
Ok(data.len())
43+
impl io::Write for Stderr {
44+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
45+
WriteSysCall::perform(2, buf);
46+
Ok(buf.len())
4147
}
4248

43-
pub fn flush(&self) -> io::Result<()> {
49+
fn flush(&mut self) -> io::Result<()> {
4450
Ok(())
4551
}
4652
}

0 commit comments

Comments
 (0)