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::{alloc, SqliteMallocString};
10
11#[cfg(any(feature = "collation", feature = "functions", feature = "vtab"))]
12pub(crate) unsafe extern "C" fn free_boxed_value<T>(p: *mut std::ffi::c_void) {
13    drop(Box::from_raw(p.cast::<T>()));
14}
15
16use crate::Result;
17use std::ffi::CStr;
18
19pub enum Named<'a> {
20    Small(SmallCString),
21    C(&'a CStr),
22}
23impl std::ops::Deref for Named<'_> {
24    type Target = CStr;
25    #[inline]
26    fn deref(&self) -> &CStr {
27        match self {
28            Named::Small(s) => s.as_cstr(),
29            Named::C(s) => s,
30        }
31    }
32}
33
34/// Database, table, column, collation, function, module, vfs name
35pub trait Name: std::fmt::Debug {
36    /// As C string
37    fn as_cstr(&self) -> Result<Named<'_>>;
38}
39impl Name for &str {
40    fn as_cstr(&self) -> Result<Named<'_>> {
41        let ss = SmallCString::new(self)?;
42        Ok(Named::Small(ss))
43    }
44}
45impl Name for &CStr {
46    #[inline]
47    fn as_cstr(&self) -> Result<Named<'_>> {
48        Ok(Named::C(self))
49    }
50}