Skip to content

Commit 0e60d80

Browse files
committed
Parameterize Connection over the stream
1 parent 04bd98e commit 0e60d80

3 files changed

Lines changed: 27 additions & 14 deletions

File tree

tokio-postgres/src/lib.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use postgres_shared::rows::RowIndex;
2828
use std::error::Error as StdError;
2929
use std::fmt;
3030
use std::sync::atomic::{AtomicUsize, Ordering};
31+
use tokio_io::{AsyncRead, AsyncWrite};
3132

3233
#[doc(inline)]
3334
pub use postgres_shared::stmt::Column;
@@ -38,7 +39,7 @@ pub use postgres_shared::{CancelData, Notification};
3839

3940
use error::{DbError, Error};
4041
use params::ConnectParams;
41-
use tls::TlsConnect;
42+
use tls::{TlsConnect, TlsStream};
4243
use types::{FromSql, ToSql, Type};
4344

4445
pub mod error;
@@ -125,9 +126,12 @@ impl Client {
125126
}
126127

127128
#[must_use = "futures do nothing unless polled"]
128-
pub struct Connection(proto::Connection);
129+
pub struct Connection<S>(proto::Connection<S>);
129130

130-
impl Connection {
131+
impl<S> Connection<S>
132+
where
133+
S: AsyncRead + AsyncWrite,
134+
{
131135
pub fn cancel_data(&self) -> CancelData {
132136
self.0.cancel_data()
133137
}
@@ -141,7 +145,10 @@ impl Connection {
141145
}
142146
}
143147

144-
impl Future for Connection {
148+
impl<S> Future for Connection<S>
149+
where
150+
S: AsyncRead + AsyncWrite,
151+
{
145152
type Item = ();
146153
type Error = Error;
147154

@@ -173,10 +180,10 @@ impl Future for CancelQuery {
173180
pub struct Handshake(proto::HandshakeFuture);
174181

175182
impl Future for Handshake {
176-
type Item = (Client, Connection);
183+
type Item = (Client, Connection<Box<TlsStream>>);
177184
type Error = Error;
178185

179-
fn poll(&mut self) -> Poll<(Client, Connection), Error> {
186+
fn poll(&mut self) -> Poll<(Client, Connection<Box<TlsStream>>), Error> {
180187
let (client, connection) = try_ready!(self.0.poll());
181188

182189
Ok(Async::Ready((Client(client), Connection(connection))))

tokio-postgres/src/proto/connection.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use postgres_protocol::message::frontend;
55
use std::collections::{HashMap, VecDeque};
66
use std::io;
77
use tokio_codec::Framed;
8+
use tokio_io::{AsyncRead, AsyncWrite};
89

910
use proto::codec::PostgresCodec;
1011
use proto::copy_in::CopyInReceiver;
11-
use tls::TlsStream;
1212
use {AsyncMessage, CancelData, Notification};
1313
use {DbError, Error};
1414

@@ -32,8 +32,8 @@ enum State {
3232
Closing,
3333
}
3434

35-
pub struct Connection {
36-
stream: Framed<Box<TlsStream>, PostgresCodec>,
35+
pub struct Connection<S> {
36+
stream: Framed<S, PostgresCodec>,
3737
cancel_data: CancelData,
3838
parameters: HashMap<String, String>,
3939
receiver: mpsc::UnboundedReceiver<Request>,
@@ -43,13 +43,16 @@ pub struct Connection {
4343
state: State,
4444
}
4545

46-
impl Connection {
46+
impl<S> Connection<S>
47+
where
48+
S: AsyncRead + AsyncWrite,
49+
{
4750
pub fn new(
48-
stream: Framed<Box<TlsStream>, PostgresCodec>,
51+
stream: Framed<S, PostgresCodec>,
4952
cancel_data: CancelData,
5053
parameters: HashMap<String, String>,
5154
receiver: mpsc::UnboundedReceiver<Request>,
52-
) -> Connection {
55+
) -> Connection<S> {
5356
Connection {
5457
stream,
5558
cancel_data,
@@ -295,7 +298,10 @@ impl Connection {
295298
}
296299
}
297300

298-
impl Future for Connection {
301+
impl<S> Future for Connection<S>
302+
where
303+
S: AsyncRead + AsyncWrite,
304+
{
299305
type Item = ();
300306
type Error = Error;
301307

tokio-postgres/src/proto/handshake.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub enum Handshake {
6161
parameters: HashMap<String, String>,
6262
},
6363
#[state_machine_future(ready)]
64-
Finished((Client, Connection)),
64+
Finished((Client, Connection<Box<TlsStream>>)),
6565
#[state_machine_future(error)]
6666
Failed(Error),
6767
}

0 commit comments

Comments
 (0)