]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/redox/stdio.rs
7a4d11b0ecb9a8c64f2845898755d6883608544e
[rust.git] / src / libstd / sys / redox / stdio.rs
1 // Copyright 2016 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::{cvt, syscall};
13 use sys::fd::FileDesc;
14
15 pub struct Stdin(());
16 pub struct Stdout(());
17 pub struct Stderr(());
18
19 impl Stdin {
20     pub fn new() -> io::Result<Stdin> { Ok(Stdin(())) }
21
22     pub fn read(&self, data: &mut [u8]) -> io::Result<usize> {
23         let fd = FileDesc::new(0);
24         let ret = fd.read(data);
25         fd.into_raw();
26         ret
27     }
28 }
29
30 impl Stdout {
31     pub fn new() -> io::Result<Stdout> { Ok(Stdout(())) }
32
33     pub fn write(&self, data: &[u8]) -> io::Result<usize> {
34         let fd = FileDesc::new(1);
35         let ret = fd.write(data);
36         fd.into_raw();
37         ret
38     }
39
40     pub fn flush(&self) -> io::Result<()> {
41         cvt(syscall::fsync(1)).and(Ok(()))
42     }
43 }
44
45 impl Stderr {
46     pub fn new() -> io::Result<Stderr> { Ok(Stderr(())) }
47
48     pub fn write(&self, data: &[u8]) -> io::Result<usize> {
49         let fd = FileDesc::new(2);
50         let ret = fd.write(data);
51         fd.into_raw();
52         ret
53     }
54
55     pub fn flush(&self) -> io::Result<()> {
56         cvt(syscall::fsync(2)).and(Ok(()))
57     }
58 }
59
60 // FIXME: right now this raw stderr handle is used in a few places because
61 //        std::io::stderr_raw isn't exposed, but once that's exposed this impl
62 //        should go away
63 impl io::Write for Stderr {
64     fn write(&mut self, data: &[u8]) -> io::Result<usize> {
65         Stderr::write(self, data)
66     }
67
68     fn flush(&mut self) -> io::Result<()> {
69         Stderr::flush(self)
70     }
71 }
72
73 pub fn is_ebadf(err: &io::Error) -> bool {
74     err.raw_os_error() == Some(::sys::syscall::EBADF as i32)
75 }
76
77 pub const STDIN_BUF_SIZE: usize = ::sys_common::io::DEFAULT_BUF_SIZE;
78
79 pub fn stderr_prints_nothing() -> bool {
80     false
81 }