pub trait Decode<'r, DB: Database>: Sized {
    // Required method
    fn decode(
        value: <DB as HasValueRef<'r>>::ValueRef
    ) -> Result<Self, BoxDynError>;
}
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 HasValueRef<'r>>::ValueRef,
    ) -> 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§

source

fn decode(value: <DB as HasValueRef<'r>>::ValueRef) -> Result<Self, BoxDynError>

Decode a new value of this type using a raw value from the database.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl Decode<'_, Mssql> for bool

source§

impl Decode<'_, Mssql> for f32

source§

impl Decode<'_, Mssql> for f64

source§

impl Decode<'_, Mssql> for i8

source§

impl Decode<'_, Mssql> for i16

source§

impl Decode<'_, Mssql> for i32

source§

impl Decode<'_, Mssql> for i64

source§

impl Decode<'_, Mssql> for u8

source§

impl Decode<'_, Mssql> for String

source§

impl Decode<'_, Mssql> for Vec<u8>

source§

impl Decode<'_, Mssql> for BigDecimal

source§

impl Decode<'_, Mssql> for DateTime<FixedOffset>

source§

impl Decode<'_, Mssql> for DateTime<Utc>

source§

impl Decode<'_, Mssql> for NaiveDate

Decodes Date values received from the server

source§

impl Decode<'_, Mssql> for NaiveDateTime

Decodes DateTime2N values received from the server

source§

impl Decode<'_, Mssql> for NaiveTime

Decodes Time values received from the server

source§

impl Decode<'_, Mssql> for Decimal

source§

impl Decode<'_, MySql> for bool

source§

impl Decode<'_, MySql> for f32

source§

impl Decode<'_, MySql> for f64

source§

impl Decode<'_, MySql> for i8

source§

impl Decode<'_, MySql> for i16

source§

impl Decode<'_, MySql> for i32

source§

impl Decode<'_, MySql> for i64

source§

impl Decode<'_, MySql> for u8

source§

impl Decode<'_, MySql> for u16

source§

impl Decode<'_, MySql> for u32

source§

impl Decode<'_, MySql> for u64

source§

impl Decode<'_, MySql> for String

source§

impl Decode<'_, MySql> for Vec<u8>

source§

impl Decode<'_, MySql> for BigDecimal

source§

impl Decode<'_, MySql> for Decimal

source§

impl Decode<'_, MySql> for Hyphenated

source§

impl Decode<'_, MySql> for Uuid

source§

impl Decode<'_, Postgres> for IpNetwork

source§

fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError>

source§

impl Decode<'_, Postgres> for bool

source§

fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError>

source§

impl Decode<'_, Postgres> for f32

source§

fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError>

source§

impl Decode<'_, Postgres> for f64

source§

fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError>

source§

impl Decode<'_, Postgres> for i8

source§

fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError>

source§

impl Decode<'_, Postgres> for i16

source§

fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError>

source§

impl Decode<'_, Postgres> for i32

source§

fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError>

source§

impl Decode<'_, Postgres> for i64

source§

fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError>

source§

impl Decode<'_, Postgres> for String

source§

fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError>

source§

impl Decode<'_, Postgres> for Vec<u8>

source§

fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError>

source§

impl Decode<'_, Postgres> for BigDecimal

source§

fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError>

source§

impl Decode<'_, Postgres> for BitVec

source§

fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError>

source§

impl Decode<'_, Postgres> for MacAddress

source§

fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError>

source§

impl Decode<'_, Postgres> for Decimal

source§

fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError>

source§

impl Decode<'_, Postgres> for Uuid

source§

fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError>

source§

impl Decode<'_, Sqlite> for BigDecimal

source§

impl Decode<'_, Sqlite> for Decimal

source§

impl Decode<'_, Sqlite> for Hyphenated

source§

impl Decode<'_, Sqlite> for Uuid

source§

impl<'db> Decode<'db, Postgres> for IpAddrwhere IpNetwork: Decode<'db, Postgres>,

source§

fn decode(value: PgValueRef<'db>) -> Result<Self, BoxDynError>

source§

impl<'r> Decode<'r, Any> for &'r strwhere &'r str: AnyDecode<'r>,

source§

fn decode(value: AnyValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r> Decode<'r, Any> for &'r [u8]where &'r [u8]: AnyDecode<'r>,

source§

fn decode(value: AnyValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r> Decode<'r, Any> for boolwhere bool: AnyDecode<'r>,

source§

fn decode(value: AnyValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r> Decode<'r, Any> for f32where f32: AnyDecode<'r>,

source§

fn decode(value: AnyValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r> Decode<'r, Any> for f64where f64: AnyDecode<'r>,

source§

fn decode(value: AnyValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r> Decode<'r, Any> for i16where i16: AnyDecode<'r>,

source§

fn decode(value: AnyValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r> Decode<'r, Any> for i32where i32: AnyDecode<'r>,

source§

fn decode(value: AnyValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r> Decode<'r, Any> for i64where i64: AnyDecode<'r>,

source§

fn decode(value: AnyValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r> Decode<'r, Any> for Stringwhere String: AnyDecode<'r>,

source§

fn decode(value: AnyValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r> Decode<'r, Any> for Vec<u8>where Vec<u8>: AnyDecode<'r>,

source§

fn decode(value: AnyValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r> Decode<'r, Any> for BigDecimalwhere BigDecimal: AnyDecode<'r>,

source§

fn decode(value: AnyValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r> Decode<'r, Any> for DateTime<Local>where DateTime<Local>: AnyDecode<'r>,

source§

fn decode(value: AnyValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r> Decode<'r, Any> for DateTime<Utc>where DateTime<Utc>: AnyDecode<'r>,

source§

fn decode(value: AnyValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r> Decode<'r, Any> for NaiveDatewhere NaiveDate: AnyDecode<'r>,

source§

fn decode(value: AnyValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r> Decode<'r, Any> for NaiveDateTimewhere NaiveDateTime: AnyDecode<'r>,

source§

fn decode(value: AnyValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r> Decode<'r, Any> for NaiveTimewhere NaiveTime: AnyDecode<'r>,

source§

fn decode(value: AnyValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r> Decode<'r, Any> for Decimalwhere Decimal: AnyDecode<'r>,

source§

fn decode(value: AnyValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r> Decode<'r, Mssql> for &'r [u8]

source§

impl<'r> Decode<'r, Mssql> for Cow<'r, str>

source§

impl<'r> Decode<'r, Mssql> for Cow<'r, [u8]>

source§

impl<'r> Decode<'r, MySql> for &'r str

source§

impl<'r> Decode<'r, MySql> for &'r [u8]

source§

impl<'r> Decode<'r, MySql> for Cow<'r, str>

source§

impl<'r> Decode<'r, MySql> for DateTime<FixedOffset>

Note: assumes the connection’s time_zone is set to +00:00 (UTC).

source§

impl<'r> Decode<'r, MySql> for DateTime<Local>

Note: assumes the connection’s time_zone is set to +00:00 (UTC).

source§

impl<'r> Decode<'r, MySql> for DateTime<Utc>

Note: assumes the connection’s time_zone is set to +00:00 (UTC).

source§

impl<'r> Decode<'r, MySql> for NaiveDate

source§

impl<'r> Decode<'r, MySql> for NaiveDateTime

source§

impl<'r> Decode<'r, MySql> for NaiveTime

source§

impl<'r> Decode<'r, MySql> for Date

source§

impl<'r> Decode<'r, MySql> for OffsetDateTime

source§

impl<'r> Decode<'r, MySql> for PrimitiveDateTime

source§

impl<'r> Decode<'r, MySql> for Time

source§

impl<'r> Decode<'r, Postgres> for &'r str

source§

fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r> Decode<'r, Postgres> for &'r [u8]

source§

fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r> Decode<'r, Postgres> for Cow<'r, str>

source§

fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r> Decode<'r, Postgres> for ()

source§

fn decode(_value: PgValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r> Decode<'r, Postgres> for DateTime<FixedOffset>

source§

fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r> Decode<'r, Postgres> for DateTime<Local>

source§

fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r> Decode<'r, Postgres> for DateTime<Utc>

source§

fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r> Decode<'r, Postgres> for NaiveDate

source§

fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r> Decode<'r, Postgres> for NaiveDateTime

source§

fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r> Decode<'r, Postgres> for NaiveTime

source§

fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r> Decode<'r, Postgres> for Date

source§

fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r> Decode<'r, Postgres> for OffsetDateTime

source§

fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r> Decode<'r, Postgres> for PrimitiveDateTime

source§

fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r> Decode<'r, Postgres> for Time

source§

fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r> Decode<'r, Sqlite> for &'r str

source§

impl<'r> Decode<'r, Sqlite> for &'r [u8]

source§

impl<'r> Decode<'r, Sqlite> for Cow<'r, str>

source§

impl<'r> Decode<'r, Sqlite> for bool

source§

impl<'r> Decode<'r, Sqlite> for f32

source§

impl<'r> Decode<'r, Sqlite> for f64

source§

impl<'r> Decode<'r, Sqlite> for i8

source§

impl<'r> Decode<'r, Sqlite> for i16

source§

impl<'r> Decode<'r, Sqlite> for i32

source§

impl<'r> Decode<'r, Sqlite> for i64

source§

impl<'r> Decode<'r, Sqlite> for u8

source§

impl<'r> Decode<'r, Sqlite> for u16

source§

impl<'r> Decode<'r, Sqlite> for u32

source§

impl<'r> Decode<'r, Sqlite> for String

source§

impl<'r> Decode<'r, Sqlite> for Vec<u8>

source§

impl<'r> Decode<'r, Sqlite> for DateTime<FixedOffset>

source§

impl<'r> Decode<'r, Sqlite> for DateTime<Local>

source§

impl<'r> Decode<'r, Sqlite> for DateTime<Utc>

source§

impl<'r> Decode<'r, Sqlite> for NaiveDate

source§

impl<'r> Decode<'r, Sqlite> for NaiveDateTime

source§

impl<'r> Decode<'r, Sqlite> for NaiveTime

source§

impl<'r> Decode<'r, Sqlite> for Date

source§

impl<'r> Decode<'r, Sqlite> for OffsetDateTime

source§

impl<'r> Decode<'r, Sqlite> for PrimitiveDateTime

source§

impl<'r> Decode<'r, Sqlite> for Time

source§

impl<'r, DB, T> Decode<'r, DB> for Option<T>where DB: Database, T: Decode<'r, DB>,

source§

fn decode(value: <DB as HasValueRef<'r>>::ValueRef) -> Result<Self, BoxDynError>

source§

impl<'r, T1> Decode<'r, Postgres> for (T1,)where T1: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,

source§

fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r, T1, T2> Decode<'r, Postgres> for (T1, T2)where T1: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>, T2: 'r + Type<Postgres> + for<'a> Decode<'a, Postgres>,

source§

fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r, T1, T2, T3> Decode<'r, Postgres> for (T1, T2, T3)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>,

source§

fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r, T1, T2, T3, T4> Decode<'r, Postgres> for (T1, T2, T3, T4)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>,

source§

fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r, T1, T2, T3, T4, T5> Decode<'r, Postgres> for (T1, T2, T3, T4, T5)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>,

source§

fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError>

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>,

source§

fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError>

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>,

source§

fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError>

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>,

source§

fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError>

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>,

source§

fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r, T> Decode<'r, Postgres> for Vec<T>where T: for<'a> Decode<'a, Postgres> + Type<Postgres>,

source§

fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<'r, T, const N: usize> Decode<'r, Postgres> for [T; N]where T: for<'a> Decode<'a, Postgres> + Type<Postgres>,

source§

fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError>

source§

impl<const N: usize> Decode<'_, Postgres> for [u8; N]

source§

fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError>

Implementors§

source§

impl Decode<'_, Postgres> for Oid

source§

impl Decode<'_, Postgres> for PgMoney

source§

impl<'de> Decode<'de, Postgres> for PgInterval

source§

impl<'r> Decode<'r, Any> for Valuewhere Value: AnyDecode<'r>,

source§

impl<'r> Decode<'r, Postgres> for PgLQuery

source§

impl<'r> Decode<'r, Postgres> for PgLTree

source§

impl<'r> Decode<'r, Postgres> for PgTimeTz<NaiveTime, FixedOffset>

source§

impl<'r> Decode<'r, Postgres> for PgTimeTz<Time, UtcOffset>

source§

impl<'r, DB> Decode<'r, DB> for &'r JsonRawValuewhere Json<Self>: Decode<'r, DB>, DB: Database,

source§

impl<'r, DB> Decode<'r, DB> for JsonValuewhere Json<Self>: Decode<'r, DB>, DB: Database,

source§

impl<'r, T> Decode<'r, Mssql> for Json<T>where T: Deserialize<'r> + 'r,

source§

impl<'r, T> Decode<'r, MySql> for Json<T>where T: 'r + Deserialize<'r>,

source§

impl<'r, T> Decode<'r, Postgres> for PgRange<T>where T: Type<Postgres> + for<'a> Decode<'a, Postgres>,

source§

impl<'r, T> Decode<'r, Postgres> for Json<T>where T: Deserialize<'r> + 'r,

source§

impl<'r, T> Decode<'r, Sqlite> for Json<T>where T: 'r + Deserialize<'r>,