Use SmallCString in most places

This commit is contained in:
Thom Chiovoloni 2020-04-14 02:04:19 -07:00 committed by Thom Chiovoloni
parent 552416039e
commit ac30e169ae
3 changed files with 16 additions and 12 deletions

View File

@ -1,4 +1,4 @@
use std::ffi::CString; use std::ffi::CStr;
use std::os::raw::{c_char, c_int}; use std::os::raw::{c_char, c_int};
#[cfg(feature = "load_extension")] #[cfg(feature = "load_extension")]
use std::path::Path; use std::path::Path;
@ -8,7 +8,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use super::ffi; use super::ffi;
use super::{str_for_sqlite, str_to_cstring}; use super::str_for_sqlite;
use super::{Connection, InterruptHandle, OpenFlags, Result}; use super::{Connection, InterruptHandle, OpenFlags, Result};
use crate::error::{error_from_handle, error_from_sqlite_code, Error}; use crate::error::{error_from_handle, error_from_sqlite_code, Error};
use crate::raw_statement::RawStatement; use crate::raw_statement::RawStatement;
@ -51,9 +51,9 @@ impl InnerConnection {
} }
pub fn open_with_flags( pub fn open_with_flags(
c_path: &CString, c_path: &CStr,
flags: OpenFlags, flags: OpenFlags,
vfs: Option<&CString>, vfs: Option<&CStr>,
) -> Result<InnerConnection> { ) -> Result<InnerConnection> {
#[cfg(not(feature = "bundled"))] #[cfg(not(feature = "bundled"))]
ensure_valid_sqlite_version(); ensure_valid_sqlite_version();
@ -171,7 +171,8 @@ impl InnerConnection {
} }
pub fn execute_batch(&mut self, sql: &str) -> Result<()> { pub fn execute_batch(&mut self, sql: &str) -> Result<()> {
let c_sql = str_to_cstring(sql)?; // use CString instead of SmallCString because it's probably big.
let c_sql = std::ffi::CString::new(sql)?;
unsafe { unsafe {
let r = ffi::sqlite3_exec( let r = ffi::sqlite3_exec(
self.db(), self.db(),
@ -196,7 +197,7 @@ impl InnerConnection {
unsafe { unsafe {
let mut errmsg: *mut c_char = ptr::null_mut(); let mut errmsg: *mut c_char = ptr::null_mut();
let r = if let Some(entry_point) = entry_point { let r = if let Some(entry_point) = entry_point {
let c_entry = str_to_cstring(entry_point)?; let c_entry = crate::str_to_cstring(entry_point)?;
ffi::sqlite3_load_extension( ffi::sqlite3_load_extension(
self.db, self.db,
dylib_str.as_ptr(), dylib_str.as_ptr(),

View File

@ -129,6 +129,7 @@ mod version;
pub mod vtab; pub mod vtab;
pub(crate) mod util; pub(crate) mod util;
pub(crate) use util::SmallCString;
// Number of cached prepared statements we'll hold on to. // Number of cached prepared statements we'll hold on to.
const STATEMENT_CACHE_DEFAULT_CAPACITY: usize = 16; const STATEMENT_CACHE_DEFAULT_CAPACITY: usize = 16;
@ -233,8 +234,8 @@ unsafe fn errmsg_to_string(errmsg: *const c_char) -> String {
String::from_utf8_lossy(c_slice).into_owned() String::from_utf8_lossy(c_slice).into_owned()
} }
fn str_to_cstring(s: &str) -> Result<CString> { fn str_to_cstring(s: &str) -> Result<SmallCString> {
Ok(CString::new(s)?) Ok(SmallCString::new(s)?)
} }
/// Returns `Ok((string ptr, len as c_int, SQLITE_STATIC | SQLITE_TRANSIENT))` /// Returns `Ok((string ptr, len as c_int, SQLITE_STATIC | SQLITE_TRANSIENT))`
@ -301,7 +302,7 @@ pub enum DatabaseName<'a> {
feature = "modern_sqlite" feature = "modern_sqlite"
))] ))]
impl DatabaseName<'_> { impl DatabaseName<'_> {
fn to_cstring(&self) -> Result<CString> { fn to_cstring(&self) -> Result<util::SmallCString> {
use self::DatabaseName::{Attached, Main, Temp}; use self::DatabaseName::{Attached, Main, Temp};
match *self { match *self {
Main => str_to_cstring("main"), Main => str_to_cstring("main"),

View File

@ -102,10 +102,11 @@ impl Session<'_> {
/// Attach a table. `None` means all tables. /// Attach a table. `None` means all tables.
pub fn attach(&mut self, table: Option<&str>) -> Result<()> { pub fn attach(&mut self, table: Option<&str>) -> Result<()> {
let table = if let Some(table) = table { let table = if let Some(table) = table {
str_to_cstring(table)?.as_ptr() Some(str_to_cstring(table)?)
} else { } else {
ptr::null() None
}; };
let table = table.as_ref().map(|s| s.as_ptr()).unwrap_or(ptr::null());
unsafe { check!(ffi::sqlite3session_attach(self.s, table)) }; unsafe { check!(ffi::sqlite3session_attach(self.s, table)) };
Ok(()) Ok(())
} }
@ -156,7 +157,8 @@ impl Session<'_> {
/// Load the difference between tables. /// Load the difference between tables.
pub fn diff(&mut self, from: DatabaseName<'_>, table: &str) -> Result<()> { pub fn diff(&mut self, from: DatabaseName<'_>, table: &str) -> Result<()> {
let from = from.to_cstring()?; let from = from.to_cstring()?;
let table = str_to_cstring(table)?.as_ptr(); let table = str_to_cstring(table)?;
let table = table.as_ptr();
unsafe { unsafe {
let mut errmsg = ptr::null_mut(); let mut errmsg = ptr::null_mut();
let r = let r =