Skip to content

Commit 6370f29

Browse files
committed
std: Stabilize parts of std::os::platform::io
This commit stabilizes the platform-specific `io` modules, specifically around the traits having to do with the raw representation of each object on each platform. Specifically, the following material was stabilized: * `AsRaw{Fd,Socket,Handle}` * `RawFd` (renamed from `Fd`) * `RawHandle` (renamed from `Handle`) * `RawSocket` (renamed from `Socket`) * `AsRaw{Fd,Socket,Handle}` implementations * `std::os::{unix, windows}::io` The following material was added as `#[unstable]`: * `FromRaw{Fd,Socket,Handle}` * Implementations for various primitives There are a number of future improvements that are possible to make to this module, but this should cover a good bit of functionality desired from these modules for now. Some specific future additions may include: * `IntoRawXXX` traits to consume the raw representation and cancel the auto-destructor. * `Fd`, `Socket`, and `Handle` abstractions that behave like Rust objects and have nice methods for various syscalls. At this time though, these are considered backwards-compatible extensions and will not be stabilized at this time. This commit is a breaking change due to the addition of `Raw` in from of the type aliases in each of the platform-specific modules. [breaking-change]
1 parent 557d434 commit 6370f29

File tree

11 files changed

+263
-59
lines changed

11 files changed

+263
-59
lines changed

src/libstd/fs/mod.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ mod tempdir;
5656
#[stable(feature = "rust1", since = "1.0.0")]
5757
pub struct File {
5858
inner: fs_imp::File,
59-
path: PathBuf,
59+
path: Option<PathBuf>,
6060
}
6161

