Skip to content

Commit d146cdb

Browse files
committed
Remove nullable_socket impl now that socket2::Socket has a niche
1 parent 78c0f7b commit d146cdb

File tree

1 file changed

+39
-90
lines changed

1 file changed

+39
-90
lines changed

stdlib/src/socket.rs

Lines changed: 39 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -738,72 +738,6 @@ mod _socket {
738738
.map(|sock| sock as RawSocket)
739739
}
740740

741-
#[cfg(unix)]
742-
mod nullable_socket {
743-
use super::*;
744-
use std::os::unix::io::AsRawFd;
745-
746-
#[derive(Debug)]
747-
#[repr(transparent)]
748-
pub struct NullableSocket(Option<socket2::Socket>);
749-
impl From<socket2::Socket> for NullableSocket {
750-
fn from(sock: socket2::Socket) -> Self {
751-
NullableSocket(Some(sock))
752-
}
753-
}
754-
impl NullableSocket {
755-
pub fn invalid() -> Self {
756-
Self(None)
757-
}
758-
pub fn get(&self) -> Option<&socket2::Socket> {
759-
self.0.as_ref()
760-
}
761-
pub fn fd(&self) -> RawSocket {
762-
self.get().map_or(INVALID_SOCKET, |sock| sock.as_raw_fd())
763-
}
764-
pub fn insert(&mut self, sock: socket2::Socket) -> &mut socket2::Socket {
765-
self.0.insert(sock)
766-
}
767-
}
768-
}
769-
#[cfg(windows)]
770-
mod nullable_socket {
771-
use super::*;
772-
use std::os::windows::io::{AsRawSocket, FromRawSocket};
773-
774-
// TODO: may change if windows changes its TcpStream repr
775-
#[derive(Debug)]
776-
#[repr(transparent)]
777-
pub struct NullableSocket(socket2::Socket);
778-
impl From<socket2::Socket> for NullableSocket {
779-
fn from(sock: socket2::Socket) -> Self {
780-
NullableSocket(sock)
781-
}
782-
}
783-
impl NullableSocket {
784-
pub fn invalid() -> Self {
785-
// TODO: may become UB in the future; maybe see rust-lang/rust#74699
786-
Self(unsafe { socket2::Socket::from_raw_socket(INVALID_SOCKET) })
787-
}
788-
pub fn get(&self) -> Option<&socket2::Socket> {
789-
(self.0.as_raw_socket() != INVALID_SOCKET).then(|| &self.0)
790-
}
791-
pub fn fd(&self) -> RawSocket {
792-
self.0.as_raw_socket()
793-
}
794-
pub fn insert(&mut self, sock: socket2::Socket) -> &mut socket2::Socket {
795-
self.0 = sock;
796-
&mut self.0
797-
}
798-
}
799-
}
800-
use nullable_socket::NullableSocket;
801-
impl Default for NullableSocket {
802-
fn default() -> Self {
803-
Self::invalid()
804-
}
805-
}
806-
807741
#[pyattr(name = "socket")]
808742
#[pyattr(name = "SocketType")]
809743
#[pyclass(name = "socket")]
@@ -813,17 +747,19 @@ mod _socket {
813747
family: AtomicCell<i32>,
814748
proto: AtomicCell<i32>,
815749
pub(crate) timeout: AtomicCell<f64>,
816-
sock: PyRwLock<NullableSocket>,
750+
sock: PyRwLock<Option<Socket>>,
817751
}
818752

