Skip to content
/ rust Public
forked from rust-lang/rust

Commit 60805dc

Browse files
committed
Deduplicate platform stdio types
1 parent 2c6a12e commit 60805dc

File tree

4 files changed

+31
-126
lines changed

4 files changed

+31
-126
lines changed

library/std/src/sys/stdio/solid.rs

+8-51
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
1+
#[expect(dead_code)]
2+
#[path = "unsupported.rs"]
3+
mod unsupported_stdio;
4+
15
use crate::io;
26
use crate::sys::pal::abi;
37

4-
pub struct Stdin;
8+
pub type Stdin = unsupported_stdio::Stdin;
59
pub struct Stdout;
6-
pub struct Stderr;
7-
struct PanicOutput;
8-
9-
impl Stdin {
10-
pub const fn new() -> Stdin {
11-
Stdin
12-
}
13-
}
14-
15-
impl io::Read for Stdin {
16-
fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
17-
Ok(0)
18-
}
19-
}
10+
pub type Stderr = Stdout;
2011

2112
impl Stdout {
2213
pub const fn new() -> Stdout {
@@ -35,46 +26,12 @@ impl io::Write for Stdout {
3526
}
3627
}
3728

38-
impl Stderr {
39-
pub const fn new() -> Stderr {
40-
Stderr
41-
}
42-
}
43-
44-
impl io::Write for Stderr {
45-
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
46-
unsafe { abi::SOLID_LOG_write(buf.as_ptr(), buf.len()) };
47-
Ok(buf.len())
48-
}
49-
50-
fn flush(&mut self) -> io::Result<()> {
51-
Ok(())
52-
}
53-
}
54-
55-
impl PanicOutput {
56-
pub const fn new() -> PanicOutput {
57-
PanicOutput
58-
}
59-
}
60-
61-
impl io::Write for PanicOutput {
62-
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
63-
unsafe { abi::SOLID_LOG_write(buf.as_ptr(), buf.len()) };
64-
Ok(buf.len())
65-
}
66-
67-
fn flush(&mut self) -> io::Result<()> {
68-
Ok(())
69-
}
70-
}
71-
72-
pub const STDIN_BUF_SIZE: usize = 0;
29+
pub const STDIN_BUF_SIZE: usize = unsupported_stdio::STDIN_BUF_SIZE;
7330

7431
pub fn is_ebadf(_err: &io::Error) -> bool {
7532
true
7633
}
7734

7835
pub fn panic_output() -> Option<impl io::Write> {
79-
Some(PanicOutput::new())
36+
Some(Stderr::new())
8037
}

library/std/src/sys/stdio/teeos.rs

+13-40
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
#![deny(unsafe_op_in_unsafe_fn)]
22

3+
#[expect(dead_code)]
4+
#[path = "unsupported.rs"]
5+
mod unsupported_stdio;
6+
37
use core::arch::asm;
48

59
use crate::io;
610

7-
pub struct Stdin;
11+
pub type Stdin = unsupported_stdio::Stdin;
812
pub struct Stdout;
9-
pub struct Stderr;
13+
pub type Stderr = Stdout;
1014

1115
const KCALL_DEBUG_CMD_PUT_BYTES: i64 = 2;
1216

@@ -25,27 +29,6 @@ unsafe fn debug_call(cap_ref: u64, call_no: i64, arg1: u64, arg2: u64) -> i32 {
2529
ret as i32
2630
}
2731

