Skip to content

Commit 3a26c92

Browse files
authored
Merge pull request #672 from sfackler/tokio-03
Upgrade to tokio 0.3
2 parents 91144b9 + b301829 commit 3a26c92

File tree

22 files changed

+58
-224
lines changed

22 files changed

+58
-224
lines changed

.circleci/config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ version: 2
2222
jobs:
2323
build:
2424
docker:
25-
- image: rust:1.41.0
25+
- image: rust:1.45.0
2626
environment:
2727
RUSTFLAGS: -D warnings
2828
- image: sfackler/rust-postgres-test:6

codegen/src/type_gen.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,7 @@ impl<'a> DatParser<'a> {
136136
fn peek(&mut self, target: char) -> bool {
137137
self.skip_ws();
138138

139-
match self.it.peek() {
140-
Some((_, ch)) if *ch == target => true,
141-
_ => false,
142-
}
139+
matches!(self.it.peek(), Some((_, ch)) if *ch == target)
143140
}
144141

145142
fn eof(&mut self) {

postgres-native-tls/Cargo.toml

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ default = ["runtime"]
1616
runtime = ["tokio-postgres/runtime"]
1717

1818
[dependencies]
19-
bytes = "0.5"
2019
futures = "0.3"
2120
native-tls = "0.2"
22-
tokio = "0.2"
23-
tokio-tls = "0.3"
21+
tokio = "0.3"
22+
tokio-native-tls = "0.2"
2423
tokio-postgres = { version = "0.5.0", path = "../tokio-postgres", default-features = false }
2524

2625
[dev-dependencies]
27-
tokio = { version = "0.2", features = ["full"] }
26+
tokio = { version = "0.3", features = ["full"] }
2827
postgres = { version = "0.17.0", path = "../postgres" }

postgres-native-tls/src/lib.rs

+10-36
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,11 @@
4848
#![doc(html_root_url = "https://docs.rs/postgres-native-tls/0.3")]
4949
#![warn(rust_2018_idioms, clippy::all, missing_docs)]
5050

51-
use bytes::{Buf, BufMut};
5251
use std::future::Future;
5352
use std::io;
54-
use std::mem::MaybeUninit;
5553
use std::pin::Pin;
5654
use std::task::{Context, Poll};
57-
use tokio::io::{AsyncRead, AsyncWrite};
55+
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
5856
use tokio_postgres::tls;
5957
#[cfg(feature = "runtime")]
6058
use tokio_postgres::tls::MakeTlsConnect;
@@ -94,15 +92,15 @@ where
9492

9593
/// A `TlsConnect` implementation using the `native-tls` crate.
9694
pub struct TlsConnector {
97-
connector: tokio_tls::TlsConnector,
95+
connector: tokio_native_tls::TlsConnector,
9896
domain: String,
9997
}
10098

10199
impl TlsConnector {
102100
/// Creates a new connector configured to connect to the specified domain.
103101
pub fn new(connector: native_tls::TlsConnector, domain: &str) -> TlsConnector {
104102
TlsConnector {
105-
connector: tokio_tls::TlsConnector::from(connector),
103+
connector: tokio_native_tls::TlsConnector::from(connector),
106104
domain: domain.to_string(),
107105
}
108106
}
@@ -129,34 +127,19 @@ where
129127
}
130128

131129
/// The stream returned by `TlsConnector`.
132-
pub struct TlsStream<S>(tokio_tls::TlsStream<S>);
130+
pub struct TlsStream<S>(tokio_native_tls::TlsStream<S>);
133131

134132
impl<S> AsyncRead for TlsStream<S>
135133
where
136134
S: AsyncRead + AsyncWrite + Unpin,
137135
{
138-
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [MaybeUninit<u8>]) -> bool {
139-
self.0.prepare_uninitialized_buffer(buf)
140-
}
141-
142136
fn poll_read(
143137
mut self: Pin<&mut Self>,
144138
cx: &mut Context<'_>,
145-
buf: &mut [u8],
146-
) -> Poll<io::Result<usize>> {
139+
buf: &mut ReadBuf<'_>,
140+
) -> Poll<io::Result<()>> {
147141
Pin::new(&mut self.0).poll_read(cx, buf)
148142
}
149-
150-
fn poll_read_buf<B: BufMut>(
151-
mut self: Pin<&mut Self>,
152-
cx: &mut Context<'_>,
153-
buf: &mut B,
154-
) -> Poll<io::Result<usize>>
155-
where
156-
Self: Sized,
157-
{
158-
Pin::new(&mut self.0).poll_read_buf(cx, buf)
159-
}
160143
}
161144

162145
impl<S> AsyncWrite for TlsStream<S>
@@ -178,25 +161,16 @@ where
178161
fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
179162
Pin::new(&mut self.0).poll_shutdown(cx)
180163
}
181-
182-
fn poll_write_buf<B: Buf>(
183-
mut self: Pin<&mut Self>,
184-
cx: &mut Context<'_>,
185-
buf: &mut B,
186-
) -> Poll<io::Result<usize>>
187-
where
188-
Self: Sized,
189-
{
190-
Pin::new(&mut self.0).poll_write_buf(cx, buf)
191-
}
192164
}
193165

