]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/ext/io.rs
Auto merge of #44013 - arielb1:coerce-snapshot, r=eddyb
[rust.git] / src / libstd / sys / unix / ext / io.rs
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.
4 //
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.
10
11 //! Unix-specific extensions to general I/O primitives
12
13 #![stable(feature = "rust1", since = "1.0.0")]
14
15 use fs;
16 use os::raw;
17 use sys;
18 use io;
19 use sys_common::{AsInner, FromInner, IntoInner};
20 use libc;
21
22 /// Raw file descriptors.
23 #[stable(feature = "rust1", since = "1.0.0")]
24 pub type RawFd = raw::c_int;
25
26 /// A trait to extract the raw unix file descriptor from an underlying
27 /// object.
28 ///
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")]
33 pub trait AsRawFd {
34     /// Extracts the raw file descriptor.
35     ///
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;
41 }
42
43 /// A trait to express the ability to construct an object from a raw file
44 /// descriptor.
45 #[stable(feature = "from_raw_os", since = "1.1.0")]
46 pub trait FromRawFd {
47     /// Constructs a new instance of `Self` from the given raw file
48     /// descriptor.
49     ///
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.
53     ///
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;
61 }
62
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")]
66 pub trait IntoRawFd {
67     /// Consumes this object, returning the raw underlying file descriptor.
68     ///
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;
74 }
75
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()
80     }
81 }
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))
86     }
87 }
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()
92     }
93 }
94
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 }
98 }
99
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 }
103 }
104
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 }
108 }