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::{AsInner, FromInner, IntoInner};
22 /// Raw file descriptors.
23 #[stable(feature = "rust1", since = "1.0.0")]
24 pub type RawFd = raw::c_int;
26 /// A trait to extract the raw unix file descriptor from an underlying
29 /// This is only available on unix platforms and must be imported in order
30 /// to call the method. Windows platforms have a corresponding `AsRawHandle`
31 /// and `AsRawSocket` set of traits.
32 #[stable(feature = "rust1", since = "1.0.0")]
34 /// Extracts the raw file descriptor.
36 /// This method does **not** pass ownership of the raw file descriptor
37 /// to the caller. The descriptor is only guaranteed to be valid while
38 /// the original object has not yet been destroyed.
39 #[stable(feature = "rust1", since = "1.0.0")]
40 fn as_raw_fd(&self) -> RawFd;
43 /// A trait to express the ability to construct an object from a raw file
45 #[stable(feature = "from_raw_os", since = "1.1.0")]
47 /// Constructs a new instance of `Self` from the given raw file
50 /// This function **consumes ownership** of the specified file
51 /// descriptor. The returned object will take responsibility for closing
52 /// it when the object goes out of scope.
54 /// This function is also unsafe as the primitives currently returned
55 /// have the contract that they are the sole owner of the file
56 /// descriptor they are wrapping. Usage of this function could
57 /// accidentally allow violating this contract which can cause memory
58 /// unsafety in code that relies on it being true.
59 #[stable(feature = "from_raw_os", since = "1.1.0")]
60 unsafe fn from_raw_fd(fd: RawFd) -> Self;
63 /// A trait to express the ability to consume an object and acquire ownership of
64 /// its raw file descriptor.
65 #[stable(feature = "into_raw_os", since = "1.4.0")]
67 /// Consumes this object, returning the raw underlying file descriptor.
69 /// This function **transfers ownership** of the underlying file descriptor
70 /// to the caller. Callers are then the unique owners of the file descriptor
71 /// and must close the descriptor once it's no longer needed.
72 #[stable(feature = "into_raw_os", since = "1.4.0")]
73 fn into_raw_fd(self) -> RawFd;
76 #[stable(feature = "rust1", since = "1.0.0")]
77 impl AsRawFd for fs::File {
78 fn as_raw_fd(&self) -> RawFd {
79 self.as_inner().fd().raw()
82 #[stable(feature = "from_raw_os", since = "1.1.0")]
83 impl FromRawFd for fs::File {
84 unsafe fn from_raw_fd(fd: RawFd) -> fs::File {
85 fs::File::from_inner(sys::fs::File::from_inner(fd))
88 #[stable(feature = "into_raw_os", since = "1.4.0")]
89 impl IntoRawFd for fs::File {
90 fn into_raw_fd(self) -> RawFd {
91 self.into_inner().into_fd().into_raw()
95 #[stable(feature = "asraw_stdio", since = "1.21.0")]
96 impl AsRawFd for io::Stdin {
97 fn as_raw_fd(&self) -> RawFd { libc::STDIN_FILENO }
100 #[stable(feature = "asraw_stdio", since = "1.21.0")]
101 impl AsRawFd for io::Stdout {
102 fn as_raw_fd(&self) -> RawFd { libc::STDOUT_FILENO }
105 #[stable(feature = "asraw_stdio", since = "1.21.0")]
106 impl AsRawFd for io::Stderr {
107 fn as_raw_fd(&self) -> RawFd { libc::STDERR_FILENO }