Trait mysql_common::value::convert::FromValue
source · pub trait FromValue: Sized {
type Intermediate: ConvIr<Self>;
// Provided methods
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§
type Intermediate: ConvIr<Self>
Provided Methods§
sourcefn from_value(v: Value) -> Self
fn from_value(v: Value) -> Self
Will panic if could not convert v
to Self
.
sourcefn from_value_opt(v: Value) -> Result<Self, FromValueError>
fn from_value_opt(v: Value) -> Result<Self, FromValueError>
Will return Err(Error::FromValueError(v))
if could not convert v
to Self
.
sourcefn get_intermediate(v: Value) -> Result<Self::Intermediate, FromValueError>
fn get_intermediate(v: Value) -> Result<Self::Intermediate, FromValueError>
Will return Err(Error::FromValueError(v))
if v
is not convertible to Self
.
Object Safety§
This trait is not object safe.