Skip to content
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

Remove unnecessary boxfutures #3525

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
24 changes: 12 additions & 12 deletions sqlx-core/src/any/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,40 +71,40 @@ impl Connection for AnyConnection {

type Options = AnyConnectOptions;

fn close(self) -> BoxFuture<'static, Result<(), Error>> {
self.backend.close()
async fn close(self) -> Result<(), Error> {
self.backend.close().await
}

fn close_hard(self) -> BoxFuture<'static, Result<(), Error>> {
self.backend.close()
async fn close_hard(self) -> Result<(), Error> {
self.backend.close().await
}

fn ping(&mut self) -> BoxFuture<'_, Result<(), Error>> {
self.backend.ping()
async fn ping(&mut self) -> Result<(), Error> {
self.backend.ping().await
}

fn begin(&mut self) -> BoxFuture<'_, Result<Transaction<'_, Self::Database>, Error>>
async fn begin(&mut self) -> Result<Transaction<'_, Self::Database>, Error>
where
Self: Sized,
{
Transaction::begin(self)
Transaction::begin(self).await
}

fn cached_statements_size(&self) -> usize {
self.backend.cached_statements_size()
}

fn clear_cached_statements(&mut self) -> BoxFuture<'_, crate::Result<()>> {
self.backend.clear_cached_statements()
async fn clear_cached_statements(&mut self) -> crate::Result<()> {
self.backend.clear_cached_statements().await
}

fn shrink_buffers(&mut self) {
self.backend.shrink_buffers()
}

#[doc(hidden)]
fn flush(&mut self) -> BoxFuture<'_, Result<(), Error>> {
self.backend.flush()
async fn flush(&mut self) -> Result<(), Error> {
self.backend.flush().await
}

#[doc(hidden)]
Expand Down
14 changes: 10 additions & 4 deletions sqlx-core/src/any/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,16 @@ impl AnyDriver {
{
Self {
migrate_database: Some(AnyMigrateDatabase {
create_database: DebugFn(DB::create_database),
database_exists: DebugFn(DB::database_exists),
drop_database: DebugFn(DB::drop_database),
force_drop_database: DebugFn(DB::force_drop_database),
create_database: DebugFn(|url| {
Box::pin(async move { DB::create_database(url).await })
}),
database_exists: DebugFn(|url| {
Box::pin(async move { DB::database_exists(url).await })
}),
drop_database: DebugFn(|url| Box::pin(async move { DB::drop_database(url).await })),
force_drop_database: DebugFn(|url| {
Box::pin(async move { DB::force_drop_database(url).await })
}),
}),
..Self::without_migrate::<DB>()
}
Expand Down
48 changes: 20 additions & 28 deletions sqlx-core/src/any/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,32 @@ use futures_core::future::BoxFuture;
use std::time::Duration;

impl MigrateDatabase for Any {
fn create_database(url: &str) -> BoxFuture<'_, Result<(), Error>> {
Box::pin(async {
driver::from_url_str(url)?
.get_migrate_database()?
.create_database(url)
.await
})
async fn create_database(url: &str) -> Result<(), Error> {
driver::from_url_str(url)?
.get_migrate_database()?
.create_database(url)
.await
}

fn database_exists(url: &str) -> BoxFuture<'_, Result<bool, Error>> {
Box::pin(async {
driver::from_url_str(url)?
.get_migrate_database()?
.database_exists(url)
.await
})
async fn database_exists(url: &str) -> Result<bool, Error> {
driver::from_url_str(url)?
.get_migrate_database()?
.database_exists(url)
.await
}

fn drop_database(url: &str) -> BoxFuture<'_, Result<(), Error>> {
Box::pin(async {
driver::from_url_str(url)?
.get_migrate_database()?
.drop_database(url)
.await
})
async fn drop_database(url: &str) -> Result<(), Error> {
driver::from_url_str(url)?
.get_migrate_database()?
.drop_database(url)
.await
}

fn force_drop_database(url: &str) -> BoxFuture<'_, Result<(), Error>> {
Box::pin(async {
driver::from_url_str(url)?
.get_migrate_database()?
.force_drop_database(url)
.await
})
async fn force_drop_database(url: &str) -> Result<(), Error> {
driver::from_url_str(url)?
.get_migrate_database()?
.force_drop_database(url)
.await
}
}

Expand Down
5 changes: 2 additions & 3 deletions sqlx-core/src/any/options.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::any::AnyConnection;
use crate::connection::{ConnectOptions, LogSettings};
use crate::error::Error;
use futures_core::future::BoxFuture;
use log::LevelFilter;
use std::str::FromStr;
use std::time::Duration;
Expand Down Expand Up @@ -48,8 +47,8 @@ impl ConnectOptions for AnyConnectOptions {
}

#[inline]
fn connect(&self) -> BoxFuture<'_, Result<AnyConnection, Error>> {
AnyConnection::connect(self)
async fn connect(&self) -> Result<AnyConnection, Error> {
AnyConnection::connect(self).await
}

fn log_statements(mut self, level: LevelFilter) -> Self {
Expand Down
14 changes: 6 additions & 8 deletions sqlx-core/src/any/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use futures_util::future::BoxFuture;

use crate::any::{Any, AnyConnection};
use crate::error::Error;
use crate::transaction::TransactionManager;
Expand All @@ -9,16 +7,16 @@ pub struct AnyTransactionManager;
impl TransactionManager for AnyTransactionManager {
type Database = Any;

fn begin(conn: &mut AnyConnection) -> BoxFuture<'_, Result<(), Error>> {
conn.backend.begin()
async fn begin(conn: &mut AnyConnection) -> Result<(), Error> {
conn.backend.begin().await
}

fn commit(conn: &mut AnyConnection) -> BoxFuture<'_, Result<(), Error>> {
conn.backend.commit()
async fn commit(conn: &mut AnyConnection) -> Result<(), Error> {
conn.backend.commit().await
}

fn rollback(conn: &mut AnyConnection) -> BoxFuture<'_, Result<(), Error>> {
conn.backend.rollback()
async fn rollback(conn: &mut AnyConnection) -> Result<(), Error> {
conn.backend.rollback().await
}

fn start_rollback(conn: &mut AnyConnection) {
Expand Down
36 changes: 22 additions & 14 deletions sqlx-core/src/connection.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::database::{Database, HasStatementCache};
use crate::error::Error;
use std::future::Future;

use crate::transaction::Transaction;
use futures_core::future::BoxFuture;
Expand Down Expand Up @@ -31,21 +32,23 @@ pub trait Connection: Send {
///
/// Therefore it is recommended to call `.close()` on a connection when you are done using it
/// and to `.await` the result to ensure the termination message is sent.
fn close(self) -> BoxFuture<'static, Result<(), Error>>;
fn close(self) -> impl Future<Output = Result<(), Error>> + Send + 'static;

/// Immediately close the connection without sending a graceful shutdown.
///
/// This should still at least send a TCP `FIN` frame to let the server know we're dying.
#[doc(hidden)]
fn close_hard(self) -> BoxFuture<'static, Result<(), Error>>;
fn close_hard(self) -> impl Future<Output = Result<(), Error>> + Send + 'static;

/// Checks if a connection to the database is still valid.
fn ping(&mut self) -> BoxFuture<'_, Result<(), Error>>;
fn ping(&mut self) -> impl Future<Output = Result<(), Error>> + Send + '_;

/// Begin a new transaction or establish a savepoint within the active transaction.
///
/// Returns a [`Transaction`] for controlling and tracking the new transaction.
fn begin(&mut self) -> BoxFuture<'_, Result<Transaction<'_, Self::Database>, Error>>
fn begin(
&mut self,
) -> impl Future<Output = Result<Transaction<'_, Self::Database>, Error>> + Send + '_
where
Self: Sized;

Expand All @@ -66,7 +69,10 @@ pub trait Connection: Send {
/// })).await
/// # }
/// ```
fn transaction<'a, F, R, E>(&'a mut self, callback: F) -> BoxFuture<'a, Result<R, E>>
fn transaction<'a, F, R, E>(
&'a mut self,
callback: F,
) -> impl Future<Output = Result<R, E>> + Send + 'a
where
for<'c> F: FnOnce(&'c mut Transaction<'_, Self::Database>) -> BoxFuture<'c, Result<R, E>>
+ 'a
Expand All @@ -76,7 +82,7 @@ pub trait Connection: Send {
R: Send,
E: From<Error> + Send,
{
Box::pin(async move {
async move {
let mut transaction = self.begin().await?;
let ret = callback(&mut transaction).await;

Expand All @@ -92,7 +98,7 @@ pub trait Connection: Send {
Err(err)
}
}
})
}
}

/// The number of statements currently cached in the connection.
Expand All @@ -105,11 +111,11 @@ pub trait Connection: Send {

/// Removes all statements from the cache, closing them on the server if
/// needed.
fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>>
fn clear_cached_statements(&mut self) -> impl Future<Output = Result<(), Error>> + Send + '_
where
Self::Database: HasStatementCache,
{
Box::pin(async move { Ok(()) })
async { Ok(()) }
}

/// Restore any buffers in the connection to their default capacity, if possible.
Expand All @@ -127,7 +133,7 @@ pub trait Connection: Send {
fn shrink_buffers(&mut self);

#[doc(hidden)]
fn flush(&mut self) -> BoxFuture<'_, Result<(), Error>>;
fn flush(&mut self) -> impl Future<Output = Result<(), Error>> + Send + '_;

#[doc(hidden)]
fn should_flush(&self) -> bool;
Expand All @@ -137,17 +143,19 @@ pub trait Connection: Send {
/// A value of [`Options`][Self::Options] is parsed from the provided connection string. This parsing
/// is database-specific.
#[inline]
fn connect(url: &str) -> BoxFuture<'static, Result<Self, Error>>
fn connect(url: &str) -> impl Future<Output = Result<Self, Error>> + Send + 'static
where
Self: Sized,
{
let options = url.parse();

Box::pin(async move { Self::connect_with(&options?).await })
async move { Self::connect_with(&options?).await }
}

/// Establish a new database connection with the provided options.
fn connect_with(options: &Self::Options) -> BoxFuture<'_, Result<Self, Error>>
fn connect_with(
options: &Self::Options,
) -> impl Future<Output = Result<Self, Error>> + Send + '_
where
Self: Sized,
{
Expand Down Expand Up @@ -219,7 +227,7 @@ pub trait ConnectOptions: 'static + Send + Sync + FromStr<Err = Error> + Debug +
}

/// Establish a new database connection with the options specified by `self`.
fn connect(&self) -> BoxFuture<'_, Result<Self::Connection, Error>>
fn connect(&self) -> impl Future<Output = Result<Self::Connection, Error>> + Send + '_
where
Self::Connection: Sized;

Expand Down
11 changes: 6 additions & 5 deletions sqlx-core/src/migrate/migrate.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
use crate::error::Error;
use crate::migrate::{AppliedMigration, MigrateError, Migration};
use futures_core::future::BoxFuture;
use std::future::Future;
use std::time::Duration;

pub trait MigrateDatabase {
// create database in url
// uses a maintenance database depending on driver
fn create_database(url: &str) -> BoxFuture<'_, Result<(), Error>>;
fn create_database(url: &str) -> impl Future<Output = Result<(), Error>> + Send + '_;

// check if the database in url exists
// uses a maintenance database depending on driver
fn database_exists(url: &str) -> BoxFuture<'_, Result<bool, Error>>;
fn database_exists(url: &str) -> impl Future<Output = Result<bool, Error>> + Send + '_;

// drop database in url
// uses a maintenance database depending on driver
fn drop_database(url: &str) -> BoxFuture<'_, Result<(), Error>>;
fn drop_database(url: &str) -> impl Future<Output = Result<(), Error>> + Send + '_;

// force drop database in url
// uses a maintenance database depending on driver
fn force_drop_database(_url: &str) -> BoxFuture<'_, Result<(), Error>> {
Box::pin(async { Err(MigrateError::ForceNotSupported)? })
fn force_drop_database(_url: &str) -> impl Future<Output = Result<(), Error>> + Send + '_ {
async { Err(MigrateError::ForceNotSupported)? }
}
}

Expand Down
8 changes: 5 additions & 3 deletions sqlx-core/src/testing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,19 @@ pub trait TestSupport: Database {
///
/// The implementation may require `DATABASE_URL` to be set in order to manage databases.
/// The user credentials it contains must have the privilege to create and drop databases.
fn test_context(args: &TestArgs) -> BoxFuture<'_, Result<TestContext<Self>, Error>>;
fn test_context(
args: &TestArgs,
) -> impl Future<Output = Result<TestContext<Self>, Error>> + Send + '_;

fn cleanup_test(db_name: &str) -> BoxFuture<'_, Result<(), Error>>;
fn cleanup_test(db_name: &str) -> impl Future<Output = Result<(), Error>> + Send + '_;

/// Cleanup any test databases that are no longer in-use.
///
/// Returns a count of the databases deleted, if possible.
///
/// The implementation may require `DATABASE_URL` to be set in order to manage databases.
/// The user credentials it contains must have the privilege to create and drop databases.
fn cleanup_test_dbs() -> BoxFuture<'static, Result<Option<usize>, Error>>;
fn cleanup_test_dbs() -> impl Future<Output = Result<Option<usize>, Error>> + Send + 'static;

/// Take a snapshot of the current state of the database (data only).
///
Expand Down
7 changes: 4 additions & 3 deletions sqlx-core/src/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::borrow::Cow;
use std::fmt::{self, Debug, Formatter};
use std::future::Future;
use std::ops::{Deref, DerefMut};

use futures_core::future::BoxFuture;
Expand All @@ -18,17 +19,17 @@ pub trait TransactionManager {
/// Begin a new transaction or establish a savepoint within the active transaction.
fn begin(
conn: &mut <Self::Database as Database>::Connection,
) -> BoxFuture<'_, Result<(), Error>>;
) -> impl Future<Output = Result<(), Error>> + Send;

/// Commit the active transaction or release the most recent savepoint.
fn commit(
conn: &mut <Self::Database as Database>::Connection,
) -> BoxFuture<'_, Result<(), Error>>;
) -> impl Future<Output = Result<(), Error>> + Send;

/// Abort the active transaction or restore from the most recent savepoint.
fn rollback(
conn: &mut <Self::Database as Database>::Connection,
) -> BoxFuture<'_, Result<(), Error>>;
) -> impl Future<Output = Result<(), Error>> + Send;

/// Starts to abort the active transaction or restore from the most recent snapshot.
fn start_rollback(conn: &mut <Self::Database as Database>::Connection);
Expand Down
Loading
Loading