Trait mysql::prelude::FromValue

source ·
pub trait FromValue: Sized {
    type Intermediate: ConvIr<Self>;

    fn from_value(v: Value) -> Self { ... }
    fn from_value_opt(v: Value) -> Result<Self, FromValueError> { ... }
    fn get_intermediate(v: Value) -> Result<Self::Intermediate, FromValueError> { ... }
}
Expand description

Implement this trait to convert value to something.

FromRow requires ability to cheaply rollback FromValue conversion. This ability is provided via Intermediate associated type.

Example implementation:

#[derive(Debug)]
pub struct StringIr {
    bytes: Vec<u8>,
}

impl ConvIr<String> for StringIr {
    fn new(v: Value) -> MyResult<StringIr> {
        match v {
            Value::Bytes(bytes) => match from_utf8(&*bytes) {
                Ok(_) => Ok(StringIr { bytes: bytes }),
                Err(_) => Err(Error::FromValueError(Value::Bytes(bytes))),
            },
            v => Err(Error::FromValueError(v)),
        }
    }
    fn commit(self) -> String {
        unsafe { String::from_utf8_unchecked(self.bytes) }
    }
    fn rollback(self) -> Value {
        Value::Bytes(self.bytes)
    }
}

impl FromValue for String {
    type Intermediate = StringIr;
}

Required Associated Types§

Provided Methods§

source

fn from_value(v: Value) -> Self

Will panic if could not convert v to Self.

source

fn from_value_opt(v: Value) -> Result<Self, FromValueError>

Will return Err(Error::FromValueError(v)) if could not convert v to Self.

source

fn get_intermediate(v: Value) -> Result<Self::Intermediate, FromValueError>

Will return Err(Error::FromValueError(v)) if v is not convertible to Self.

Implementations on Foreign Types§

source§

impl FromValue for Decimal

§

type Intermediate = ParseIr<Decimal>

source§

fn from_value(v: Value) -> Decimal

source§

impl FromValue for i16

source§

impl FromValue for BigInt

source§

impl FromValue for Vec<u8, Global>

source§

impl FromValue for u128

source§

impl FromValue for Uuid

source§

impl FromValue for i8

source§

impl FromValue for bool

source§

impl FromValue for BigUint

source§

impl FromValue for Time

§

type Intermediate = ParseIr<Time>

source§

impl<T> FromValue for Option<T>where
T: FromValue,

source§

impl FromValue for i32

source§

impl FromValue for u16

source§

impl FromValue for u8

source§

impl FromValue for PrimitiveDateTime

§

type Intermediate = ParseIr<PrimitiveDateTime>

source§

impl FromValue for u64

source§

impl FromValue for f32

source§

impl FromValue for String

source§

impl FromValue for BigDecimal

§

type Intermediate = ParseIr<BigDecimal>

source§

fn from_value(v: Value) -> BigDecimal

source§

impl FromValue for Date

§

type Intermediate = ParseIr<Date>

source§

impl FromValue for i64

source§

impl FromValue for i128

source§

impl FromValue for isize

source§

impl FromValue for Value

source§

impl FromValue for Duration

§

type Intermediate = ParseIr<Duration>

source§

impl FromValue for Duration

source§

impl FromValue for f64

source§

impl FromValue for u32

source§

impl FromValue for usize

Implementors§