Trait mysql_async::prelude::FromValue [] [src]

pub trait FromValue: Sized {
    type Intermediate: ConvIr<Self>;
    fn from_value(v: Value) -> Self { ... }
    fn from_value_opt(v: Value) -> Result<Self> { ... }
    fn get_intermediate(v: Value) -> Result<Self::Intermediate> { ... }
}

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) -> Result<StringIr> {
        match v {
            Value::Bytes(bytes) => match from_utf8(&*bytes) {
                Ok(_) => Ok(StringIr { bytes: bytes }),
                Err(_) => Err(ErrorKind::FromValue(Value::Bytes(bytes)).into()),
            },
            v => Err(ErrorKind::FromValue(v).into()),
        }
    }
    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;
}

Associated Types

Provided Methods

Will panic if could not convert v to Self.

Will return Err(ErrorKind::FromValue(v).into()) if could not convert v to Self.

Will return Err(ErrorKind::FromValue(v).into()) if v is not convertible to Self.

Implementors