6262
/// Metadata information about a file.
@@ -171,7 +171,7 @@ impl File {
171171
reason = "this abstraction is imposed by this library instead \
172172
of the underlying OS and may be removed")]
173173
pub fn path(&self) -> Option<&Path> {
174-
Some(&self.path)
174+
self.path.as_ref().map(|p| &**p)
175175
}
176176

177177
/// Attempt to sync all OS-internal metadata to disk.
@@ -273,6 +273,12 @@ impl File {
273273
impl AsInner<fs_imp::File> for File {
274274
fn as_inner(&self) -> &fs_imp::File { &self.inner }
275275
}
276+
impl FromInner<fs_imp::File> for File {
277+
fn from_inner(f: fs_imp::File) -> File {
278+
File { inner: f, path: None }
279+
}
280+
}
281+
276282
#[stable(feature = "rust1", since = "1.0.0")]
277283
impl Read for File {
278284
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
@@ -381,7 +387,7 @@ impl OpenOptions {
381387
pub fn open<P: AsRef<Path>>(&self, path: P) -> io::Result<File> {
382388
let path = path.as_ref();
383389
let inner = try!(fs_imp::File::open(path, &self.0));
384-
Ok(File { path: path.to_path_buf(), inner: inner })
390+
Ok(File { path: Some(path.to_path_buf()), inner: inner })
385391
}
386392
}
387393

src/libstd/net/tcp.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use io::prelude::*;
1717
use io;
1818
use net::{ToSocketAddrs, SocketAddr, Shutdown};
1919
use sys_common::net2 as net_imp;
20-
use sys_common::AsInner;
20+
use sys_common::{AsInner, FromInner};
2121

2222
/// A structure which represents a TCP stream between a local socket and a
2323
/// remote socket.
@@ -172,6 +172,10 @@ impl AsInner<net_imp::TcpStream> for TcpStream {
172172
fn as_inner(&self) -> &net_imp::TcpStream { &self.0 }
173173
}
174174

175+
impl FromInner<net_imp::TcpStream> for TcpStream {
176+
fn from_inner(inner: net_imp::TcpStream) -> TcpStream { TcpStream(inner) }
177+
}
178+
175179
impl TcpListener {
176180
/// Creates a new `TcpListener` which will be bound to the specified
177181
/// address.
@@ -245,6 +249,12 @@ impl AsInner<net_imp::TcpListener> for TcpListener {
245249
fn as_inner(&self) -> &net_imp::TcpListener { &self.0 }
246250
}
247251

252+
impl FromInner<net_imp::TcpListener> for TcpListener {
253+
fn from_inner(inner: net_imp::TcpListener) -> TcpListener {
254+
TcpListener(inner)
255+
}
256+
}
257+
248258
#[cfg(test)]
249259
mod tests {
250260
use prelude::v1::*;

src/libstd/net/udp.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use prelude::v1::*;
1616
use io::{self, Error, ErrorKind};
1717
use net::{ToSocketAddrs, SocketAddr, IpAddr};
1818
use sys_common::net2 as net_imp;
19-
use sys_common::AsInner;
19+
use sys_common::{AsInner, FromInner};
2020

2121
/// A User Datagram Protocol socket.
2222
///
@@ -140,6 +140,10 @@ impl AsInner<net_imp::UdpSocket> for UdpSocket {
140140
fn as_inner(&self) -> &net_imp::UdpSocket { &self.0 }
141141
}
142142

143+
impl FromInner<net_imp::UdpSocket> for UdpSocket {
144+
fn from_inner(inner: net_imp::UdpSocket) -> UdpSocket { UdpSocket(inner) }
145+
}
146+
143147
#[cfg(test)]
144148
mod tests {
145149
use prelude::v1::*;

src/libstd/sys/common/net2.rs

+18
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,12 @@ impl TcpStream {
222222
}
223223
}
224224

225+
impl FromInner<Socket> for TcpStream {
226+
fn from_inner(socket: Socket) -> TcpStream {
227+
TcpStream { inner: socket }
228+
}
229+
}
230+
225231
////////////////////////////////////////////////////////////////////////////////
226232
// TCP listeners
227233
////////////////////////////////////////////////////////////////////////////////
@@ -275,6 +281,12 @@ impl TcpListener {
275281
}
276282
}
277283

284+
impl FromInner<Socket> for TcpListener {
285+
fn from_inner(socket: Socket) -> TcpListener {
286+
TcpListener { inner: socket }
287+
}
288+
}
289+
278290
////////////////////////////////////////////////////////////////////////////////
279291
// UDP
280292
////////////////////////////////////////////////////////////////////////////////
@@ -387,3 +399,9 @@ impl UdpSocket {
387399
self.inner.duplicate().map(|s| UdpSocket { inner: s })
388400
}
389401
}
402+
403+
impl FromInner<Socket> for UdpSocket {
404+
fn from_inner(socket: Socket) -> UdpSocket {
405+
UdpSocket { inner: socket }
406+
}
407+
}

src/libstd/sys/unix/ext.rs

+92-23
Original file line numberDiff line numberDiff line change
@@ -32,102 +32,171 @@
3232
#![stable(feature = "rust1", since = "1.0.0")]
3333

3434
/// Unix-specific extensions to general I/O primitives
35-
#[unstable(feature = "io_ext",
36-
reason = "may want a slightly different organization or a more \
37-
general file descriptor primitive")]
35+
#[stable(feature = "rust1", since = "1.0.0")]
3836
pub mod io {
3937
#[allow(deprecated)] use old_io;
4038
use fs;
4139
use libc;
4240
use net;
43-
use sys_common::AsInner;
41+
use sys_common::{net2, AsInner, FromInner};
42+
use sys;
4443

4544
/// Raw file descriptors.
46-
pub type Fd = libc::c_int;
47-
48-
/// Extract raw file descriptor
45+
#[stable(feature = "rust1", since = "1.0.0")]
46+
pub type RawFd = libc::c_int;
47+
48+
/// A trait to extract the raw unix file descriptor from an underlying
49+
/// object.
50+
///
51+
/// This is only available on unix platforms and must be imported in order
52+
/// to call the method. Windows platforms have a corresponding `AsRawHandle`
53+
/// and `AsRawSocket` set of traits.
54+
#[stable(feature = "rust1", since = "1.0.0")]
4955
pub trait AsRawFd {
50-
/// Extract the raw file descriptor, without taking any ownership.
51-
fn as_raw_fd(&self) -> Fd;
56+
/// Extract the raw file descriptor.
57+
///
58+
/// This method does **not** pass ownership of the raw file descriptor
59+
/// to the caller. The descriptor is only guarantee to be valid while
60+
/// the original object has not yet been destroyed.
61+
#[stable(feature = "rust1", since = "1.0.0")]
62+
fn as_raw_fd(&self) -> RawFd;
63+
}
64+
65+
/// A trait to express the ability to construct an object from a raw file
66+
/// descriptor.
67+
#[unstable(feature = "from_raw_os",
68+
reason = "recent addition to std::os::unix::io")]
69+
pub trait FromRawFd {
70+
/// Constructs a new instances of `Self` from the given raw file
71+
/// descriptor.
72+
///
73+
/// This function **consumes ownership** of the specified file
74+
/// descriptor. The returned object will take responsibility for closing
75+
/// it when the object goes out of scope.
76+
///
77+
/// Callers should normally only pass in a valid file descriptor to this
78+
/// method or otherwise methods will return errors.
79+
fn from_raw_fd(fd: RawFd) -> Self;
5280
}
5381

5482
#[allow(deprecated)]
83+
#[stable(feature = "rust1", since = "1.0.0")]
5584
impl AsRawFd for old_io::fs::File {
56-
fn as_raw_fd(&self) -> Fd {
85+
fn as_raw_fd(&self) -> RawFd {
5786
self.as_inner().fd()
5887
}
5988
}
6089

90+
#[stable(feature = "rust1", since = "1.0.0")]
6191
impl AsRawFd for fs::File {
62-
fn as_raw_fd(&self) -> Fd {
92+
fn as_raw_fd(&self) -> RawFd {
6393
self.as_inner().fd().raw()
6494
}
6595
}
96+
#[unstable(feature = "from_raw_os", reason = "trait is unstable")]
97+
impl FromRawFd for fs::File {
98+
fn from_raw_fd(fd: RawFd) -> fs::File {
99+
fs::File::from_inner(sys::fs2::File::from_inner(fd))
100+
}
101+
}
66102

67103
#[allow(deprecated)]
104+
#[stable(feature = "rust1", since = "1.0.0")]
68105
impl AsRawFd for old_io::pipe::PipeStream {
69-
fn as_raw_fd(&self) -> Fd {
106+
fn as_raw_fd(&self) -> RawFd {
70107
self.as_inner().fd()
71108
}
72109
}
73110

74111
#[allow(deprecated)]
112+
#[stable(feature = "rust1", since = "1.0.0")]
75113
impl AsRawFd for old_io::net::pipe::UnixStream {
76-
fn as_raw_fd(&self) -> Fd {
114+
fn as_raw_fd(&self) -> RawFd {
77115
self.as_inner().fd()
78116
}
79117
}
80118

81119
#[allow(deprecated)]
120+
#[stable(feature = "rust1", since = "1.0.0")]
82121
impl AsRawFd for old_io::net::pipe::UnixListener {
83-
fn as_raw_fd(&self) -> Fd {
122+
fn as_raw_fd(&self) -> RawFd {
84123
self.as_inner().fd()
85124
}
86125
}
87126

88127
#[allow(deprecated)]
128+
#[stable(feature = "rust1", since = "1.0.0")]
89129
impl AsRawFd for old_io::net::pipe::UnixAcceptor {
90-
fn as_raw_fd(&self) -> Fd {
130+
fn as_raw_fd(&self) -> RawFd {
91131
self.as_inner().fd()
92132
}
93133
}
94134

135+
#[stable(feature = "rust1", since = "1.0.0")]
95136
#[allow(deprecated)]
96137
impl AsRawFd for old_io::net::tcp::TcpStream {
97-
fn as_raw_fd(&self) -> Fd {
138+
fn as_raw_fd(&self) -> RawFd {
98139
self.as_inner().fd()
99140
}
100141
}
101142

143+
#[stable(feature = "rust1", since = "1.0.0")]
102144
#[allow(deprecated)]
103145
impl AsRawFd for old_io::net::tcp::TcpListener {
104-
fn as_raw_fd(&self) -> Fd {
146+
fn as_raw_fd(&self) -> RawFd {
105147
self.as_inner().fd()
106148
}
107149
}
108150

151+
#[stable(feature = "rust1", since = "1.0.0")]
109152
#[allow(deprecated)]
110153
impl AsRawFd for old_io::net::tcp::TcpAcceptor {
111-
fn as_raw_fd(&self) -> Fd {
154+
fn as_raw_fd(&self) -> RawFd {
112155
self.as_inner().fd()
113156
}
114157
}
115158

116159
#[allow(deprecated)]
160+
#[stable(feature = "rust1", since = "1.0.0")]
117161
impl AsRawFd for old_io::net::udp::UdpSocket {
118-
fn as_raw_fd(&self) -> Fd {
162+
fn as_raw_fd(&self) -> RawFd {
119163
self.as_inner().fd()
120164
}
121165
}
122166

167+
#[stable(feature = "rust1", since = "1.0.0")]
123168
impl AsRawFd for net::TcpStream {
124-
fn as_raw_fd(&self) -> Fd { *self.as_inner().socket().as_inner() }
169+
fn as_raw_fd(&self) -> RawFd { *self.as_inner().socket().as_inner() }
125170
}
171+
#[stable(feature = "rust1", since = "1.0.0")]
126172
impl AsRawFd for net::TcpListener {
127-
fn as_raw_fd(&self) -> Fd { *self.as_inner().socket().as_inner() }
173+
fn as_raw_fd(&self) -> RawFd { *self.as_inner().socket().as_inner() }
128174
}
175+
#[stable(feature = "rust1", since = "1.0.0")]
129176
impl AsRawFd for net::UdpSocket {
130-
fn as_raw_fd(&self) -> Fd { *self.as_inner().socket().as_inner() }
177+
fn as_raw_fd(&self) -> RawFd { *self.as_inner().socket().as_inner() }
178+
}
179+
180+
#[unstable(feature = "from_raw_os", reason = "trait is unstable")]
181+
impl FromRawFd for net::TcpStream {
182+
fn from_raw_fd(fd: RawFd) -> net::TcpStream {
183+
let socket = sys::net::Socket::from_inner(fd);
184+
net::TcpStream::from_inner(net2::TcpStream::from_inner(socket))
185+
}
186+
}
187+
#[unstable(feature = "from_raw_os", reason = "trait is unstable")]
188+
impl FromRawFd for net::TcpListener {
189+
fn from_raw_fd(fd: RawFd) -> net::TcpListener {
190+
let socket = sys::net::Socket::from_inner(fd);
191+
net::TcpListener::from_inner(net2::TcpListener::from_inner(socket))
192+
}
193+
}
194+
#[unstable(feature = "from_raw_os", reason = "trait is unstable")]
195+
impl FromRawFd for net::UdpSocket {
196+
fn from_raw_fd(fd: RawFd) -> net::UdpSocket {
197+
let socket = sys::net::Socket::from_inner(fd);
198+
net::UdpSocket::from_inner(net2::UdpSocket::from_inner(socket))
199+
}
131200
}
132201
}
133202

@@ -302,7 +371,7 @@ pub mod process {
302371
#[stable(feature = "rust1", since = "1.0.0")]
303372
pub mod prelude {
304373
#[doc(no_inline)]
305-
pub use super::io::{Fd, AsRawFd};
374+
pub use super::io::{RawFd, AsRawFd};
306375
#[doc(no_inline)] #[stable(feature = "rust1", since = "1.0.0")]
307376
pub use super::ffi::{OsStrExt, OsStringExt};
308377
#[doc(no_inline)]

src/libstd/sys/unix/fs2.rs

+6
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,12 @@ fn cstr(path: &Path) -> io::Result<CString> {
280280
Ok(cstring)
281281
}
282282

283+
impl FromInner<c_int> for File {
284+
fn from_inner(fd: c_int) -> File {
285+
File(FileDesc::new(fd))
286+
}
287+
}
288+
283289
pub fn mkdir(p: &Path) -> io::Result<()> {
284290
let p = try!(cstr(p));
285291
try!(cvt(unsafe { libc::mkdir(p.as_ptr(), 0o777) }));

src/libstd/sys/unix/net.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use str;
1717
use sys::c;
1818
use net::SocketAddr;
1919
use sys::fd::FileDesc;
20-
use sys_common::AsInner;
20+
use sys_common::{AsInner, FromInner};
2121

2222
pub use sys::{cvt, cvt_r};
2323

@@ -72,3 +72,7 @@ impl Socket {
7272
impl AsInner<c_int> for Socket {
7373
fn as_inner(&self) -> &c_int { self.0.as_inner() }
7474
}
75+
76+
impl FromInner<c_int> for Socket {
77+
fn from_inner(fd: c_int) -> Socket { Socket(FileDesc::new(fd)) }
78+
}

0 commit comments

Comments
 (0)