From 734f18c9856637ac3e498ae242f2397496231cb4 Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Tue, 24 May 2016 19:36:38 -0400 Subject: [PATCH] Impl `From` for converting Value <-> BorrowedValue --- src/types/borrowed_value.rs | 34 ++++++++++++++++++++++++---------- src/types/from_sql.rs | 2 +- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/types/borrowed_value.rs b/src/types/borrowed_value.rs index 8b97a33..cbbb977 100644 --- a/src/types/borrowed_value.rs +++ b/src/types/borrowed_value.rs @@ -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 { match *self { BorrowedValue::Integer(i) => Ok(i), @@ -49,3 +39,27 @@ impl<'a> BorrowedValue<'a> { } } } + +impl<'a> From> 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), + } + } +} diff --git a/src/types/from_sql.rs b/src/types/from_sql.rs index a996347..c733f07 100644 --- a/src/types/from_sql.rs +++ b/src/types/from_sql.rs @@ -55,6 +55,6 @@ impl FromSql for Option { impl FromSql for Value { fn column_result(value: BorrowedValue) -> Result { - Ok(value.to_value()) + Ok(value.into()) } }