aboutsummaryrefslogtreecommitdiff
path: root/src/types/value_ref.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/types/value_ref.rs')
-rw-r--r--src/types/value_ref.rs103
1 files changed, 96 insertions, 7 deletions
diff --git a/src/types/value_ref.rs b/src/types/value_ref.rs
index 2f32434..c0d81ca 100644
--- a/src/types/value_ref.rs
+++ b/src/types/value_ref.rs
@@ -4,7 +4,7 @@ use crate::types::{FromSqlError, FromSqlResult};
/// A non-owning [dynamic type value](http://sqlite.org/datatype3.html). Typically the
/// memory backing this value is owned by SQLite.
///
-/// See [`Value`](enum.Value.html) for an owning dynamic type value.
+/// See [`Value`](Value) for an owning dynamic type value.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum ValueRef<'a> {
/// The value is a `NULL` value.
@@ -21,6 +21,8 @@ pub enum ValueRef<'a> {
impl ValueRef<'_> {
/// Returns SQLite fundamental datatype.
+ #[inline]
+ #[must_use]
pub fn data_type(&self) -> Type {
match *self {
ValueRef::Null => Type::Null,
@@ -34,7 +36,9 @@ impl ValueRef<'_> {
impl<'a> ValueRef<'a> {
/// If `self` is case `Integer`, returns the integral value. Otherwise,
- /// returns `Err(Error::InvalidColumnType)`.
+ /// returns [`Err(Error::InvalidColumnType)`](crate::Error::
+ /// InvalidColumnType).
+ #[inline]
pub fn as_i64(&self) -> FromSqlResult<i64> {
match *self {
ValueRef::Integer(i) => Ok(i),
@@ -42,8 +46,23 @@ impl<'a> ValueRef<'a> {
}
}
+ /// If `self` is case `Null` returns None.
+ /// If `self` is case `Integer`, returns the integral value.
+ /// Otherwise returns [`Err(Error::InvalidColumnType)`](crate::Error::
+ /// InvalidColumnType).
+ #[inline]
+ pub fn as_i64_or_null(&self) -> FromSqlResult<Option<i64>> {
+ match *self {
+ ValueRef::Null => Ok(None),
+ ValueRef::Integer(i) => Ok(Some(i)),
+ _ => Err(FromSqlError::InvalidType),
+ }
+ }
+
/// If `self` is case `Real`, returns the floating point value. Otherwise,
- /// returns `Err(Error::InvalidColumnType)`.
+ /// returns [`Err(Error::InvalidColumnType)`](crate::Error::
+ /// InvalidColumnType).
+ #[inline]
pub fn as_f64(&self) -> FromSqlResult<f64> {
match *self {
ValueRef::Real(f) => Ok(f),
@@ -51,8 +70,22 @@ impl<'a> ValueRef<'a> {
}
}
+ /// If `self` is case `Null` returns None.
+ /// If `self` is case `Real`, returns the floating point value.
+ /// Otherwise returns [`Err(Error::InvalidColumnType)`](crate::Error::
+ /// InvalidColumnType).
+ #[inline]
+ pub fn as_f64_or_null(&self) -> FromSqlResult<Option<f64>> {
+ match *self {
+ ValueRef::Null => Ok(None),
+ ValueRef::Real(f) => Ok(Some(f)),
+ _ => Err(FromSqlError::InvalidType),
+ }
+ }
+
/// If `self` is case `Text`, returns the string value. Otherwise, returns
- /// `Err(Error::InvalidColumnType)`.
+ /// [`Err(Error::InvalidColumnType)`](crate::Error::InvalidColumnType).
+ #[inline]
pub fn as_str(&self) -> FromSqlResult<&'a str> {
match *self {
ValueRef::Text(t) => {
@@ -62,17 +95,69 @@ impl<'a> ValueRef<'a> {
}
}
+ /// If `self` is case `Null` returns None.
+ /// If `self` is case `Text`, returns the string value.
+ /// Otherwise returns [`Err(Error::InvalidColumnType)`](crate::Error::
+ /// InvalidColumnType).
+ #[inline]
+ pub fn as_str_or_null(&self) -> FromSqlResult<Option<&'a str>> {
+ match *self {
+ ValueRef::Null => Ok(None),
+ ValueRef::Text(t) => std::str::from_utf8(t)
+ .map_err(|e| FromSqlError::Other(Box::new(e)))
+ .map(Some),
+ _ => Err(FromSqlError::InvalidType),
+ }
+ }
+
/// If `self` is case `Blob`, returns the byte slice. Otherwise, returns
- /// `Err(Error::InvalidColumnType)`.
+ /// [`Err(Error::InvalidColumnType)`](crate::Error::InvalidColumnType).
+ #[inline]
pub fn as_blob(&self) -> FromSqlResult<&'a [u8]> {
match *self {
ValueRef::Blob(b) => Ok(b),
_ => Err(FromSqlError::InvalidType),
}
}
+
+ /// If `self` is case `Null` returns None.
+ /// If `self` is case `Blob`, returns the byte slice.
+ /// Otherwise returns [`Err(Error::InvalidColumnType)`](crate::Error::
+ /// InvalidColumnType).
+ #[inline]
+ pub fn as_blob_or_null(&self) -> FromSqlResult<Option<&'a [u8]>> {
+ match *self {
+ ValueRef::Null => Ok(None),
+ ValueRef::Blob(b) => Ok(Some(b)),
+ _ => Err(FromSqlError::InvalidType),
+ }
+ }
+
+ /// Returns the byte slice that makes up this ValueRef if it's either
+ /// [`ValueRef::Blob`] or [`ValueRef::Text`].
+ #[inline]
+ pub fn as_bytes(&self) -> FromSqlResult<&'a [u8]> {
+ match self {
+ ValueRef::Text(s) | ValueRef::Blob(s) => Ok(s),
+ _ => Err(FromSqlError::InvalidType),
+ }
+ }
+
+ /// If `self` is case `Null` returns None.
+ /// If `self` is [`ValueRef::Blob`] or [`ValueRef::Text`] returns the byte
+ /// slice that makes up this value
+ #[inline]
+ pub fn as_bytes_or_null(&self) -> FromSqlResult<Option<&'a [u8]>> {
+ match *self {
+ ValueRef::Null => Ok(None),
+ ValueRef::Text(s) | ValueRef::Blob(s) => Ok(Some(s)),
+ _ => Err(FromSqlError::InvalidType),
+ }
+ }
}
impl From<ValueRef<'_>> for Value {
+ #[inline]
fn from(borrowed: ValueRef<'_>) -> Value {
match borrowed {
ValueRef::Null => Value::Null,
@@ -88,18 +173,21 @@ impl From<ValueRef<'_>> for Value {
}
impl<'a> From<&'a str> for ValueRef<'a> {
+ #[inline]
fn from(s: &str) -> ValueRef<'_> {
ValueRef::Text(s.as_bytes())
}
}
impl<'a> From<&'a [u8]> for ValueRef<'a> {
+ #[inline]
fn from(s: &[u8]) -> ValueRef<'_> {
ValueRef::Blob(s)
}
}
impl<'a> From<&'a Value> for ValueRef<'a> {
+ #[inline]
fn from(value: &'a Value) -> ValueRef<'a> {
match *value {
Value::Null => ValueRef::Null,
@@ -115,6 +203,7 @@ impl<'a, T> From<Option<T>> for ValueRef<'a>
where
T: Into<ValueRef<'a>>,
{
+ #[inline]
fn from(s: Option<T>) -> ValueRef<'a> {
match s {
Some(x) => x.into(),
@@ -140,7 +229,7 @@ impl<'a> ValueRef<'a> {
!text.is_null(),
"unexpected SQLITE_TEXT value type with NULL data"
);
- let s = from_raw_parts(text as *const u8, len as usize);
+ let s = from_raw_parts(text.cast::<u8>(), len as usize);
ValueRef::Text(s)
}
ffi::SQLITE_BLOB => {
@@ -158,7 +247,7 @@ impl<'a> ValueRef<'a> {
!blob.is_null(),
"unexpected SQLITE_BLOB value type with NULL data"
);
- ValueRef::Blob(from_raw_parts(blob as *const u8, len as usize))
+ ValueRef::Blob(from_raw_parts(blob.cast::<u8>(), len as usize))
} else {
// The return value from sqlite3_value_blob() for a zero-length BLOB
// is a NULL pointer.