Struct SqliteMallocString

Source
#[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 can be used for nullable ones (it’s still just one pointer).

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

Source

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!

Source

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!

Source

pub(crate) fn into_inner(self) -> NonNull<c_char>

Get the pointer behind self. After this is called, we no longer manage it.

Source

pub(crate) fn into_raw(self) -> *mut c_char

Get the pointer behind self. After this is called, we no longer manage it.

Source

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.

Source

pub(crate) fn as_cstr(&self) -> &CStr

Source

pub(crate) fn to_string_lossy(&self) -> Cow<'_, str>

Source

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_asserts 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.

Trait Implementations§

Source§

impl Drop for SqliteMallocString

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.