Skip to content

Fix a few nits pointed out by clippy #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion postgres-protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ macro_rules! from_usize {
impl FromUsize for $t {
#[inline]
fn from_usize(x: usize) -> io::Result<$t> {
if x > <$t>::max_value() as usize {
if x > <$t>::MAX as usize {
Err(io::Error::new(
io::ErrorKind::InvalidInput,
"value too large to transmit",
Expand Down
2 changes: 1 addition & 1 deletion postgres-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,7 @@ impl ToSql for IpAddr {
}

fn downcast(len: usize) -> Result<i32, Box<dyn Error + Sync + Send>> {
if len > i32::max_value() as usize {
if len > i32::MAX as usize {
Err("value too large to transmit".into())
} else {
Ok(len as i32)
Expand Down
1 change: 0 additions & 1 deletion postgres-types/src/special.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use bytes::BytesMut;
use postgres_protocol::types;
use std::error::Error;
use std::{i32, i64};

use crate::{FromSql, IsNull, ToSql, Type};

Expand Down
42 changes: 3 additions & 39 deletions tokio-postgres/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::codec::{BackendMessages, FrontendMessage};
use crate::codec::BackendMessages;
use crate::config::SslMode;
use crate::connection::{Request, RequestMessages};
use crate::copy_both::CopyBothDuplex;
Expand All @@ -23,7 +23,7 @@ use fallible_iterator::FallibleIterator;
use futures_channel::mpsc;
use futures_util::{future, pin_mut, ready, StreamExt, TryStreamExt};
use parking_lot::Mutex;
use postgres_protocol::message::{backend::Message, frontend};
use postgres_protocol::message::backend::Message;
use postgres_types::BorrowToSql;
use std::collections::HashMap;
use std::fmt;
Expand Down Expand Up @@ -493,43 +493,7 @@ impl Client {
///
/// The transaction will roll back by default - use the `commit` method to commit it.
pub async fn transaction(&mut self) -> Result<Transaction<'_>, Error> {
struct RollbackIfNotDone<'me> {
client: &'me Client,
done: bool,
}

impl<'a> Drop for RollbackIfNotDone<'a> {
fn drop(&mut self) {
if self.done {
return;
}

let buf = self.client.inner().with_buf(|buf| {
frontend::query("ROLLBACK", buf).unwrap();
buf.split().freeze()
});
let _ = self
.client
.inner()
.send(RequestMessages::Single(FrontendMessage::Raw(buf)));
}
}

// This is done, as `Future` created by this method can be dropped after
// `RequestMessages` is synchronously send to the `Connection` by
// `batch_execute()`, but before `Responses` is asynchronously polled to
// completion. In that case `Transaction` won't be created and thus
// won't be rolled back.
{
let mut cleaner = RollbackIfNotDone {
client: self,
done: false,
};
self.batch_execute("BEGIN").await?;
cleaner.done = true;
}

Ok(Transaction::new(self))
self.build_transaction().start().await
}

/// Returns a builder for a transaction with custom settings.
Expand Down
8 changes: 4 additions & 4 deletions tokio-postgres/src/generic_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ pub trait GenericClient: private::Sealed {
parameter_types: &[Type],
) -> Result<Statement, Error>;

/// Like `Client::transaction`.
async fn transaction(&mut self) -> Result<Transaction<'_>, Error>;
/// Like [`Client::transaction`].
async fn transaction<'a>(&'a mut self) -> Result<Transaction<'a>, Error>;

/// Like `Client::batch_execute`.
/// Like [`Client::batch_execute`].
async fn batch_execute(&self, query: &str) -> Result<(), Error>;

/// Returns a reference to the underlying `Client`.
Expand Down Expand Up @@ -148,7 +148,7 @@ impl GenericClient for Client {
self.prepare_typed(query, parameter_types).await
}

async fn transaction(&mut self) -> Result<Transaction<'_>, Error> {
async fn transaction<'a>(&'a mut self) -> Result<Transaction<'a>, Error> {
self.transaction().await
}

Expand Down
40 changes: 38 additions & 2 deletions tokio-postgres/src/transaction_builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::{Client, Error, Transaction};
use postgres_protocol::message::frontend;

use crate::{codec::FrontendMessage, connection::RequestMessages, Client, Error, Transaction};

/// The isolation level of a database transaction.
#[derive(Debug, Copy, Clone)]
Expand Down Expand Up @@ -106,7 +108,41 @@ impl<'a> TransactionBuilder<'a> {
query.push_str(s);
}

self.client.batch_execute(&query).await?;
struct RollbackIfNotDone<'me> {
client: &'me Client,
done: bool,
}

impl Drop for RollbackIfNotDone<'_> {
fn drop(&mut self) {
if self.done {
return;
}

let buf = self.client.inner().with_buf(|buf| {
frontend::query("ROLLBACK", buf).unwrap();
buf.split().freeze()
});
let _ = self
.client
.inner()
.send(RequestMessages::Single(FrontendMessage::Raw(buf)));
}
}

// This is done as `Future` created by this method can be dropped after
// `RequestMessages` is synchronously send to the `Connection` by
// `batch_execute()`, but before `Responses` is asynchronously polled to
// completion. In that case `Transaction` won't be created and thus
// won't be rolled back.
{
let mut cleaner = RollbackIfNotDone {
client: self.client,
done: false,
};
self.client.batch_execute(&query).await?;
cleaner.done = true;
}

Ok(Transaction::new(self.client))
}
Expand Down