pub trait Decode<'r, DB>: Sizedwhere
DB: Database,{
// Required method
fn decode(
value: <DB as Database>::ValueRef<'r>,
) -> Result<Self, Box<dyn Error + Sync + Send>>;
}
Expand description
A type that can be decoded from the database.
§How can I implement Decode
?
A manual implementation of Decode
can be useful when adding support for
types externally to SQLx.
The following showcases how to implement Decode
to be generic over Database
. The
implementation can be marginally simpler if you remove the DB
type parameter and explicitly
use the concrete ValueRef
and TypeInfo
types.
struct MyType;
// DB is the database driver
// `'r` is the lifetime of the `Row` being decoded
impl<'r, DB: Database> Decode<'r, DB> for MyType
where
// we want to delegate some of the work to string decoding so let's make sure strings
// are supported by the database
&'r str: Decode<'r, DB>
{
fn decode(
value: <DB as Database>::ValueRef<'r>,
) -> Result<MyType, Box<dyn Error + 'static + Send + Sync>> {
// the interface of ValueRef is largely unstable at the moment
// so this is not directly implementable
// however, you can delegate to a type that matches the format of the type you want
// to decode (such as a UTF-8 string)
let value = <&str as Decode<DB>>::decode(value)?;
// now you can parse this into your type (assuming there is a `FromStr`)
Ok(value.parse()?)
}
}
Required Methods§
Object Safety§
Implementations on Foreign Types§
source§impl<'r, T1, T2, T3> Decode<'r, Postgres> for (T1, T2, T3)
impl<'r, T1, T2, T3> Decode<'r, Postgres> for (T1, T2, T3)
fn decode( value: PgValueRef<'r>, ) -> Result<(T1, T2, T3), Box<dyn Error + Sync + Send>>
source§impl<'r, T1, T2, T3, T4> Decode<'r, Postgres> for (T1, T2, T3, T4)
impl<'r, T1, T2, T3, T4> Decode<'r, Postgres> for (T1, T2, T3, T4)
fn decode( value: PgValueRef<'r>, ) -> Result<(T1, T2, T3, T4), Box<dyn Error + Sync + Send>>
source§impl<'r, T1, T2, T3, T4, T5> Decode<'r, Postgres> for (T1, T2, T3, T4, T5)
impl<'r, T1, T2, T3, T4, T5> Decode<'r, Postgres> for (T1, T2, T3, T4, T5)
fn decode( value: PgValueRef<'r>, ) -> Result<(T1, T2, T3, T4, T5), Box<dyn Error + Sync + Send>>
source§impl<'r, T1, T2, T3, T4, T5, T6> Decode<'r, Postgres> for (T1, T2, T3, T4, T5, T6)where
T1: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T2: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T3: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T4: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T5: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T6: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
impl<'r, T1, T2, T3, T4, T5, T6> Decode<'r, Postgres> for (T1, T2, T3, T4, T5, T6)where
T1: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T2: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T3: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T4: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T5: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T6: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
fn decode( value: PgValueRef<'r>, ) -> Result<(T1, T2, T3, T4, T5, T6), Box<dyn Error + Sync + Send>>
source§impl<'r, T1, T2, T3, T4, T5, T6, T7> Decode<'r, Postgres> for (T1, T2, T3, T4, T5, T6, T7)where
T1: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T2: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T3: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T4: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T5: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T6: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T7: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
impl<'r, T1, T2, T3, T4, T5, T6, T7> Decode<'r, Postgres> for (T1, T2, T3, T4, T5, T6, T7)where
T1: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T2: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T3: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T4: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T5: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T6: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T7: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
fn decode( value: PgValueRef<'r>, ) -> Result<(T1, T2, T3, T4, T5, T6, T7), Box<dyn Error + Sync + Send>>
source§impl<'r, T1, T2, T3, T4, T5, T6, T7, T8> Decode<'r, Postgres> for (T1, T2, T3, T4, T5, T6, T7, T8)where
T1: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T2: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T3: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T4: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T5: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T6: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T7: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T8: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
impl<'r, T1, T2, T3, T4, T5, T6, T7, T8> Decode<'r, Postgres> for (T1, T2, T3, T4, T5, T6, T7, T8)where
T1: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T2: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T3: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T4: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T5: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T6: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T7: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T8: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
fn decode( value: PgValueRef<'r>, ) -> Result<(T1, T2, T3, T4, T5, T6, T7, T8), Box<dyn Error + Sync + Send>>
source§impl<'r, T1, T2, T3, T4, T5, T6, T7, T8, T9> Decode<'r, Postgres> for (T1, T2, T3, T4, T5, T6, T7, T8, T9)where
T1: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T2: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T3: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T4: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T5: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T6: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T7: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T8: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T9: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
impl<'r, T1, T2, T3, T4, T5, T6, T7, T8, T9> Decode<'r, Postgres> for (T1, T2, T3, T4, T5, T6, T7, T8, T9)where
T1: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T2: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T3: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T4: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T5: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T6: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T7: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T8: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
T9: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,
fn decode( value: PgValueRef<'r>, ) -> Result<(T1, T2, T3, T4, T5, T6, T7, T8, T9), Box<dyn Error + Sync + Send>>
Implementors§
impl Decode<'_, MySql> for BigDecimal
impl Decode<'_, MySql> for Decimal
impl Decode<'_, MySql> for Uuid
impl Decode<'_, MySql> for Hyphenated
impl Decode<'_, MySql> for Simple
impl Decode<'_, Postgres> for IpNetwork
impl Decode<'_, Postgres> for Oid
impl Decode<'_, Postgres> for PgCiText
impl Decode<'_, Postgres> for PgMoney
impl Decode<'_, Postgres> for MacAddress
impl Decode<'_, Postgres> for BigDecimal
§Note: NaN
BigDecimal
has a greater range than NUMERIC
(see the corresponding Encode
impl for details)
but cannot represent NaN
, so decoding may return an error.
impl Decode<'_, Postgres> for BitVec
impl Decode<'_, Postgres> for Decimal
§Note: rust_decimal::Decimal
Has a Smaller Range than NUMERIC
NUMERIC
is can have up to 131,072 digits before the decimal point, and 16,384 digits after it.
See [Section 8.1, Numeric Types] of the Postgres manual for details.
However, rust_decimal::Decimal
is limited to a maximum absolute magnitude of 296 - 1,
a number with 67 decimal digits, and a minimum absolute magnitude of 10-28, a number with, unsurprisingly,
28 decimal digits.
Thus, in contrast with BigDecimal
, NUMERIC
can actually represent every possible value of rust_decimal::Decimal
,
but not the other way around. This means that encoding should never fail, but decoding can.
impl Decode<'_, Postgres> for Uuid
impl Decode<'_, Sqlite> for Uuid
impl Decode<'_, Sqlite> for Hyphenated
impl Decode<'_, Sqlite> for Simple
impl<'de> Decode<'de, Postgres> for PgInterval
impl<'r> Decode<'r, MySql> for MySqlTime
impl<'r> Decode<'r, MySql> for DateTime<Local>
Note: assumes the connection’s time_zone
is set to +00:00
(UTC).
impl<'r> Decode<'r, MySql> for DateTime<Utc>
Note: assumes the connection’s time_zone
is set to +00:00
(UTC).
impl<'r> Decode<'r, MySql> for NaiveDate
impl<'r> Decode<'r, MySql> for NaiveDateTime
impl<'r> Decode<'r, MySql> for NaiveTime
Decode from a TIME
value.
§Errors
Returns an error if the TIME
value is negative or exceeds 23:59:59.999999
.