Skip to content

Commit 6d1e4dd

Browse files
committed
kmc-solid: Remove FileDesc
Removes the private type `std::sys::solid::net::FileDesc`, replacing its only usage in `std::sys::solid::net::Socket` with `std::os::solid::io:: OwnedFd`.
1 parent cbfab81 commit 6d1e4dd

File tree

1 file changed

+43
-115
lines changed
  • library/std/src/sys/solid

1 file changed

+43
-115
lines changed

library/std/src/sys/solid/net.rs

+43-115
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
io::{self, BorrowedBuf, BorrowedCursor, ErrorKind, IoSlice, IoSliceMut},
66
mem,
77
net::{Shutdown, SocketAddr},
8-
os::solid::io::{AsRawFd, FromRawFd, IntoRawFd},
8+
os::solid::io::{AsRawFd, FromRawFd, IntoRawFd, OwnedFd},
99
ptr, str,
1010
sys_common::net::{getsockopt, setsockopt, sockaddr_to_addr},
1111
sys_common::IntoInner,
@@ -29,95 +29,6 @@ const fn max_iov() -> usize {
2929
1024
3030
}
3131

32-
/// A file descriptor.
33-
#[rustc_layout_scalar_valid_range_start(0)]
34-
// libstd/os/raw/mod.rs assures me that every libstd-supported platform has a
35-
// 32-bit c_int. Below is -2, in two's complement, but that only works out
36-
// because c_int is 32 bits.
37-
#[rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FE)]
38-
struct FileDesc {
39-
fd: c_int,
40-
}
41-
42-
impl FileDesc {
43-
#[inline]
44-
fn new(fd: c_int) -> FileDesc {
45-
assert_ne!(fd, -1i32);
46-
// Safety: we just asserted that the value is in the valid range and
47-
// isn't `-1` (the only value bigger than `0xFF_FF_FF_FE` unsigned)
48-
unsafe { FileDesc { fd } }
49-
}
50-
51-
#[inline]
52-
fn raw(&self) -> c_int {
53-
self.fd
54-
}
55-
56-
/// Extracts the actual file descriptor without closing it.
57-
#[inline]
58-
fn into_raw(self) -> c_int {
59-
let fd = self.fd;
60-
mem::forget(self);
61-
fd
62-
}
63-
64-
fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
65-
let ret = cvt(unsafe {
66-
netc::read(self.fd, buf.as_mut_ptr() as *mut c_void, cmp::min(buf.len(), READ_LIMIT))
67-
})?;
68-
Ok(ret as usize)
69-
}
70-
71-
fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
72-
let ret = cvt(unsafe {
73-
netc::readv(
74-
self.fd,
75-
bufs.as_ptr() as *const netc::iovec,
76-
cmp::min(bufs.len(), max_iov()) as c_int,
77-
)
78-
})?;
79-
Ok(ret as usize)
80-
}
81-
82-
#[inline]
83-
fn is_read_vectored(&self) -> bool {
84-
true
85-
}
86-
87-
fn write(&self, buf: &[u8]) -> io::Result<usize> {
88-
let ret = cvt(unsafe {
89-
netc::write(self.fd, buf.as_ptr() as *const c_void, cmp::min(buf.len(), READ_LIMIT))
90-
})?;
91-
Ok(ret as usize)
92-
}
93-
94-
fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
95-
let ret = cvt(unsafe {
96-
netc::writev(
97-
self.fd,
98-
bufs.as_ptr() as *const netc::iovec,
99-
cmp::min(bufs.len(), max_iov()) as c_int,
100-
)
101-
})?;
102-
Ok(ret as usize)
103-
}
104-
105-
#[inline]
106-
fn is_write_vectored(&self) -> bool {
107-
true
108-
}
109-
110-
fn duplicate(&self) -> io::Result<FileDesc> {
111-
cvt(unsafe { netc::dup(self.fd) }).map(Self::new)
112-
}
113-
}
114-
115-
impl Drop for FileDesc {
116-
fn drop(&mut self) {
117-
unsafe { netc::close(self.fd) };
118-
}
119-
}
120-
12132
#[doc(hidden)]
12233
pub trait IsMinusOne {
12334
fn is_minus_one(&self) -> bool;
@@ -206,7 +117,7 @@ pub(super) fn decode_error_kind(er: abi::ER) -> ErrorKind {
206117

207118
pub fn init() {}
208119

209-
pub struct Socket(FileDesc);
120+
pub struct Socket(OwnedFd);
210121

211122
impl Socket {
212123
pub fn new(addr: &SocketAddr, ty: c_int) -> io::Result<Socket> {
@@ -220,16 +131,13 @@ impl Socket {
220131
pub fn new_raw(fam: c_int, ty: c_int) -> io::Result<Socket> {
221132
unsafe {
222133
let fd = cvt(netc::socket(fam, ty, 0))?;
223-
let fd = FileDesc::new(fd);
224-
let socket = Socket(fd);
225-
226-
Ok(socket)
134+
Ok(Self::from_raw_fd(fd))
227135
}
228136
}
229137

230138
pub fn connect(&self, addr: &SocketAddr) -> io::Result<()> {
231139
let (addr, len) = addr.into_inner();
232-
cvt(unsafe { netc::connect(self.0.raw(), addr.as_ptr(), len) })?;
140+
cvt(unsafe { netc::connect(self.as_raw_fd(), addr.as_ptr(), len) })?;
233141
Ok(())
234142
}
235143

@@ -258,14 +166,14 @@ impl Socket {
258166
timeout.tv_usec = 1;
259167
}
260168

261-
let fds = netc::fd_set { num_fds: 1, fds: [self.0.raw()] };
169+
let fds = netc::fd_set { num_fds: 1, fds: [self.as_raw_fd()] };
262170

263171
let mut writefds = fds;
264172
let mut errorfds = fds;
265173

266174
let n = unsafe {
267175
cvt(netc::select(
268-
self.0.raw() + 1,
176+
self.as_raw_fd() + 1,
269177
ptr::null_mut(),
270178
&mut writefds,
271179
&mut errorfds,
@@ -288,18 +196,17 @@ impl Socket {
288196
}
289197

290198
pub fn accept(&self, storage: *mut sockaddr, len: *mut socklen_t) -> io::Result<Socket> {
291-
let fd = cvt_r(|| unsafe { netc::accept(self.0.raw(), storage, len) })?;
292-
let fd = FileDesc::new(fd);
293-
Ok(Socket(fd))
199+
let fd = cvt_r(|| unsafe { netc::accept(self.as_raw_fd(), storage, len) })?;
200+
unsafe { Ok(Self::from_raw_fd(fd)) }
294201
}
295202

296203
pub fn duplicate(&self) -> io::Result<Socket> {
297-
self.0.duplicate().map(Socket)
204+
Ok(Self(self.0.try_clone()?))
298205
}
299206

300207
fn recv_with_flags(&self, mut buf: BorrowedCursor<'_>, flags: c_int) -> io::Result<()> {
301208
let ret = cvt(unsafe {
302-
netc::recv(self.0.raw(), buf.as_mut().as_mut_ptr().cast(), buf.capacity(), flags)
209+
netc::recv(self.as_raw_fd(), buf.as_mut().as_mut_ptr().cast(), buf.capacity(), flags)
303210
})?;
304211
unsafe {
305212
buf.advance(ret as usize);
@@ -324,12 +231,19 @@ impl Socket {
324231
}
325232

326233
pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
327-
self.0.read_vectored(bufs)
234+
let ret = cvt(unsafe {
235+
netc::readv(
236+
self.as_raw_fd(),
237+
bufs.as_ptr() as *const netc::iovec,
238+
cmp::min(bufs.len(), max_iov()) as c_int,
239+
)
240+
})?;
241+
Ok(ret as usize)
328242
}
329243

330244
#[inline]
331245
pub fn is_read_vectored(&self) -> bool {
332-
self.0.is_read_vectored()
246+
true
333247
}
334248

335249
fn recv_from_with_flags(
@@ -342,7 +256,7 @@ impl Socket {
342256

343257
let n = cvt(unsafe {
344258
netc::recvfrom(
345-
self.0.raw(),
259+
self.as_raw_fd(),
346260
buf.as_mut_ptr() as *mut c_void,
347261
buf.len(),
348262
flags,
@@ -362,16 +276,30 @@ impl Socket {
362276
}
363277

364278
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
365-
self.0.write(buf)
279+
let ret = cvt(unsafe {
280+
netc::write(
281+
self.as_raw_fd(),
282+
buf.as_ptr() as *const c_void,
283+
cmp::min(buf.len(), READ_LIMIT),
284+
)
285+
})?;
286+
Ok(ret as usize)
366287
}
367288

368289
pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
369-
self.0.write_vectored(bufs)
290+
let ret = cvt(unsafe {
291+
netc::writev(
292+
self.as_raw_fd(),
293+
bufs.as_ptr() as *const netc::iovec,
294+
cmp::min(bufs.len(), max_iov()) as c_int,
295+
)
296+
})?;
297+
Ok(ret as usize)
370298
}
371299

372300
#[inline]
373301
pub fn is_write_vectored(&self) -> bool {
374-
self.0.is_write_vectored()
302+
true
375303
}
376304

377305
pub fn set_timeout(&self, dur: Option<Duration>, kind: c_int) -> io::Result<()> {
@@ -417,7 +345,7 @@ impl Socket {
417345
Shutdown::Read => netc::SHUT_RD,
418346
Shutdown::Both => netc::SHUT_RDWR,
419347
};
420-
cvt(unsafe { netc::shutdown(self.0.raw(), how) })?;
348+
cvt(unsafe { netc::shutdown(self.as_raw_fd(), how) })?;
421349
Ok(())
422350
}
423351

@@ -448,7 +376,7 @@ impl Socket {
448376
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
449377
let mut nonblocking = nonblocking as c_int;
450378
cvt(unsafe {
451-
netc::ioctl(self.0.raw(), netc::FIONBIO, (&mut nonblocking) as *mut c_int as _)
379+
netc::ioctl(self.as_raw_fd(), netc::FIONBIO, (&mut nonblocking) as *mut c_int as _)
452380
})
453381
.map(drop)
454382
}
@@ -460,27 +388,27 @@ impl Socket {
460388

461389
// This method is used by sys_common code to abstract over targets.
462390
pub fn as_raw(&self) -> c_int {
463-
self.0.raw()
391+
self.as_raw_fd()
464392
}
465393
}
466394

467395
impl AsRawFd for Socket {
468396
#[inline]
469397
fn as_raw_fd(&self) -> c_int {
470-
self.0.fd
398+
self.0.as_raw_fd()
471399
}
472400
}
473401

474402
impl FromRawFd for Socket {
475403
#[inline]
476404
unsafe fn from_raw_fd(fd: c_int) -> Socket {
477-
Socket(FileDesc::new(fd))
405+
unsafe { Self(FromRawFd::from_raw_fd(fd)) }
478406
}
479407
}
480408

481409
impl IntoRawFd for Socket {
482410
#[inline]
483411
fn into_raw_fd(self) -> c_int {
484-
self.0.into_raw()
412+
self.0.into_raw_fd()
485413
}
486414
}

0 commit comments

Comments
 (0)