From 673f133cab453407c2e73c7e58596e1bff00ea3b Mon Sep 17 00:00:00 2001 From: Austin Bonander Date: Wed, 25 Mar 2020 17:33:03 -0700 Subject: [PATCH] un-generify `Error` and support downcasting in `DatabaseError` --- sqlx-core/src/connection.rs | 10 +- sqlx-core/src/cursor.rs | 2 +- sqlx-core/src/decode.rs | 2 +- sqlx-core/src/error.rs | 144 +++++++++++++++--- sqlx-core/src/executor.rs | 8 +- sqlx-core/src/io/tls.rs | 6 +- sqlx-core/src/mysql/connection.rs | 20 ++- sqlx-core/src/mysql/cursor.rs | 4 +- sqlx-core/src/mysql/database.rs | 4 +- sqlx-core/src/mysql/error.rs | 24 ++- sqlx-core/src/mysql/executor.rs | 16 +- sqlx-core/src/mysql/mod.rs | 2 +- sqlx-core/src/mysql/protocol/auth_plugin.rs | 2 +- sqlx-core/src/mysql/protocol/auth_switch.rs | 2 +- sqlx-core/src/mysql/protocol/column_count.rs | 2 +- sqlx-core/src/mysql/protocol/column_def.rs | 2 +- .../src/mysql/protocol/com_stmt_prepare_ok.rs | 2 +- sqlx-core/src/mysql/protocol/eof.rs | 2 +- sqlx-core/src/mysql/protocol/err.rs | 2 +- sqlx-core/src/mysql/protocol/handshake.rs | 2 +- sqlx-core/src/mysql/protocol/ok.rs | 2 +- sqlx-core/src/mysql/protocol/row.rs | 4 +- sqlx-core/src/mysql/row.rs | 6 +- sqlx-core/src/mysql/rsa.rs | 6 +- sqlx-core/src/mysql/stream.rs | 28 ++-- sqlx-core/src/mysql/tls.rs | 7 +- sqlx-core/src/mysql/types/bool.rs | 2 +- sqlx-core/src/mysql/types/bytes.rs | 4 +- sqlx-core/src/mysql/types/chrono.rs | 10 +- sqlx-core/src/mysql/types/float.rs | 4 +- sqlx-core/src/mysql/types/int.rs | 8 +- sqlx-core/src/mysql/types/mod.rs | 2 +- sqlx-core/src/mysql/types/str.rs | 4 +- sqlx-core/src/mysql/types/time.rs | 12 +- sqlx-core/src/mysql/types/uint.rs | 8 +- sqlx-core/src/mysql/value.rs | 2 +- sqlx-core/src/pool/connection.rs | 8 +- sqlx-core/src/pool/executor.rs | 8 +- sqlx-core/src/pool/inner.rs | 29 ++-- sqlx-core/src/pool/mod.rs | 14 +- sqlx-core/src/pool/options.rs | 2 +- sqlx-core/src/postgres/connection.rs | 14 +- sqlx-core/src/postgres/cursor.rs | 8 +- sqlx-core/src/postgres/database.rs | 6 +- sqlx-core/src/postgres/error.rs | 24 ++- sqlx-core/src/postgres/executor.rs | 54 ++++--- sqlx-core/src/postgres/listen.rs | 22 +-- sqlx-core/src/postgres/mod.rs | 2 +- .../src/postgres/protocol/authentication.rs | 8 +- .../src/postgres/protocol/backend_key_data.rs | 2 +- .../src/postgres/protocol/command_complete.rs | 2 +- sqlx-core/src/postgres/protocol/data_row.rs | 2 +- sqlx-core/src/postgres/protocol/message.rs | 4 +- .../protocol/notification_response.rs | 2 +- .../protocol/parameter_description.rs | 2 +- .../src/postgres/protocol/ready_for_query.rs | 2 +- sqlx-core/src/postgres/protocol/response.rs | 6 +- .../src/postgres/protocol/row_description.rs | 2 +- sqlx-core/src/postgres/protocol/sasl.rs | 6 +- sqlx-core/src/postgres/row.rs | 6 +- sqlx-core/src/postgres/sasl.rs | 2 +- sqlx-core/src/postgres/stream.rs | 14 +- sqlx-core/src/postgres/tls.rs | 11 +- sqlx-core/src/postgres/types/array.rs | 2 +- sqlx-core/src/postgres/types/bigdecimal.rs | 6 +- sqlx-core/src/postgres/types/bool.rs | 2 +- sqlx-core/src/postgres/types/bytes.rs | 4 +- sqlx-core/src/postgres/types/chrono.rs | 10 +- sqlx-core/src/postgres/types/float.rs | 4 +- sqlx-core/src/postgres/types/int.rs | 10 +- sqlx-core/src/postgres/types/ipnetwork.rs | 4 +- sqlx-core/src/postgres/types/json.rs | 6 +- sqlx-core/src/postgres/types/mod.rs | 2 +- sqlx-core/src/postgres/types/raw/array.rs | 16 +- sqlx-core/src/postgres/types/raw/numeric.rs | 6 +- sqlx-core/src/postgres/types/raw/record.rs | 4 +- sqlx-core/src/postgres/types/raw/sequence.rs | 8 +- sqlx-core/src/postgres/types/record.rs | 2 +- sqlx-core/src/postgres/types/str.rs | 4 +- sqlx-core/src/postgres/types/time.rs | 10 +- sqlx-core/src/postgres/types/uuid.rs | 2 +- sqlx-core/src/postgres/value.rs | 4 +- sqlx-core/src/query.rs | 16 +- sqlx-core/src/query_as.rs | 16 +- sqlx-core/src/row.rs | 22 +-- sqlx-core/src/sqlite/arguments.rs | 12 +- sqlx-core/src/sqlite/connection.rs | 14 +- sqlx-core/src/sqlite/cursor.rs | 4 +- sqlx-core/src/sqlite/database.rs | 4 +- sqlx-core/src/sqlite/error.rs | 26 +++- sqlx-core/src/sqlite/executor.rs | 6 +- sqlx-core/src/sqlite/mod.rs | 2 +- sqlx-core/src/sqlite/row.rs | 6 +- sqlx-core/src/sqlite/statement.rs | 14 +- sqlx-core/src/sqlite/types/bool.rs | 2 +- sqlx-core/src/sqlite/types/bytes.rs | 4 +- sqlx-core/src/sqlite/types/float.rs | 4 +- sqlx-core/src/sqlite/types/int.rs | 4 +- sqlx-core/src/sqlite/types/mod.rs | 2 +- sqlx-core/src/sqlite/types/str.rs | 4 +- sqlx-core/src/transaction.rs | 16 +- src/result_ext.rs | 10 +- 102 files changed, 518 insertions(+), 405 deletions(-) diff --git a/sqlx-core/src/connection.rs b/sqlx-core/src/connection.rs index 34125189c..1b3e313cb 100644 --- a/sqlx-core/src/connection.rs +++ b/sqlx-core/src/connection.rs @@ -36,7 +36,7 @@ where /// /// [`commit`]: crate::transaction::Transaction::commit /// [`rollback`]: crate::transaction::Transaction::rollback - fn begin(self) -> BoxFuture<'static, crate::Result>> + fn begin(self) -> BoxFuture<'static, crate::Result>> where Self: Sized, { @@ -48,16 +48,16 @@ where /// This method is **not required** for safe and consistent operation. However, it is /// recommended to call it instead of letting a connection `drop` as the database server /// will be faster at cleaning up resources. - fn close(self) -> BoxFuture<'static, crate::Result>; + fn close(self) -> BoxFuture<'static, crate::Result<()>>; /// Checks if a connection to the database is still valid. - fn ping(&mut self) -> BoxFuture>; + fn ping(&mut self) -> BoxFuture>; } /// Represents a type that can directly establish a new connection. pub trait Connect: Connection { /// Establish a new database connection. - fn connect(url: T) -> BoxFuture<'static, crate::Result> + fn connect(url: T) -> BoxFuture<'static, crate::Result> where T: TryInto, Self: Sized; @@ -79,7 +79,7 @@ where C: Connect, { #[allow(dead_code)] - pub(crate) async fn resolve(&mut self) -> crate::Result { + pub(crate) async fn resolve(&mut self) -> crate::Result<&'_ mut C> { if let ConnectionSource::Pool(pool) = self { let conn = pool.acquire().await?; diff --git a/sqlx-core/src/cursor.rs b/sqlx-core/src/cursor.rs index d46c063c5..d98ccb693 100644 --- a/sqlx-core/src/cursor.rs +++ b/sqlx-core/src/cursor.rs @@ -64,7 +64,7 @@ where /// Creates a future that attempts to resolve the next row in the cursor. fn next<'cur>( &'cur mut self, - ) -> BoxFuture<'cur, crate::Result>::Row>>>; + ) -> BoxFuture<'cur, crate::Result>::Row>>>; } // Prevent users from implementing the `Row` trait. diff --git a/sqlx-core/src/decode.rs b/sqlx-core/src/decode.rs index b851ef1ce..373610467 100644 --- a/sqlx-core/src/decode.rs +++ b/sqlx-core/src/decode.rs @@ -9,5 +9,5 @@ where Self: Sized + 'de, DB: Database, { - fn decode(value: >::RawValue) -> crate::Result; + fn decode(value: >::RawValue) -> crate::Result; } diff --git a/sqlx-core/src/error.rs b/sqlx-core/src/error.rs index 5c4ac8435..dc74d14e8 100644 --- a/sqlx-core/src/error.rs +++ b/sqlx-core/src/error.rs @@ -1,4 +1,4 @@ -//! Errorand Result types. +//! Errorand Result types. use crate::database::Database; use crate::types::Type; @@ -19,20 +19,20 @@ macro_rules! decode_err { } /// A specialized `Result` type for SQLx. -pub type Result = std::result::Result>; +pub type Result = std::result::Result; /// A generic error that represents all the ways a method can fail inside of SQLx. #[derive(Debug)] #[non_exhaustive] -pub enum Error { - /// Errorcommunicating with the database. +pub enum Error { + /// Error communicating with the database. Io(io::Error), /// Connection URL was malformed. UrlParse(url::ParseError), /// An error was returned by the database. - Database(Box), + Database(Box), /// No row was returned during [`query::Map::fetch_one`] or `QueryAs::fetch_one`. /// @@ -75,17 +75,17 @@ pub enum Error { Decode(Box), } -impl Error { +impl Error { #[allow(dead_code)] pub(crate) fn decode(err: E) -> Self where E: StdError + Send + Sync + 'static, { - Error::::Decode(err.into()) + Error::Decode(err.into()) } #[allow(dead_code)] - pub(crate) fn mismatched_types(expected: DB::TypeInfo) -> Self + pub(crate) fn mismatched_types(expected: DB::TypeInfo) -> Self where T: Type, { @@ -100,7 +100,7 @@ impl Error { } } -impl StdError for Error { +impl StdError for Error { fn source(&self) -> Option<&(dyn StdError + 'static)> { match self { Error::Io(error) => Some(error), @@ -108,13 +108,14 @@ impl StdError for Error { Error::PoolTimedOut(Some(error)) => Some(&**error), Error::Decode(error) => Some(&**error), Error::Tls(error) => Some(&**error), + Error::Database(error) => Some(error.as_ref_err()), _ => None, } } } -impl Display for Error { +impl Display for Error { // IntellijRust does not understand that [non_exhaustive] applies only for downstream crates // noinspection RsMatchCheck fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -156,28 +157,28 @@ impl Display for Error { } } -impl From for Error { +impl From for Error { #[inline] fn from(err: io::Error) -> Self { Error::Io(err) } } -impl From for Error { +impl From for Error { #[inline] fn from(err: io::ErrorKind) -> Self { Error::Io(err.into()) } } -impl From for Error { +impl From for Error { #[inline] fn from(err: url::ParseError) -> Self { Error::UrlParse(err) } } -impl From> for Error { +impl From> for Error { #[inline] fn from(err: ProtocolError) -> Self { Error::Protocol(err.args.to_string().into_boxed_str()) @@ -186,14 +187,14 @@ impl From> for Error { #[cfg(feature = "tls")] #[cfg_attr(docsrs, doc(cfg(feature = "tls")))] -impl From for Error { +impl From for Error { #[inline] fn from(err: async_native_tls::Error) -> Self { Error::Tls(err.into()) } } -impl From> for Error { +impl From> for Error { #[inline] fn from(err: TlsError<'_>) -> Self { Error::Tls(err.args.to_string().into()) @@ -201,7 +202,7 @@ impl From> for Error { } /// An error that was returned by the database. -pub trait DatabaseError: StdError + Send + Sync { +pub trait DatabaseError: StdError + Send + Sync + 'static { /// The primary, human-readable error message. fn message(&self) -> &str; @@ -229,6 +230,115 @@ pub trait DatabaseError: StdError + Send + Sync { fn constraint_name(&self) -> Option<&str> { None } + + #[doc(hidden)] + fn as_ref_err(&self) -> &(dyn StdError + Send + Sync + 'static); + + #[doc(hidden)] + fn as_mut_err(&mut self) -> &mut (dyn StdError + Send + Sync + 'static); + + #[doc(hidden)] + fn into_box_err(self: Box) -> Box; +} + +impl dyn DatabaseError { + /// Downcast this `&dyn DatabaseError` to a specific database error type: + /// + /// * [PgError][crate::postgres::PgError] (if the `postgres` feature is active) + /// * [MySqlError][crate::mysql::MySqlError] (if the `mysql` feature is active) + /// * [SqliteError][crate::sqlite::SqliteError] (if the `sqlite` feature is active) + /// + /// In a generic context you can use the [crate::database::Database::Error] associated type. + /// + /// ### Panics + /// If the type does not match; this is in contrast with [StdError::downcast_ref] + /// which returns `Option`. This was a deliberate design decision in favor of brevity as in + /// almost all cases you should know which database error type you're expecting. + /// + /// In any other cases, use [Self::try_downcast_ref] instead. + pub fn downcast_ref(&self) -> &T { + self.try_downcast_ref::().unwrap_or_else(|| { + panic!( + "downcasting to wrong DatabaseError type; original error: {:?}", + self + ) + }) + } + + /// Downcast this `&dyn DatabaseError` to a specific database error type: + /// + /// * [PgError][crate::postgres::PgError] (if the `postgres` feature is active) + /// * [MySqlError][crate::mysql::MySqlError] (if the `mysql` feature is active) + /// * [SqliteError][crate::sqlite::SqliteError] (if the `sqlite` feature is active) + /// + /// In a generic context you can use the [crate::database::Database::Error] associated type. + /// + /// Returns `None` if the downcast fails (the types do not match) + pub fn try_downcast_ref(&self) -> Option<&T> { + self.as_ref_err().downcast_ref() + } + + /// Only meant for internal use so no `try_` variant is currently provided + pub(crate) fn downcast_mut(&mut self) -> &mut T { + // tried to express this as the following: + // + // if let Some(e) = self.as_mut_err().downcast_mut() { return e; } + // + // however it didn't like using `self` again in the panic format + if self.as_ref_err().is::() { + return self.as_mut_err().downcast_mut().unwrap(); + } + + panic!( + "downcasting to wrong DatabaseError type; original error: {:?}", + self + ) + } + + /// Downcast this `Box` to a specific database error type: + /// + /// * [PgError][crate::postgres::PgError] (if the `postgres` feature is active) + /// * [MySqlError][crate::mysql::MySqlError] (if the `mysql` feature is active) + /// * [SqliteError][crate::sqlite::SqliteError] (if the `sqlite` feature is active) + /// + /// In a generic context you can use the [crate::database::Database::Error] associated type. + /// + /// ### Panics + /// If the type does not match; this is in contrast with [std::error::Error::downcast] + /// which returns `Result`. This was a deliberate design decision in favor of + /// brevity as in almost all cases you should know which database error type you're expecting. + /// + /// In any other cases, use [Self::try_downcast] instead. + pub fn downcast(self: Box) -> Box { + self.try_downcast().unwrap_or_else(|e| { + panic!( + "downcasting to wrong DatabaseError type; original error: {:?}", + e + ) + }) + } + + /// Downcast this `Box` to a specific database error type: + /// + /// * [PgError][crate::postgres::PgError] (if the `postgres` feature is active) + /// * [MySqlError][crate::mysql::MySqlError] (if the `mysql` feature is active) + /// * [SqliteError][crate::sqlite::SqliteError] (if the `sqlite` feature is active) + /// + /// In a generic context you can use the [crate::database::Database::Error] associated type. + /// + /// Returns `Err(self)` if the downcast fails (the types do not match). + pub fn try_downcast( + self: Box, + ) -> std::result::Result, Box> { + if self.as_ref_err().is::() { + Ok(self + .into_box_err() + .downcast() + .expect("type mismatch between DatabaseError::as_ref_err() and into_box_err()")) + } else { + Err(self) + } + } } /// Used by the `protocol_error!()` macro for a lazily evaluated conversion to diff --git a/sqlx-core/src/executor.rs b/sqlx-core/src/executor.rs index 3008c3c3c..0379bcf83 100644 --- a/sqlx-core/src/executor.rs +++ b/sqlx-core/src/executor.rs @@ -28,7 +28,7 @@ where fn execute<'e, 'q: 'e, 'c: 'e, E: 'e>( &'c mut self, query: E, - ) -> BoxFuture<'e, crate::Result> + ) -> BoxFuture<'e, crate::Result> where E: Execute<'q, Self::Database>; @@ -52,7 +52,7 @@ where fn describe<'e, 'q, E: 'e>( &'e mut self, query: E, - ) -> BoxFuture<'e, crate::Result>> + ) -> BoxFuture<'e, crate::Result>> where E: Execute<'q, Self::Database>; } @@ -100,7 +100,7 @@ where fn execute<'e, 'q: 'e, 'c: 'e, E: 'e>( &'c mut self, query: E, - ) -> BoxFuture<'e, crate::Result> + ) -> BoxFuture<'e, crate::Result> where E: Execute<'q, Self::Database>, { @@ -118,7 +118,7 @@ where fn describe<'e, 'q, E: 'e>( &'e mut self, query: E, - ) -> BoxFuture<'e, crate::Result>> + ) -> BoxFuture<'e, crate::Result>> where E: Execute<'q, Self::Database>, { diff --git a/sqlx-core/src/io/tls.rs b/sqlx-core/src/io/tls.rs index 664c93e32..2a7e3b4be 100644 --- a/sqlx-core/src/io/tls.rs +++ b/sqlx-core/src/io/tls.rs @@ -22,7 +22,7 @@ enum Inner { } impl MaybeTlsStream { - pub async fn connect(url: &Url, default_port: u16) -> crate::Result { + pub async fn connect(url: &Url, default_port: u16) -> crate::Result { let conn = TcpStream::connect((url.host(), url.port(default_port))).await?; Ok(Self { inner: Inner::NotTls(conn), @@ -42,11 +42,11 @@ impl MaybeTlsStream { #[cfg(feature = "tls")] #[cfg_attr(docsrs, doc(cfg(feature = "tls")))] - pub async fn upgrade( + pub async fn upgrade( &mut self, url: &Url, connector: async_native_tls::TlsConnector, - ) -> crate::Result { + ) -> crate::Result<()> { let conn = match std::mem::replace(&mut self.inner, Upgrading) { NotTls(conn) => conn, Tls(_) => return Err(tls_err!("connection already upgraded").into()), diff --git a/sqlx-core/src/mysql/connection.rs b/sqlx-core/src/mysql/connection.rs index 699cb9b03..7aea36cc5 100644 --- a/sqlx-core/src/mysql/connection.rs +++ b/sqlx-core/src/mysql/connection.rs @@ -105,7 +105,7 @@ async fn rsa_encrypt_with_nonce( public_key_request_id: u8, password: &str, nonce: &[u8], -) -> crate::Result> { +) -> crate::Result> { // https://mariadb.com/kb/en/caching_sha2_password-authentication-plugin/ if stream.is_tls() { @@ -133,7 +133,7 @@ async fn make_auth_response( plugin: &AuthPlugin, password: &str, nonce: &[u8], -) -> crate::Result> { +) -> crate::Result> { match plugin { AuthPlugin::CachingSha2Password | AuthPlugin::MySqlNativePassword => { Ok(plugin.scramble(password, nonce)) @@ -143,7 +143,7 @@ async fn make_auth_response( } } -async fn establish(stream: &mut MySqlStream, url: &Url) -> crate::Result { +async fn establish(stream: &mut MySqlStream, url: &Url) -> crate::Result<()> { // https://dev.mysql.com/doc/dev/mysql-server/8.0.12/page_protocol_connection_phase.html // https://mariadb.com/kb/en/connection/ @@ -243,7 +243,7 @@ async fn establish(stream: &mut MySqlStream, url: &Url) -> crate::Result crate::Result { +async fn close(mut stream: MySqlStream) -> crate::Result<()> { // TODO: Actually tell MySQL that we're closing stream.flush().await?; @@ -252,7 +252,7 @@ async fn close(mut stream: MySqlStream) -> crate::Result { Ok(()) } -async fn ping(stream: &mut MySqlStream) -> crate::Result { +async fn ping(stream: &mut MySqlStream) -> crate::Result<()> { stream.wait_until_ready().await?; stream.is_ready = false; @@ -268,9 +268,7 @@ async fn ping(stream: &mut MySqlStream) -> crate::Result { } impl MySqlConnection { - pub(super) async fn new( - url: std::result::Result, - ) -> crate::Result { + pub(super) async fn new(url: std::result::Result) -> crate::Result { let url = url?; let mut stream = MySqlStream::new(&url).await?; @@ -319,7 +317,7 @@ SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci; } impl Connect for MySqlConnection { - fn connect(url: T) -> BoxFuture<'static, crate::Result> + fn connect(url: T) -> BoxFuture<'static, crate::Result> where T: TryInto, Self: Sized, @@ -330,12 +328,12 @@ impl Connect for MySqlConnection { impl Connection for MySqlConnection { #[inline] - fn close(self) -> BoxFuture<'static, crate::Result> { + fn close(self) -> BoxFuture<'static, crate::Result<()>> { Box::pin(close(self.stream)) } #[inline] - fn ping(&mut self) -> BoxFuture> { + fn ping(&mut self) -> BoxFuture> { Box::pin(ping(&mut self.stream)) } } diff --git a/sqlx-core/src/mysql/cursor.rs b/sqlx-core/src/mysql/cursor.rs index 602ba041a..14ad503fd 100644 --- a/sqlx-core/src/mysql/cursor.rs +++ b/sqlx-core/src/mysql/cursor.rs @@ -53,14 +53,14 @@ impl<'c, 'q> Cursor<'c, 'q> for MySqlCursor<'c, 'q> { } } - fn next(&mut self) -> BoxFuture>>> { + fn next(&mut self) -> BoxFuture>>> { Box::pin(next(self)) } } async fn next<'a, 'c: 'a, 'q: 'a>( cursor: &'a mut MySqlCursor<'c, 'q>, -) -> crate::Result>> { +) -> crate::Result>> { let mut conn = cursor.source.resolve().await?; // The first time [next] is called we need to actually execute our diff --git a/sqlx-core/src/mysql/database.rs b/sqlx-core/src/mysql/database.rs index 4ec38f8e8..e75d77328 100644 --- a/sqlx-core/src/mysql/database.rs +++ b/sqlx-core/src/mysql/database.rs @@ -1,6 +1,6 @@ use crate::cursor::HasCursor; use crate::database::Database; -use crate::mysql::error::MySqlDatabaseError; +use crate::mysql::error::MySqlError; use crate::row::HasRow; use crate::value::HasRawValue; @@ -19,7 +19,7 @@ impl Database for MySql { type RawBuffer = Vec; - type Error = MySqlDatabaseError; + type Error = MySqlError; } impl<'c> HasRow<'c> for MySql { diff --git a/sqlx-core/src/mysql/error.rs b/sqlx-core/src/mysql/error.rs index 01f6d4e06..8aa03c9a0 100644 --- a/sqlx-core/src/mysql/error.rs +++ b/sqlx-core/src/mysql/error.rs @@ -6,15 +6,15 @@ use crate::mysql::protocol::ErrPacket; use crate::mysql::MySql; #[derive(Debug)] -pub struct MySqlDatabaseError(pub(super) ErrPacket); +pub struct MySqlError(pub(super) ErrPacket); -impl Display for MySqlDatabaseError { +impl Display for MySqlError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.pad(self.message()) } } -impl DatabaseError for MySqlDatabaseError { +impl DatabaseError for MySqlError { fn message(&self) -> &str { &*self.0.error_message } @@ -22,12 +22,24 @@ impl DatabaseError for MySqlDatabaseError { fn code(&self) -> Option<&str> { self.0.sql_state.as_deref() } + + fn as_ref_err(&self) -> &(dyn StdError + Send + Sync + 'static) { + self + } + + fn as_mut_err(&mut self) -> &mut (dyn StdError + Send + Sync + 'static) { + self + } + + fn into_box_err(self: Box) -> Box { + self + } } -impl StdError for MySqlDatabaseError {} +impl StdError for MySqlError {} -impl From for crate::Error { - fn from(err: MySqlDatabaseError) -> Self { +impl From for crate::Error { + fn from(err: MySqlError) -> Self { crate::Error::Database(Box::new(err)) } } diff --git a/sqlx-core/src/mysql/executor.rs b/sqlx-core/src/mysql/executor.rs index c605e6362..469c366cd 100644 --- a/sqlx-core/src/mysql/executor.rs +++ b/sqlx-core/src/mysql/executor.rs @@ -11,7 +11,7 @@ use crate::mysql::{MySql, MySqlArguments, MySqlCursor, MySqlTypeInfo}; impl super::MySqlConnection { // Creates a prepared statement for the passed query string - async fn prepare(&mut self, query: &str) -> crate::Result { + async fn prepare(&mut self, query: &str) -> crate::Result { // https://dev.mysql.com/doc/dev/mysql-server/8.0.11/page_protocol_com_stmt_prepare.html self.stream.send(ComStmtPrepare { query }, true).await?; @@ -25,7 +25,7 @@ impl super::MySqlConnection { ComStmtPrepareOk::read(packet) } - async fn drop_column_defs(&mut self, count: usize) -> crate::Result { + async fn drop_column_defs(&mut self, count: usize) -> crate::Result<()> { for _ in 0..count { let _column = ColumnDefinition::read(self.stream.receive().await?)?; } @@ -39,7 +39,7 @@ impl super::MySqlConnection { // Gets a cached prepared statement ID _or_ prepares the statement if not in the cache // At the end we should have [cache_statement] and [cache_statement_columns] filled - async fn get_or_prepare(&mut self, query: &str) -> crate::Result { + async fn get_or_prepare(&mut self, query: &str) -> crate::Result { if let Some(&id) = self.cache_statement.get(query) { Ok(id) } else { @@ -63,7 +63,7 @@ impl super::MySqlConnection { &mut self, query: &str, arguments: Option, - ) -> crate::Result> { + ) -> crate::Result> { self.stream.wait_until_ready().await?; self.stream.is_ready = false; @@ -93,7 +93,7 @@ impl super::MySqlConnection { } } - async fn affected_rows(&mut self) -> crate::Result { + async fn affected_rows(&mut self) -> crate::Result { let mut rows = 0; loop { @@ -132,7 +132,7 @@ impl super::MySqlConnection { // method is not named describe to work around an intellijrust bug // otherwise it marks someone trying to describe the connection as "method is private" - async fn do_describe(&mut self, query: &str) -> crate::Result> { + async fn do_describe(&mut self, query: &str) -> crate::Result> { self.stream.wait_until_ready().await?; let stmt = self.prepare(query).await?; @@ -178,7 +178,7 @@ impl Executor for super::MySqlConnection { fn execute<'e, 'q: 'e, 'c: 'e, E: 'e>( &'c mut self, query: E, - ) -> BoxFuture<'e, crate::Result> + ) -> BoxFuture<'e, crate::Result> where E: Execute<'q, Self::Database>, { @@ -201,7 +201,7 @@ impl Executor for super::MySqlConnection { fn describe<'e, 'q, E: 'e>( &'e mut self, query: E, - ) -> BoxFuture<'e, crate::Result>> + ) -> BoxFuture<'e, crate::Result>> where E: Execute<'q, Self::Database>, { diff --git a/sqlx-core/src/mysql/mod.rs b/sqlx-core/src/mysql/mod.rs index 769b42476..42458cce4 100644 --- a/sqlx-core/src/mysql/mod.rs +++ b/sqlx-core/src/mysql/mod.rs @@ -4,7 +4,7 @@ pub use arguments::MySqlArguments; pub use connection::MySqlConnection; pub use cursor::MySqlCursor; pub use database::MySql; -pub use error::MySqlDatabaseError; +pub use error::MySqlError; pub use row::MySqlRow; pub use types::MySqlTypeInfo; pub use value::{MySqlData, MySqlValue}; diff --git a/sqlx-core/src/mysql/protocol/auth_plugin.rs b/sqlx-core/src/mysql/protocol/auth_plugin.rs index b2188c2bf..a67915c04 100644 --- a/sqlx-core/src/mysql/protocol/auth_plugin.rs +++ b/sqlx-core/src/mysql/protocol/auth_plugin.rs @@ -15,7 +15,7 @@ pub enum AuthPlugin { } impl AuthPlugin { - pub(crate) fn from_opt_str(s: Option<&str>) -> crate::Result { + pub(crate) fn from_opt_str(s: Option<&str>) -> crate::Result { match s { Some("mysql_native_password") | None => Ok(AuthPlugin::MySqlNativePassword), Some("caching_sha2_password") => Ok(AuthPlugin::CachingSha2Password), diff --git a/sqlx-core/src/mysql/protocol/auth_switch.rs b/sqlx-core/src/mysql/protocol/auth_switch.rs index 0769fa24e..02d5d3586 100644 --- a/sqlx-core/src/mysql/protocol/auth_switch.rs +++ b/sqlx-core/src/mysql/protocol/auth_switch.rs @@ -10,7 +10,7 @@ pub(crate) struct AuthSwitch { } impl AuthSwitch { - pub(crate) fn read(mut buf: &[u8]) -> crate::Result + pub(crate) fn read(mut buf: &[u8]) -> crate::Result where Self: Sized, { diff --git a/sqlx-core/src/mysql/protocol/column_count.rs b/sqlx-core/src/mysql/protocol/column_count.rs index cd2ef069a..49082cd2a 100644 --- a/sqlx-core/src/mysql/protocol/column_count.rs +++ b/sqlx-core/src/mysql/protocol/column_count.rs @@ -9,7 +9,7 @@ pub struct ColumnCount { } impl ColumnCount { - pub(crate) fn read(mut buf: &[u8]) -> crate::Result { + pub(crate) fn read(mut buf: &[u8]) -> crate::Result { let columns = buf.get_uint_lenenc::()?.unwrap_or(0); Ok(Self { columns }) diff --git a/sqlx-core/src/mysql/protocol/column_def.rs b/sqlx-core/src/mysql/protocol/column_def.rs index 566ee0f78..81324938b 100644 --- a/sqlx-core/src/mysql/protocol/column_def.rs +++ b/sqlx-core/src/mysql/protocol/column_def.rs @@ -35,7 +35,7 @@ impl ColumnDefinition { } impl ColumnDefinition { - pub(crate) fn read(mut buf: &[u8]) -> crate::Result { + pub(crate) fn read(mut buf: &[u8]) -> crate::Result { // catalog : string let catalog = buf.get_str_lenenc::()?; diff --git a/sqlx-core/src/mysql/protocol/com_stmt_prepare_ok.rs b/sqlx-core/src/mysql/protocol/com_stmt_prepare_ok.rs index e015e7361..57210809b 100644 --- a/sqlx-core/src/mysql/protocol/com_stmt_prepare_ok.rs +++ b/sqlx-core/src/mysql/protocol/com_stmt_prepare_ok.rs @@ -20,7 +20,7 @@ pub(crate) struct ComStmtPrepareOk { } impl ComStmtPrepareOk { - pub(crate) fn read(mut buf: &[u8]) -> crate::Result { + pub(crate) fn read(mut buf: &[u8]) -> crate::Result { let header = buf.get_u8()?; if header != 0x00 { diff --git a/sqlx-core/src/mysql/protocol/eof.rs b/sqlx-core/src/mysql/protocol/eof.rs index de6899b1d..d355059c9 100644 --- a/sqlx-core/src/mysql/protocol/eof.rs +++ b/sqlx-core/src/mysql/protocol/eof.rs @@ -13,7 +13,7 @@ pub struct EofPacket { } impl EofPacket { - pub(crate) fn read(mut buf: &[u8]) -> crate::Result + pub(crate) fn read(mut buf: &[u8]) -> crate::Result where Self: Sized, { diff --git a/sqlx-core/src/mysql/protocol/err.rs b/sqlx-core/src/mysql/protocol/err.rs index dc5ca524e..0d35ce94d 100644 --- a/sqlx-core/src/mysql/protocol/err.rs +++ b/sqlx-core/src/mysql/protocol/err.rs @@ -14,7 +14,7 @@ pub struct ErrPacket { } impl ErrPacket { - pub(crate) fn read(mut buf: &[u8], capabilities: Capabilities) -> crate::Result + pub(crate) fn read(mut buf: &[u8], capabilities: Capabilities) -> crate::Result where Self: Sized, { diff --git a/sqlx-core/src/mysql/protocol/handshake.rs b/sqlx-core/src/mysql/protocol/handshake.rs index ac74319f6..dd9c474e2 100644 --- a/sqlx-core/src/mysql/protocol/handshake.rs +++ b/sqlx-core/src/mysql/protocol/handshake.rs @@ -19,7 +19,7 @@ pub(crate) struct Handshake { } impl Handshake { - pub(crate) fn read(mut buf: &[u8]) -> crate::Result + pub(crate) fn read(mut buf: &[u8]) -> crate::Result where Self: Sized, { diff --git a/sqlx-core/src/mysql/protocol/ok.rs b/sqlx-core/src/mysql/protocol/ok.rs index 254b2dfd6..d31911887 100644 --- a/sqlx-core/src/mysql/protocol/ok.rs +++ b/sqlx-core/src/mysql/protocol/ok.rs @@ -17,7 +17,7 @@ pub(crate) struct OkPacket { } impl OkPacket { - pub(crate) fn read(mut buf: &[u8]) -> crate::Result + pub(crate) fn read(mut buf: &[u8]) -> crate::Result where Self: Sized, { diff --git a/sqlx-core/src/mysql/protocol/row.rs b/sqlx-core/src/mysql/protocol/row.rs index 45d4a657d..50e02f6f9 100644 --- a/sqlx-core/src/mysql/protocol/row.rs +++ b/sqlx-core/src/mysql/protocol/row.rs @@ -60,7 +60,7 @@ impl<'c> Row<'c> { columns: &'c [MySqlTypeInfo], values: &'c mut Vec>>, binary: bool, - ) -> crate::Result { + ) -> crate::Result { let buffer = &*buf; values.clear(); @@ -163,7 +163,7 @@ impl<'c> Row<'c> { // use super::*; // // #[test] -// fn null_bitmap_test() -> crate::Result { +// fn null_bitmap_test() -> crate::Result<()> { // let column_len = ColumnCount::decode(&[26])?; // assert_eq!(column_len.columns, 26); // diff --git a/sqlx-core/src/mysql/row.rs b/sqlx-core/src/mysql/row.rs index fa9c4583c..82cb141ce 100644 --- a/sqlx-core/src/mysql/row.rs +++ b/sqlx-core/src/mysql/row.rs @@ -20,7 +20,7 @@ impl<'c> Row<'c> for MySqlRow<'c> { } #[doc(hidden)] - fn try_get_raw(&self, index: I) -> crate::Result> + fn try_get_raw(&self, index: I) -> crate::Result> where I: ColumnIndex<'c, Self>, { @@ -38,7 +38,7 @@ impl<'c> Row<'c> for MySqlRow<'c> { } impl<'c> ColumnIndex<'c, MySqlRow<'c>> for usize { - fn index(&self, row: &MySqlRow<'c>) -> crate::Result { + fn index(&self, row: &MySqlRow<'c>) -> crate::Result { let len = Row::len(row); if *self >= len { @@ -50,7 +50,7 @@ impl<'c> ColumnIndex<'c, MySqlRow<'c>> for usize { } impl<'c> ColumnIndex<'c, MySqlRow<'c>> for str { - fn index(&self, row: &MySqlRow<'c>) -> crate::Result { + fn index(&self, row: &MySqlRow<'c>) -> crate::Result { row.names .get(self) .ok_or_else(|| crate::Error::ColumnNotFound((*self).into())) diff --git a/sqlx-core/src/mysql/rsa.rs b/sqlx-core/src/mysql/rsa.rs index 09d74fc83..298726d4b 100644 --- a/sqlx-core/src/mysql/rsa.rs +++ b/sqlx-core/src/mysql/rsa.rs @@ -7,7 +7,7 @@ use rand::{thread_rng, Rng}; // For the love of crypto, please delete as much of this as possible and use the RSA crate // directly when that PR is merged -pub fn encrypt(key: &[u8], message: &[u8]) -> crate::Result> { +pub fn encrypt(key: &[u8], message: &[u8]) -> crate::Result> { let key = std::str::from_utf8(key).map_err(|_err| { // TODO(@abonander): protocol_err doesn't like referring to [err] protocol_err!("unexpected error decoding what should be UTF-8") @@ -97,7 +97,7 @@ fn oaep_encrypt( rng: &mut R, pub_key: &PublicKey, msg: &[u8], -) -> crate::Result> { +) -> crate::Result> { // size of [n] in bytes let k = (pub_key.n.bits() + 7) / 8; @@ -141,7 +141,7 @@ struct PublicKey { e: BigUint, } -fn parse(key: &str) -> crate::Result { +fn parse(key: &str) -> crate::Result { // This takes advantage of the knowledge that we know // we are receiving a PKCS#8 RSA Public Key at all // times from MySQL diff --git a/sqlx-core/src/mysql/stream.rs b/sqlx-core/src/mysql/stream.rs index 44fe5d95c..ed1cb4610 100644 --- a/sqlx-core/src/mysql/stream.rs +++ b/sqlx-core/src/mysql/stream.rs @@ -5,7 +5,7 @@ use byteorder::{ByteOrder, LittleEndian}; use crate::io::{Buf, BufMut, BufStream, MaybeTlsStream}; use crate::mysql::protocol::{Capabilities, Encode, EofPacket, ErrPacket, OkPacket}; use crate::mysql::MySql; -use crate::mysql::MySqlDatabaseError; +use crate::mysql::MySqlError; use crate::url::Url; // Size before a packet is split @@ -33,7 +33,7 @@ pub(crate) struct MySqlStream { } impl MySqlStream { - pub(super) async fn new(url: &Url) -> crate::Result { + pub(super) async fn new(url: &Url) -> crate::Result { let stream = MaybeTlsStream::connect(&url, 3306).await?; let mut capabilities = Capabilities::PROTOCOL_41 @@ -69,12 +69,12 @@ impl MySqlStream { self.stream.is_tls() } - pub(super) fn shutdown(&self) -> crate::Result { + pub(super) fn shutdown(&self) -> crate::Result<()> { Ok(self.stream.shutdown(Shutdown::Both)?) } #[inline] - pub(super) async fn send(&mut self, packet: T, initial: bool) -> crate::Result + pub(super) async fn send(&mut self, packet: T, initial: bool) -> crate::Result<()> where T: Encode + std::fmt::Debug, { @@ -87,7 +87,7 @@ impl MySqlStream { } #[inline] - pub(super) async fn flush(&mut self) -> crate::Result { + pub(super) async fn flush(&mut self) -> crate::Result<()> { Ok(self.stream.flush().await?) } @@ -121,13 +121,13 @@ impl MySqlStream { } #[inline] - pub(super) async fn receive(&mut self) -> crate::Result { + pub(super) async fn receive(&mut self) -> crate::Result<&[u8]> { self.read().await?; Ok(self.packet()) } - pub(super) async fn read(&mut self) -> crate::Result { + pub(super) async fn read(&mut self) -> crate::Result<()> { self.packet_buf.clear(); self.packet_len = 0; @@ -167,7 +167,7 @@ impl MySqlStream { } impl MySqlStream { - pub(crate) async fn maybe_receive_eof(&mut self) -> crate::Result { + pub(crate) async fn maybe_receive_eof(&mut self) -> crate::Result<()> { if !self.capabilities.contains(Capabilities::DEPRECATE_EOF) { let _eof = EofPacket::read(self.receive().await?)?; } @@ -175,7 +175,7 @@ impl MySqlStream { Ok(()) } - pub(crate) fn maybe_handle_eof(&mut self) -> crate::Result> { + pub(crate) fn maybe_handle_eof(&mut self) -> crate::Result> { if !self.capabilities.contains(Capabilities::DEPRECATE_EOF) && self.packet()[0] == 0xFE { Ok(Some(EofPacket::read(self.packet())?)) } else { @@ -183,21 +183,21 @@ impl MySqlStream { } } - pub(crate) fn handle_unexpected(&mut self) -> crate::Result { + pub(crate) fn handle_unexpected(&mut self) -> crate::Result { Err(protocol_err!("unexpected packet identifier 0x{:X?}", self.packet()[0]).into()) } - pub(crate) fn handle_err(&mut self) -> crate::Result { + pub(crate) fn handle_err(&mut self) -> crate::Result { self.is_ready = true; - Err(MySqlDatabaseError(ErrPacket::read(self.packet(), self.capabilities)?).into()) + Err(MySqlError(ErrPacket::read(self.packet(), self.capabilities)?).into()) } - pub(crate) fn handle_ok(&mut self) -> crate::Result { + pub(crate) fn handle_ok(&mut self) -> crate::Result { self.is_ready = true; OkPacket::read(self.packet()) } - pub(crate) async fn wait_until_ready(&mut self) -> crate::Result { + pub(crate) async fn wait_until_ready(&mut self) -> crate::Result<()> { if !self.is_ready { loop { let packet_id = self.receive().await?[0]; diff --git a/sqlx-core/src/mysql/tls.rs b/sqlx-core/src/mysql/tls.rs index 84b6a46b5..5860c4825 100644 --- a/sqlx-core/src/mysql/tls.rs +++ b/sqlx-core/src/mysql/tls.rs @@ -3,10 +3,7 @@ use crate::mysql::stream::MySqlStream; use crate::url::Url; #[cfg_attr(not(feature = "tls"), allow(unused_variables))] -pub(super) async fn upgrade_if_needed( - stream: &mut MySqlStream, - url: &Url, -) -> crate::Result { +pub(super) async fn upgrade_if_needed(stream: &mut MySqlStream, url: &Url) -> crate::Result<()> { #[cfg_attr(not(feature = "tls"), allow(unused_imports))] use crate::mysql::protocol::Capabilities; @@ -93,7 +90,7 @@ async fn try_upgrade( url: &Url, ca_file: Option<&str>, accept_invalid_hostnames: bool, -) -> crate::Result { +) -> crate::Result<()> { use crate::mysql::protocol::SslRequest; use crate::runtime::fs; diff --git a/sqlx-core/src/mysql/types/bool.rs b/sqlx-core/src/mysql/types/bool.rs index 587331840..1878f6c79 100644 --- a/sqlx-core/src/mysql/types/bool.rs +++ b/sqlx-core/src/mysql/types/bool.rs @@ -18,7 +18,7 @@ impl Encode for bool { } impl<'de> Decode<'de, MySql> for bool { - fn decode(value: MySqlValue<'de>) -> crate::Result { + fn decode(value: MySqlValue<'de>) -> crate::Result { match value.try_get()? { MySqlData::Binary(buf) => Ok(buf.get(0).map(|&b| b != 0).unwrap_or_default()), diff --git a/sqlx-core/src/mysql/types/bytes.rs b/sqlx-core/src/mysql/types/bytes.rs index 6f50e27b4..216d2800d 100644 --- a/sqlx-core/src/mysql/types/bytes.rs +++ b/sqlx-core/src/mysql/types/bytes.rs @@ -38,7 +38,7 @@ impl Encode for Vec { } impl<'de> Decode<'de, MySql> for Vec { - fn decode(value: MySqlValue<'de>) -> crate::Result { + fn decode(value: MySqlValue<'de>) -> crate::Result { match value.try_get()? { MySqlData::Binary(buf) | MySqlData::Text(buf) => Ok(buf.to_vec()), } @@ -46,7 +46,7 @@ impl<'de> Decode<'de, MySql> for Vec { } impl<'de> Decode<'de, MySql> for &'de [u8] { - fn decode(value: MySqlValue<'de>) -> crate::Result { + fn decode(value: MySqlValue<'de>) -> crate::Result { match value.try_get()? { MySqlData::Binary(buf) | MySqlData::Text(buf) => Ok(buf), } diff --git a/sqlx-core/src/mysql/types/chrono.rs b/sqlx-core/src/mysql/types/chrono.rs index 41ab27001..a811cf733 100644 --- a/sqlx-core/src/mysql/types/chrono.rs +++ b/sqlx-core/src/mysql/types/chrono.rs @@ -26,7 +26,7 @@ impl Encode for DateTime { } impl<'de> Decode<'de, MySql> for DateTime { - fn decode(value: MySqlValue<'de>) -> crate::Result { + fn decode(value: MySqlValue<'de>) -> crate::Result { let naive: NaiveDateTime = Decode::::decode(value)?; Ok(DateTime::from_utc(naive, Utc)) @@ -66,7 +66,7 @@ impl Encode for NaiveTime { } impl<'de> Decode<'de, MySql> for NaiveTime { - fn decode(buf: MySqlValue<'de>) -> crate::Result { + fn decode(buf: MySqlValue<'de>) -> crate::Result { match buf.try_get()? { MySqlData::Binary(mut buf) => { // data length, expecting 8 or 12 (fractional seconds) @@ -110,7 +110,7 @@ impl Encode for NaiveDate { } impl<'de> Decode<'de, MySql> for NaiveDate { - fn decode(buf: MySqlValue<'de>) -> crate::Result { + fn decode(buf: MySqlValue<'de>) -> crate::Result { match buf.try_get()? { MySqlData::Binary(buf) => Ok(decode_date(&buf[1..])), @@ -163,7 +163,7 @@ impl Encode for NaiveDateTime { } impl<'de> Decode<'de, MySql> for NaiveDateTime { - fn decode(buf: MySqlValue<'de>) -> crate::Result { + fn decode(buf: MySqlValue<'de>) -> crate::Result { match buf.try_get()? { MySqlData::Binary(buf) => { let len = buf[0]; @@ -214,7 +214,7 @@ fn encode_time(time: &NaiveTime, include_micros: bool, buf: &mut Vec) { } } -fn decode_time(len: u8, mut buf: &[u8]) -> crate::Result { +fn decode_time(len: u8, mut buf: &[u8]) -> crate::Result { let hour = buf.get_u8()?; let minute = buf.get_u8()?; let seconds = buf.get_u8()?; diff --git a/sqlx-core/src/mysql/types/float.rs b/sqlx-core/src/mysql/types/float.rs index 591eeed1c..2f55bbbef 100644 --- a/sqlx-core/src/mysql/types/float.rs +++ b/sqlx-core/src/mysql/types/float.rs @@ -35,7 +35,7 @@ impl Encode for f32 { } impl<'de> Decode<'de, MySql> for f32 { - fn decode(value: MySqlValue<'de>) -> crate::Result { + fn decode(value: MySqlValue<'de>) -> crate::Result { match value.try_get()? { MySqlData::Binary(mut buf) => buf .read_i32::() @@ -67,7 +67,7 @@ impl Encode for f64 { } impl<'de> Decode<'de, MySql> for f64 { - fn decode(value: MySqlValue<'de>) -> crate::Result { + fn decode(value: MySqlValue<'de>) -> crate::Result { match value.try_get()? { MySqlData::Binary(mut buf) => buf .read_i64::() diff --git a/sqlx-core/src/mysql/types/int.rs b/sqlx-core/src/mysql/types/int.rs index 001a76ede..a67fb6dd8 100644 --- a/sqlx-core/src/mysql/types/int.rs +++ b/sqlx-core/src/mysql/types/int.rs @@ -23,7 +23,7 @@ impl Encode for i8 { } impl<'de> Decode<'de, MySql> for i8 { - fn decode(value: MySqlValue<'de>) -> crate::Result { + fn decode(value: MySqlValue<'de>) -> crate::Result { match value.try_get()? { MySqlData::Binary(mut buf) => buf.read_i8().map_err(Into::into), @@ -48,7 +48,7 @@ impl Encode for i16 { } impl<'de> Decode<'de, MySql> for i16 { - fn decode(value: MySqlValue<'de>) -> crate::Result { + fn decode(value: MySqlValue<'de>) -> crate::Result { match value.try_get()? { MySqlData::Binary(mut buf) => buf.read_i16::().map_err(Into::into), @@ -73,7 +73,7 @@ impl Encode for i32 { } impl<'de> Decode<'de, MySql> for i32 { - fn decode(value: MySqlValue<'de>) -> crate::Result { + fn decode(value: MySqlValue<'de>) -> crate::Result { match value.try_get()? { MySqlData::Binary(mut buf) => buf.read_i32::().map_err(Into::into), @@ -98,7 +98,7 @@ impl Encode for i64 { } impl<'de> Decode<'de, MySql> for i64 { - fn decode(value: MySqlValue<'de>) -> crate::Result { + fn decode(value: MySqlValue<'de>) -> crate::Result { match value.try_get()? { MySqlData::Binary(mut buf) => buf.read_i64::().map_err(Into::into), diff --git a/sqlx-core/src/mysql/types/mod.rs b/sqlx-core/src/mysql/types/mod.rs index ceb8bfe2a..aca54e552 100644 --- a/sqlx-core/src/mysql/types/mod.rs +++ b/sqlx-core/src/mysql/types/mod.rs @@ -317,7 +317,7 @@ impl<'de, T> Decode<'de, MySql> for Option where T: Decode<'de, MySql>, { - fn decode(value: MySqlValue<'de>) -> crate::Result { + fn decode(value: MySqlValue<'de>) -> crate::Result { Ok(if value.get().is_some() { Some(>::decode(value)?) } else { diff --git a/sqlx-core/src/mysql/types/str.rs b/sqlx-core/src/mysql/types/str.rs index a3cddd1f0..421a4f54e 100644 --- a/sqlx-core/src/mysql/types/str.rs +++ b/sqlx-core/src/mysql/types/str.rs @@ -41,7 +41,7 @@ impl Encode for String { } impl<'de> Decode<'de, MySql> for &'de str { - fn decode(value: MySqlValue<'de>) -> crate::Result { + fn decode(value: MySqlValue<'de>) -> crate::Result { match value.try_get()? { MySqlData::Binary(buf) | MySqlData::Text(buf) => { from_utf8(buf).map_err(crate::Error::decode) @@ -51,7 +51,7 @@ impl<'de> Decode<'de, MySql> for &'de str { } impl<'de> Decode<'de, MySql> for String { - fn decode(value: MySqlValue<'de>) -> crate::Result { + fn decode(value: MySqlValue<'de>) -> crate::Result { <&'de str as Decode>::decode(value).map(ToOwned::to_owned) } } diff --git a/sqlx-core/src/mysql/types/time.rs b/sqlx-core/src/mysql/types/time.rs index 949a8e863..dee6ac99f 100644 --- a/sqlx-core/src/mysql/types/time.rs +++ b/sqlx-core/src/mysql/types/time.rs @@ -28,7 +28,7 @@ impl Encode for OffsetDateTime { } impl<'de> Decode<'de, MySql> for OffsetDateTime { - fn decode(value: MySqlValue<'de>) -> crate::Result { + fn decode(value: MySqlValue<'de>) -> crate::Result { let primitive: PrimitiveDateTime = Decode::::decode(value)?; Ok(primitive.assume_utc()) @@ -68,7 +68,7 @@ impl Encode for Time { } impl<'de> Decode<'de, MySql> for Time { - fn decode(value: MySqlValue<'de>) -> crate::Result { + fn decode(value: MySqlValue<'de>) -> crate::Result { match value.try_get()? { MySqlData::Binary(mut buf) => { // data length, expecting 8 or 12 (fractional seconds) @@ -123,7 +123,7 @@ impl Encode for Date { } impl<'de> Decode<'de, MySql> for Date { - fn decode(value: MySqlValue<'de>) -> crate::Result { + fn decode(value: MySqlValue<'de>) -> crate::Result { match value.try_get()? { MySqlData::Binary(buf) => decode_date(&buf[1..]), MySqlData::Text(buf) => { @@ -170,7 +170,7 @@ impl Encode for PrimitiveDateTime { } impl<'de> Decode<'de, MySql> for PrimitiveDateTime { - fn decode(value: MySqlValue<'de>) -> crate::Result { + fn decode(value: MySqlValue<'de>) -> crate::Result { match value.try_get()? { MySqlData::Binary(buf) => { let len = buf[0]; @@ -218,7 +218,7 @@ fn encode_date(date: &Date, buf: &mut Vec) { buf.push(date.day()); } -fn decode_date(buf: &[u8]) -> crate::Result { +fn decode_date(buf: &[u8]) -> crate::Result { Date::try_from_ymd( LittleEndian::read_u16(buf) as i32, buf[2] as u8, @@ -237,7 +237,7 @@ fn encode_time(time: &Time, include_micros: bool, buf: &mut Vec) { } } -fn decode_time(len: u8, mut buf: &[u8]) -> crate::Result { +fn decode_time(len: u8, mut buf: &[u8]) -> crate::Result