Skip to main content

rusqlite/util/
mod.rs

1// Internal utilities
2pub(crate) mod param_cache;
3mod small_cstr;
4pub(crate) use param_cache::ParamIndexCache;
5pub(crate) use small_cstr::SmallCString;
6
7// Doesn't use any modern features or vtab stuff, but is only used by them.
8mod sqlite_string;
9pub(crate) use sqlite_string::{SqliteMallocString, alloc};
10
11pub(crate) unsafe extern "C" fn free_boxed_value<T>(p: *mut std::ffi::c_void) {
12    drop(unsafe { Box::from_raw(p.cast::<T>()) });
13}
14
15pub(crate) unsafe fn from_raw_parts<'a, T>(data: *const T, len: std::ffi::c_int) -> &'a [T] {
16    if len > 0 {
17        return unsafe { std::slice::from_raw_parts(data, len as usize) };
18    }
19    &[]
20}
21#[cfg(any(feature = "session", feature = "vtab"))]
22pub(crate) unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: std::ffi::c_int) -> &'a mut [T] {
23    if len > 0 {
24        return unsafe { std::slice::from_raw_parts_mut(data, len as usize) };
25    }
26    &mut []
27}
28
29use crate::Result;
30use std::ffi::CStr;
31
32pub enum Named<'a> {
33    Small(SmallCString),
34    C(&'a CStr),
35}
36impl std::ops::Deref for Named<'_> {
37    type Target = CStr;
38
39    #[inline]
40    fn deref(&self) -> &CStr {
41        match self {
42            Named::Small(s) => s.as_cstr(),
43            Named::C(s) => s,
44        }
45    }
46}
47
48/// Database, table, column, collation, function, module, vfs name
49pub trait Name: std::fmt::Debug + Copy {
50    /// As C string
51    fn as_cstr(&self) -> Result<Named<'_>>;
52}
53impl Name for &str {
54    fn as_cstr(&self) -> Result<Named<'_>> {
55        let ss = SmallCString::new(self)?;
56        Ok(Named::Small(ss))
57    }
58}
59impl Name for &CStr {
60    #[inline]
61    fn as_cstr(&self) -> Result<Named<'_>> {
62        Ok(Named::C(self))
63    }
64}