Skip to main content

Decode

Trait Decode 

pub trait Decode<'r, DB>: Sized
where 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§

fn decode( value: <DB as Database>::ValueRef<'r>, ) -> Result<Self, Box<dyn Error + Sync + Send>>

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

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

§

impl Decode<'_, Postgres> for bool

§

fn decode(value: PgValueRef<'_>) -> Result<bool, Box<dyn Error + Sync + Send>>

§

impl Decode<'_, Postgres> for f32

§

fn decode(value: PgValueRef<'_>) -> Result<f32, Box<dyn Error + Sync + Send>>

§

impl Decode<'_, Postgres> for f64

§

fn decode(value: PgValueRef<'_>) -> Result<f64, Box<dyn Error + Sync + Send>>

§

impl Decode<'_, Postgres> for i8

§

fn decode(value: PgValueRef<'_>) -> Result<i8, Box<dyn Error + Sync + Send>>

§

impl Decode<'_, Postgres> for i16

§

fn decode(value: PgValueRef<'_>) -> Result<i16, Box<dyn Error + Sync + Send>>

§

impl Decode<'_, Postgres> for i32

§

fn decode(value: PgValueRef<'_>) -> Result<i32, Box<dyn Error + Sync + Send>>

§

impl Decode<'_, Postgres> for i64

§

fn decode(value: PgValueRef<'_>) -> Result<i64, Box<dyn Error + Sync + Send>>

§

impl Decode<'_, Postgres> for Box<[u8]>

§

fn decode( value: PgValueRef<'_>, ) -> Result<Box<[u8]>, Box<dyn Error + Sync + Send>>

§

impl Decode<'_, Postgres> for String

§

fn decode(value: PgValueRef<'_>) -> Result<String, Box<dyn Error + Sync + Send>>

§

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

§

fn decode( value: PgValueRef<'_>, ) -> Result<Vec<u8>, Box<dyn Error + Sync + Send>>

§

impl<'a> Decode<'a, Any> for &'a str

§

fn decode( value: <Any as Database>::ValueRef<'a>, ) -> Result<&'a str, Box<dyn Error + Sync + Send>>

§

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

§

fn decode( value: PgValueRef<'r>, ) -> Result<&'r str, Box<dyn Error + Sync + Send>>

§

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

§

fn decode( value: PgValueRef<'r>, ) -> Result<&'r [u8], Box<dyn Error + Sync + Send>>

§

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

§

fn decode( value: PgValueRef<'r>, ) -> Result<Cow<'r, str>, Box<dyn Error + Sync + Send>>

§

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

§

fn decode(_value: PgValueRef<'r>) -> Result<(), Box<dyn Error + Sync + Send>>

§

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

§

fn decode( value: PgValueRef<'r>, ) -> Result<Box<str>, Box<dyn Error + Sync + Send>>

§

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

§

fn decode( value: <Any as Database>::ValueRef<'r>, ) -> Result<&'r [u8], Box<dyn Error + Sync + Send>>

§

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

§

fn decode( value: <Any as Database>::ValueRef<'r>, ) -> Result<bool, Box<dyn Error + Sync + Send>>

§

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

§

fn decode(value: AnyValueRef<'r>) -> Result<f32, Box<dyn Error + Sync + Send>>

§

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

§

fn decode( value: <Any as Database>::ValueRef<'r>, ) -> Result<f64, Box<dyn Error + Sync + Send>>

§

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

§

fn decode( value: <Any as Database>::ValueRef<'r>, ) -> Result<i16, Box<dyn Error + Sync + Send>>

§

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

§

fn decode( value: <Any as Database>::ValueRef<'r>, ) -> Result<i32, Box<dyn Error + Sync + Send>>

§

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

§

fn decode( value: <Any as Database>::ValueRef<'r>, ) -> Result<i64, Box<dyn Error + Sync + Send>>

§

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

§

fn decode( value: <Any as Database>::ValueRef<'r>, ) -> Result<String, Box<dyn Error + Sync + Send>>

§

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

§

fn decode( value: <Any as Database>::ValueRef<'r>, ) -> Result<Vec<u8>, Box<dyn Error + Sync + Send>>

§

impl<'r, DB> Decode<'r, DB> for NonZero<i8>
where DB: Database, i8: Decode<'r, DB>,

§

fn decode( value: <DB as Database>::ValueRef<'r>, ) -> Result<NonZero<i8>, Box<dyn Error + Sync + Send>>

§

impl<'r, DB> Decode<'r, DB> for NonZero<i16>
where DB: Database, i16: Decode<'r, DB>,

§

fn decode( value: <DB as Database>::ValueRef<'r>, ) -> Result<NonZero<i16>, Box<dyn Error + Sync + Send>>

§

impl<'r, DB> Decode<'r, DB> for NonZero<i32>
where DB: Database, i32: Decode<'r, DB>,

§

fn decode( value: <DB as Database>::ValueRef<'r>, ) -> Result<NonZero<i32>, Box<dyn Error + Sync + Send>>

§

impl<'r, DB> Decode<'r, DB> for NonZero<i64>
where DB: Database, i64: Decode<'r, DB>,

§

fn decode( value: <DB as Database>::ValueRef<'r>, ) -> Result<NonZero<i64>, Box<dyn Error + Sync + Send>>

§

impl<'r, DB> Decode<'r, DB> for NonZero<u8>
where DB: Database, u8: Decode<'r, DB>,

§

fn decode( value: <DB as Database>::ValueRef<'r>, ) -> Result<NonZero<u8>, Box<dyn Error + Sync + Send>>

§

impl<'r, DB> Decode<'r, DB> for NonZero<u16>
where DB: Database, u16: Decode<'r, DB>,

§

fn decode( value: <DB as Database>::ValueRef<'r>, ) -> Result<NonZero<u16>, Box<dyn Error + Sync + Send>>

§

impl<'r, DB> Decode<'r, DB> for NonZero<u32>
where DB: Database, u32: Decode<'r, DB>,

§

fn decode( value: <DB as Database>::ValueRef<'r>, ) -> Result<NonZero<u32>, Box<dyn Error + Sync + Send>>

§

impl<'r, DB> Decode<'r, DB> for NonZero<u64>
where DB: Database, u64: Decode<'r, DB>,

§

fn decode( value: <DB as Database>::ValueRef<'r>, ) -> Result<NonZero<u64>, Box<dyn Error + Sync + Send>>

§

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

§

fn decode( value: <DB as Database>::ValueRef<'r>, ) -> Result<Option<T>, Box<dyn Error + Sync + Send>>

§

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

§

fn decode(value: PgValueRef<'r>) -> Result<(T1,), Box<dyn Error + Sync + Send>>

§

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

§

fn decode( value: PgValueRef<'r>, ) -> Result<(T1, T2), Box<dyn Error + Sync + Send>>

§

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

§

fn decode( value: PgValueRef<'r>, ) -> Result<(T1, T2, T3), Box<dyn Error + Sync + Send>>

§

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

§

fn decode( value: PgValueRef<'r>, ) -> Result<(T1, T2, T3, T4), Box<dyn Error + Sync + Send>>

§

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

§

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, 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, 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, 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, T> Decode<'r, Postgres> for Vec<T>
where T: for<'a> Decode<'a, Postgres> + Type<Postgres>,

§

fn decode(value: PgValueRef<'r>) -> Result<Vec<T>, Box<dyn Error + Sync + Send>>

§

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

§

fn decode(value: PgValueRef<'r>) -> Result<[T; N], Box<dyn Error + Sync + Send>>

§

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

§

fn decode( value: PgValueRef<'_>, ) -> Result<[u8; N], Box<dyn Error + Sync + Send>>

Implementors§

§

impl Decode<'_, Postgres> for Oid

§

impl Decode<'_, Postgres> for PgCiText

§

impl Decode<'_, Postgres> for PgMoney

§

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<'de> Decode<'de, Postgres> for PgInterval

§

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

§

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

§

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

§

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

§

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

§

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

§

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

§

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

§

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

§

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

§

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

§

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

§

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

§

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

§

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

§

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

§

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

§

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

§

impl<'r, DB> Decode<'r, DB> for &'r RawValue
where Json<&'r RawValue>: Decode<'r, DB>, DB: Database,

§

impl<'r, DB> Decode<'r, DB> for Value
where Json<Value>: Decode<'r, DB>, DB: Database,

§

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

§

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

§

impl<'r, T> Decode<'r, Postgres> for Text<T>
where T: FromStr, Box<dyn Error + Sync + Send>: From<<T as FromStr>::Err>,