Struct sqlite3_ext::UnsafePtr

source ·
pub struct UnsafePtr<T: ?Sized> { /* private fields */ }
Expand description

Pass arbitrary pointers through SQLite as BLOBs.

Using this technique to pass pointers through SQLite is insecure and error-prone. A much better solution is available via PassedRef. Pointers passed through this interface require manual memory management, for example using Box::into_raw or std::mem::forget.

Examples

This example uses static memory to avoid memory management.

use sqlite3_ext::{UnsafePtr, function::Context, Result, ValueRef};

const VAL: &str = "static memory";
const SUBTYPE: u8 = 'S' as _;

fn produce_ptr(ctx: &Context, args: &mut [&mut ValueRef]) -> UnsafePtr<str> {
    UnsafePtr::new(VAL, SUBTYPE)
}

fn consume_ptr(ctx: &Context, args: &mut [&mut ValueRef]) -> Result<()> {
    let val: &str = unsafe { &*UnsafePtr::from_value_ref(args[0], SUBTYPE)?.get() };
    assert_eq!(val, "static memory");
    Ok(())
}

This example uses a boxed value to manage memory:

use sqlite3_ext::{UnsafePtr, function::Context, Result, ValueRef};

const SUBTYPE: u8 = 'S' as _;

fn produce_ptr(ctx: &Context, args: &mut [&mut ValueRef]) -> UnsafePtr<u64> {
    let val = Box::new(100u64);
    UnsafePtr::new(Box::into_raw(val), SUBTYPE)
}

fn consume_ptr(ctx: &Context, args: &mut [&mut ValueRef]) -> Result<()> {
    let val: Box<u64> =
        unsafe { Box::from_raw(UnsafePtr::from_value_ref(args[0], SUBTYPE)?.get_mut()) };
    assert_eq!(*val, 100);
    Ok(())
}

Implementations§

Create a new UnsafePtr with the given subtype.

Subtype verification requires SQLite 3.9.0. On earlier versions of SQLite, the subtype field is ignored.

Retrieve an UnsafePtr from a ValueRef.

The subtype provided to this method must match the subtype originally provided to UnsafeRef.

This method will fail if the value cannot be interpreted as a pointer. It will create a null pointer if the value is SQL NULL.

Subtype verification requires SQLite 3.9.0. On earlier versions of SQLite, the subtype field is ignored.

Get the stored pointer.

Get the stored pointer, mutably.

Trait Implementations§

Formats the value using the given formatter. Read more

Sets an arbitrary pointer to the context result.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.