1
1
use std:: {
2
- io,
3
2
future:: Future ,
3
+ io,
4
4
mem:: MaybeUninit ,
5
5
pin:: Pin ,
6
6
sync:: Arc ,
@@ -16,14 +16,15 @@ use tokio_postgres::tls::{ChannelBinding, MakeTlsConnect, TlsConnect};
16
16
use tokio_rustls:: { client:: TlsStream , TlsConnector } ;
17
17
use webpki:: { DNSName , DNSNameRef } ;
18
18
19
-
20
19
pub struct MakeRustlsConnect {
21
20
config : Arc < ClientConfig > ,
22
21
}
23
22
24
23
impl MakeRustlsConnect {
25
24
pub fn new ( config : ClientConfig ) -> Self {
26
- Self { config : Arc :: new ( config) }
25
+ Self {
26
+ config : Arc :: new ( config) ,
27
+ }
27
28
}
28
29
}
29
30
59
60
type Future = Pin < Box < dyn Future < Output = io:: Result < RustlsStream < S > > > > > ;
60
61
61
62
fn connect ( self , stream : S ) -> Self :: Future {
62
- self . connector . connect ( self . hostname . as_ref ( ) , stream)
63
+ self . connector
64
+ . connect ( self . hostname . as_ref ( ) , stream)
63
65
. map_ok ( |s| RustlsStream ( Box :: pin ( s) ) )
64
66
. boxed ( )
65
67
}
77
79
Some ( certs) if certs. len ( ) > 0 => {
78
80
let sha256 = digest:: digest ( & digest:: SHA256 , certs[ 0 ] . as_ref ( ) ) ;
79
81
ChannelBinding :: tls_server_end_point ( sha256. as_ref ( ) . into ( ) )
80
- } ,
82
+ }
81
83
_ => ChannelBinding :: none ( ) ,
82
84
}
83
85
}
@@ -87,15 +89,23 @@ impl<S> AsyncRead for RustlsStream<S>
87
89
where
88
90
S : AsyncRead + AsyncWrite + Unpin ,
89
91
{
90
- fn poll_read ( mut self : Pin < & mut Self > , cx : & mut Context , buf : & mut [ u8 ] ) -> Poll < tokio:: io:: Result < usize > > {
92
+ fn poll_read (
93
+ mut self : Pin < & mut Self > ,
94
+ cx : & mut Context ,
95
+ buf : & mut [ u8 ] ,
96
+ ) -> Poll < tokio:: io:: Result < usize > > {
91
97
self . 0 . as_mut ( ) . poll_read ( cx, buf)
92
98
}
93
99
94
100
unsafe fn prepare_uninitialized_buffer ( & self , buf : & mut [ MaybeUninit < u8 > ] ) -> bool {
95
101
self . 0 . prepare_uninitialized_buffer ( buf)
96
102
}
97
103
98
- fn poll_read_buf < B : BufMut > ( mut self : Pin < & mut Self > , cx : & mut Context , buf : & mut B ) -> Poll < tokio:: io:: Result < usize > >
104
+ fn poll_read_buf < B : BufMut > (
105
+ mut self : Pin < & mut Self > ,
106
+ cx : & mut Context ,
107
+ buf : & mut B ,
108
+ ) -> Poll < tokio:: io:: Result < usize > >
99
109
where
100
110
Self : Sized ,
101
111
{
@@ -107,7 +117,11 @@ impl<S> AsyncWrite for RustlsStream<S>
107
117
where
108
118
S : AsyncRead + AsyncWrite + Unpin ,
109
119
{
110
- fn poll_write ( mut self : Pin < & mut Self > , cx : & mut Context , buf : & [ u8 ] ) -> Poll < tokio:: io:: Result < usize > > {
120
+ fn poll_write (
121
+ mut self : Pin < & mut Self > ,
122
+ cx : & mut Context ,
123
+ buf : & [ u8 ] ,
124
+ ) -> Poll < tokio:: io:: Result < usize > > {
111
125
self . 0 . as_mut ( ) . poll_write ( cx, buf)
112
126
}
113
127
@@ -119,7 +133,11 @@ where
119
133
self . 0 . as_mut ( ) . poll_shutdown ( cx)
120
134
}
121
135
122
- fn poll_write_buf < B : Buf > ( mut self : Pin < & mut Self > , cx : & mut Context , buf : & mut B ) -> Poll < tokio:: io:: Result < usize > >
136
+ fn poll_write_buf < B : Buf > (
137
+ mut self : Pin < & mut Self > ,
138
+ cx : & mut Context ,
139
+ buf : & mut B ,
140
+ ) -> Poll < tokio:: io:: Result < usize > >
123
141
where
124
142
Self : Sized ,
125
143
{
@@ -133,11 +151,16 @@ mod tests {
133
151
134
152
#[ tokio:: test]
135
153
async fn it_works ( ) {
154
+ env_logger:: builder ( ) . is_test ( true ) . try_init ( ) . unwrap ( ) ;
155
+
136
156
let config = rustls:: ClientConfig :: new ( ) ;
137
157
let tls = super :: MakeRustlsConnect :: new ( config) ;
138
- let ( client, conn) = tokio_postgres:: connect ( "sslmode=require host=localhost user=postgres" , tls) . await . unwrap ( ) ;
158
+ let ( client, conn) =
159
+ tokio_postgres:: connect ( "sslmode=require host=localhost user=postgres" , tls)
160
+ . await
161
+ . expect ( "connect" ) ;
139
162
tokio:: spawn ( conn. map_err ( |e| panic ! ( "{:?}" , e) ) ) ;
140
- let stmt = client. prepare ( "SELECT 1" ) . await . unwrap ( ) ;
141
- let _ = client. query ( & stmt, & [ ] ) . await . unwrap ( ) ;
163
+ let stmt = client. prepare ( "SELECT 1" ) . await . expect ( "prepare" ) ;
164
+ let _ = client. query ( & stmt, & [ ] ) . await . expect ( "query" ) ;
142
165
}
143
166
}
0 commit comments