753+
const _: () = assert!(std::mem::size_of::<Option<Socket>>() == std::mem::size_of::<Socket>());
754+
819755
impl Default for PySocket {
820756
fn default() -> Self {
821757
PySocket {
822758
kind: AtomicCell::default(),
823759
family: AtomicCell::default(),
824760
proto: AtomicCell::default(),
825761
timeout: AtomicCell::new(-1.0),
826-
sock: PyRwLock::new(NullableSocket::invalid()),
762+
sock: PyRwLock::new(None),
827763
}
828764
}
829765
}
@@ -850,7 +786,7 @@ mod _socket {
850786

851787
impl PySocket {
852788
pub fn sock_opt(&self) -> Option<PyMappedRwLockReadGuard<'_, Socket>> {
853-
PyRwLockReadGuard::try_map(self.sock.read(), |sock| sock.get()).ok()
789+
PyRwLockReadGuard::try_map(self.sock.read(), |sock| sock.as_ref()).ok()
854790
}
855791

856792
pub fn sock(&self) -> io::Result<PyMappedRwLockReadGuard<'_, Socket>> {
@@ -1406,13 +1342,16 @@ mod _socket {
14061342
#[pymethod]
14071343
#[inline]
14081344
fn detach(&self) -> RawSocket {
1409-
let sock = std::mem::replace(&mut *self.sock.write(), NullableSocket::invalid());
1410-
std::mem::ManuallyDrop::new(sock).fd()
1345+
let sock = self.sock.write().take();
1346+
sock.map_or(INVALID_SOCKET, into_sock_fileno)
14111347
}
14121348

14131349
#[pymethod]
14141350
fn fileno(&self) -> RawSocket {
1415-
self.sock.read().fd()
1351+
self.sock
1352+
.read()
1353+
.as_ref()
1354+
.map_or(INVALID_SOCKET, sock_fileno)
14161355
}
14171356

14181357
#[pymethod]
@@ -1465,38 +1404,47 @@ mod _socket {
14651404
name: i32,
14661405
buflen: OptionalArg<i32>,
14671406
vm: &VirtualMachine,
1468-
) -> PyResult {
1469-
let fd = self.sock.read().fd() as _;
1407+
) -> Result<PyObjectRef, IoOrPyException> {
1408+
let sock = self.sock()?;
1409+
let fd = sock_fileno(&sock);
14701410
let buflen = buflen.unwrap_or(0);
14711411
if buflen == 0 {
14721412
let mut flag: libc::c_int = 0;
14731413
let mut flagsize = std::mem::size_of::<libc::c_int>() as _;
14741414
let ret = unsafe {
14751415
c::getsockopt(
1476-
fd,
1416+
fd as _,
14771417
level,
14781418
name,
14791419
&mut flag as *mut libc::c_int as *mut _,
14801420
&mut flagsize,
14811421
)
14821422
};
14831423
if ret < 0 {
1484-
return Err(crate::vm::stdlib::os::errno_err(vm));
1424+
return Err(crate::common::os::errno().into());
14851425
}
14861426
Ok(vm.ctx.new_int(flag).into())
14871427
} else {
14881428
if buflen <= 0 || buflen > 1024 {
1489-
return Err(vm.new_os_error("getsockopt buflen out of range".to_owned()));
1429+
return Err(vm
1430+
.new_os_error("getsockopt buflen out of range".to_owned())
1431+
.into());
14901432
}
14911433
let mut buf = vec![0u8; buflen as usize];
14921434
let mut buflen = buflen as _;
14931435
let ret = unsafe {
1494-
c::getsockopt(fd, level, name, buf.as_mut_ptr() as *mut _, &mut buflen)
1436+
c::getsockopt(
1437+
fd as _,
1438+
level,
1439+
name,
1440+
buf.as_mut_ptr() as *mut _,
1441+
&mut buflen,
1442+
)
14951443
};
1496-
buf.truncate(buflen as usize);
14971444
if ret < 0 {
1498-
return Err(crate::vm::stdlib::os::errno_err(vm));
1445+
return Err(crate::common::os::errno().into());
14991446
}
1447+
buf.truncate(buflen as usize);
15001448
Ok(vm.ctx.new_bytes(buf).into())
15011449
}
15021450
}
@@ -1509,32 +1457,33 @@ mod _socket {
15091457
value: Option<Either<ArgBytesLike, i32>>,
15101458
optlen: OptionalArg<u32>,
15111459
vm: &VirtualMachine,
1512-
) -> PyResult<()> {
1513-
let fd = self.sock.read().fd() as _;
1460+
) -> Result<(), IoOrPyException> {
1461+
let sock = self.sock()?;
1462+
let fd = sock_fileno(&sock);
15141463
let ret = match (value, optlen) {
15151464
(Some(Either::A(b)), OptionalArg::Missing) => b.with_ref(|b| unsafe {
1516-
c::setsockopt(fd, level, name, b.as_ptr() as *const _, b.len() as _)
1465+
c::setsockopt(fd as _, level, name, b.as_ptr() as *const _, b.len() as _)
15171466
}),
15181467
(Some(Either::B(ref val)), OptionalArg::Missing) => unsafe {
15191468
c::setsockopt(
1520-
fd,
1469+
fd as _,
15211470
level,
15221471
name,
15231472
val as *const i32 as *const _,
15241473
std::mem::size_of::<i32>() as _,
15251474
)
15261475
},
15271476
(None, OptionalArg::Present(optlen)) => unsafe {
1528-
c::setsockopt(fd, level, name, std::ptr::null(), optlen as _)
1477+
c::setsockopt(fd as _, level, name, std::ptr::null(), optlen as _)
15291478
},
15301479
_ => {
1531-
return Err(
1532-
vm.new_type_error("expected the value arg xor the optlen arg".to_owned())
1533-
);
1480+
return Err(vm
1481+
.new_type_error("expected the value arg xor the optlen arg".to_owned())
1482+
.into());
15341483
}
15351484
};
15361485
if ret < 0 {
1537-
Err(crate::vm::stdlib::os::errno_err(vm))
1486+
Err(crate::common::os::errno().into())
15381487
} else {
15391488
Ok(())
15401489
}
@@ -1573,7 +1522,7 @@ mod _socket {
15731522
format!(
15741523
"<socket object, fd={}, family={}, type={}, proto={}>",
15751524
// cast because INVALID_SOCKET is unsigned, so would show usize::MAX instead of -1
1576-
self.sock.read().fd() as i64,
1525+
self.fileno() as i64,
15771526
self.family.load(),
15781527
self.kind.load(),
15791528
self.proto.load(),

0 commit comments

Comments
 (0)