@@ -4,6 +4,10 @@ use std::io::prelude::*;
44use std:: net:: TcpStream ;
55#[ cfg( feature = "unix_socket" ) ]
66use 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
812use { SslMode , ConnectError , ConnectParams , ConnectTarget } ;
913use io:: { NegotiateSsl , StreamWrapper } ;
@@ -13,6 +17,9 @@ use message::FrontendMessage::SslRequest;
1317const 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.
1623pub struct Stream ( InternalStream ) ;
1724
1825impl 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+
4472enum InternalStream {
4573 Tcp ( TcpStream ) ,
4674 #[ cfg( feature = "unix_socket" ) ]
0 commit comments