Skip to content

Commit afbaacf

Browse files
authored
Merge pull request #5 from sfackler/into-unix
Add conversions to/from Unix sockets
2 parents 3ad8ef1 + 0bf7b5b commit afbaacf

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

src/socket.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use std::fmt;
1212
use std::io::{self, Read, Write};
1313
use std::net::{self, Ipv4Addr, Ipv6Addr, Shutdown};
1414
use std::time::Duration;
15+
#[cfg(all(unix, feature = "unix"))]
16+
use std::os::unix::net::{UnixDatagram, UnixListener, UnixStream};
1517

1618
#[cfg(unix)]
1719
use libc as c;
@@ -66,6 +68,33 @@ impl Socket {
6668
self.into()
6769
}
6870

71+
/// Consumes this `Socket`, converting it into a `UnixStream`.
72+
///
73+
/// This function is only available on Unix when the `unix` feature is
74+
/// enabled.
75+
#[cfg(all(unix, feature = "unix"))]
76+
pub fn into_unix_stream(self) -> UnixStream {
77+
self.into()
78+
}
79+
80+
/// Consumes this `Socket`, converting it into a `UnixListener`.
81+
///
82+
/// This function is only available on Unix when the `unix` feature is
83+
/// enabled.
84+
#[cfg(all(unix, feature = "unix"))]
85+
pub fn into_unix_listener(self) -> UnixListener {
86+
self.into()
87+
}
88+
89+
/// Consumes this `Socket`, converting it into a `UnixDatagram`.
90+
///
91+
/// This function is only available on Unix when the `unix` feature is
92+
/// enabled.
93+
#[cfg(all(unix, feature = "unix"))]
94+
pub fn into_unix_datagram(self) -> UnixDatagram {
95+
self.into()
96+
}
97+
6998
/// Initiate a connection on this socket to the specified address.
7099
///
71100
/// This function directly corresponds to the connect(2) function on Windows
@@ -614,6 +643,27 @@ impl From<net::UdpSocket> for Socket {
614643
}
615644
}
616645

646+
#[cfg(all(unix, feature = "unix"))]
647+
impl From<UnixStream> for Socket {
648+
fn from(socket: UnixStream) -> Socket {
649+
Socket { inner: socket.into() }
650+
}
651+
}
652+
653+
#[cfg(all(unix, feature = "unix"))]
654+
impl From<UnixListener> for Socket {
655+
fn from(socket: UnixListener) -> Socket {
656+
Socket { inner: socket.into() }
657+
}
658+
}
659+
660+
#[cfg(all(unix, feature = "unix"))]
661+
impl From<UnixDatagram> for Socket {
662+
fn from(socket: UnixDatagram) -> Socket {
663+
Socket { inner: socket.into() }
664+
}
665+
}
666+
617667
impl From<Socket> for net::TcpStream {
618668
fn from(socket: Socket) -> net::TcpStream {
619669
socket.inner.into()
@@ -632,6 +682,27 @@ impl From<Socket> for net::UdpSocket {
632682
}
633683
}
634684

685+
#[cfg(all(unix, feature = "unix"))]
686+
impl From<Socket> for UnixStream {
687+
fn from(socket: Socket) -> UnixStream {
688+
socket.inner.into()
689+
}
690+
}
691+
692+
#[cfg(all(unix, feature = "unix"))]
693+
impl From<Socket> for UnixListener {
694+
fn from(socket: Socket) -> UnixListener {
695+
socket.inner.into()
696+
}
697+
}
698+
699+
#[cfg(all(unix, feature = "unix"))]
700+
impl From<Socket> for UnixDatagram {
701+
fn from(socket: Socket) -> UnixDatagram {
702+
socket.inner.into()
703+
}
704+
}
705+
635706
impl Domain {
636707
/// Domain for IPv4 communication, corresponding to `AF_INET`.
637708
pub fn ipv4() -> Domain {

src/sys/unix/mod.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use std::ops::Neg;
1919
use std::os::unix::prelude::*;
2020
use std::sync::atomic::{AtomicBool, Ordering, ATOMIC_BOOL_INIT};
2121
use std::time::{Duration, Instant};
22+
#[cfg(feature = "unix")]
23+
use std::os::unix::net::{UnixDatagram, UnixListener, UnixStream};
2224

2325
use libc::{self, c_void, c_int};
2426
use libc::{sockaddr, socklen_t, ssize_t};
@@ -830,6 +832,27 @@ impl From<Socket> for net::UdpSocket {
830832
}
831833
}
832834

835+
#[cfg(all(unix, feature = "unix"))]
836+
impl From<Socket> for UnixStream {
837+
fn from(socket: Socket) -> UnixStream {
838+
unsafe { UnixStream::from_raw_fd(socket.into_raw_fd()) }
839+
}
840+
}
841+
842+
#[cfg(all(unix, feature = "unix"))]
843+
impl From<Socket> for UnixListener {
844+
fn from(socket: Socket) -> UnixListener {
845+
unsafe { UnixListener::from_raw_fd(socket.into_raw_fd()) }
846+
}
847+
}
848+
849+
#[cfg(all(unix, feature = "unix"))]
850+
impl From<Socket> for UnixDatagram {
851+
fn from(socket: Socket) -> UnixDatagram {
852+
unsafe { UnixDatagram::from_raw_fd(socket.into_raw_fd()) }
853+
}
854+
}
855+
833856
impl From<net::TcpStream> for Socket {
834857
fn from(socket: net::TcpStream) -> Socket {
835858
unsafe { Socket::from_raw_fd(socket.into_raw_fd()) }
@@ -848,6 +871,27 @@ impl From<net::UdpSocket> for Socket {
848871
}
849872
}
850873

874+
#[cfg(all(unix, feature = "unix"))]
875+
impl From<UnixStream> for Socket {
876+
fn from(socket: UnixStream) -> Socket {
877+
unsafe { Socket::from_raw_fd(socket.into_raw_fd()) }
878+
}
879+
}
880+
881+
#[cfg(all(unix, feature = "unix"))]
882+
impl From<UnixListener> for Socket {
883+
fn from(socket: UnixListener) -> Socket {
884+
unsafe { Socket::from_raw_fd(socket.into_raw_fd()) }
885+
}
886+
}
887+
888+
#[cfg(all(unix, feature = "unix"))]
889+
impl From<UnixDatagram> for Socket {
890+
fn from(socket: UnixDatagram) -> Socket {
891+
unsafe { Socket::from_raw_fd(socket.into_raw_fd()) }
892+
}
893+
}
894+
851895
fn max_len() -> usize {
852896
// The maximum read limit on most posix-like systems is `SSIZE_MAX`,
853897
// with the man page quoting that if the count of bytes to read is

0 commit comments

Comments
 (0)