Skip to content

Commit a12812b

Browse files
committed
Refactor From impls for Socket
1 parent 7e05419 commit a12812b

File tree

3 files changed

+64
-160
lines changed

3 files changed

+64
-160
lines changed

src/socket.rs

Lines changed: 24 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ use std::io::{self, Read, Write};
1111
#[cfg(not(target_os = "redox"))]
1212
use std::io::{IoSlice, IoSliceMut};
1313
use std::net::{self, Ipv4Addr, Ipv6Addr, Shutdown};
14-
#[cfg(all(feature = "all", unix))]
15-
use std::os::unix::net::{UnixDatagram, UnixListener, UnixStream};
14+
#[cfg(unix)]
15+
use std::os::unix::io::{FromRawFd, IntoRawFd};
16+
#[cfg(windows)]
17+
use std::os::windows::io::{FromRawSocket, IntoRawSocket};
1618
use std::time::Duration;
1719

1820
use crate::sys;
@@ -978,93 +980,53 @@ impl fmt::Debug for Socket {
978980
}
979981
}
980982

983+
/// Macro to convert from one network type to another.
984+
macro_rules! from_raw {
985+
($ty: ty, $socket: expr) => {{
986+
#[cfg(unix)]
987+
unsafe {
988+
<$ty>::from_raw_fd($socket.into_raw_fd())
989+
}
990+
#[cfg(windows)]
991+
unsafe {
992+
<$ty>::from_raw_socket($socket.into_raw_socket())
993+
}
994+
}};
995+
}
996+
981997
impl From<net::TcpStream> for Socket {
982998
fn from(socket: net::TcpStream) -> Socket {
983-
Socket {
984-
inner: sys::Socket::from(socket).inner(),
985-
}
999+
from_raw!(Socket, socket)
9861000
}
9871001
}
9881002

9891003
impl From<net::TcpListener> for Socket {
9901004
fn from(socket: net::TcpListener) -> Socket {
991-
Socket {
992-
inner: sys::Socket::from(socket).inner(),
993-
}
1005+
from_raw!(Socket, socket)
9941006
}
9951007
}
9961008

9971009
impl From<net::UdpSocket> for Socket {
9981010
fn from(socket: net::UdpSocket) -> Socket {
999-
Socket {
1000-
inner: sys::Socket::from(socket).inner(),
1001-
}
1002-
}
1003-
}
1004-
1005-
#[cfg(all(feature = "all", unix))]
1006-
impl From<UnixStream> for Socket {
1007-
fn from(socket: UnixStream) -> Socket {
1008-
Socket {
1009-
inner: sys::Socket::from(socket).inner(),
1010-
}
1011-
}
1012-
}
1013-
1014-
#[cfg(all(feature = "all", unix))]
1015-
impl From<UnixListener> for Socket {
1016-
fn from(socket: UnixListener) -> Socket {
1017-
Socket {
1018-
inner: sys::Socket::from(socket).inner(),
1019-
}
1020-
}
1021-
}
1022-
1023-
#[cfg(all(feature = "all", unix))]
1024-
impl From<UnixDatagram> for Socket {
1025-
fn from(socket: UnixDatagram) -> Socket {
1026-
Socket {
1027-
inner: sys::Socket::from(socket).inner(),
1028-
}
1011+
from_raw!(Socket, socket)
10291012
}
10301013
}
10311014

10321015
impl From<Socket> for net::TcpStream {
10331016
fn from(socket: Socket) -> net::TcpStream {
1034-
sys::Socket::from_inner(socket.inner).into()
1017+
from_raw!(net::TcpStream, socket)
10351018
}
10361019
}
10371020

10381021
impl From<Socket> for net::TcpListener {
10391022
fn from(socket: Socket) -> net::TcpListener {
1040-
sys::Socket::from_inner(socket.inner).into()
1023+
from_raw!(net::TcpListener, socket)
10411024
}
10421025
}
10431026

10441027
impl From<Socket> for net::UdpSocket {
10451028
fn from(socket: Socket) -> net::UdpSocket {
1046-
sys::Socket::from_inner(socket.inner).into()
1047-
}
1048-
}
1049-
1050-
#[cfg(all(feature = "all", unix))]
1051-
impl From<Socket> for UnixStream {
1052-
fn from(socket: Socket) -> UnixStream {
1053-
sys::Socket::from_inner(socket.inner).into()
1054-
}
1055-
}
1056-
1057-
#[cfg(all(feature = "all", unix))]
1058-
impl From<Socket> for UnixListener {
1059-
fn from(socket: Socket) -> UnixListener {
1060-
sys::Socket::from_inner(socket.inner).into()
1061-
}
1062-
}
1063-
1064-
#[cfg(all(feature = "all", unix))]
1065-
impl From<Socket> for UnixDatagram {
1066-
fn from(socket: Socket) -> UnixDatagram {
1067-
sys::Socket::from_inner(socket.inner).into()
1029+
from_raw!(net::UdpSocket, socket)
10681030
}
10691031
}
10701032

