]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/wasm/stdio.rs
Refactor stderr_prints_nothing into a more modular function
[rust.git] / src / libstd / sys / wasm / stdio.rs
1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use io;
12 use sys::{ReadSysCall, WriteSysCall};
13
14 pub struct Stdin;
15 pub struct Stdout;
16 pub struct Stderr;
17
18 impl Stdin {
19     pub fn new() -> io::Result<Stdin> {
20         Ok(Stdin)
21     }
22
23     pub fn read(&self, data: &mut [u8]) -> io::Result<usize> {
24         Ok(ReadSysCall::perform(0, data))
25     }
26 }
27
28 impl Stdout {
29     pub fn new() -> io::Result<Stdout> {
30         Ok(Stdout)
31     }
32
33     pub fn write(&self, data: &[u8]) -> io::Result<usize> {
34         WriteSysCall::perform(1, data);
35         Ok(data.len())
36     }
37
38     pub fn flush(&self) -> io::Result<()> {
39         Ok(())
40     }
41 }
42
43 impl Stderr {
44     pub fn new() -> io::Result<Stderr> {
45         Ok(Stderr)
46     }
47
48     pub fn write(&self, data: &[u8]) -> io::Result<usize> {
49         WriteSysCall::perform(2, data);
50         Ok(data.len())
51     }
52
53     pub fn flush(&self) -> io::Result<()> {
54         Ok(())
55     }
56 }
57
58 impl io::Write for Stderr {
59     fn write(&mut self, data: &[u8]) -> io::Result<usize> {
60         (&*self).write(data)
61     }
62     fn flush(&mut self) -> io::Result<()> {
63         (&*self).flush()
64     }
65 }
66
67 pub const STDIN_BUF_SIZE: usize = 0;
68
69 pub fn is_ebadf(_err: &io::Error) -> bool {
70     true
71 }
72
73 pub fn panic_output() -> Option<impl io::Write> {
74     if cfg!(feature = "wasm_syscall") {
75         Stderr::new().ok()
76     } else {
77         None
78     }
79 }