Skip to content

Commit cc8d8fe

Browse files
committed
Unify no-op drop impls
1 parent 4a5a277 commit cc8d8fe

File tree

5 files changed

+49
-27
lines changed

5 files changed

+49
-27
lines changed

postgres/src/client.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
11
use crate::{Config, CopyInWriter, CopyOutReader, RowIter, Statement, ToStatement, Transaction};
2+
use std::ops::{Deref, DerefMut};
23
use tokio::runtime::Runtime;
34
use tokio_postgres::tls::{MakeTlsConnect, TlsConnect};
45
use tokio_postgres::types::{ToSql, Type};
56
use tokio_postgres::{Error, Row, SimpleQueryMessage, Socket};
67

8+
pub(crate) struct Rt<'a>(pub &'a mut Runtime);
9+
10+
// no-op impl to extend the borrow until drop
11+
impl Drop for Rt<'_> {
12+
fn drop(&mut self) {}
13+
}
14+
15+
impl Deref for Rt<'_> {
16+
type Target = Runtime;
17+
18+
#[inline]
19+
fn deref(&self) -> &Runtime {
20+
self.0
21+
}
22+
}
23+
24+
impl DerefMut for Rt<'_> {
25+
#[inline]
26+
fn deref_mut(&mut self) -> &mut Runtime {
27+
self.0
28+
}
29+
}
30+
731
/// A synchronous PostgreSQL client.
832
pub struct Client {
933
runtime: Runtime,
@@ -38,6 +62,10 @@ impl Client {
3862
Config::new()
3963
}
4064

65+
fn rt(&mut self) -> Rt<'_> {
66+
Rt(&mut self.runtime)
67+
}
68+
4169
/// Executes a statement, returning the number of rows modified.
4270
///
4371
/// A statement may contain parameters, specified by `$n`, where `n` is the index of the parameter of the list
@@ -236,7 +264,7 @@ impl Client {
236264
let stream = self
237265
.runtime
238266
.block_on(self.client.query_raw(query, params))?;
239-
Ok(RowIter::new(&mut self.runtime, stream))
267+
Ok(RowIter::new(self.rt(), stream))
240268
}
241269

