]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/ext/io.rs
Rollup merge of #56217 - frewsxcv:frewsxcv-float-parse, r=QuietMisdreavus
[rust.git] / src / libstd / sys / unix / ext / io.rs
1 //! Unix-specific extensions to general I/O primitives
2
3 #![stable(feature = "rust1", since = "1.0.0")]
4
5 use fs;
6 use os::raw;
7 use sys;
8 use io;
9 use sys_common::{AsInner, FromInner, IntoInner};
10 use libc;
11
12 /// Raw file descriptors.
13 #[stable(feature = "rust1", since = "1.0.0")]
14 pub type RawFd = raw::c_int;
15
16 /// A trait to extract the raw unix file descriptor from an underlying
17 /// object.
18 ///
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")]
23 pub trait AsRawFd {
24     /// Extracts the raw file descriptor.
25     ///
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;
31 }
32
33 /// A trait to express the ability to construct an object from a raw file
34 /// descriptor.
35 #[stable(feature = "from_raw_os", since = "1.1.0")]
36 pub trait FromRawFd {
37     /// Constructs a new instance of `Self` from the given raw file
38     /// descriptor.
39     ///
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.
43     ///
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;
51 }
52
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")]
56 pub trait IntoRawFd {
57     /// Consumes this object, returning the raw underlying file descriptor.
58     ///
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;
64 }
65
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()
70     }
71 }
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))
76     }
77 }
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()
82     }
83 }
84
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 }
88 }
89
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 }
93 }
94
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 }
98 }