]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/stdio.rs
Auto merge of #27273 - tshepang:claim-not-accurate, r=steveklabnik
[rust.git] / src / libstd / sys / unix / stdio.rs
1 // Copyright 2015 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 libc;
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(libc::STDIN_FILENO);
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(libc::STDOUT_FILENO);
35         let ret = fd.write(data);
36         fd.into_raw();
37         ret
38     }
39 }
40
41 impl Stderr {
42     pub fn new() -> io::Result<Stderr> { Ok(Stderr(())) }
43
44     pub fn write(&self, data: &[u8]) -> io::Result<usize> {
45         let fd = FileDesc::new(libc::STDERR_FILENO);
46         let ret = fd.write(data);
47         fd.into_raw();
48         ret
49     }
50 }
51
52 // FIXME: right now this raw stderr handle is used in a few places because
53 //        std::io::stderr_raw isn't exposed, but once that's exposed this impl
54 //        should go away
55 impl io::Write for Stderr {
56     fn write(&mut self, data: &[u8]) -> io::Result<usize> {
57         Stderr::write(self, data)
58     }
59     fn flush(&mut self) -> io::Result<()> { Ok(()) }
60 }