1 //! Unix-specific extensions to general I/O primitives
3 #![stable(feature = "rust1", since = "1.0.0")]
9 use crate::sys_common
::{self, AsInner, FromInner, IntoInner}
;
12 /// Raw file descriptors.
13 #[stable(feature = "rust1", since = "1.0.0")]
14 pub type RawFd
= raw
::c_int
;
16 /// A trait to extract the raw unix file descriptor from an underlying
19 /// This is only available on unix platforms and must be imported in order
20 /// to call the method. Windows platforms have a corresponding `AsRawHandle`
21 /// and `AsRawSocket` set of traits.
22 #[stable(feature = "rust1", since = "1.0.0")]
24 /// Extracts the raw file descriptor.
26 /// This method does **not** pass ownership of the raw file descriptor
27 /// to the caller. The descriptor is only guaranteed to be valid while
28 /// the original object has not yet been destroyed.
29 #[stable(feature = "rust1", since = "1.0.0")]
30 fn as_raw_fd(&self) -> RawFd
;
33 /// A trait to express the ability to construct an object from a raw file
35 #[stable(feature = "from_raw_os", since = "1.1.0")]
37 /// Constructs a new instance of `Self` from the given raw file
40 /// This function **consumes ownership** of the specified file
41 /// descriptor. The returned object will take responsibility for closing
42 /// it when the object goes out of scope.
44 /// This function is also unsafe as the primitives currently returned
45 /// have the contract that they are the sole owner of the file
46 /// descriptor they are wrapping. Usage of this function could
47 /// accidentally allow violating this contract which can cause memory
48 /// unsafety in code that relies on it being true.
49 #[stable(feature = "from_raw_os", since = "1.1.0")]
50 unsafe fn from_raw_fd(fd
: RawFd
) -> Self;
53 /// A trait to express the ability to consume an object and acquire ownership of
54 /// its raw file descriptor.
55 #[stable(feature = "into_raw_os", since = "1.4.0")]
57 /// Consumes this object, returning the raw underlying file descriptor.
59 /// This function **transfers ownership** of the underlying file descriptor
60 /// to the caller. Callers are then the unique owners of the file descriptor
61 /// and must close the descriptor once it's no longer needed.
62 #[stable(feature = "into_raw_os", since = "1.4.0")]
63 fn into_raw_fd(self) -> RawFd
;
66 #[stable(feature = "rust1", since = "1.0.0")]
67 impl AsRawFd
for fs
::File
{
68 fn as_raw_fd(&self) -> RawFd
{
69 self.as_inner().fd().raw()
72 #[stable(feature = "from_raw_os", since = "1.1.0")]
73 impl FromRawFd
for fs
::File
{
74 unsafe fn from_raw_fd(fd
: RawFd
) -> fs
::File
{
75 fs
::File
::from_inner(sys
::fs
::File
::from_inner(fd
))
78 #[stable(feature = "into_raw_os", since = "1.4.0")]
79 impl IntoRawFd
for fs
::File
{
80 fn into_raw_fd(self) -> RawFd
{
81 self.into_inner().into_fd().into_raw()
85 #[stable(feature = "asraw_stdio", since = "1.21.0")]
86 impl AsRawFd
for io
::Stdin
{
87 fn as_raw_fd(&self) -> RawFd { libc::STDIN_FILENO }
90 #[stable(feature = "asraw_stdio", since = "1.21.0")]
91 impl AsRawFd
for io
::Stdout
{
92 fn as_raw_fd(&self) -> RawFd { libc::STDOUT_FILENO }
95 #[stable(feature = "asraw_stdio", since = "1.21.0")]
96 impl AsRawFd
for io
::Stderr
{
97 fn as_raw_fd(&self) -> RawFd { libc::STDERR_FILENO }
100 #[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
101 impl<'a
> AsRawFd
for io
::StdinLock
<'a
> {
102 fn as_raw_fd(&self) -> RawFd { libc::STDIN_FILENO }
105 #[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
106 impl<'a
> AsRawFd
for io
::StdoutLock
<'a
> {
107 fn as_raw_fd(&self) -> RawFd { libc::STDOUT_FILENO }
110 #[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
111 impl<'a
> AsRawFd
for io
::StderrLock
<'a
> {
112 fn as_raw_fd(&self) -> RawFd { libc::STDERR_FILENO }
115 #[stable(feature = "rust1", since = "1.0.0")]
116 impl AsRawFd
for net
::TcpStream
{
117 fn as_raw_fd(&self) -> RawFd { *self.as_inner().socket().as_inner() }
120 #[stable(feature = "rust1", since = "1.0.0")]
121 impl AsRawFd
for net
::TcpListener
{
122 fn as_raw_fd(&self) -> RawFd { *self.as_inner().socket().as_inner() }
125 #[stable(feature = "rust1", since = "1.0.0")]
126 impl AsRawFd
for net
::UdpSocket
{
127 fn as_raw_fd(&self) -> RawFd { *self.as_inner().socket().as_inner() }
130 #[stable(feature = "from_raw_os", since = "1.1.0")]
131 impl FromRawFd
for net
::TcpStream
{
132 unsafe fn from_raw_fd(fd
: RawFd
) -> net
::TcpStream
{
133 let socket
= sys
::net
::Socket
::from_inner(fd
);
134 net
::TcpStream
::from_inner(sys_common
::net
::TcpStream
::from_inner(socket
))
138 #[stable(feature = "from_raw_os", since = "1.1.0")]
139 impl FromRawFd
for net
::TcpListener
{
140 unsafe fn from_raw_fd(fd
: RawFd
) -> net
::TcpListener
{
141 let socket
= sys
::net
::Socket
::from_inner(fd
);
142 net
::TcpListener
::from_inner(sys_common
::net
::TcpListener
::from_inner(socket
))
146 #[stable(feature = "from_raw_os", since = "1.1.0")]
147 impl FromRawFd
for net
::UdpSocket
{
148 unsafe fn from_raw_fd(fd
: RawFd
) -> net
::UdpSocket
{
149 let socket
= sys
::net
::Socket
::from_inner(fd
);
150 net
::UdpSocket
::from_inner(sys_common
::net
::UdpSocket
::from_inner(socket
))
154 #[stable(feature = "into_raw_os", since = "1.4.0")]
155 impl IntoRawFd
for net
::TcpStream
{
156 fn into_raw_fd(self) -> RawFd
{
157 self.into_inner().into_socket().into_inner()
160 #[stable(feature = "into_raw_os", since = "1.4.0")]
161 impl IntoRawFd
for net
::TcpListener
{
162 fn into_raw_fd(self) -> RawFd
{
163 self.into_inner().into_socket().into_inner()
166 #[stable(feature = "into_raw_os", since = "1.4.0")]
167 impl IntoRawFd
for net
::UdpSocket
{
168 fn into_raw_fd(self) -> RawFd
{
169 self.into_inner().into_socket().into_inner()