src/sys/unix.rs

Lines changed: 37 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ use std::cmp::min;
1111
use std::io::{IoSlice, IoSliceMut};
1212
use std::mem::{self, size_of, size_of_val, MaybeUninit};
1313
use std::net::Shutdown;
14-
use std::net::{self, Ipv4Addr, Ipv6Addr};
14+
use std::net::{Ipv4Addr, Ipv6Addr};
1515
#[cfg(feature = "all")]
1616
use std::os::unix::ffi::OsStrExt;
1717
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd};
18+
#[cfg(feature = "all")]
1819
use std::os::unix::net::{UnixDatagram, UnixListener, UnixStream};
1920
#[cfg(feature = "all")]
2021
use std::path::Path;
@@ -975,15 +976,21 @@ impl Socket {
975976
}
976977
}
977978

978-
#[cfg(not(any(target_os = "solaris", target_os = "illumos")))]
979+
#[cfg(all(
980+
feature = "all",
981+
not(any(target_os = "solaris", target_os = "illumos"))
982+
))]
979983
pub fn reuse_port(&self) -> io::Result<bool> {
980984
unsafe {
981985
let raw: c_int = self.getsockopt(libc::SOL_SOCKET, libc::SO_REUSEPORT)?;
982986
Ok(raw != 0)
983987
}
984988
}
985989

986-
#[cfg(not(any(target_os = "solaris", target_os = "illumos")))]
990+
#[cfg(all(
991+
feature = "all",
992+
not(any(target_os = "solaris", target_os = "illumos"))
993+
))]
987994
pub fn set_reuse_port(&self, reuse: bool) -> io::Result<()> {
988995
unsafe { self.setsockopt(libc::SOL_SOCKET, libc::SO_REUSEPORT, reuse as c_int) }
989996
}
@@ -1033,10 +1040,6 @@ impl Socket {
10331040
pub fn inner(self) -> SysSocket {
10341041
self.fd
10351042
}
1036-
1037-
pub fn from_inner(fd: SysSocket) -> Socket {
1038-
Socket { fd }
1039-
}
10401043
}
10411044

10421045
impl fmt::Debug for Socket {
@@ -1095,75 +1098,51 @@ impl FromRawFd for crate::Socket {
10951098
}
10961099
}
10971100