194166
impl<S> tls::TlsStream for TlsStream<S>
195167
where
196168
S: AsyncRead + AsyncWrite + Unpin,
197169
{
198170
fn channel_binding(&self) -> ChannelBinding {
199-
// FIXME https://github.com/tokio-rs/tokio/issues/1383
200-
ChannelBinding::none()
171+
match self.0.get_ref().tls_server_end_point().ok().flatten() {
172+
Some(buf) => ChannelBinding::tls_server_end_point(buf),
173+
None => ChannelBinding::none(),
174+
}
201175
}
202176
}

postgres-openssl/Cargo.toml

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ default = ["runtime"]
1616
runtime = ["tokio-postgres/runtime"]
1717

1818
[dependencies]
19-
bytes = "0.5"
2019
futures = "0.3"
2120
openssl = "0.10"
22-
tokio = "0.2"
23-
tokio-openssl = "0.4"
21+
tokio = "0.3"
22+
tokio-openssl = "0.5"
2423
tokio-postgres = { version = "0.5.0", path = "../tokio-postgres", default-features = false }
2524

2625
[dev-dependencies]
27-
tokio = { version = "0.2", features = ["full"] }
26+
tokio = { version = "0.3", features = ["full"] }
2827
postgres = { version = "0.17.0", path = "../postgres" }

postgres-openssl/src/lib.rs

+3-31
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
#![doc(html_root_url = "https://docs.rs/postgres-openssl/0.3")]
4343
#![warn(rust_2018_idioms, clippy::all, missing_docs)]
4444

45-
use bytes::{Buf, BufMut};
4645
#[cfg(feature = "runtime")]
4746
use openssl::error::ErrorStack;
4847
use openssl::hash::MessageDigest;
@@ -53,12 +52,11 @@ use openssl::ssl::{ConnectConfiguration, SslRef};
5352
use std::fmt::Debug;
5453
use std::future::Future;
5554
use std::io;
56-
use std::mem::MaybeUninit;
5755
use std::pin::Pin;
5856
#[cfg(feature = "runtime")]
5957
use std::sync::Arc;
6058
use std::task::{Context, Poll};
61-
use tokio::io::{AsyncRead, AsyncWrite};
59+
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
6260
use tokio_openssl::{HandshakeError, SslStream};
6361
use tokio_postgres::tls;
6462
#[cfg(feature = "runtime")]
@@ -157,28 +155,13 @@ impl<S> AsyncRead for TlsStream<S>
157155
where
158156
S: AsyncRead + AsyncWrite + Unpin,
159157
{
160-
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [MaybeUninit<u8>]) -> bool {
161-
self.0.prepare_uninitialized_buffer(buf)
162-
}
163-
164158
fn poll_read(
165159
mut self: Pin<&mut Self>,
166160
cx: &mut Context<'_>,
167-
buf: &mut [u8],
168-
) -> Poll<io::Result<usize>> {
161+
buf: &mut ReadBuf<'_>,
162+
) -> Poll<io::Result<()>> {
169163
Pin::new(&mut self.0).poll_read(cx, buf)
170164
}
171-
172-
fn poll_read_buf<B: BufMut>(
173-
mut self: Pin<&mut Self>,
174-
cx: &mut Context<'_>,
175-
buf: &mut B,
176-
) -> Poll<io::Result<usize>>
177-
where
178-
Self: Sized,
179-
{
180-
Pin::new(&mut self.0).poll_read_buf(cx, buf)
181-
}
182165
}
183166

