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};
#[cfg(feature = "load_extension")]
use std::path::Path;
@ -8,7 +8,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use super::ffi;
use super::{str_for_sqlite, str_to_cstring};
use super::str_for_sqlite;
use super::{Connection, InterruptHandle, OpenFlags, Result};
use crate::error::{error_from_handle, error_from_sqlite_code, Error};
use crate::raw_statement::RawStatement;
@ -51,9 +51,9 @@ impl InnerConnection {
}
pub fn open_with_flags(
c_path: &CString,
c_path: &CStr,
flags: OpenFlags,
vfs: Option<&CString>,
vfs: Option<&CStr>,
) -> Result<InnerConnection> {
#[cfg(not(feature = "bundled"))]
ensure_valid_sqlite_version();
@ -171,7 +171,8 @@ impl InnerConnection {
}
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 {
let r = ffi::sqlite3_exec(
self.db(),
@ -196,7 +197,7 @@ impl InnerConnection {
unsafe {
let mut errmsg: *mut c_char = ptr::null_mut();
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(
self.db,
dylib_str.as_ptr(),

View File

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

View File

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