242270
/// Creates a new prepared statement.
@@ -326,7 +354,7 @@ impl Client {
326354
T: ?Sized + ToStatement,
327355
{
328356
let sink = self.runtime.block_on(self.client.copy_in(query))?;
329-
Ok(CopyInWriter::new(&mut self.runtime, sink))
357+
Ok(CopyInWriter::new(self.rt(), sink))
330358
}
331359

332360
/// Executes a `COPY TO STDOUT` statement, returning a reader of the resulting data.
@@ -354,7 +382,7 @@ impl Client {
354382
T: ?Sized + ToStatement,
355383
{
356384
let stream = self.runtime.block_on(self.client.copy_out(query))?;
357-
CopyOutReader::new(&mut self.runtime, stream)
385+
CopyOutReader::new(self.rt(), stream)
358386
}
359387

360388
/// Executes a sequence of SQL statements using the simple query protocol.

postgres/src/copy_in_writer.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
1+
use crate::Rt;
12
use bytes::{Bytes, BytesMut};
23
use futures::SinkExt;
34
use std::io;
45
use std::io::Write;
56
use std::pin::Pin;
6-
use tokio::runtime::Runtime;
77
use tokio_postgres::{CopyInSink, Error};
88

99
/// The writer returned by the `copy_in` method.
1010
///
1111
/// The copy *must* be explicitly completed via the `finish` method. If it is not, the copy will be aborted.
1212
pub struct CopyInWriter<'a> {
13-
runtime: &'a mut Runtime,
13+
runtime: Rt<'a>,
1414
sink: Pin<Box<CopyInSink<Bytes>>>,
1515
buf: BytesMut,
1616
}
1717

18-
// no-op impl to extend borrow until drop
19-
impl Drop for CopyInWriter<'_> {
20-
fn drop(&mut self) {}
21-
}
22-
2318
impl<'a> CopyInWriter<'a> {
24-
pub(crate) fn new(runtime: &'a mut Runtime, sink: CopyInSink<Bytes>) -> CopyInWriter<'a> {
19+
pub(crate) fn new(runtime: Rt<'a>, sink: CopyInSink<Bytes>) -> CopyInWriter<'a> {
2520
CopyInWriter {
2621
runtime,
2722
sink: Box::pin(sink),

postgres/src/copy_out_reader.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
1+
use crate::Rt;
12
use bytes::{Buf, Bytes};
23
use futures::StreamExt;
34
use std::io::{self, BufRead, Cursor, Read};
45
use std::pin::Pin;
5-
use tokio::runtime::Runtime;
66
use tokio_postgres::{CopyOutStream, Error};
77

88
/// The reader returned by the `copy_out` method.
99
pub struct CopyOutReader<'a> {
10-
runtime: &'a mut Runtime,
10+
runtime: Rt<'a>,
1111
stream: Pin<Box<CopyOutStream>>,
1212
cur: Cursor<Bytes>,
1313
}
1414

15-
// no-op impl to extend borrow until drop
16-
impl Drop for CopyOutReader<'_> {
17-
fn drop(&mut self) {}
18-
}
19-
2015
impl<'a> CopyOutReader<'a> {
2116
pub(crate) fn new(
22-
runtime: &'a mut Runtime,
17+
mut runtime: Rt<'a>,
2318
stream: CopyOutStream,
2419
) -> Result<CopyOutReader<'a>, Error> {
2520
let mut stream = Box::pin(stream);

postgres/src/row_iter.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
use crate::Rt;
12
use fallible_iterator::FallibleIterator;
23
use futures::StreamExt;
34
use std::pin::Pin;
4-
use tokio::runtime::Runtime;
55
use tokio_postgres::{Error, Row, RowStream};
66

77
/// The iterator returned by `query_raw`.
88
pub struct RowIter<'a> {
9-
runtime: &'a mut Runtime,
9+
runtime: Rt<'a>,
1010
it: Pin<Box<RowStream>>,
1111
}
1212

@@ -16,7 +16,7 @@ impl Drop for RowIter<'_> {
1616
}
1717

1818
impl<'a> RowIter<'a> {
19-
pub(crate) fn new(runtime: &'a mut Runtime, stream: RowStream) -> RowIter<'a> {
19+
pub(crate) fn new(runtime: Rt<'a>, stream: RowStream) -> RowIter<'a> {
2020
RowIter {
2121
runtime,
2222
it: Box::pin(stream),

postgres/src/transaction.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{CopyInWriter, CopyOutReader, Portal, RowIter, Statement, ToStatement};
1+
use crate::{CopyInWriter, CopyOutReader, Portal, RowIter, Rt, Statement, ToStatement};
22
use tokio::runtime::Runtime;
33
use tokio_postgres::types::{ToSql, Type};
44
use tokio_postgres::{Error, Row, SimpleQueryMessage};
@@ -23,6 +23,10 @@ impl<'a> Transaction<'a> {
2323
}
2424
}
2525

26+
fn rt(&mut self) -> Rt<'_> {
27+
Rt(self.runtime)
28+
}
29+
2630
/// Consumes the transaction, committing all changes made within it.
2731
pub fn commit(self) -> Result<(), Error> {
2832
self.runtime.block_on(self.transaction.commit())
@@ -95,7 +99,7 @@ impl<'a> Transaction<'a> {
9599
let stream = self
96100
.runtime
97101
.block_on(self.transaction.query_raw(query, params))?;
98-
Ok(RowIter::new(self.runtime, stream))
102+
Ok(RowIter::new(self.rt(), stream))
99103
}
100104

101105
/// Binds parameters to a statement, creating a "portal".
@@ -133,7 +137,7 @@ impl<'a> Transaction<'a> {
133137
let stream = self
134138
.runtime
135139
.block_on(self.transaction.query_portal_raw(portal, max_rows))?;
136-
Ok(RowIter::new(self.runtime, stream))
140+
Ok(RowIter::new(self.rt(), stream))
137141
}
138142

139143
/// Like `Client::copy_in`.
@@ -142,7 +146,7 @@ impl<'a> Transaction<'a> {
142146
T: ?Sized + ToStatement,
143147
{
144148
let sink = self.runtime.block_on(self.transaction.copy_in(query))?;
145-
Ok(CopyInWriter::new(self.runtime, sink))
149+
Ok(CopyInWriter::new(self.rt(), sink))
146150
}
147151

148152
/// Like `Client::copy_out`.
@@ -151,7 +155,7 @@ impl<'a> Transaction<'a> {
151155
T: ?Sized + ToStatement,
152156
{
153157
let stream = self.runtime.block_on(self.transaction.copy_out(query))?;
154-
CopyOutReader::new(self.runtime, stream)
158+
CopyOutReader::new(self.rt(), stream)
155159
}
156160

157161
/// Like `Client::simple_query`.

0 commit comments

Comments
 (0)