diff --git a/libsqlite3-sys/src/lib.rs b/libsqlite3-sys/src/lib.rs index 8f4712d..a3411c5 100644 --- a/libsqlite3-sys/src/lib.rs +++ b/libsqlite3-sys/src/lib.rs @@ -48,7 +48,7 @@ pub const SQLITE_NULL : c_int = 5; pub type SqliteDestructor = extern "C" fn(*mut c_void); pub fn SQLITE_TRANSIENT() -> SqliteDestructor { - unsafe { mem::transmute(-1is) } + unsafe { mem::transmute(-1isize) } } pub fn code_to_str(code: c_int) -> &'static str { diff --git a/src/lib.rs b/src/lib.rs index 8286d7a..2579580 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -48,7 +48,7 @@ //! } //! } //! ``` -#![feature(unsafe_destructor, core, std_misc, path, libc, rustc_private, collections, hash)] +#![feature(unsafe_destructor, core, std_misc, path, libc, rustc_private, collections)] #![cfg_attr(test, feature(test))] extern crate libc; @@ -425,7 +425,7 @@ bitflags! { impl InnerSqliteConnection { fn open_with_flags(path: &str, flags: SqliteOpenFlags) -> SqliteResult { - let c_path = CString::from_slice(path.as_bytes()); + let c_path = try!(str_to_cstring(path)); unsafe { let mut db: *mut ffi::sqlite3 = mem::uninitialized(); let r = ffi::sqlite3_open_v2(c_path.as_ptr(), &mut db, flags.bits(), ptr::null()); @@ -476,7 +476,7 @@ impl InnerSqliteConnection { } fn execute_batch(&mut self, sql: &str) -> SqliteResult<()> { - let c_sql = CString::from_slice(sql.as_bytes()); + let c_sql = try!(str_to_cstring(sql)); unsafe { let mut errmsg: *mut c_char = mem::uninitialized(); let r = ffi::sqlite3_exec(self.db, c_sql.as_ptr(), None, ptr::null_mut(), &mut errmsg); @@ -515,7 +515,7 @@ impl InnerSqliteConnection { conn: &'a SqliteConnection, sql: &str) -> SqliteResult> { let mut c_stmt: *mut ffi::sqlite3_stmt = unsafe { mem::uninitialized() }; - let c_sql = CString::from_slice(sql.as_bytes()); + let c_sql = try!(str_to_cstring(sql)); let r = unsafe { let len_with_nul = (sql.len() + 1) as c_int; ffi::sqlite3_prepare_v2(self.db, c_sql.as_ptr(), len_with_nul, &mut c_stmt, diff --git a/src/types.rs b/src/types.rs index b63c03a..a15f4a5 100644 --- a/src/types.rs +++ b/src/types.rs @@ -55,12 +55,11 @@ extern crate time; use libc::{c_int, c_double, c_char}; -use std::ffi as std_ffi; -use std::ffi::{CString}; +use std::ffi::{CStr}; use std::mem; use std::str; use super::ffi; -use super::{SqliteResult, SqliteError}; +use super::{SqliteResult, SqliteError, str_to_cstring}; const SQLITE_DATETIME_FMT: &'static str = "%Y-%m-%d %H:%M:%S"; @@ -90,8 +89,11 @@ raw_to_impl!(c_double, sqlite3_bind_double); impl<'a> ToSql for &'a str { unsafe fn bind_parameter(&self, stmt: *mut ffi::sqlite3_stmt, col: c_int) -> c_int { - let c_str = CString::from_slice(self.as_bytes()); - ffi::sqlite3_bind_text(stmt, col, c_str.as_ptr(), -1, Some(ffi::SQLITE_TRANSIENT())) + if let Ok(c_str) = str_to_cstring(self) { + ffi::sqlite3_bind_text(stmt, col, c_str.as_ptr(), -1, Some(ffi::SQLITE_TRANSIENT())) + } else { + ffi::SQLITE_MISUSE + } } } @@ -177,8 +179,7 @@ impl FromSql for String { if c_text.is_null() { Ok("".to_string()) } else { - let c_text = c_text as *const c_char; - let c_slice = std_ffi::c_str_to_bytes(&c_text); + let c_slice = CStr::from_ptr(c_text as *const c_char).to_bytes(); let utf8_str = str::from_utf8(c_slice); utf8_str .map(|s| { s.to_string() })