Skip to content

Commit 3fe79df

Browse files
committed
Deduplicate unsupported Stdin
1 parent 1a08321 commit 3fe79df

File tree

6 files changed

+26
-108
lines changed

6 files changed

+26
-108
lines changed

library/std/src/sys/pal/solid/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ pub mod process;
3131
pub mod stdio;
3232
pub use self::itron::{thread, thread_parking};
3333
pub mod time;
34+
#[allow(dead_code)]
35+
#[path = "../unsupported/stdio.rs"]
36+
mod unsupported_stdio;
3437

3538
// SAFETY: must be called only once during runtime initialization.
3639
// NOTE: this is not guaranteed to run, for example when Rust code is called externally.
Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
11
use super::abi;
22
use crate::io;
33

4-
pub struct Stdin;
4+
pub type Stdin = super::unsupported_stdio::Stdin;
55
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-
}
6+
pub type Stderr = Stdout;
207

218
impl Stdout {
229
pub const fn new() -> Stdout {
@@ -35,46 +22,12 @@ impl io::Write for Stdout {
3522
}
3623
}
3724

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;
25+
pub const STDIN_BUF_SIZE: usize = super::unsupported_stdio::STDIN_BUF_SIZE;
7326

7427
pub fn is_ebadf(_err: &io::Error) -> bool {
7528
true
7629
}
7730

7831
pub fn panic_output() -> Option<impl io::Write> {
79-
Some(PanicOutput::new())
32+
Some(Stderr::new())
8033
}

library/std/src/sys/pal/teeos/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ pub mod thread;
2323
#[allow(non_upper_case_globals)]
2424
#[path = "../unix/time.rs"]
2525
pub mod time;
26+
#[allow(dead_code)]
27+
#[path = "../unsupported/stdio.rs"]
28+
mod unsupported_stdio;
2629

2730
#[path = "../unix/sync"]
2831
pub mod sync {

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

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use core::arch::asm;
44

55
use crate::io;
66

7-
pub struct Stdin;
7+
pub type Stdin = super::unsupported_stdio::Stdin;
88
pub struct Stdout;
9-
pub struct Stderr;
9+
pub type Stderr = Stdout;
1010

1111
const KCALL_DEBUG_CMD_PUT_BYTES: i64 = 2;
1212

@@ -25,27 +25,6 @@ unsafe fn debug_call(cap_ref: u64, call_no: i64, arg1: u64, arg2: u64) -> i32 {
2525
ret as i32
2626
}
2727

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-
4928
impl Stdout {
5029
pub const fn new() -> Stdout {
5130
Stdout
@@ -54,31 +33,21 @@ impl Stdout {
5433

5534
impl io::Write for Stdout {
5635
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
57-
print_buf(buf)
58-
}
36+
// Corresponds to `HM_DEBUG_PUT_BYTES_LIMIT`.
37+
const MAX_LEN: usize = 512;
38+
let len = buf.len().min(MAX_LEN);
39+
let result =
40+
unsafe { debug_call(0, KCALL_DEBUG_CMD_PUT_BYTES, buf.as_ptr() as u64, len as u64) };
5941

60-
fn flush(&mut self) -> io::Result<()> {
61-
Ok(())
62-
}
63-
}
64-
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)
42+
if result == 0 { Ok(len) } else { Err(io::Error::from(io::ErrorKind::InvalidInput)) }
7443
}
7544

7645
fn flush(&mut self) -> io::Result<()> {
7746
Ok(())
7847
}
7948
}
8049

81-
pub const STDIN_BUF_SIZE: usize = 0;
50+
pub const STDIN_BUF_SIZE: usize = super::unsupported_stdio::STDIN_BUF_SIZE;
8251

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

library/std/src/sys/pal/xous/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ pub mod process;
1313
pub mod stdio;
1414
pub mod thread;
1515
pub mod time;
16+
#[allow(dead_code)]
17+
#[path = "../unsupported/stdio.rs"]
18+
mod unsupported_stdio;
1619

1720
#[path = "../unsupported/common.rs"]
1821
mod common;

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

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,10 @@
11
use crate::io;
2-
3-
pub struct Stdin;
4-
pub struct Stdout {}
5-
pub struct Stderr;
6-
72
use crate::os::xous::ffi::{Connection, lend, try_lend, try_scalar};
83
use crate::os::xous::services::{LogLend, LogScalar, log_server, try_connect};
94

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-
}
5+
pub type Stdin = super::unsupported_stdio::Stdin;
6+
pub struct Stdout;
7+
pub struct Stderr;
218

229
impl Stdout {
2310
pub const fn new() -> Stdout {
@@ -73,7 +60,7 @@ impl io::Write for Stderr {
7360
}
7461
}
7562

76-
pub const STDIN_BUF_SIZE: usize = 0;
63+
pub const STDIN_BUF_SIZE: usize = super::unsupported_stdio::STDIN_BUF_SIZE;
7764

7865
pub fn is_ebadf(_err: &io::Error) -> bool {
7966
true

0 commit comments

Comments
 (0)