184167
impl<S> AsyncWrite for TlsStream<S>
@@ -200,17 +183,6 @@ where
200183
fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
201184
Pin::new(&mut self.0).poll_shutdown(cx)
202185
}
203-
204-
fn poll_write_buf<B: Buf>(
205-
mut self: Pin<&mut Self>,
206-
cx: &mut Context<'_>,
207-
buf: &mut B,
208-
) -> Poll<io::Result<usize>>
209-
where
210-
Self: Sized,
211-
{
212-
Pin::new(&mut self.0).poll_write_buf(cx, buf)
213-
}
214186
}
215187

216188
impl<S> tls::TlsStream for TlsStream<S>

postgres-protocol/src/authentication/sasl.rs

+4-16
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,7 @@ impl<'a> Parser<'a> {
330330
}
331331

332332
fn printable(&mut self) -> io::Result<&'a str> {
333-
self.take_while(|c| match c {
334-
'\x21'..='\x2b' | '\x2d'..='\x7e' => true,
335-
_ => false,
336-
})
333+
self.take_while(|c| matches!(c, '\x21'..='\x2b' | '\x2d'..='\x7e'))
337334
}
338335

339336
fn nonce(&mut self) -> io::Result<&'a str> {
@@ -343,10 +340,7 @@ impl<'a> Parser<'a> {
343340
}
344341

345342
fn base64(&mut self) -> io::Result<&'a str> {
346-
self.take_while(|c| match c {
347-
'a'..='z' | 'A'..='Z' | '0'..='9' | '/' | '+' | '=' => true,
348-
_ => false,
349-
})
343+
self.take_while(|c| matches!(c, 'a'..='z' | 'A'..='Z' | '0'..='9' | '/' | '+' | '='))
350344
}
351345

352346
fn salt(&mut self) -> io::Result<&'a str> {
@@ -356,10 +350,7 @@ impl<'a> Parser<'a> {
356350
}
357351

358352
fn posit_number(&mut self) -> io::Result<u32> {
359-
let n = self.take_while(|c| match c {
360-
'0'..='9' => true,
361-
_ => false,
362-
})?;
353+
let n = self.take_while(|c| matches!(c, '0'..='9'))?;
363354
n.parse()
364355
.map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))
365356
}
@@ -396,10 +387,7 @@ impl<'a> Parser<'a> {
396387
}
397388

398389
fn value(&mut self) -> io::Result<&'a str> {
399-
self.take_while(|c| match c {
400-
'\0' | '=' | ',' => false,
401-
_ => true,
402-
})
390+
self.take_while(|c| matches!(c, '\0' | '=' | ','))
403391
}
404392

405393
fn server_error(&mut self) -> io::Result<Option<&'a str>> {

postgres-types/src/lib.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,7 @@ const NSEC_PER_USEC: u64 = 1_000;
144144
macro_rules! accepts {
145145
($($expected:ident),+) => (
146146
fn accepts(ty: &$crate::Type) -> bool {
147-
match *ty {
148-
$($crate::Type::$expected)|+ => true,
149-
_ => false
150-
}
147+
matches!(*ty, $($crate::Type::$expected)|+)
151148
}
152149
)
153150
}

