Redo FromSql to make implementing it not unsafe.

Pass implementers a BorrowedValue instead of relying on them to use
the FFI interface. We take the responsibility of converting the raw
statement and column index into a BorrowedValue.
This commit is contained in:
John Gallagher
2016-05-23 21:49:54 -04:00
parent c90cd37c00
commit 5b0cdbaa56
5 changed files with 133 additions and 187 deletions

View File

@@ -2,7 +2,7 @@ extern crate time;
use libc::c_int;
use {Error, Result};
use types::{FromSql, ToSql};
use types::{FromSql, ToSql, BorrowedValue};
use ffi::sqlite3_stmt;
@@ -16,16 +16,11 @@ impl ToSql for time::Timespec {
}
impl FromSql for time::Timespec {
unsafe fn column_result(stmt: *mut sqlite3_stmt, col: c_int) -> Result<time::Timespec> {
let s = try!(String::column_result(stmt, col));
match time::strptime(&s, SQLITE_DATETIME_FMT) {
fn column_result(value: BorrowedValue) -> Result<Self> {
value.as_str().and_then(|s| match time::strptime(s, SQLITE_DATETIME_FMT) {
Ok(tm) => Ok(tm.to_timespec()),
Err(err) => Err(Error::FromSqlConversionFailure(Box::new(err))),
}
}
unsafe fn column_has_valid_sqlite_type(stmt: *mut sqlite3_stmt, col: c_int) -> bool {
String::column_has_valid_sqlite_type(stmt, col)
})
}
}