[][src]Trait diesel::deserialize::FromSql

pub trait FromSql<A, DB: Backend>: Sized {
    fn from_sql(bytes: Option<&DB::RawValue>) -> Result<Self>;
}

Deserialize a single field of a given SQL type.

When possible, implementations of this trait should prefer to use an existing implementation, rather than reading from bytes. (For example, if you are implementing this for an enum which is represented as an integer in the database, prefer i32::from_sql(bytes) over reading from bytes directly)

Types which implement this trait should also have #[derive(FromSqlRow)]

Backend specific details

Examples

Most implementations of this trait will be defined in terms of an existing implementation.

#[repr(i32)]
#[derive(Debug, Clone, Copy)]
pub enum MyEnum {
    A = 1,
    B = 2,
}

impl<DB> FromSql<Integer, DB> for MyEnum
where
    DB: Backend,
    i32: FromSql<Integer, DB>,
{
    fn from_sql(bytes: Option<&DB::RawValue>) -> deserialize::Result<Self> {
        match i32::from_sql(bytes)? {
            1 => Ok(MyEnum::A),
            2 => Ok(MyEnum::B),
            x => Err(format!("Unrecognized variant {}", x).into()),
        }
    }
}

Required methods

fn from_sql(bytes: Option<&DB::RawValue>) -> Result<Self>

See the trait documentation.

Loading content...

Implementations on Foreign Types

impl FromSql<Datetime, Mysql> for MYSQL_TIME[src]

impl FromSql<Timestamp, Mysql> for MYSQL_TIME[src]

impl FromSql<Time, Mysql> for MYSQL_TIME[src]

impl FromSql<Date, Mysql> for MYSQL_TIME[src]

impl FromSql<Datetime, Mysql> for NaiveDateTime[src]

impl FromSql<Timestamp, Mysql> for NaiveDateTime[src]

impl FromSql<Time, Mysql> for NaiveTime[src]

impl FromSql<Date, Mysql> for NaiveDate[src]

impl FromSql<TinyInt, Mysql> for i8[src]

impl FromSql<Unsigned<TinyInt>, Mysql> for u8[src]

impl FromSql<Unsigned<SmallInt>, Mysql> for u16[src]

impl FromSql<Unsigned<Integer>, Mysql> for u32[src]

impl FromSql<Unsigned<BigInt>, Mysql> for u64[src]

impl FromSql<Bool, Mysql> for bool[src]

impl<DB: Backend<RawValue = [u8]>> FromSql<Float, DB> for f32[src]

impl<DB: Backend<RawValue = [u8]>> FromSql<Double, DB> for f64[src]

impl<DB: Backend<RawValue = [u8]>> FromSql<SmallInt, DB> for i16[src]

impl<DB: Backend<RawValue = [u8]>> FromSql<Integer, DB> for i32[src]

impl<DB: Backend<RawValue = [u8]>> FromSql<BigInt, DB> for i64[src]

impl<T, ST, DB> FromSql<Nullable<ST>, DB> for Option<T> where
    T: FromSql<ST, DB>,
    DB: Backend,
    ST: NotNull
[src]

impl<ST, DB> FromSql<ST, DB> for String where
    DB: Backend,
    *const str: FromSql<ST, DB>, 
[src]

impl<DB: Backend<RawValue = [u8]>> FromSql<Text, DB> for *const str[src]

The returned pointer is only valid for the lifetime to the argument of from_sql. This impl is intended for uses where you want to write a new impl in terms of String, but don't want to allocate. We have to return a raw pointer instead of a reference with a lifetime due to the structure of FromSql

impl<ST, DB> FromSql<ST, DB> for Vec<u8> where
    DB: Backend,
    *const [u8]: FromSql<ST, DB>, 
[src]

impl<DB: Backend<RawValue = [u8]>> FromSql<Binary, DB> for *const [u8][src]

The returned pointer is only valid for the lifetime to the argument of from_sql. This impl is intended for uses where you want to write a new impl in terms of Vec<u8>, but don't want to allocate. We have to return a raw pointer instead of a reference with a lifetime due to the structure of FromSql

impl<'a, T: ?Sized, ST, DB> FromSql<ST, DB> for Cow<'a, T> where
    T: 'a + ToOwned,
    DB: Backend,
    T::Owned: FromSql<ST, DB>, 
[src]

Loading content...

Implementors

Loading content...