sqlite_tiny/api/
ffiext.rs1use crate::ffi;
4use std::ffi::c_int;
5
6#[doc(hidden)]
12pub unsafe fn sqlite3_last_error(retval: i32, database: *mut ffi::sqlite3) -> crate::error::Error {
13 use std::borrow::Cow;
14 use std::ffi::CStr;
15
16 let error = ffi::sqlite3_errstr(retval);
18 let mut message = match error.is_null() {
19 true => Cow::Borrowed("Unknown"),
20 false => CStr::from_ptr(error).to_string_lossy(),
21 };
22
23 if !database.is_null() {
25 let error = ffi::sqlite3_errmsg(database);
27 let message_ = CStr::from_ptr(error).to_string_lossy();
28 message = Cow::Owned(format!("{message} ({message_})"));
29 }
30 crate::err!("SQLite error: {message}")
31}
32
33#[doc(hidden)]
39#[inline]
40pub unsafe fn sqlite3_check_result(retval: i32, database: *mut ffi::sqlite3) -> Result<(), crate::error::Error> {
41 match retval {
42 ffi::SQLITE_OK => Ok(()),
43 _ => Err(sqlite3_last_error(retval, database)),
44 }
45}
46
47#[derive(Debug)]
49pub struct PointerMut<T> {
50 ptr: *mut T,
52 on_drop: unsafe extern "C" fn(*mut T) -> c_int,
54}
55impl<T> PointerMut<T> {
56 pub fn new(ptr: *mut T, on_drop: unsafe extern "C" fn(*mut T) -> c_int) -> Self {
61 assert!(!ptr.is_null(), "cannot create an owned NULL pointer");
62 Self { ptr, on_drop }
63 }
64
65 pub const fn as_ptr(&self) -> *mut T {
67 self.ptr
68 }
69}
70impl<T> Drop for PointerMut<T> {
71 fn drop(&mut self) {
72 unsafe { (self.on_drop)(self.ptr) };
74 }
75}
76
77#[derive(Debug)]
79pub enum PointerMutFlex<'a, T> {
80 Borrowed(&'a mut PointerMut<T>),
82 Owned(PointerMut<T>),
84}
85impl<T> PointerMutFlex<'_, T> {
86 pub const fn as_ptr(&self) -> *mut T {
88 match self {
89 PointerMutFlex::Borrowed(pointer_ref) => pointer_ref.as_ptr(),
90 PointerMutFlex::Owned(pointer_mut) => pointer_mut.as_ptr(),
91 }
92 }
93}