Skip to content

Commit 2a6f075

Browse files
committed
De-generify StreamWrapper and NegotiateSsl
This makes NegotiateSsl object safe which is convenient for things like r2d2.
1 parent ce4da6b commit 2a6f075

2 files changed

Lines changed: 43 additions & 28 deletions

File tree

src/io_util.rs

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,57 @@ use message::FrontendMessage::SslRequest;
1414

1515
const DEFAULT_PORT: u16 = 5432;
1616

17-
pub trait StreamWrapper<S: Read+Write>: Read+Write+Send {
18-
fn get_ref(&self) -> &S;
19-
fn get_mut(&mut self) -> &mut S;
17+
pub struct Stream(InternalStream);
18+
19+
impl Read for Stream {
20+
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
21+
self.0.read(buf)
22+
}
23+
}
24+
25+
impl Write for Stream {
26+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
27+
self.0.write(buf)
28+
}
29+
30+
fn flush(&mut self) -> io::Result<()> {
31+
self.0.flush()
32+
}
33+
}
34+
35+
impl StreamWrapper for Stream {
36+
fn get_ref(&self) -> &Stream {
37+
self
38+
}
39+
40+
fn get_mut(&mut self) -> &mut Stream {
41+
self
42+
}
2043
}
2144

22-
impl<S: Read+Write+Send> StreamWrapper<S> for SslStream<S> {
23-
fn get_ref(&self) -> &S {
45+
pub trait StreamWrapper: Read+Write+Send {
46+
fn get_ref(&self) -> &Stream;
47+
fn get_mut(&mut self) -> &mut Stream;
48+
}
49+
50+
impl StreamWrapper for SslStream<Stream> {
51+
fn get_ref(&self) -> &Stream {
2452
self.get_ref()
2553
}
2654

27-
fn get_mut(&mut self) -> &mut S {
55+
fn get_mut(&mut self) -> &mut Stream {
2856
self.get_mut()
2957
}
3058
}
3159

3260
pub trait NegotiateSsl {
33-
fn negotiate_ssl<S>(&mut self, host: &str, stream: S)
34-
-> Result<Box<StreamWrapper<S>>, Box<Error>>
35-
where S: Read+Write+Send+'static;
61+
fn negotiate_ssl(&mut self, host: &str, stream: Stream)
62+
-> Result<Box<StreamWrapper>, Box<Error>>;
3663
}
3764

3865
impl NegotiateSsl for SslContext {
39-
fn negotiate_ssl<S>(&mut self, _: &str, stream: S) -> Result<Box<StreamWrapper<S>>, Box<Error>>
40-
where S: Read+Write+Send+'static {
66+
fn negotiate_ssl(&mut self, _: &str, stream: Stream)
67+
-> Result<Box<StreamWrapper>, Box<Error>> {
4168
let stream = try!(SslStream::new(self, stream));
4269
Ok(Box::new(stream))
4370
}
@@ -56,8 +83,7 @@ pub enum SslMode<N = NoSsl> {
5683
pub enum NoSsl {}
5784

5885
impl NegotiateSsl for NoSsl {
59-
fn negotiate_ssl<S: Read+Write>(&mut self, _: &str, _: S)
60-
-> Result<Box<StreamWrapper<S>>, Box<Error>> {
86+
fn negotiate_ssl(&mut self, _: &str, _: Stream) -> Result<Box<StreamWrapper>, Box<Error>> {
6187
match *self {}
6288
}
6389
}
@@ -68,16 +94,6 @@ pub enum InternalStream {
6894
Unix(UnixStream),
6995
}
7096

71-
impl StreamWrapper<InternalStream> for InternalStream {
72-
fn get_ref(&self) -> &InternalStream {
73-
self
74-
}
75-
76-
fn get_mut(&mut self) -> &mut InternalStream {
77-
self
78-
}
79-
}
80-
8197
impl Read for InternalStream {
8298
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
8399
match *self {
@@ -122,9 +138,9 @@ fn open_socket(params: &ConnectParams) -> Result<InternalStream, ConnectError> {
122138
}
123139

124140
pub fn initialize_stream<N>(params: &ConnectParams, ssl: &mut SslMode<N>)
125-
-> Result<Box<StreamWrapper<InternalStream>>, ConnectError>
141+
-> Result<Box<StreamWrapper>, ConnectError>
126142
where N: NegotiateSsl {
127-
let mut socket = try!(open_socket(params));
143+
let mut socket = Stream(try!(open_socket(params)));
128144

129145
let (ssl_required, negotiator) = match *ssl {
130146
SslMode::None => return Ok(Box::new(socket)),

src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,10 @@ use std::path::PathBuf;
7979
pub use error::{Error, ConnectError, SqlState, DbError, ErrorPosition};
8080
#[doc(inline)]
8181
pub use types::{Oid, Type, Kind, ToSql, FromSql};
82-
pub use io_util::{SslMode, NegotiateSsl, StreamWrapper, NoSsl};
82+
pub use io_util::{SslMode, NegotiateSsl, StreamWrapper, NoSsl, Stream};
8383
use types::IsNull;
8484
#[doc(inline)]
8585
pub use types::Slice;
86-
use io_util::InternalStream;
8786
use message::BackendMessage::*;
8887
use message::FrontendMessage::*;
8988
use message::{FrontendMessage, BackendMessage, RowDescriptionEntry};
@@ -465,7 +464,7 @@ struct CachedStatement {
465464
}
466465

467466
struct InnerConnection {
468-
stream: BufStream<Box<StreamWrapper<InternalStream>>>,
467+
stream: BufStream<Box<StreamWrapper>>,
469468
notice_handler: Box<HandleNotice>,
470469
notifications: VecDeque<Notification>,
471470
cancel_data: CancelData,

0 commit comments

Comments
 (0)