Skip to content

Commit f8ccb82

Browse files
committed
Implement AsRawFd/AsRawHandle for Stream
1 parent 9a19f01 commit f8ccb82

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

src/priv_io.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ use std::io::prelude::*;
44
use std::net::TcpStream;
55
#[cfg(feature = "unix_socket")]
66
use unix_socket::UnixStream;
7+
#[cfg(unix)]
8+
use std::os::unix::io::{AsRawFd, RawFd};
9+
#[cfg(windows)]
10+
use std::os::windows::io::{AsRawHandle, RawHandle};
711

812
use {SslMode, ConnectError, ConnectParams, ConnectTarget};
913
use io::{NegotiateSsl, StreamWrapper};
@@ -13,6 +17,9 @@ use message::FrontendMessage::SslRequest;
1317
const DEFAULT_PORT: u16 = 5432;
1418

1519
/// A connection to the Postgres server.
20+
///
21+
/// It implements `Read`, `Write` and `StreamWrapper`, as well as `AsRawFd` on
22+
/// Unix platforms and `AsRawHandle` on Windows platforms.
1623
pub struct Stream(InternalStream);
1724

1825
impl Read for Stream {
@@ -41,6 +48,27 @@ impl StreamWrapper for Stream {
4148
}
4249
}
4350

51+
#[cfg(unix)]
52+
impl AsRawFd for Stream {
53+
fn as_raw_fd(&self) -> RawFd {
54+
match self.0 {
55+
InternalStream::Tcp(ref s) => s.as_raw_fd(),
56+
#[cfg(feature = "unix_socket")]
57+
InternalStream::Unix(ref s) => s.as_raw_fd(),
58+
}
59+
}
60+
}
61+
62+
#[cfg(windows)]
63+
impl AsRawHandle for Stream {
64+
fn as_raw_handle(&self) -> RawHandle {
65+
// Unix sockets aren't supported on windows, so no need to match
66+
match self.0 {
67+
InternalStream::Tcp(ref s) => s.as_raw_handle(),
68+
}
69+
}
70+
}
71+
4472
enum InternalStream {
4573
Tcp(TcpStream),
4674
#[cfg(feature = "unix_socket")]

0 commit comments

Comments
 (0)