1098-
impl From<Socket> for net::TcpStream {
1099-
fn from(socket: Socket) -> net::TcpStream {
1100-
unsafe { net::TcpStream::from_raw_fd(socket.into_raw_fd()) }
1101-
}
1102-
}
1103-
1104-
impl From<Socket> for net::TcpListener {
1105-
fn from(socket: Socket) -> net::TcpListener {
1106-
unsafe { net::TcpListener::from_raw_fd(socket.into_raw_fd()) }
1107-
}
1108-
}
1109-
1110-
impl From<Socket> for net::UdpSocket {
1111-
fn from(socket: Socket) -> net::UdpSocket {
1112-
unsafe { net::UdpSocket::from_raw_fd(socket.into_raw_fd()) }
1113-
}
1114-
}
1115-
1116-
impl From<Socket> for UnixStream {
1117-
fn from(socket: Socket) -> UnixStream {
1101+
#[cfg(feature = "all")]
1102+
impl From<crate::Socket> for UnixStream {
1103+
fn from(socket: crate::Socket) -> UnixStream {
11181104
unsafe { UnixStream::from_raw_fd(socket.into_raw_fd()) }
11191105
}
11201106
}
11211107

1122-
impl From<Socket> for UnixListener {
1123-
fn from(socket: Socket) -> UnixListener {
1108+
#[cfg(feature = "all")]
1109+
impl From<crate::Socket> for UnixListener {
1110+
fn from(socket: crate::Socket) -> UnixListener {
11241111
unsafe { UnixListener::from_raw_fd(socket.into_raw_fd()) }
11251112
}
11261113
}
11271114

1128-
impl From<Socket> for UnixDatagram {
1129-
fn from(socket: Socket) -> UnixDatagram {
1115+
#[cfg(feature = "all")]
1116+
impl From<crate::Socket> for UnixDatagram {
1117+
fn from(socket: crate::Socket) -> UnixDatagram {
11301118
unsafe { UnixDatagram::from_raw_fd(socket.into_raw_fd()) }
11311119
}
11321120
}
11331121

1134-
impl From<net::TcpStream> for Socket {
1135-
fn from(socket: net::TcpStream) -> Socket {
1136-
unsafe { Socket::from_raw_fd(socket.into_raw_fd()) }
1137-
}
1138-
}
1139-
1140-
impl From<net::TcpListener> for Socket {
1141-
fn from(socket: net::TcpListener) -> Socket {
1142-
unsafe { Socket::from_raw_fd(socket.into_raw_fd()) }
1143-
}
1144-
}
1145-
1146-
impl From<net::UdpSocket> for Socket {
1147-
fn from(socket: net::UdpSocket) -> Socket {
1148-
unsafe { Socket::from_raw_fd(socket.into_raw_fd()) }
1149-
}
1150-
}
1151-
1152-
impl From<UnixStream> for Socket {
1153-
fn from(socket: UnixStream) -> Socket {
1154-
unsafe { Socket::from_raw_fd(socket.into_raw_fd()) }
1122+
#[cfg(feature = "all")]
1123+
impl From<UnixStream> for crate::Socket {
1124+
fn from(socket: UnixStream) -> crate::Socket {
1125+
crate::Socket {
1126+
inner: socket.into_raw_fd(),
1127+
}
11551128
}
11561129
}
11571130

1158-
impl From<UnixListener> for Socket {
1159-
fn from(socket: UnixListener) -> Socket {
1160-
unsafe { Socket::from_raw_fd(socket.into_raw_fd()) }
1131+
#[cfg(feature = "all")]
1132+
impl From<UnixListener> for crate::Socket {
1133+
fn from(socket: UnixListener) -> crate::Socket {
1134+
crate::Socket {
1135+
inner: socket.into_raw_fd(),
1136+
}
11611137
}
11621138
}
11631139

1164-
impl From<UnixDatagram> for Socket {
1165-
fn from(socket: UnixDatagram) -> Socket {
1166-
unsafe { Socket::from_raw_fd(socket.into_raw_fd()) }
1140+
#[cfg(feature = "all")]
1141+
impl From<UnixDatagram> for crate::Socket {
1142+
fn from(socket: UnixDatagram) -> crate::Socket {
1143+
crate::Socket {
1144+
inner: socket.into_raw_fd(),
1145+
}
11671146
}
11681147
}
11691148

src/sys/windows.rs

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -842,13 +842,15 @@ impl Socket {
842842
}
843843
}
844844

845+
#[cfg(feature = "all")]
845846
pub fn out_of_band_inline(&self) -> io::Result<bool> {
846847
unsafe {
847848
let raw: c_int = self.getsockopt(SOL_SOCKET, SO_OOBINLINE)?;
848849
Ok(raw != 0)
849850
}
850851
}
851852

853+
#[cfg(feature = "all")]
852854
pub fn set_out_of_band_inline(&self, oob_inline: bool) -> io::Result<()> {
853855
unsafe { self.setsockopt(SOL_SOCKET, SO_OOBINLINE, oob_inline as c_int) }
854856
}
@@ -886,10 +888,6 @@ impl Socket {
886888
pub fn inner(self) -> SysSocket {
887889
self.socket
888890
}
889-
890-
pub fn from_inner(socket: SysSocket) -> Socket {
891-
Socket { socket }
892-
}
893891
}
894892

895893
impl fmt::Debug for Socket {
@@ -950,42 +948,6 @@ impl FromRawSocket for crate::Socket {
950948
}
951949
}
952950

953-
impl From<Socket> for net::TcpStream {
954-
fn from(socket: Socket) -> net::TcpStream {
955-
unsafe { net::TcpStream::from_raw_socket(socket.into_raw_socket()) }
956-
}
957-
}
958-
959-
impl From<Socket> for net::TcpListener {
960-
fn from(socket: Socket) -> net::TcpListener {
961-
unsafe { net::TcpListener::from_raw_socket(socket.into_raw_socket()) }
962-
}
963-
}
964-
965-
impl From<Socket> for net::UdpSocket {
966-
fn from(socket: Socket) -> net::UdpSocket {
967-
unsafe { net::UdpSocket::from_raw_socket(socket.into_raw_socket()) }
968-
}
969-
}
970-
971-
impl From<net::TcpStream> for Socket {
972-
fn from(socket: net::TcpStream) -> Socket {
973-
unsafe { Socket::from_raw_socket(socket.into_raw_socket()) }
974-
}
975-
}
976-
977-
impl From<net::TcpListener> for Socket {
978-
fn from(socket: net::TcpListener) -> Socket {
979-
unsafe { Socket::from_raw_socket(socket.into_raw_socket()) }
980-
}
981-
}
982-
983-
impl From<net::UdpSocket> for Socket {
984-
fn from(socket: net::UdpSocket) -> Socket {
985-
unsafe { Socket::from_raw_socket(socket.into_raw_socket()) }
986-
}
987-
}
988-
989951
pub(crate) fn close(socket: SysSocket) {
990952
unsafe {
991953
let _ = sock::closesocket(socket);
@@ -1124,6 +1086,7 @@ fn test_ipv6() {
11241086
}
11251087

11261088
#[test]
1089+
#[cfg(feature = "all")]
11271090
fn test_out_of_band_inline() {
11281091
let tcp = Socket {
11291092
socket: socket(AF_INET, SOCK_STREAM, 0).unwrap(),

0 commit comments

Comments
 (0)