]> git.proxmox.com Git - rustc.git/blob - src/libstd/sys/unix/ext/io.rs
Imported Upstream version 1.3.0+dfsg1
[rustc.git] / src / libstd / sys / unix / ext / io.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 //! Unix-specific extensions to general I/O primitives
12
13 #![stable(feature = "rust1", since = "1.0.0")]
14
15 use fs;
16 use net;
17 use os::raw;
18 use sys;
19 use sys_common::{self, AsInner, FromInner, IntoInner};
20
21 /// Raw file descriptors.
22 #[stable(feature = "rust1", since = "1.0.0")]
23 pub type RawFd = raw::c_int;
24
25 /// A trait to extract the raw unix file descriptor from an underlying
26 /// object.
27 ///
28 /// This is only available on unix platforms and must be imported in order
29 /// to call the method. Windows platforms have a corresponding `AsRawHandle`
30 /// and `AsRawSocket` set of traits.
31 #[stable(feature = "rust1", since = "1.0.0")]
32 pub trait AsRawFd {
33 /// Extracts the raw file descriptor.
34 ///
35 /// This method does **not** pass ownership of the raw file descriptor
36 /// to the caller. The descriptor is only guarantee to be valid while
37 /// the original object has not yet been destroyed.
38 #[stable(feature = "rust1", since = "1.0.0")]
39 fn as_raw_fd(&self) -> RawFd;
40 }
41
42 /// A trait to express the ability to construct an object from a raw file
43 /// descriptor.
44 #[stable(feature = "from_raw_os", since = "1.1.0")]
45 pub trait FromRawFd {
46 /// Constructs a new instances of `Self` from the given raw file
47 /// descriptor.
48 ///
49 /// This function **consumes ownership** of the specified file
50 /// descriptor. The returned object will take responsibility for closing
51 /// it when the object goes out of scope.
52 ///
53 /// This function is also unsafe as the primitives currently returned
54 /// have the contract that they are the sole owner of the file
55 /// descriptor they are wrapping. Usage of this function could
56 /// accidentally allow violating this contract which can cause memory
57 /// unsafety in code that relies on it being true.
58 #[stable(feature = "from_raw_os", since = "1.1.0")]
59 unsafe fn from_raw_fd(fd: RawFd) -> Self;
60 }
61
62 /// A trait to express the ability to consume an object and acquire ownership of
63 /// its raw file descriptor.
64 #[unstable(feature = "into_raw_os", reason = "recently added API")]
65 pub trait IntoRawFd {
66 /// Consumes this object, returning the raw underlying file descriptor.
67 ///
68 /// This function **transfers ownership** of the underlying file descriptor
69 /// to the caller. Callers are then the unique owners of the file descriptor
70 /// and must close the descriptor once it's no longer needed.
71 fn into_raw_fd(self) -> RawFd;
72 }
73
74 #[stable(feature = "rust1", since = "1.0.0")]
75 impl AsRawFd for fs::File {
76 fn as_raw_fd(&self) -> RawFd {
77 self.as_inner().fd().raw()
78 }
79 }
80 #[stable(feature = "from_raw_os", since = "1.1.0")]
81 impl FromRawFd for fs::File {
82 unsafe fn from_raw_fd(fd: RawFd) -> fs::File {
83 fs::File::from_inner(sys::fs::File::from_inner(fd))
84 }
85 }
86 impl IntoRawFd for fs::File {
87 fn into_raw_fd(self) -> RawFd {
88 self.into_inner().into_fd().into_raw()
89 }
90 }
91
92 #[stable(feature = "rust1", since = "1.0.0")]
93 impl AsRawFd for net::TcpStream {
94 fn as_raw_fd(&self) -> RawFd { *self.as_inner().socket().as_inner() }
95 }
96 #[stable(feature = "rust1", since = "1.0.0")]
97 impl AsRawFd for net::TcpListener {
98 fn as_raw_fd(&self) -> RawFd { *self.as_inner().socket().as_inner() }
99 }
100 #[stable(feature = "rust1", since = "1.0.0")]
101 impl AsRawFd for net::UdpSocket {
102 fn as_raw_fd(&self) -> RawFd { *self.as_inner().socket().as_inner() }
103 }
104
105 #[stable(feature = "from_raw_os", since = "1.1.0")]
106 impl FromRawFd for net::TcpStream {
107 unsafe fn from_raw_fd(fd: RawFd) -> net::TcpStream {
108 let socket = sys::net::Socket::from_inner(fd);
109 net::TcpStream::from_inner(sys_common::net::TcpStream::from_inner(socket))
110 }
111 }
112 #[stable(feature = "from_raw_os", since = "1.1.0")]
113 impl FromRawFd for net::TcpListener {
114 unsafe fn from_raw_fd(fd: RawFd) -> net::TcpListener {
115 let socket = sys::net::Socket::from_inner(fd);
116 net::TcpListener::from_inner(sys_common::net::TcpListener::from_inner(socket))
117 }
118 }
119 #[stable(feature = "from_raw_os", since = "1.1.0")]
120 impl FromRawFd for net::UdpSocket {
121 unsafe fn from_raw_fd(fd: RawFd) -> net::UdpSocket {
122 let socket = sys::net::Socket::from_inner(fd);
123 net::UdpSocket::from_inner(sys_common::net::UdpSocket::from_inner(socket))
124 }
125 }
126
127 impl IntoRawFd for net::TcpStream {
128 fn into_raw_fd(self) -> RawFd {
129 self.into_inner().into_socket().into_inner()
130 }
131 }
132 impl IntoRawFd for net::TcpListener {
133 fn into_raw_fd(self) -> RawFd {
134 self.into_inner().into_socket().into_inner()
135 }
136 }
137 impl IntoRawFd for net::UdpSocket {
138 fn into_raw_fd(self) -> RawFd {
139 self.into_inner().into_socket().into_inner()
140 }
141 }