#[repr(transparent)]pub(crate) struct SqliteMallocString {
ptr: NonNull<c_char>,
_boo: PhantomData<Box<[c_char]>>,
}
Expand description
A string we own that’s allocated on the SQLite heap. Automatically calls
sqlite3_free
when dropped, unless into_raw
(or into_inner
) is called
on it. If constructed from a rust string, sqlite3_malloc
is used.
It has identical representation to a nonnull *mut c_char
, so you can use
it transparently as one. It’s nonnull, so Option
Most strings shouldn’t use this! Only places where the string needs to be
freed with sqlite3_free
. This includes sqlite3_extended_sql
results,
some error message pointers… Note that misuse is extremely dangerous!
Note that this is not a lossless interface. Incoming strings with internal NULs are modified, and outgoing strings which are non-UTF8 are modified. This seems unavoidable – it tries very hard to not panic.
Fields§
§ptr: NonNull<c_char>
§_boo: PhantomData<Box<[c_char]>>
Implementations§
Source§impl SqliteMallocString
impl SqliteMallocString
Sourcepub(crate) unsafe fn from_raw_nonnull(ptr: NonNull<c_char>) -> Self
pub(crate) unsafe fn from_raw_nonnull(ptr: NonNull<c_char>) -> Self
SAFETY: Caller must be certain that m
a nul-terminated c string
allocated by sqlite3_malloc
, and that SQLite expects us to free it!
Sourcepub(crate) unsafe fn from_raw(ptr: *mut c_char) -> Option<Self>
pub(crate) unsafe fn from_raw(ptr: *mut c_char) -> Option<Self>
SAFETY: Caller must be certain that m
a nul-terminated c string
allocated by sqlite3_malloc
, and that SQLite expects us to free it!
Sourcepub(crate) fn into_inner(self) -> NonNull<c_char>
pub(crate) fn into_inner(self) -> NonNull<c_char>
Get the pointer behind self
. After this is called, we no longer manage
it.
Sourcepub(crate) fn into_raw(self) -> *mut c_char
pub(crate) fn into_raw(self) -> *mut c_char
Get the pointer behind self
. After this is called, we no longer manage
it.
Sourcepub(crate) fn as_ptr(&self) -> *const c_char
pub(crate) fn as_ptr(&self) -> *const c_char
Borrow the pointer behind self
. We still manage it when this function
returns. If you want to relinquish ownership, use into_raw
.
pub(crate) fn as_cstr(&self) -> &CStr
pub(crate) fn to_string_lossy(&self) -> Cow<'_, str>
Sourcepub(crate) fn from_str(s: &str) -> Self
pub(crate) fn from_str(s: &str) -> Self
Convert s
into a SQLite string.
This should almost never be done except for cases like error messages or other strings that SQLite frees.
If s
contains internal NULs, we’ll replace them with
NUL_REPLACE_CHAR
.
Except for debug_assert
s which may trigger during testing, this
function never panics. If we hit integer overflow or the allocation
fails, we call handle_alloc_error
which aborts the program after
calling a global hook.
This means it’s safe to use in extern “C” functions even outside
catch_unwind
.