diff --git a/src/functions.rs b/src/functions.rs index d22a43e..6e8b7c5 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -63,22 +63,22 @@ use types::{ToSql, ToSqlOutput, FromSql, FromSqlError, ValueRef}; use {Result, Error, Connection, str_to_cstring, InnerConnection}; -pub fn set_result<'a>(ctx: *mut sqlite3_context, result: &ToSqlOutput<'a>) { +pub unsafe fn set_result<'a>(ctx: *mut sqlite3_context, result: &ToSqlOutput<'a>) { let value = match *result { ToSqlOutput::Borrowed(v) => v, ToSqlOutput::Owned(ref v) => ValueRef::from(v), #[cfg(feature = "blob")] ToSqlOutput::ZeroBlob(len) => { - return unsafe { ffi::sqlite3_result_zeroblob(ctx, len) }; + return ffi::sqlite3_result_zeroblob(ctx, len); } }; match value { - ValueRef::Null => unsafe { ffi::sqlite3_result_null(ctx) }, - ValueRef::Integer(i) => unsafe { ffi::sqlite3_result_int64(ctx, i) }, - ValueRef::Real(r) => unsafe { ffi::sqlite3_result_double(ctx, r) }, - ValueRef::Text(s) => unsafe { + ValueRef::Null => ffi::sqlite3_result_null(ctx), + ValueRef::Integer(i) => ffi::sqlite3_result_int64(ctx, i), + ValueRef::Real(r) => ffi::sqlite3_result_double(ctx, r), + ValueRef::Text(s) => { let length = s.len(); if length > ::std::i32::MAX as usize { ffi::sqlite3_result_error_toobig(ctx); @@ -96,7 +96,7 @@ pub fn set_result<'a>(ctx: *mut sqlite3_context, result: &ToSqlOutput<'a>) { ffi::sqlite3_result_text(ctx, c_str.as_ptr(), length as c_int, destructor); } }, - ValueRef::Blob(b) => unsafe { + ValueRef::Blob(b) => { let length = b.len(); if length > ::std::i32::MAX as usize { ffi::sqlite3_result_error_toobig(ctx); diff --git a/src/vtab/csvtab.rs b/src/vtab/csvtab.rs index 694b47f..d7c6b99 100644 --- a/src/vtab/csvtab.rs +++ b/src/vtab/csvtab.rs @@ -58,7 +58,7 @@ impl CSVTab { } impl VTab for CSVTab { - fn connect(db: *mut ffi::sqlite3, _aux: *mut c_void, args: &[&[u8]]) -> Result { + unsafe fn connect(db: *mut ffi::sqlite3, _aux: *mut c_void, args: &[&[u8]]) -> Result { if args.len() < 4 { return Err(Error::ModuleError("no CSV file specified".to_owned())); } @@ -159,7 +159,7 @@ impl CSVTabCursor { fn new(reader: csv::Reader) -> CSVTabCursor { CSVTabCursor { base: Default::default(), - reader: reader, + reader, row_number: 0, cols: Vec::new(), eof: false, @@ -168,8 +168,8 @@ impl CSVTabCursor { } impl VTabCursor for CSVTabCursor { - fn vtab(&self) -> &mut CSVTab { - unsafe { &mut *(self.base.pVtab as *mut CSVTab) } + fn vtab(&self) -> &CSVTab { + unsafe { & *(self.base.pVtab as *const CSVTab) } } fn filter(&mut self, diff --git a/src/vtab/int_array.rs b/src/vtab/int_array.rs index 14992ee..fa6ff86 100644 --- a/src/vtab/int_array.rs +++ b/src/vtab/int_array.rs @@ -2,7 +2,6 @@ //! Port of C ["intarray"](http://www.sqlite.org/cgi/src/finfo?name=src/test_intarray.h). use std::cell::RefCell; use std::default::Default; -use std::mem; use std::os::raw::{c_char, c_int, c_void}; use std::rc::Rc; @@ -61,14 +60,14 @@ struct IntArrayVTab { } impl VTab for IntArrayVTab { - fn connect(db: *mut ffi::sqlite3, + unsafe fn connect(db: *mut ffi::sqlite3, aux: *mut c_void, _args: &[&[u8]]) -> Result { - let array = unsafe { mem::transmute(aux) }; + let array = aux as *const Rc>>; let vtab = IntArrayVTab { base: Default::default(), - array: array, + array, }; try!(declare_vtab(db, "CREATE TABLE x(value INTEGER PRIMARY KEY)")); Ok(vtab) @@ -102,8 +101,8 @@ impl IntArrayVTabCursor { } impl VTabCursor for IntArrayVTabCursor { - fn vtab(&self) -> &mut IntArrayVTab { - unsafe { &mut *(self.base.pVtab as *mut IntArrayVTab) } + fn vtab(&self) -> &IntArrayVTab { + unsafe { & *(self.base.pVtab as *const IntArrayVTab) } } fn filter(&mut self, _idx_num: c_int, diff --git a/src/vtab/mod.rs b/src/vtab/mod.rs index 07792ea..5bc27a5 100644 --- a/src/vtab/mod.rs +++ b/src/vtab/mod.rs @@ -2,7 +2,6 @@ //! (See http://sqlite.org/vtab.html) use std::borrow::Cow::{self, Borrowed, Owned}; use std::ffi::CString; -use std::mem; use std::os::raw::{c_char, c_int, c_void}; use std::ptr; use std::slice; @@ -46,12 +45,12 @@ pub trait VTab>: Sized { /// Create a new instance of a virtual table in response to a CREATE VIRTUAL TABLE statement. /// The `db` parameter is a pointer to the SQLite database connection that is executing /// the CREATE VIRTUAL TABLE statement. - fn create(db: *mut ffi::sqlite3, aux: *mut c_void, args: &[&[u8]]) -> Result { + unsafe fn create(db: *mut ffi::sqlite3, aux: *mut c_void, args: &[&[u8]]) -> Result { Self::connect(db, aux, args) } /// Similar to `create`. The difference is that `connect` is called to establish a new connection /// to an _existing_ virtual table whereas `create` is called to create a new virtual table from scratch. - fn connect(db: *mut ffi::sqlite3, aux: *mut c_void, args: &[&[u8]]) -> Result; + unsafe fn connect(db: *mut ffi::sqlite3, aux: *mut c_void, args: &[&[u8]]) -> Result; /// Determine the best way to access the virtual table. fn best_index(&self, info: &mut IndexInfo) -> Result<()>; /// Create a new cursor used for accessing a virtual table. @@ -183,7 +182,7 @@ impl<'a> IndexConstraintUsage<'a> { /// Virtual table cursor trait. pub trait VTabCursor>: Sized { /// Accessor to the associated virtual table. - fn vtab(&self) -> &mut V; + fn vtab(&self) -> &V; /// Begin a search of a virtual table. fn filter(&mut self, idx_num: c_int, idx_str: Option<&str>, args: &Values) -> Result<()>; /// Advance cursor to the next row of a result set initiated by `filter`. @@ -206,7 +205,7 @@ impl Context { pub fn set_result(&mut self, value: &T) { let t = value.to_sql(); match t { - Ok(ref value) => set_result(self.0, value), + Ok(ref value) => unsafe { set_result(self.0, value) }, Err(err) => unsafe { report_error(self.0, &err) }, } } @@ -296,7 +295,7 @@ impl InnerConnection { ffi::sqlite3_create_module_v2(self.db(), c_name.as_ptr(), module, - mem::transmute(boxed_aux), + boxed_aux as *mut c_void, Some(free_boxed_value::)) } } @@ -313,9 +312,9 @@ impl InnerConnection { } /// Declare the schema of a virtual table. -pub fn declare_vtab(db: *mut ffi::sqlite3, sql: &str) -> Result<()> { +pub unsafe fn declare_vtab(db: *mut ffi::sqlite3, sql: &str) -> Result<()> { let c_sql = try!(CString::new(sql)); - let rc = unsafe { ffi::sqlite3_declare_vtab(db, c_sql.as_ptr()) }; + let rc = ffi::sqlite3_declare_vtab(db, c_sql.as_ptr()); if rc == ffi::SQLITE_OK { Ok(()) } else { @@ -335,7 +334,7 @@ pub fn escape_double_quote(identifier: &str) -> Cow { // FIXME copy/paste from function.rs unsafe extern "C" fn free_boxed_value(p: *mut c_void) { - let _: Box = Box::from_raw(mem::transmute(p)); + let _: Box = Box::from_raw(p as *mut T); } #[macro_export] diff --git a/src/vtab/series.rs b/src/vtab/series.rs index b05468f..5056668 100644 --- a/src/vtab/series.rs +++ b/src/vtab/series.rs @@ -61,7 +61,7 @@ struct SeriesTab { impl VTab for SeriesTab { - fn connect(db: *mut ffi::sqlite3, + unsafe fn connect(db: *mut ffi::sqlite3, _aux: *mut c_void, _args: &[&[u8]]) -> Result { @@ -172,8 +172,8 @@ impl SeriesTabCursor { } } impl VTabCursor for SeriesTabCursor { - fn vtab(&self) -> &mut SeriesTab { - unsafe { &mut *(self.base.pVtab as *mut SeriesTab) } + fn vtab(&self) -> &SeriesTab { + unsafe { & *(self.base.pVtab as *const SeriesTab) } } fn filter(&mut self, idx_num: c_int,