sqlite: condense allow(unsafe_code)

This commit is contained in:
Ryan Leckey 2020-03-29 15:55:09 -07:00
parent 913f906d81
commit b8537d0a87
7 changed files with 9 additions and 32 deletions

View File

@ -75,7 +75,6 @@ impl SqliteArgumentValue {
let index = index as c_int;
// https://sqlite.org/c3ref/bind_blob.html
#[allow(unsafe_code)]
let status: c_int = match self {
SqliteArgumentValue::Blob(value) => {
// TODO: Handle bytes that are too large

View File

@ -43,7 +43,6 @@ pub struct SqliteConnection {
// <https://www.sqlite.org/c3ref/c_config_covering_index_scan.html#sqliteconfigmultithread>
#[allow(unsafe_code)]
unsafe impl Send for SqliteConnectionHandle {}
async fn establish(url: Result<Url, url::ParseError>) -> crate::Result<SqliteConnection> {
@ -71,7 +70,6 @@ async fn establish(url: Result<Url, url::ParseError>) -> crate::Result<SqliteCon
| SQLITE_OPEN_SHAREDCACHE;
// <https://www.sqlite.org/c3ref/open.html>
#[allow(unsafe_code)]
let status = unsafe { sqlite3_open_v2(filename.as_ptr(), &mut handle, flags, null()) };
if handle.is_null() {
@ -82,7 +80,6 @@ async fn establish(url: Result<Url, url::ParseError>) -> crate::Result<SqliteCon
if status != SQLITE_OK {
// Close the handle if there was an error here
// https://sqlite.org/c3ref/close.html
#[allow(unsafe_code)]
unsafe {
let _ = sqlite3_close(handle);
}
@ -92,7 +89,6 @@ async fn establish(url: Result<Url, url::ParseError>) -> crate::Result<SqliteCon
// Enable extended result codes
// https://www.sqlite.org/c3ref/extended_result_codes.html
#[allow(unsafe_code)]
unsafe {
sqlite3_extended_result_codes(handle, 1);
}
@ -163,7 +159,6 @@ impl Drop for SqliteConnection {
// Next close the statement
// https://sqlite.org/c3ref/close.html
#[allow(unsafe_code)]
unsafe {
let _ = sqlite3_close(self.handle());
}

View File

@ -18,10 +18,8 @@ pub struct SqliteError {
impl SqliteError {
pub(super) fn from_connection(conn: *mut sqlite3) -> Self {
#[allow(unsafe_code)]
let code: c_int = unsafe { sqlite3_extended_errcode(conn) };
#[allow(unsafe_code)]
let message = unsafe {
let err = sqlite3_errmsg(conn);
debug_assert!(!err.is_null());

View File

@ -60,7 +60,6 @@ impl SqliteConnection {
// completed INSERT, UPDATE or DELETE statement.
// https://www.sqlite.org/c3ref/changes.html
#[allow(unsafe_code)]
let changes = unsafe { sqlite3_changes(self.handle()) };
changes as u64
}

View File

@ -1,5 +1,10 @@
//! **SQLite** database and connection types.
// SQLite is a C library. All interactions require FFI which is unsafe.
// All unsafe blocks should have comments pointing to SQLite docs and ensuring that we maintain
// invariants.
#![allow(unsafe_code)]
mod arguments;
mod connection;
mod cursor;

View File

@ -15,10 +15,7 @@ impl crate::row::private_row::Sealed for SqliteRow<'_> {}
// safe across threads as long as we don't call [sqlite3_step]
// That should not be possible as long as an immutable borrow is held on the connection
#[allow(unsafe_code)]
unsafe impl Send for SqliteRow<'_> {}
#[allow(unsafe_code)]
unsafe impl Sync for SqliteRow<'_> {}
impl<'c> SqliteRow<'c> {

View File

@ -31,7 +31,6 @@ impl<'c> SqliteValue<'c> {
}
fn r#type(&self) -> Option<SqliteType> {
#[allow(unsafe_code)]
let type_code = unsafe { sqlite3_column_type(self.statement.handle(), self.index) };
// SQLITE_INTEGER, SQLITE_FLOAT, SQLITE_TEXT, SQLITE_BLOB, or SQLITE_NULL
@ -48,31 +47,21 @@ impl<'c> SqliteValue<'c> {
/// Returns the 32-bit INTEGER result.
pub(super) fn int(&self) -> i32 {
#[allow(unsafe_code)]
unsafe {
sqlite3_column_int(self.statement.handle(), self.index)
}
unsafe { sqlite3_column_int(self.statement.handle(), self.index) }
}
/// Returns the 64-bit INTEGER result.
pub(super) fn int64(&self) -> i64 {
#[allow(unsafe_code)]
unsafe {
sqlite3_column_int64(self.statement.handle(), self.index)
}
unsafe { sqlite3_column_int64(self.statement.handle(), self.index) }
}
/// Returns the 64-bit, REAL result.
pub(super) fn double(&self) -> f64 {
#[allow(unsafe_code)]
unsafe {
sqlite3_column_double(self.statement.handle(), self.index)
}
unsafe { sqlite3_column_double(self.statement.handle(), self.index) }
}
/// Returns the UTF-8 TEXT result.
pub(super) fn text(&self) -> Option<&'c str> {
#[allow(unsafe_code)]
unsafe {
let ptr = sqlite3_column_text(self.statement.handle(), self.index) as *const i8;
@ -86,14 +75,12 @@ impl<'c> SqliteValue<'c> {
fn bytes(&self) -> usize {
// Returns the size of the result in bytes.
#[allow(unsafe_code)]
let len = unsafe { sqlite3_column_bytes(self.statement.handle(), self.index) };
len as usize
}
/// Returns the BLOB result.
pub(super) fn blob(&self) -> &'c [u8] {
#[allow(unsafe_code)]
let ptr = unsafe { sqlite3_column_blob(self.statement.handle(), self.index) };
if ptr.is_null() {
@ -101,10 +88,7 @@ impl<'c> SqliteValue<'c> {
return &[];
}
#[allow(unsafe_code)]
unsafe {
slice::from_raw_parts(ptr as *const u8, self.bytes())
}
unsafe { slice::from_raw_parts(ptr as *const u8, self.bytes()) }
}
}