]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/wasm/stdio.rs
Rollup merge of #47117 - tinaun:no_more_dups, r=frewsxcv
[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::{Void, unsupported};
13
14 pub struct Stdin(Void);
15 pub struct Stdout;
16 pub struct Stderr;
17
18 impl Stdin {
19     pub fn new() -> io::Result<Stdin> {
20         unsupported()
21     }
22
23     pub fn read(&self, _data: &mut [u8]) -> io::Result<usize> {
24         match self.0 {}
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         // If runtime debugging is enabled at compile time we'll invoke some
35         // runtime functions that are defined in our src/etc/wasm32-shim.js
36         // debugging script. Note that this ffi function call is intended
37         // *purely* for debugging only and should not be relied upon.
38         if !super::DEBUG {
39             return unsupported()
40         }
41         extern {
42             fn rust_wasm_write_stdout(data: *const u8, len: usize);
43         }
44         unsafe {
45             rust_wasm_write_stdout(data.as_ptr(), data.len())
46         }
47         Ok(data.len())
48     }
49
50     pub fn flush(&self) -> io::Result<()> {
51         Ok(())
52     }
53 }
54
55 impl Stderr {
56     pub fn new() -> io::Result<Stderr> {
57         Ok(Stderr)
58     }
59
60     pub fn write(&self, data: &[u8]) -> io::Result<usize> {
61         // See comments in stdout for what's going on here.
62         if !super::DEBUG {
63             return unsupported()
64         }
65         extern {
66             fn rust_wasm_write_stderr(data: *const u8, len: usize);
67         }
68         unsafe {
69             rust_wasm_write_stderr(data.as_ptr(), data.len())
70         }
71         Ok(data.len())
72     }
73
74     pub fn flush(&self) -> io::Result<()> {
75         Ok(())
76     }
77 }
78
79 impl io::Write for Stderr {
80     fn write(&mut self, data: &[u8]) -> io::Result<usize> {
81         (&*self).write(data)
82     }
83     fn flush(&mut self) -> io::Result<()> {
84         (&*self).flush()
85     }
86 }
87
88 pub const STDIN_BUF_SIZE: usize = 0;
89
90 pub fn is_ebadf(_err: &io::Error) -> bool {
91     true
92 }