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.
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.
11 //! Unix-specific extensions to general I/O primitives
13 #![stable(feature = "rust1", since = "1.0.0")]
19 use sys_common
::{self, AsInner, FromInner, IntoInner}
;
21 /// Raw file descriptors.
22 #[stable(feature = "rust1", since = "1.0.0")]
23 pub type RawFd
= raw
::c_int
;
25 /// A trait to extract the raw unix file descriptor from an underlying
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")]
33 /// Extracts the raw file descriptor.
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
;
42 /// A trait to express the ability to construct an object from a raw file
44 #[stable(feature = "from_raw_os", since = "1.1.0")]
46 /// Constructs a new instances of `Self` from the given raw file
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.
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;
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")]
66 /// Consumes this object, returning the raw underlying file descriptor.
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
;
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()
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
))
86 impl IntoRawFd
for fs
::File
{
87 fn into_raw_fd(self) -> RawFd
{
88 self.into_inner().into_fd().into_raw()
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() }
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() }
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() }
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
))
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
))
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
))
127 impl IntoRawFd
for net
::TcpStream
{
128 fn into_raw_fd(self) -> RawFd
{
129 self.into_inner().into_socket().into_inner()
132 impl IntoRawFd
for net
::TcpListener
{
133 fn into_raw_fd(self) -> RawFd
{
134 self.into_inner().into_socket().into_inner()
137 impl IntoRawFd
for net
::UdpSocket
{
138 fn into_raw_fd(self) -> RawFd
{
139 self.into_inner().into_socket().into_inner()