mirror of
https://github.com/isar/rusqlite.git
synced 2025-08-21 05:41:06 +08:00
Add #[inline]
and #[cold]
in far more places
This commit is contained in:
@@ -9,6 +9,7 @@ use crate::Result;
|
||||
|
||||
/// ISO 8601 calendar date without timezone => "YYYY-MM-DD"
|
||||
impl ToSql for NaiveDate {
|
||||
#[inline]
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
let date_str = self.format("%Y-%m-%d").to_string();
|
||||
Ok(ToSqlOutput::from(date_str))
|
||||
@@ -17,6 +18,7 @@ impl ToSql for NaiveDate {
|
||||
|
||||
/// "YYYY-MM-DD" => ISO 8601 calendar date without timezone.
|
||||
impl FromSql for NaiveDate {
|
||||
#[inline]
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
value
|
||||
.as_str()
|
||||
@@ -29,6 +31,7 @@ impl FromSql for NaiveDate {
|
||||
|
||||
/// ISO 8601 time without timezone => "HH:MM:SS.SSS"
|
||||
impl ToSql for NaiveTime {
|
||||
#[inline]
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
let date_str = self.format("%H:%M:%S%.f").to_string();
|
||||
Ok(ToSqlOutput::from(date_str))
|
||||
@@ -55,6 +58,7 @@ impl FromSql for NaiveTime {
|
||||
/// ISO 8601 combined date and time without timezone =>
|
||||
/// "YYYY-MM-DDTHH:MM:SS.SSS"
|
||||
impl ToSql for NaiveDateTime {
|
||||
#[inline]
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
let date_str = self.format("%Y-%m-%dT%H:%M:%S%.f").to_string();
|
||||
Ok(ToSqlOutput::from(date_str))
|
||||
@@ -84,6 +88,7 @@ impl FromSql for NaiveDateTime {
|
||||
/// Date and time with time zone => UTC RFC3339 timestamp
|
||||
/// ("YYYY-MM-DDTHH:MM:SS.SSS+00:00").
|
||||
impl<Tz: TimeZone> ToSql for DateTime<Tz> {
|
||||
#[inline]
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
Ok(ToSqlOutput::from(self.with_timezone(&Utc).to_rfc3339()))
|
||||
}
|
||||
@@ -120,6 +125,7 @@ impl FromSql for DateTime<Utc> {
|
||||
|
||||
/// RFC3339 ("YYYY-MM-DDTHH:MM:SS.SSS[+-]HH:MM") into `DateTime<Local>`.
|
||||
impl FromSql for DateTime<Local> {
|
||||
#[inline]
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
let utc_dt = DateTime::<Utc>::column_result(value)?;
|
||||
Ok(utc_dt.with_timezone(&Local))
|
||||
|
@@ -94,6 +94,7 @@ pub trait FromSql: Sized {
|
||||
macro_rules! from_sql_integral(
|
||||
($t:ident) => (
|
||||
impl FromSql for $t {
|
||||
#[inline]
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
let i = i64::column_result(value)?;
|
||||
i.try_into().map_err(|_| FromSqlError::OutOfRange(i))
|
||||
@@ -114,12 +115,14 @@ from_sql_integral!(u64);
|
||||
from_sql_integral!(usize);
|
||||
|
||||
impl FromSql for i64 {
|
||||
#[inline]
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
value.as_i64()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromSql for f32 {
|
||||
#[inline]
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
match value {
|
||||
ValueRef::Integer(i) => Ok(i as f32),
|
||||
@@ -130,6 +133,7 @@ impl FromSql for f32 {
|
||||
}
|
||||
|
||||
impl FromSql for f64 {
|
||||
#[inline]
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
match value {
|
||||
ValueRef::Integer(i) => Ok(i as f64),
|
||||
@@ -140,36 +144,42 @@ impl FromSql for f64 {
|
||||
}
|
||||
|
||||
impl FromSql for bool {
|
||||
#[inline]
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
i64::column_result(value).map(|i| !matches!(i, 0))
|
||||
}
|
||||
}
|
||||
|
||||
impl FromSql for String {
|
||||
#[inline]
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
value.as_str().map(ToString::to_string)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromSql for Box<str> {
|
||||
#[inline]
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
value.as_str().map(Into::into)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromSql for std::rc::Rc<str> {
|
||||
#[inline]
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
value.as_str().map(Into::into)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromSql for std::sync::Arc<str> {
|
||||
#[inline]
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
value.as_str().map(Into::into)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromSql for Vec<u8> {
|
||||
#[inline]
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
value.as_blob().map(|b| b.to_vec())
|
||||
}
|
||||
@@ -177,6 +187,7 @@ impl FromSql for Vec<u8> {
|
||||
|
||||
#[cfg(feature = "i128_blob")]
|
||||
impl FromSql for i128 {
|
||||
#[inline]
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
use byteorder::{BigEndian, ByteOrder};
|
||||
|
||||
@@ -192,6 +203,7 @@ impl FromSql for i128 {
|
||||
|
||||
#[cfg(feature = "uuid")]
|
||||
impl FromSql for uuid::Uuid {
|
||||
#[inline]
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
value
|
||||
.as_blob()
|
||||
@@ -204,6 +216,7 @@ impl FromSql for uuid::Uuid {
|
||||
}
|
||||
|
||||
impl<T: FromSql> FromSql for Option<T> {
|
||||
#[inline]
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
match value {
|
||||
ValueRef::Null => Ok(None),
|
||||
@@ -213,6 +226,7 @@ impl<T: FromSql> FromSql for Option<T> {
|
||||
}
|
||||
|
||||
impl FromSql for Value {
|
||||
#[inline]
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
Ok(value.into())
|
||||
}
|
||||
|
@@ -119,11 +119,11 @@ pub enum Type {
|
||||
impl fmt::Display for Type {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match *self {
|
||||
Type::Null => write!(f, "Null"),
|
||||
Type::Integer => write!(f, "Integer"),
|
||||
Type::Real => write!(f, "Real"),
|
||||
Type::Text => write!(f, "Text"),
|
||||
Type::Blob => write!(f, "Blob"),
|
||||
Type::Null => f.pad("Null"),
|
||||
Type::Integer => f.pad("Integer"),
|
||||
Type::Real => f.pad("Real"),
|
||||
Type::Text => f.pad("Text"),
|
||||
Type::Blob => f.pad("Blob"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -260,9 +260,9 @@ mod test {
|
||||
assert_eq!("text", row.get::<_, String>(1).unwrap());
|
||||
assert_eq!(1, row.get::<_, c_int>(2).unwrap());
|
||||
assert!((1.5 - row.get::<_, c_double>(3).unwrap()).abs() < EPSILON);
|
||||
assert!(row.get::<_, Option<c_int>>(4).unwrap().is_none());
|
||||
assert!(row.get::<_, Option<c_double>>(4).unwrap().is_none());
|
||||
assert!(row.get::<_, Option<String>>(4).unwrap().is_none());
|
||||
assert_eq!(row.get::<_, Option<c_int>>(4).unwrap(), None);
|
||||
assert_eq!(row.get::<_, Option<c_double>>(4).unwrap(), None);
|
||||
assert_eq!(row.get::<_, Option<String>>(4).unwrap(), None);
|
||||
|
||||
// check some invalid types
|
||||
|
||||
|
@@ -7,6 +7,7 @@ use crate::Result;
|
||||
|
||||
/// Serialize JSON `Value` to text.
|
||||
impl ToSql for Value {
|
||||
#[inline]
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
Ok(ToSqlOutput::from(serde_json::to_string(self).unwrap()))
|
||||
}
|
||||
@@ -14,6 +15,7 @@ impl ToSql for Value {
|
||||
|
||||
/// Deserialize text/blob to JSON `Value`.
|
||||
impl FromSql for Value {
|
||||
#[inline]
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
match value {
|
||||
ValueRef::Text(s) => serde_json::from_slice(s),
|
||||
|
@@ -8,6 +8,7 @@ const SQLITE_DATETIME_FMT: &str = "%Y-%m-%dT%H:%M:%S.%NZ";
|
||||
const SQLITE_DATETIME_FMT_LEGACY: &str = "%Y-%m-%d %H:%M:%S:%N %z";
|
||||
|
||||
impl ToSql for OffsetDateTime {
|
||||
#[inline]
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
let time_string = self.to_offset(UtcOffset::UTC).format(SQLITE_DATETIME_FMT);
|
||||
Ok(ToSqlOutput::from(time_string))
|
||||
|
@@ -32,6 +32,7 @@ impl<'a, T: ?Sized> From<&'a T> for ToSqlOutput<'a>
|
||||
where
|
||||
&'a T: Into<ValueRef<'a>>,
|
||||
{
|
||||
#[inline]
|
||||
fn from(t: &'a T) -> Self {
|
||||
ToSqlOutput::Borrowed(t.into())
|
||||
}
|
||||
@@ -45,6 +46,7 @@ where
|
||||
macro_rules! from_value(
|
||||
($t:ty) => (
|
||||
impl From<$t> for ToSqlOutput<'_> {
|
||||
#[inline]
|
||||
fn from(t: $t) -> Self { ToSqlOutput::Owned(t.into())}
|
||||
}
|
||||
)
|
||||
@@ -74,6 +76,7 @@ from_value!(i128);
|
||||
from_value!(uuid::Uuid);
|
||||
|
||||
impl ToSql for ToSqlOutput<'_> {
|
||||
#[inline]
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
Ok(match *self {
|
||||
ToSqlOutput::Borrowed(v) => ToSqlOutput::Borrowed(v),
|
||||
@@ -95,24 +98,28 @@ pub trait ToSql {
|
||||
}
|
||||
|
||||
impl<T: ToSql + ToOwned + ?Sized> ToSql for Cow<'_, T> {
|
||||
#[inline]
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
self.as_ref().to_sql()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ToSql + ?Sized> ToSql for Box<T> {
|
||||
#[inline]
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
self.as_ref().to_sql()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ToSql + ?Sized> ToSql for std::rc::Rc<T> {
|
||||
#[inline]
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
self.as_ref().to_sql()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ToSql + ?Sized> ToSql for std::sync::Arc<T> {
|
||||
#[inline]
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
self.as_ref().to_sql()
|
||||
}
|
||||
@@ -133,6 +140,7 @@ impl<T: ToSql + ?Sized> ToSql for std::sync::Arc<T> {
|
||||
macro_rules! to_sql_self(
|
||||
($t:ty) => (
|
||||
impl ToSql for $t {
|
||||
#[inline]
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
Ok(ToSqlOutput::from(*self))
|
||||
}
|
||||
@@ -162,6 +170,7 @@ to_sql_self!(uuid::Uuid);
|
||||
macro_rules! to_sql_self_fallible(
|
||||
($t:ty) => (
|
||||
impl ToSql for $t {
|
||||
#[inline]
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
Ok(ToSqlOutput::Owned(Value::Integer(
|
||||
i64::try_from(*self).map_err(
|
||||
@@ -182,42 +191,49 @@ impl<T: ?Sized> ToSql for &'_ T
|
||||
where
|
||||
T: ToSql,
|
||||
{
|
||||
#[inline]
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
(*self).to_sql()
|
||||
}
|
||||
}
|
||||
|
||||
impl ToSql for String {
|
||||
#[inline]
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
Ok(ToSqlOutput::from(self.as_str()))
|
||||
}
|
||||
}
|
||||
|
||||
impl ToSql for str {
|
||||
#[inline]
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
Ok(ToSqlOutput::from(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl ToSql for Vec<u8> {
|
||||
#[inline]
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
Ok(ToSqlOutput::from(self.as_slice()))
|
||||
}
|
||||
}
|
||||
|
||||
impl ToSql for [u8] {
|
||||
#[inline]
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
Ok(ToSqlOutput::from(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl ToSql for Value {
|
||||
#[inline]
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
Ok(ToSqlOutput::from(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ToSql> ToSql for Option<T> {
|
||||
#[inline]
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
match *self {
|
||||
None => Ok(ToSqlOutput::from(Null)),
|
||||
|
@@ -5,6 +5,7 @@ use url::Url;
|
||||
|
||||
/// Serialize `Url` to text.
|
||||
impl ToSql for Url {
|
||||
#[inline]
|
||||
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
|
||||
Ok(ToSqlOutput::from(self.as_str()))
|
||||
}
|
||||
@@ -12,6 +13,7 @@ impl ToSql for Url {
|
||||
|
||||
/// Deserialize text to `Url`.
|
||||
impl FromSql for Url {
|
||||
#[inline]
|
||||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
|
||||
match value {
|
||||
ValueRef::Text(s) => {
|
||||
|
@@ -19,18 +19,21 @@ pub enum Value {
|
||||
}
|
||||
|
||||
impl From<Null> for Value {
|
||||
#[inline]
|
||||
fn from(_: Null) -> Value {
|
||||
Value::Null
|
||||
}
|
||||
}
|
||||
|
||||
impl From<bool> for Value {
|
||||
#[inline]
|
||||
fn from(i: bool) -> Value {
|
||||
Value::Integer(i as i64)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<isize> for Value {
|
||||
#[inline]
|
||||
fn from(i: isize) -> Value {
|
||||
Value::Integer(i as i64)
|
||||
}
|
||||
@@ -38,6 +41,7 @@ impl From<isize> for Value {
|
||||
|
||||
#[cfg(feature = "i128_blob")]
|
||||
impl From<i128> for Value {
|
||||
#[inline]
|
||||
fn from(i: i128) -> Value {
|
||||
use byteorder::{BigEndian, ByteOrder};
|
||||
let mut buf = vec![0u8; 16];
|
||||
@@ -50,6 +54,7 @@ impl From<i128> for Value {
|
||||
|
||||
#[cfg(feature = "uuid")]
|
||||
impl From<uuid::Uuid> for Value {
|
||||
#[inline]
|
||||
fn from(id: uuid::Uuid) -> Value {
|
||||
Value::Blob(id.as_bytes().to_vec())
|
||||
}
|
||||
@@ -58,6 +63,7 @@ impl From<uuid::Uuid> for Value {
|
||||
macro_rules! from_i64(
|
||||
($t:ty) => (
|
||||
impl From<$t> for Value {
|
||||
#[inline]
|
||||
fn from(i: $t) -> Value {
|
||||
Value::Integer(i64::from(i))
|
||||
}
|
||||
@@ -73,30 +79,35 @@ from_i64!(u16);
|
||||
from_i64!(u32);
|
||||
|
||||
impl From<i64> for Value {
|
||||
#[inline]
|
||||
fn from(i: i64) -> Value {
|
||||
Value::Integer(i)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<f32> for Value {
|
||||
#[inline]
|
||||
fn from(f: f32) -> Value {
|
||||
Value::Real(f.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<f64> for Value {
|
||||
#[inline]
|
||||
fn from(f: f64) -> Value {
|
||||
Value::Real(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for Value {
|
||||
#[inline]
|
||||
fn from(s: String) -> Value {
|
||||
Value::Text(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<u8>> for Value {
|
||||
#[inline]
|
||||
fn from(v: Vec<u8>) -> Value {
|
||||
Value::Blob(v)
|
||||
}
|
||||
@@ -106,6 +117,7 @@ impl<T> From<Option<T>> for Value
|
||||
where
|
||||
T: Into<Value>,
|
||||
{
|
||||
#[inline]
|
||||
fn from(v: Option<T>) -> Value {
|
||||
match v {
|
||||
Some(x) => x.into(),
|
||||
@@ -116,6 +128,7 @@ where
|
||||
|
||||
impl Value {
|
||||
/// Returns SQLite fundamental datatype.
|
||||
#[inline]
|
||||
pub fn data_type(&self) -> Type {
|
||||
match *self {
|
||||
Value::Null => Type::Null,
|
||||
|
@@ -21,6 +21,7 @@ pub enum ValueRef<'a> {
|
||||
|
||||
impl ValueRef<'_> {
|
||||
/// Returns SQLite fundamental datatype.
|
||||
#[inline]
|
||||
pub fn data_type(&self) -> Type {
|
||||
match *self {
|
||||
ValueRef::Null => Type::Null,
|
||||
@@ -35,6 +36,7 @@ impl ValueRef<'_> {
|
||||
impl<'a> ValueRef<'a> {
|
||||
/// If `self` is case `Integer`, returns the integral value. Otherwise,
|
||||
/// returns `Err(Error::InvalidColumnType)`.
|
||||
#[inline]
|
||||
pub fn as_i64(&self) -> FromSqlResult<i64> {
|
||||
match *self {
|
||||
ValueRef::Integer(i) => Ok(i),
|
||||
@@ -44,6 +46,7 @@ impl<'a> ValueRef<'a> {
|
||||
|
||||
/// If `self` is case `Real`, returns the floating point value. Otherwise,
|
||||
/// returns `Err(Error::InvalidColumnType)`.
|
||||
#[inline]
|
||||
pub fn as_f64(&self) -> FromSqlResult<f64> {
|
||||
match *self {
|
||||
ValueRef::Real(f) => Ok(f),
|
||||
@@ -53,6 +56,7 @@ impl<'a> ValueRef<'a> {
|
||||
|
||||
/// If `self` is case `Text`, returns the string value. Otherwise, returns
|
||||
/// `Err(Error::InvalidColumnType)`.
|
||||
#[inline]
|
||||
pub fn as_str(&self) -> FromSqlResult<&'a str> {
|
||||
match *self {
|
||||
ValueRef::Text(t) => {
|
||||
@@ -64,6 +68,7 @@ impl<'a> ValueRef<'a> {
|
||||
|
||||
/// If `self` is case `Blob`, returns the byte slice. Otherwise, returns
|
||||
/// `Err(Error::InvalidColumnType)`.
|
||||
#[inline]
|
||||
pub fn as_blob(&self) -> FromSqlResult<&'a [u8]> {
|
||||
match *self {
|
||||
ValueRef::Blob(b) => Ok(b),
|
||||
@@ -73,6 +78,7 @@ impl<'a> ValueRef<'a> {
|
||||
}
|
||||
|
||||
impl From<ValueRef<'_>> for Value {
|
||||
#[inline]
|
||||
fn from(borrowed: ValueRef<'_>) -> Value {
|
||||
match borrowed {
|
||||
ValueRef::Null => Value::Null,
|
||||
@@ -88,18 +94,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 +124,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(),
|
||||
|
Reference in New Issue
Block a user