diff --git a/src/column.rs b/src/column.rs index 07adc10..0fc6f20 100644 --- a/src/column.rs +++ b/src/column.rs @@ -141,7 +141,9 @@ mod test { "BEGIN; CREATE TABLE foo(x INTEGER, y TEXT); INSERT INTO foo VALUES(4, NULL); - END;").unwrap(); + END;", + ) + .unwrap(); let mut stmt = db.prepare("SELECT x as renamed, y FROM foo").unwrap(); let mut rows = stmt.query(crate::NO_PARAMS).unwrap(); let row = rows.next().unwrap().unwrap(); diff --git a/src/error.rs b/src/error.rs index 3506f62..1a91baf 100644 --- a/src/error.rs +++ b/src/error.rs @@ -182,9 +182,11 @@ impl fmt::Display for Error { Error::QueryReturnedNoRows => write!(f, "Query returned no rows"), Error::InvalidColumnIndex(i) => write!(f, "Invalid column index: {}", i), Error::InvalidColumnName(ref name) => write!(f, "Invalid column name: {}", name), - Error::InvalidColumnType(i, ref name, ref t) => { - write!(f, "Invalid column type {} at index: {}, name: {}", t, i, name) - } + Error::InvalidColumnType(i, ref name, ref t) => write!( + f, + "Invalid column type {} at index: {}, name: {}", + t, i, name + ), Error::StatementChangedRows(i) => write!(f, "Query changed {} rows", i), #[cfg(feature = "functions")] @@ -309,7 +311,7 @@ macro_rules! check { ($funcall:expr) => {{ let rc = $funcall; if rc != crate::ffi::SQLITE_OK { - Err(crate::error::error_from_sqlite_code(rc, None))?; + return Err(crate::error::error_from_sqlite_code(rc, None).into()); } }}; } diff --git a/src/functions.rs b/src/functions.rs index 06f681d..89aa9e4 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -688,9 +688,9 @@ mod test { use std::f64::EPSILON; use std::os::raw::c_double; - use crate::functions::{Aggregate, Context}; #[cfg(feature = "window")] use crate::functions::WindowAggregate; + use crate::functions::{Aggregate, Context}; use crate::{Connection, Error, Result, NO_PARAMS}; fn half(ctx: &Context<'_>) -> Result { diff --git a/src/inner_connection.rs b/src/inner_connection.rs index b54f3d4..f6ddc11 100644 --- a/src/inner_connection.rs +++ b/src/inner_connection.rs @@ -1,12 +1,12 @@ use std::ffi::CString; -use std::mem; +use std::mem::MaybeUninit; use std::os::raw::c_int; #[cfg(feature = "load_extension")] use std::path::Path; use std::ptr; use std::str; use std::sync::atomic::{AtomicBool, Ordering}; -use std::sync::{Arc, Mutex, Once, ONCE_INIT}; +use std::sync::{Arc, Mutex, Once}; use super::ffi; use super::{str_for_sqlite, str_to_cstring}; @@ -78,8 +78,10 @@ impl InnerConnection { } 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()); + let mut db = MaybeUninit::uninit(); + let r = + ffi::sqlite3_open_v2(c_path.as_ptr(), db.as_mut_ptr(), flags.bits(), ptr::null()); + let db: *mut ffi::sqlite3 = db.assume_init(); if r != ffi::SQLITE_OK { let e = if db.is_null() { error_from_sqlite_code(r, None) @@ -180,21 +182,27 @@ impl InnerConnection { let dylib_str = super::path_to_cstring(dylib_path)?; unsafe { - let mut errmsg: *mut c_char = mem::uninitialized(); + let mut errmsg = MaybeUninit::uninit(); let r = if let Some(entry_point) = entry_point { let c_entry = str_to_cstring(entry_point)?; ffi::sqlite3_load_extension( self.db, dylib_str.as_ptr(), c_entry.as_ptr(), - &mut errmsg, + errmsg.as_mut_ptr(), ) } else { - ffi::sqlite3_load_extension(self.db, dylib_str.as_ptr(), ptr::null(), &mut errmsg) + ffi::sqlite3_load_extension( + self.db, + dylib_str.as_ptr(), + ptr::null(), + errmsg.as_mut_ptr(), + ) }; if r == ffi::SQLITE_OK { Ok(()) } else { + let errmsg: *mut c_char = errmsg.assume_init(); let message = super::errmsg_to_string(&*errmsg); ffi::sqlite3_free(errmsg as *mut ::std::os::raw::c_void); Err(error_from_sqlite_code(r, Some(message))) @@ -207,7 +215,7 @@ impl InnerConnection { } pub fn prepare<'a>(&mut self, conn: &'a Connection, sql: &str) -> Result> { - let mut c_stmt: *mut ffi::sqlite3_stmt = unsafe { mem::uninitialized() }; + let mut c_stmt = MaybeUninit::uninit(); let (c_sql, len, _) = str_for_sqlite(sql)?; let r = unsafe { if cfg!(feature = "unlock_notify") { @@ -217,7 +225,7 @@ impl InnerConnection { self.db(), c_sql, len, - &mut c_stmt, + c_stmt.as_mut_ptr(), ptr::null_mut(), ); if !unlock_notify::is_locked(self.db, rc) { @@ -230,9 +238,10 @@ impl InnerConnection { } rc } else { - ffi::sqlite3_prepare_v2(self.db(), c_sql, len, &mut c_stmt, ptr::null_mut()) + ffi::sqlite3_prepare_v2(self.db(), c_sql, len, c_stmt.as_mut_ptr(), ptr::null_mut()) } }; + let c_stmt: *mut ffi::sqlite3_stmt = unsafe { c_stmt.assume_init() }; self.decode_result(r) .map(|_| Statement::new(conn, RawStatement::new(c_stmt))) } @@ -280,7 +289,7 @@ impl Drop for InnerConnection { } #[cfg(not(feature = "bundled"))] -static SQLITE_VERSION_CHECK: Once = ONCE_INIT; +static SQLITE_VERSION_CHECK: Once = Once::new(); #[cfg(not(feature = "bundled"))] pub static BYPASS_VERSION_CHECK: AtomicBool = AtomicBool::new(false); @@ -328,7 +337,7 @@ rusqlite was built against SQLite {} but the runtime SQLite version is {}. To fi }); } -static SQLITE_INIT: Once = ONCE_INIT; +static SQLITE_INIT: Once = Once::new(); pub static BYPASS_SQLITE_INIT: AtomicBool = AtomicBool::new(false); fn ensure_safe_sqlite_threading_mode() -> Result<()> { diff --git a/src/lib.rs b/src/lib.rs index 275b949..23b5007 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -966,23 +966,24 @@ mod test { // statement first. let raw_stmt = { use super::str_to_cstring; - use std::mem; + use std::mem::MaybeUninit; use std::os::raw::c_int; use std::ptr; let raw_db = db.db.borrow_mut().db; let sql = "SELECT 1"; - let mut raw_stmt: *mut ffi::sqlite3_stmt = unsafe { mem::uninitialized() }; + let mut raw_stmt = MaybeUninit::uninit(); let rc = unsafe { ffi::sqlite3_prepare_v2( raw_db, str_to_cstring(sql).unwrap().as_ptr(), (sql.len() + 1) as c_int, - &mut raw_stmt, + raw_stmt.as_mut_ptr(), ptr::null_mut(), ) }; assert_eq!(rc, ffi::SQLITE_OK); + let raw_stmt: *mut ffi::sqlite3_stmt = unsafe { raw_stmt.assume_init() }; raw_stmt }; @@ -1339,7 +1340,6 @@ mod test { match result.unwrap_err() { Error::SqliteFailure(err, _) => { assert_eq!(err.code, ErrorCode::OperationInterrupted); - return; } err => { panic!("Unexpected error {}", err); diff --git a/src/raw_statement.rs b/src/raw_statement.rs index b4d9b1e..58e9d0d 100644 --- a/src/raw_statement.rs +++ b/src/raw_statement.rs @@ -46,7 +46,10 @@ impl RawStatement { let ptr = ffi::sqlite3_column_name(self.0, idx); // If ptr is null here, it's an OOM, so there's probably nothing // meaningful we can do. Just assert instead of returning None. - assert!(!ptr.is_null(), "Null pointer from sqlite3_column_name: Out of memory?"); + assert!( + !ptr.is_null(), + "Null pointer from sqlite3_column_name: Out of memory?" + ); Some(CStr::from_ptr(ptr)) } } diff --git a/src/row.rs b/src/row.rs index 7271969..9847a76 100644 --- a/src/row.rs +++ b/src/row.rs @@ -223,15 +223,21 @@ impl<'stmt> Row<'stmt> { let idx = idx.idx(self.stmt)?; let value = self.stmt.value_ref(idx); FromSql::column_result(value).map_err(|err| match err { - FromSqlError::InvalidType => Error::InvalidColumnType(idx, self.stmt.column_name(idx).into(), value.data_type()), + FromSqlError::InvalidType => { + Error::InvalidColumnType(idx, self.stmt.column_name(idx).into(), value.data_type()) + } FromSqlError::OutOfRange(i) => Error::IntegralValueOutOfRange(idx, i), FromSqlError::Other(err) => { Error::FromSqlConversionFailure(idx as usize, value.data_type(), err) } #[cfg(feature = "i128_blob")] - FromSqlError::InvalidI128Size(_) => Error::InvalidColumnType(idx, self.stmt.column_name(idx).into(), value.data_type()), + FromSqlError::InvalidI128Size(_) => { + Error::InvalidColumnType(idx, self.stmt.column_name(idx).into(), value.data_type()) + } #[cfg(feature = "uuid")] - FromSqlError::InvalidUuidSize(_) => Error::InvalidColumnType(idx, self.stmt.column_name(idx).into(), value.data_type()), + FromSqlError::InvalidUuidSize(_) => { + Error::InvalidColumnType(idx, self.stmt.column_name(idx).into(), value.data_type()) + } }) } diff --git a/src/session.rs b/src/session.rs index 23c3a15..4ff88b6 100644 --- a/src/session.rs +++ b/src/session.rs @@ -4,7 +4,7 @@ use std::ffi::CStr; use std::io::{Read, Write}; use std::marker::PhantomData; -use std::mem; +use std::mem::MaybeUninit; use std::os::raw::{c_char, c_int, c_uchar, c_void}; use std::panic::{catch_unwind, RefUnwindSafe}; use std::ptr; @@ -43,8 +43,9 @@ impl Session<'_> { let db = db.db.borrow_mut().db; - let mut s: *mut ffi::sqlite3_session = unsafe { mem::uninitialized() }; - check!(unsafe { ffi::sqlite3session_create(db, name.as_ptr(), &mut s) }); + let mut s = MaybeUninit::uninit(); + check!(unsafe { ffi::sqlite3session_create(db, name.as_ptr(), s.as_mut_ptr()) }); + let s: *mut ffi::sqlite3_session = unsafe { s.assume_init() }; Ok(Session { phantom: PhantomData, @@ -112,8 +113,9 @@ impl Session<'_> { /// Generate a Changeset pub fn changeset(&mut self) -> Result { let mut n = 0; - let mut cs: *mut c_void = unsafe { mem::uninitialized() }; - check!(unsafe { ffi::sqlite3session_changeset(self.s, &mut n, &mut cs) }); + let mut cs = MaybeUninit::uninit(); + check!(unsafe { ffi::sqlite3session_changeset(self.s, &mut n, cs.as_mut_ptr()) }); + let cs: *mut c_void = unsafe { cs.assume_init() }; Ok(Changeset { cs, n }) } @@ -133,8 +135,9 @@ impl Session<'_> { /// Generate a Patchset pub fn patchset(&mut self) -> Result { let mut n = 0; - let mut ps: *mut c_void = unsafe { mem::uninitialized() }; - check!(unsafe { ffi::sqlite3session_patchset(self.s, &mut n, &mut ps) }); + let mut ps = MaybeUninit::uninit(); + check!(unsafe { ffi::sqlite3session_patchset(self.s, &mut n, ps.as_mut_ptr()) }); + let ps: *mut c_void = unsafe { ps.assume_init() }; // TODO Validate: same struct Ok(Changeset { cs: ps, n }) } @@ -157,9 +160,10 @@ impl Session<'_> { let from = from.to_cstring()?; let table = str_to_cstring(table)?.as_ptr(); unsafe { - let mut errmsg: *mut c_char = mem::uninitialized(); - let r = ffi::sqlite3session_diff(self.s, from.as_ptr(), table, &mut errmsg); + let mut errmsg = MaybeUninit::uninit(); + let r = ffi::sqlite3session_diff(self.s, from.as_ptr(), table, errmsg.as_mut_ptr()); if r != ffi::SQLITE_OK { + let errmsg: *mut c_char = errmsg.assume_init(); let message = errmsg_to_string(&*errmsg); ffi::sqlite3_free(errmsg as *mut ::std::os::raw::c_void); return Err(error_from_sqlite_code(r, Some(message))); @@ -254,15 +258,17 @@ impl Changeset { /// Invert a changeset pub fn invert(&self) -> Result { let mut n = 0; - let mut cs: *mut c_void = unsafe { mem::uninitialized() }; - check!(unsafe { ffi::sqlite3changeset_invert(self.n, self.cs, &mut n, &mut cs) }); + let mut cs = MaybeUninit::uninit(); + check!(unsafe { ffi::sqlite3changeset_invert(self.n, self.cs, &mut n, cs.as_mut_ptr()) }); + let cs: *mut c_void = unsafe { cs.assume_init() }; Ok(Changeset { cs, n }) } /// Create an iterator to traverse a changeset pub fn iter(&self) -> Result> { - let mut it: *mut ffi::sqlite3_changeset_iter = unsafe { mem::uninitialized() }; - check!(unsafe { ffi::sqlite3changeset_start(&mut it, self.n, self.cs) }); + let mut it = MaybeUninit::uninit(); + check!(unsafe { ffi::sqlite3changeset_start(it.as_mut_ptr(), self.n, self.cs) }); + let it: *mut ffi::sqlite3_changeset_iter = unsafe { it.assume_init() }; Ok(ChangesetIter { phantom: PhantomData, it, @@ -273,8 +279,11 @@ impl Changeset { /// Concatenate two changeset objects pub fn concat(a: &Changeset, b: &Changeset) -> Result { let mut n = 0; - let mut cs: *mut c_void = unsafe { mem::uninitialized() }; - check!(unsafe { ffi::sqlite3changeset_concat(a.n, a.cs, b.n, b.cs, &mut n, &mut cs) }); + let mut cs = MaybeUninit::uninit(); + check!(unsafe { + ffi::sqlite3changeset_concat(a.n, a.cs, b.n, b.cs, &mut n, cs.as_mut_ptr()) + }); + let cs: *mut c_void = unsafe { cs.assume_init() }; Ok(Changeset { cs, n }) } } @@ -298,14 +307,15 @@ impl ChangesetIter<'_> { /// Create an iterator on `input` pub fn start_strm<'input>(input: &'input mut dyn Read) -> Result> { let input_ref = &input; - let mut it: *mut ffi::sqlite3_changeset_iter = unsafe { mem::uninitialized() }; + let mut it = MaybeUninit::uninit(); check!(unsafe { ffi::sqlite3changeset_start_strm( - &mut it, + it.as_mut_ptr(), Some(x_input), input_ref as *const &mut dyn Read as *mut c_void, ) }); + let it: *mut ffi::sqlite3_changeset_iter = unsafe { it.assume_init() }; Ok(ChangesetIter { phantom: PhantomData, it, @@ -385,12 +395,13 @@ impl ChangesetItem { /// `SQLITE_CHANGESET_CONFLICT` conflict handler callback. pub fn conflict(&self, col: usize) -> Result> { unsafe { - let mut p_value: *mut ffi::sqlite3_value = mem::uninitialized(); + let mut p_value = MaybeUninit::uninit(); check!(ffi::sqlite3changeset_conflict( self.it, col as i32, - &mut p_value + p_value.as_mut_ptr() )); + let p_value: *mut ffi::sqlite3_value = p_value.assume_init(); Ok(ValueRef::from_value(p_value)) } } @@ -413,8 +424,13 @@ impl ChangesetItem { /// `SQLITE_INSERT`. pub fn new_value(&self, col: usize) -> Result> { unsafe { - let mut p_value: *mut ffi::sqlite3_value = mem::uninitialized(); - check!(ffi::sqlite3changeset_new(self.it, col as i32, &mut p_value)); + let mut p_value = MaybeUninit::uninit(); + check!(ffi::sqlite3changeset_new( + self.it, + col as i32, + p_value.as_mut_ptr() + )); + let p_value: *mut ffi::sqlite3_value = p_value.assume_init(); Ok(ValueRef::from_value(p_value)) } } @@ -425,8 +441,13 @@ impl ChangesetItem { /// `SQLITE_UPDATE`. pub fn old_value(&self, col: usize) -> Result> { unsafe { - let mut p_value: *mut ffi::sqlite3_value = mem::uninitialized(); - check!(ffi::sqlite3changeset_old(self.it, col as i32, &mut p_value)); + let mut p_value = MaybeUninit::uninit(); + check!(ffi::sqlite3changeset_old( + self.it, + col as i32, + p_value.as_mut_ptr() + )); + let p_value: *mut ffi::sqlite3_value = p_value.assume_init(); Ok(ValueRef::from_value(p_value)) } } @@ -437,14 +458,15 @@ impl ChangesetItem { let mut code = 0; let mut indirect = 0; let tab = unsafe { - let mut pz_tab: *const c_char = mem::uninitialized(); + let mut pz_tab = MaybeUninit::uninit(); check!(ffi::sqlite3changeset_op( self.it, - &mut pz_tab, + pz_tab.as_mut_ptr(), &mut number_of_columns, &mut code, &mut indirect )); + let pz_tab: *const c_char = pz_tab.assume_init(); CStr::from_ptr(pz_tab) }; let table_name = tab.to_str()?; @@ -460,12 +482,13 @@ impl ChangesetItem { pub fn pk(&self) -> Result<&[u8]> { let mut number_of_columns = 0; unsafe { - let mut pks: *mut c_uchar = mem::uninitialized(); + let mut pks = MaybeUninit::uninit(); check!(ffi::sqlite3changeset_pk( self.it, - &mut pks, + pks.as_mut_ptr(), &mut number_of_columns )); + let pks: *mut c_uchar = pks.assume_init(); Ok(from_raw_parts(pks, number_of_columns as usize)) } } @@ -479,8 +502,9 @@ pub struct Changegroup { impl Changegroup { pub fn new() -> Result { - let mut cg: *mut ffi::sqlite3_changegroup = unsafe { mem::uninitialized() }; - check!(unsafe { ffi::sqlite3changegroup_new(&mut cg) }); + let mut cg = MaybeUninit::uninit(); + check!(unsafe { ffi::sqlite3changegroup_new(cg.as_mut_ptr()) }); + let cg: *mut ffi::sqlite3_changegroup = unsafe { cg.assume_init() }; Ok(Changegroup { cg }) } @@ -506,8 +530,9 @@ impl Changegroup { /// Obtain a composite Changeset pub fn output(&mut self) -> Result { let mut n = 0; - let mut output: *mut c_void = unsafe { mem::uninitialized() }; - check!(unsafe { ffi::sqlite3changegroup_output(self.cg, &mut n, &mut output) }); + let mut output = MaybeUninit::uninit(); + check!(unsafe { ffi::sqlite3changegroup_output(self.cg, &mut n, output.as_mut_ptr()) }); + let output: *mut c_void = unsafe { output.assume_init() }; Ok(Changeset { cs: output, n }) } diff --git a/src/vtab/mod.rs b/src/vtab/mod.rs index 2753b2e..f0196c2 100644 --- a/src/vtab/mod.rs +++ b/src/vtab/mod.rs @@ -472,7 +472,9 @@ impl Values<'_> { } FromSqlError::OutOfRange(i) => Error::IntegralValueOutOfRange(idx, i), #[cfg(feature = "i128_blob")] - FromSqlError::InvalidI128Size(_) => Error::InvalidColumnType(idx, idx.to_string(), value.data_type()), + FromSqlError::InvalidI128Size(_) => { + Error::InvalidColumnType(idx, idx.to_string(), value.data_type()) + } #[cfg(feature = "uuid")] FromSqlError::InvalidUuidSize(_) => { Error::FromSqlConversionFailure(idx, value.data_type(), Box::new(err))