]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/vxworks/ext/io.rs
6bcc59495e3635511bd7d63bbf807d8ec44e8a33
[rust.git] / src / libstd / sys / vxworks / ext / io.rs
1 //! Unix-specific extensions to general I/O primitives
2
3 #![stable(feature = "rust1", since = "1.0.0")]
4
5 use crate::fs;
6 use crate::os::raw;
7 use crate::sys;
8 use crate::io;
9 use crate::sys_common::{AsInner, FromInner, IntoInner};
10
11 /// Raw file descriptors.
12 #[stable(feature = "rust1", since = "1.0.0")]
13 pub type RawFd = raw::c_int;
14
15 /// A trait to extract the raw unix file descriptor from an underlying
16 /// object.
17 ///
18 /// This is only available on unix platforms and must be imported in order
19 /// to call the method. Windows platforms have a corresponding `AsRawHandle`
20 /// and `AsRawSocket` set of traits.
21 #[stable(feature = "rust1", since = "1.0.0")]
22 pub trait AsRawFd {
23     /// Extracts the raw file descriptor.
24     ///
25     /// This method does **not** pass ownership of the raw file descriptor
26     /// to the caller. The descriptor is only guaranteed to be valid while
27     /// the original object has not yet been destroyed.
28     #[stable(feature = "rust1", since = "1.0.0")]
29     fn as_raw_fd(&self) -> RawFd;
30 }
31
32 /// A trait to express the ability to construct an object from a raw file
33 /// descriptor.
34 #[stable(feature = "from_raw_os", since = "1.1.0")]
35 pub trait FromRawFd {
36     /// Constructs a new instance of `Self` from the given raw file
37     /// descriptor.
38     ///
39     /// This function **consumes ownership** of the specified file
40     /// descriptor. The returned object will take responsibility for closing
41     /// it when the object goes out of scope.
42     ///
43     /// This function is also unsafe as the primitives currently returned
44     /// have the contract that they are the sole owner of the file
45     /// descriptor they are wrapping. Usage of this function could
46     /// accidentally allow violating this contract which can cause memory
47     /// unsafety in code that relies on it being true.
48     #[stable(feature = "from_raw_os", since = "1.1.0")]
49     unsafe fn from_raw_fd(fd: RawFd) -> Self;
50 }
51
52 /// A trait to express the ability to consume an object and acquire ownership of
53 /// its raw file descriptor.
54 #[stable(feature = "into_raw_os", since = "1.4.0")]
55 pub trait IntoRawFd {
56     /// Consumes this object, returning the raw underlying file descriptor.
57     ///
58     /// This function **transfers ownership** of the underlying file descriptor
59     /// to the caller. Callers are then the unique owners of the file descriptor
60     /// and must close the descriptor once it's no longer needed.
61     #[stable(feature = "into_raw_os", since = "1.4.0")]
62     fn into_raw_fd(self) -> RawFd;
63 }
64
65 #[stable(feature = "rust1", since = "1.0.0")]
66 impl AsRawFd for fs::File {
67     fn as_raw_fd(&self) -> RawFd {
68         self.as_inner().fd().raw()
69     }
70 }
71 #[stable(feature = "from_raw_os", since = "1.1.0")]
72 impl FromRawFd for fs::File {
73     unsafe fn from_raw_fd(fd: RawFd) -> fs::File {
74         fs::File::from_inner(sys::fs::File::from_inner(fd))
75     }
76 }
77 #[stable(feature = "into_raw_os", since = "1.4.0")]
78 impl IntoRawFd for fs::File {
79     fn into_raw_fd(self) -> RawFd {
80         self.into_inner().into_fd().into_raw()
81     }
82 }
83
84 #[stable(feature = "asraw_stdio", since = "1.21.0")]
85 impl AsRawFd for io::Stdin {
86     fn as_raw_fd(&self) -> RawFd { libc::STDIN_FILENO }
87 }
88
89 #[stable(feature = "asraw_stdio", since = "1.21.0")]
90 impl AsRawFd for io::Stdout {
91     fn as_raw_fd(&self) -> RawFd { libc::STDOUT_FILENO }
92 }
93
94 #[stable(feature = "asraw_stdio", since = "1.21.0")]
95 impl AsRawFd for io::Stderr {
96     fn as_raw_fd(&self) -> RawFd { libc::STDERR_FILENO }
97 }
98
99 #[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
100 impl<'a> AsRawFd for io::StdinLock<'a> {
101     fn as_raw_fd(&self) -> RawFd { libc::STDIN_FILENO }
102 }
103
104 #[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
105 impl<'a> AsRawFd for io::StdoutLock<'a> {
106     fn as_raw_fd(&self) -> RawFd { libc::STDOUT_FILENO }
107 }
108
109 #[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
110 impl<'a> AsRawFd for io::StderrLock<'a> {
111     fn as_raw_fd(&self) -> RawFd { libc::STDERR_FILENO }
112 }