Remove TryMapRow and MapRow

These traits were a workaround for a bug in rustc that an earlier
revision of the API triggered, but the API has since been changed.
This commit is contained in:
Jonas Platte 2020-11-17 20:28:36 +01:00 committed by Ryan Leckey
parent 2890d154a3
commit 9eca6413fe
No known key found for this signature in database
GPG key ID: F8AA68C235AB08C9
7 changed files with 12 additions and 75 deletions

View file

@ -54,7 +54,6 @@ impl_acquire!(Any, AnyConnection);
impl_column_index_for_row!(AnyRow);
impl_column_index_for_statement!(AnyStatement);
impl_into_maybe_pool!(Any, AnyConnection);
impl_map_row!(Any, AnyRow);
// required because some databases have a different handling of NULL
impl_encode_for_option!(Any);

View file

@ -36,7 +36,6 @@ pub type MssqlPool = crate::pool::Pool<Mssql>;
impl_into_arguments_for_arguments!(MssqlArguments);
impl_executor_for_pool_connection!(Mssql, MssqlConnection, MssqlRow);
impl_executor_for_transaction!(Mssql, MssqlRow);
impl_map_row!(Mssql, MssqlRow);
impl_acquire!(Mssql, MssqlConnection);
impl_column_index_for_row!(MssqlRow);
impl_column_index_for_statement!(MssqlStatement);

View file

@ -43,7 +43,6 @@ pub type MySqlPoolOptions = crate::pool::PoolOptions<MySql>;
impl_into_arguments_for_arguments!(MySqlArguments);
impl_executor_for_pool_connection!(MySql, MySqlConnection, MySqlRow);
impl_executor_for_transaction!(MySql, MySqlRow);
impl_map_row!(MySql, MySqlRow);
impl_acquire!(MySql, MySqlConnection);
impl_column_index_for_row!(MySqlRow);
impl_column_index_for_statement!(MySqlStatement);

View file

@ -44,7 +44,6 @@ pub type PgPoolOptions = crate::pool::PoolOptions<Postgres>;
impl_into_arguments_for_arguments!(PgArguments);
impl_executor_for_pool_connection!(Postgres, PgConnection, PgRow);
impl_executor_for_transaction!(Postgres, PgRow);
impl_map_row!(Postgres, PgRow);
impl_acquire!(Postgres, PgConnection);
impl_column_index_for_row!(PgRow);
impl_column_index_for_statement!(PgStatement);

View file

@ -115,12 +115,15 @@ where
/// The [`query_as`](super::query_as::query_as) method will construct a mapped query using
/// a [`FromRow`](super::from_row::FromRow) implementation.
#[inline]
pub fn map<F, O>(self, f: F) -> Map<'q, DB, impl TryMapRow<DB, Output = O>, A>
pub fn map<F, O>(
self,
mut f: F,
) -> Map<'q, DB, impl FnMut(DB::Row) -> Result<O, Error> + Send, A>
where
F: MapRow<DB, Output = O>,
F: FnMut(DB::Row) -> O + Send,
O: Unpin,
{
self.try_map(MapRowAdapter(f))
self.try_map(move |row| Ok(f(row)))
}
/// Map each row in the result to another type.
@ -128,9 +131,10 @@ where
/// The [`query_as`](super::query_as::query_as) method will construct a mapped query using
/// a [`FromRow`](super::from_row::FromRow) implementation.
#[inline]
pub fn try_map<F>(self, f: F) -> Map<'q, DB, F, A>
pub fn try_map<F, O>(self, f: F) -> Map<'q, DB, F, A>
where
F: TryMapRow<DB>,
F: FnMut(DB::Row) -> Result<O, Error> + Send,
O: Unpin,
{
Map {
inner: self,
@ -252,7 +256,7 @@ where
impl<'q, DB, F, O, A> Map<'q, DB, F, A>
where
DB: Database,
F: TryMapRow<DB, Output = O>,
F: FnMut(DB::Row) -> Result<O, Error> + Send,
O: Send + Unpin,
A: 'q + Send + IntoArguments<'q, DB>,
{
@ -295,7 +299,7 @@ where
r#yield!(match v {
Either::Left(v) => Either::Left(v),
Either::Right(row) => {
Either::Right(self.mapper.try_map_row(row)?)
Either::Right((self.mapper)(row)?)
}
});
}
@ -345,47 +349,13 @@ where
let row = executor.fetch_optional(self.inner).await?;
if let Some(row) = row {
self.mapper.try_map_row(row).map(Some)
(self.mapper)(row).map(Some)
} else {
Ok(None)
}
}
}
// A (hopefully) temporary workaround for an internal compiler error (ICE) involving higher-ranked
// trait bounds (HRTBs), associated types and closures.
//
// See https://github.com/rust-lang/rust/issues/62529
pub trait TryMapRow<DB: Database>: Send {
type Output: Unpin;
fn try_map_row(&mut self, row: DB::Row) -> Result<Self::Output, Error>;
}
pub trait MapRow<DB: Database>: Send {
type Output: Unpin;
fn map_row(&mut self, row: DB::Row) -> Self::Output;
}
// A private adapter that implements [MapRow] in terms of [TryMapRow]
// Just ends up Ok wrapping it
struct MapRowAdapter<F>(F);
impl<DB: Database, O, F> TryMapRow<DB> for MapRowAdapter<F>
where
O: Unpin,
F: MapRow<DB, Output = O>,
{
type Output = O;
fn try_map_row(&mut self, row: DB::Row) -> Result<Self::Output, Error> {
Ok(self.0.map_row(row))
}
}
// Make a SQL query from a statement.
pub(crate) fn query_statement<'q, DB>(
statement: &'q <DB as HasStatement<'q>>::Statement,
@ -444,30 +414,3 @@ where
persistent: true,
}
}
#[allow(unused_macros)]
macro_rules! impl_map_row {
($DB:ident, $R:ident) => {
impl<O: Unpin, F> crate::query::MapRow<$DB> for F
where
F: Send + FnMut($R) -> O,
{
type Output = O;
fn map_row(&mut self, row: $R) -> O {
(self)(row)
}
}
impl<O: Unpin, F> crate::query::TryMapRow<$DB> for F
where
F: Send + FnMut($R) -> Result<O, crate::error::Error>,
{
type Output = O;
fn try_map_row(&mut self, row: $R) -> Result<O, crate::error::Error> {
(self)(row)
}
}
};
}

View file

@ -45,7 +45,6 @@ pub type SqlitePoolOptions = crate::pool::PoolOptions<Sqlite>;
impl_into_arguments_for_arguments!(SqliteArguments<'q>);
impl_executor_for_pool_connection!(Sqlite, SqliteConnection, SqliteRow);
impl_executor_for_transaction!(Sqlite, SqliteRow);
impl_map_row!(Sqlite, SqliteRow);
impl_column_index_for_row!(SqliteRow);
impl_column_index_for_statement!(SqliteStatement);
impl_acquire!(Sqlite, SqliteConnection);

View file

@ -130,7 +130,6 @@ pub use self::decode::Decode;
/// Types and traits for the `query` family of functions and macros.
pub mod query {
pub use sqlx_core::query::{Map, Query};
pub use sqlx_core::query::{MapRow, TryMapRow};
pub use sqlx_core::query_as::QueryAs;
pub use sqlx_core::query_scalar::QueryScalar;
}