postgres-types/src/special.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,7 @@ impl<'a, T: FromSql<'a>> FromSql<'a> for Timestamp<T> {
7575
}
7676

7777
fn accepts(ty: &Type) -> bool {
78-
match *ty {
79-
Type::TIMESTAMP | Type::TIMESTAMPTZ if T::accepts(ty) => true,
80-
_ => false,
81-
}
78+
matches!(*ty, Type::TIMESTAMP | Type::TIMESTAMPTZ if T::accepts(ty))
8279
}
8380
}
8481

@@ -99,10 +96,7 @@ impl<T: ToSql> ToSql for Timestamp<T> {
9996
}
10097

10198
fn accepts(ty: &Type) -> bool {
102-
match *ty {
103-
Type::TIMESTAMP | Type::TIMESTAMPTZ if T::accepts(ty) => true,
104-
_ => false,
105-
}
99+
matches!(*ty, Type::TIMESTAMP | Type::TIMESTAMPTZ if T::accepts(ty))
106100
}
107101

108102
to_sql_checked!();

postgres/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fallible-iterator = "0.2"
3636
futures = "0.3"
3737
tokio-postgres = { version = "0.5.5", path = "../tokio-postgres" }
3838

39-
tokio = { version = "0.2", features = ["rt-core", "time"] }
39+
tokio = { version = "0.3", features = ["rt", "time"] }
4040
log = "0.4"
4141

4242
[dev-dependencies]

postgres/src/cancel_token.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ impl CancelToken {
2626
where
2727
T: MakeTlsConnect<Socket>,
2828
{
29-
runtime::Builder::new()
29+
runtime::Builder::new_current_thread()
3030
.enable_all()
31-
.basic_scheduler()
3231
.build()
3332
.unwrap() // FIXME don't unwrap
3433
.block_on(self.0.cancel_query(tls))

postgres/src/config.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,8 @@ impl Config {
336336
T::Stream: Send,
337337
<T::TlsConnect as TlsConnect<Socket>>::Future: Send,
338338
{
339-
let mut runtime = runtime::Builder::new()
339+
let runtime = runtime::Builder::new_current_thread()
340340
.enable_all()
341-
.basic_scheduler()
342341
.build()
343342
.unwrap(); // FIXME don't unwrap
344343

postgres/src/connection.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ impl Connection {
4545
where
4646
F: FnOnce() -> T,
4747
{
48-
self.runtime.enter(f)
48+
let _guard = self.runtime.enter();
49+
f()
4950
}
5051

5152
pub fn block_on<F, T>(&mut self, future: F) -> Result<T, Error>

postgres/src/notifications.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use fallible_iterator::FallibleIterator;
66
use futures::{ready, FutureExt};
77
use std::task::Poll;
88
use std::time::Duration;
9-
use tokio::time::{self, Delay, Instant};
9+
use tokio::time::{self, Instant, Sleep};
1010

1111
/// Notifications from a PostgreSQL backend.
1212
pub struct Notifications<'a> {
@@ -64,7 +64,7 @@ impl<'a> Notifications<'a> {
6464
/// This iterator may start returning `Some` after previously returning `None` if more notifications are received.
6565
pub fn timeout_iter(&mut self, timeout: Duration) -> TimeoutIter<'_> {
6666
TimeoutIter {
67-
delay: self.connection.enter(|| time::delay_for(timeout)),
67+
delay: self.connection.enter(|| time::sleep(timeout)),
6868
timeout,
6969
connection: self.connection.as_ref(),
7070
}
@@ -124,7 +124,7 @@ impl<'a> FallibleIterator for BlockingIter<'a> {
124124
/// A time-limited blocking iterator over pending notifications.
125125
pub struct TimeoutIter<'a> {
126126
connection: ConnectionRef<'a>,
127-
delay: Delay,
127+
delay: Sleep,
128128
timeout: Duration,
129129
}
130130

0 commit comments

Comments
 (0)