Impl From for converting Value <-> BorrowedValue

This commit is contained in:
John Gallagher 2016-05-24 19:36:38 -04:00
parent 5b0cdbaa56
commit 734f18c985
2 changed files with 25 additions and 11 deletions

View File

@ -11,16 +11,6 @@ pub enum BorrowedValue<'a> {
}
impl<'a> BorrowedValue<'a> {
pub fn to_value(&self) -> Value {
match *self {
BorrowedValue::Null => Value::Null,
BorrowedValue::Integer(i) => Value::Integer(i),
BorrowedValue::Real(r) => Value::Real(r),
BorrowedValue::Text(s) => Value::Text(s.to_string()),
BorrowedValue::Blob(b) => Value::Blob(b.to_vec()),
}
}
pub fn as_i64(&self) -> Result<i64> {
match *self {
BorrowedValue::Integer(i) => Ok(i),
@ -49,3 +39,27 @@ impl<'a> BorrowedValue<'a> {
}
}
}
impl<'a> From<BorrowedValue<'a>> for Value {
fn from(borrowed: BorrowedValue) -> Value {
match borrowed {
BorrowedValue::Null => Value::Null,
BorrowedValue::Integer(i) => Value::Integer(i),
BorrowedValue::Real(r) => Value::Real(r),
BorrowedValue::Text(s) => Value::Text(s.to_string()),
BorrowedValue::Blob(b) => Value::Blob(b.to_vec()),
}
}
}
impl<'a> From<&'a Value> for BorrowedValue<'a> {
fn from(value: &'a Value) -> BorrowedValue<'a> {
match *value {
Value::Null => BorrowedValue::Null,
Value::Integer(i) => BorrowedValue::Integer(i),
Value::Real(r) => BorrowedValue::Real(r),
Value::Text(ref s) => BorrowedValue::Text(s),
Value::Blob(ref b) => BorrowedValue::Blob(b),
}
}
}

View File

@ -55,6 +55,6 @@ impl<T: FromSql> FromSql for Option<T> {
impl FromSql for Value {
fn column_result(value: BorrowedValue) -> Result<Self> {
Ok(value.to_value())
Ok(value.into())
}
}