1 use std::fs::File;
2 use std::io::{Read, Write};
3 use std::os::unix::io::{IntoRawFd, AsRawFd, FromRawFd, RawFd};
4
5 use libc;
6
7 use {io, Ready, Poll, PollOpt, Token};
8 use event::Evented;
9 use unix::EventedFd;
10 use sys::unix::cvt;
11
set_nonblock(fd: libc::c_int) -> io::Result<()>12 pub fn set_nonblock(fd: libc::c_int) -> io::Result<()> {
13 unsafe {
14 let flags = libc::fcntl(fd, libc::F_GETFL);
15 cvt(libc::fcntl(fd, libc::F_SETFL, flags | libc::O_NONBLOCK)).map(|_|())
16 }
17 }
18
set_cloexec(fd: libc::c_int) -> io::Result<()>19 pub fn set_cloexec(fd: libc::c_int) -> io::Result<()> {
20 unsafe {
21 let flags = libc::fcntl(fd, libc::F_GETFD);
22 cvt(libc::fcntl(fd, libc::F_SETFD, flags | libc::FD_CLOEXEC)).map(|_| ())
23 }
24 }
25
26 /*
27 *
28 * ===== Basic IO type =====
29 *
30 */
31
32 /// Manages a FD
33 #[derive(Debug)]
34 pub struct Io {
35 fd: File,
36 }
37
38 impl Io {
39 /// Try to clone the FD
try_clone(&self) -> io::Result<Io>40 pub fn try_clone(&self) -> io::Result<Io> {
41 Ok(Io { fd: self.fd.try_clone()? })
42 }
43 }
44
45 impl FromRawFd for Io {
from_raw_fd(fd: RawFd) -> Io46 unsafe fn from_raw_fd(fd: RawFd) -> Io {
47 Io { fd: File::from_raw_fd(fd) }
48 }
49 }
50
51 impl IntoRawFd for Io {
into_raw_fd(self) -> RawFd52 fn into_raw_fd(self) -> RawFd {
53 self.fd.into_raw_fd()
54 }
55 }
56
57 impl AsRawFd for Io {
as_raw_fd(&self) -> RawFd58 fn as_raw_fd(&self) -> RawFd {
59 self.fd.as_raw_fd()
60 }
61 }
62
63 impl Evented for Io {
register(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt) -> io::Result<()>64 fn register(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt) -> io::Result<()> {
65 EventedFd(&self.as_raw_fd()).register(poll, token, interest, opts)
66 }
67
reregister(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt) -> io::Result<()>68 fn reregister(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt) -> io::Result<()> {
69 EventedFd(&self.as_raw_fd()).reregister(poll, token, interest, opts)
70 }
71
deregister(&self, poll: &Poll) -> io::Result<()>72 fn deregister(&self, poll: &Poll) -> io::Result<()> {
73 EventedFd(&self.as_raw_fd()).deregister(poll)
74 }
75 }
76
77 impl Read for Io {
read(&mut self, dst: &mut [u8]) -> io::Result<usize>78 fn read(&mut self, dst: &mut [u8]) -> io::Result<usize> {
79 (&self.fd).read(dst)
80 }
81 }
82
83 impl<'a> Read for &'a Io {
read(&mut self, dst: &mut [u8]) -> io::Result<usize>84 fn read(&mut self, dst: &mut [u8]) -> io::Result<usize> {
85 (&self.fd).read(dst)
86 }
87 }
88
89 impl Write for Io {
write(&mut self, src: &[u8]) -> io::Result<usize>90 fn write(&mut self, src: &[u8]) -> io::Result<usize> {
91 (&self.fd).write(src)
92 }
93
flush(&mut self) -> io::Result<()>94 fn flush(&mut self) -> io::Result<()> {
95 (&self.fd).flush()
96 }
97 }
98
99 impl<'a> Write for &'a Io {
write(&mut self, src: &[u8]) -> io::Result<usize>100 fn write(&mut self, src: &[u8]) -> io::Result<usize> {
101 (&self.fd).write(src)
102 }
103
flush(&mut self) -> io::Result<()>104 fn flush(&mut self) -> io::Result<()> {
105 (&self.fd).flush()
106 }
107 }
108