28-
fn print_buf(s: &[u8]) -> io::Result<usize> {
29-
// Corresponds to `HM_DEBUG_PUT_BYTES_LIMIT`.
30-
const MAX_LEN: usize = 512;
31-
let len = if s.len() > MAX_LEN { MAX_LEN } else { s.len() };
32-
let result = unsafe { debug_call(0, KCALL_DEBUG_CMD_PUT_BYTES, s.as_ptr() as u64, len as u64) };
33-
34-
if result == 0 { Ok(len) } else { Err(io::Error::from(io::ErrorKind::InvalidInput)) }
35-
}
36-
37-
impl Stdin {
38-
pub const fn new() -> Stdin {
39-
Stdin
40-
}
41-
}
42-
43-
impl io::Read for Stdin {
44-
fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
45-
Ok(0)
46-
}
47-
}
48-
4932
impl Stdout {
5033
pub const fn new() -> Stdout {
5134
Stdout
@@ -54,31 +37,21 @@ impl Stdout {
5437

5538
impl io::Write for Stdout {
5639
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
57-
print_buf(buf)
58-
}
59-
60-
fn flush(&mut self) -> io::Result<()> {
61-
Ok(())
62-
}
63-
}
40+
// Corresponds to `HM_DEBUG_PUT_BYTES_LIMIT`.
41+
const MAX_LEN: usize = 512;
42+
let len = buf.len().min(MAX_LEN);
43+
let result =
44+
unsafe { debug_call(0, KCALL_DEBUG_CMD_PUT_BYTES, buf.as_ptr() as u64, len as u64) };
6445

65-
impl Stderr {
66-
pub const fn new() -> Stderr {
67-
Stderr
68-
}
69-
}
70-
71-
impl io::Write for Stderr {
72-
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
73-
print_buf(buf)
46+
if result == 0 { Ok(len) } else { Err(io::Error::from(io::ErrorKind::InvalidInput)) }
7447
}
7548

7649
fn flush(&mut self) -> io::Result<()> {
7750
Ok(())
7851
}
7952
}
8053

81-
pub const STDIN_BUF_SIZE: usize = 0;
54+
pub const STDIN_BUF_SIZE: usize = unsupported_stdio::STDIN_BUF_SIZE;
8255

8356
pub fn is_ebadf(err: &io::Error) -> bool {
8457
err.raw_os_error() == Some(libc::EBADF as i32)

library/std/src/sys/stdio/unsupported.rs

+1-17
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::io;
22

33
pub struct Stdin;
44
pub struct Stdout;
5-
pub struct Stderr;
5+
pub type Stderr = Stdout;
66

77
impl Stdin {
88
pub const fn new() -> Stdin {
@@ -32,22 +32,6 @@ impl io::Write for Stdout {
3232
}
3333
}
3434

35-
impl Stderr {
36-
pub const fn new() -> Stderr {
37-
Stderr
38-
}
39-
}
40-
41-
impl io::Write for Stderr {
42-
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
43-
Ok(buf.len())
44-
}
45-
46-
fn flush(&mut self) -> io::Result<()> {
47-
Ok(())
48-
}
49-
}
50-
5135
pub const STDIN_BUF_SIZE: usize = 0;
5236

5337
pub fn is_ebadf(_err: &io::Error) -> bool {

library/std/src/sys/stdio/xous.rs

+9-18
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
1-
use crate::io;
2-
3-
pub struct Stdin;
4-
pub struct Stdout {}
5-
pub struct Stderr;
1+
#[expect(dead_code)]
2+
#[path = "unsupported.rs"]
3+
mod unsupported_stdio;
64

5+
use crate::io;
76
use crate::os::xous::ffi::{Connection, lend, try_lend, try_scalar};
87
use crate::os::xous::services::{LogLend, LogScalar, log_server, try_connect};
98

10-
impl Stdin {
11-
pub const fn new() -> Stdin {
12-
Stdin
13-
}
14-
}
15-
16-
impl io::Read for Stdin {
17-
fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
18-
Ok(0)
19-
}
20-
}
9+
pub type Stdin = unsupported_stdio::Stdin;
10+
pub struct Stdout;
11+
pub struct Stderr;
2112

2213
impl Stdout {
2314
pub const fn new() -> Stdout {
24-
Stdout {}
15+
Stdout
2516
}
2617
}
2718

@@ -73,7 +64,7 @@ impl io::Write for Stderr {
7364
}
7465
}
7566

76-
pub const STDIN_BUF_SIZE: usize = 0;
67+
pub const STDIN_BUF_SIZE: usize = unsupported_stdio::STDIN_BUF_SIZE;
7768

7869
pub fn is_ebadf(_err: &io::Error) -> bool {
7970
true

0 commit comments

Comments
 (0)