mirror of
https://github.com/isar/rusqlite.git
synced 2025-11-01 22:38:55 +08:00
Rust 2018 idioms
This commit is contained in:
33
src/lib.rs
33
src/lib.rs
@@ -62,17 +62,14 @@
|
||||
//! ```
|
||||
#![allow(unknown_lints)]
|
||||
|
||||
extern crate libsqlite3_sys as ffi;
|
||||
extern crate lru_cache;
|
||||
use libsqlite3_sys as ffi;
|
||||
|
||||
#[macro_use]
|
||||
extern crate bitflags;
|
||||
#[cfg(any(test, feature = "vtab"))]
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
|
||||
#[cfg(feature = "i128_blob")]
|
||||
extern crate byteorder;
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::convert;
|
||||
use std::default::Default;
|
||||
@@ -142,7 +139,7 @@ pub mod vtab;
|
||||
// Number of cached prepared statements we'll hold on to.
|
||||
const STATEMENT_CACHE_DEFAULT_CAPACITY: usize = 16;
|
||||
/// To be used when your statement has no [parameter](https://sqlite.org/lang_expr.html#varparam).
|
||||
pub const NO_PARAMS: &[&ToSql] = &[];
|
||||
pub const NO_PARAMS: &[&dyn ToSql] = &[];
|
||||
|
||||
/// A typedef of the result returned by many methods.
|
||||
pub type Result<T> = result::Result<T, Error>;
|
||||
@@ -343,7 +340,7 @@ impl Connection {
|
||||
///
|
||||
/// Will return `Err` if `sql` cannot be converted to a C-compatible string
|
||||
/// or if the underlying SQLite call fails.
|
||||
pub fn execute_named(&self, sql: &str, params: &[(&str, &ToSql)]) -> Result<usize> {
|
||||
pub fn execute_named(&self, sql: &str, params: &[(&str, &dyn ToSql)]) -> Result<usize> {
|
||||
self.prepare(sql)
|
||||
.and_then(|mut stmt| stmt.execute_named(params))
|
||||
}
|
||||
@@ -383,7 +380,7 @@ impl Connection {
|
||||
where
|
||||
P: IntoIterator,
|
||||
P::Item: ToSql,
|
||||
F: FnOnce(&Row) -> T,
|
||||
F: FnOnce(&Row<'_, '_>) -> T,
|
||||
{
|
||||
let mut stmt = self.prepare(sql)?;
|
||||
stmt.query_row(params, f)
|
||||
@@ -399,9 +396,9 @@ impl Connection {
|
||||
///
|
||||
/// Will return `Err` if `sql` cannot be converted to a C-compatible string
|
||||
/// or if the underlying SQLite call fails.
|
||||
pub fn query_row_named<T, F>(&self, sql: &str, params: &[(&str, &ToSql)], f: F) -> Result<T>
|
||||
pub fn query_row_named<T, F>(&self, sql: &str, params: &[(&str, &dyn ToSql)], f: F) -> Result<T>
|
||||
where
|
||||
F: FnOnce(&Row) -> T,
|
||||
F: FnOnce(&Row<'_, '_>) -> T,
|
||||
{
|
||||
let mut stmt = self.prepare(sql)?;
|
||||
let mut rows = stmt.query_named(params)?;
|
||||
@@ -438,7 +435,7 @@ impl Connection {
|
||||
where
|
||||
P: IntoIterator,
|
||||
P::Item: ToSql,
|
||||
F: FnOnce(&Row) -> result::Result<T, E>,
|
||||
F: FnOnce(&Row<'_, '_>) -> result::Result<T, E>,
|
||||
E: convert::From<Error>,
|
||||
{
|
||||
let mut stmt = self.prepare(sql)?;
|
||||
@@ -594,7 +591,7 @@ impl Connection {
|
||||
}
|
||||
|
||||
impl fmt::Debug for Connection {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("Connection")
|
||||
.field("path", &self.path)
|
||||
.finish()
|
||||
@@ -1055,12 +1052,12 @@ impl Drop for InnerConnection {
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
extern crate tempdir;
|
||||
use self::tempdir::TempDir;
|
||||
pub use super::*;
|
||||
use crate::ffi;
|
||||
pub use std::error::Error as StdError;
|
||||
pub use std::fmt;
|
||||
use tempdir;
|
||||
|
||||
// this function is never called, but is still type checked; in
|
||||
// particular, calls with specific instantiations will require
|
||||
@@ -1548,7 +1545,9 @@ mod test {
|
||||
for (i, v) in vals.iter().enumerate() {
|
||||
let i_to_insert = i as i64;
|
||||
assert_eq!(
|
||||
insert_stmt.execute(&[&i_to_insert as &ToSql, &v]).unwrap(),
|
||||
insert_stmt
|
||||
.execute(&[&i_to_insert as &dyn ToSql, &v])
|
||||
.unwrap(),
|
||||
1
|
||||
);
|
||||
}
|
||||
@@ -1566,7 +1565,7 @@ mod test {
|
||||
}
|
||||
|
||||
mod query_and_then_tests {
|
||||
extern crate libsqlite3_sys as ffi;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -1576,7 +1575,7 @@ mod test {
|
||||
}
|
||||
|
||||
impl fmt::Display for CustomError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> ::std::result::Result<(), fmt::Error> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> ::std::result::Result<(), fmt::Error> {
|
||||
match *self {
|
||||
CustomError::SomeError => write!(f, "{}", self.description()),
|
||||
CustomError::Sqlite(ref se) => write!(f, "{}: {}", self.description(), se),
|
||||
@@ -1589,7 +1588,7 @@ mod test {
|
||||
"my custom error"
|
||||
}
|
||||
|
||||
fn cause(&self) -> Option<&StdError> {
|
||||
fn cause(&self) -> Option<&dyn StdError> {
|
||||
match *self {
|
||||
CustomError::SomeError => None,
|
||||
CustomError::Sqlite(ref se) => Some(se),
|
||||
|
||||
Reference in New Issue
Block a user