Struct 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§

Source§

impl<T: ?Sized> UnsafePtr<T>

Source

pub fn new(ptr: *const T, subtype: u8) -> Self

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.

Source

pub fn from_value_ref(val: &mut ValueRef, subtype: u8) -> Result<Self>

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.

Source

pub fn get(&self) -> *const T

Get the stored pointer.

Source

pub fn get_mut(&mut self) -> *mut T

Get the stored pointer, mutably.

Trait Implementations§

Source§

impl<T: Debug + ?Sized> Debug for UnsafePtr<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: 'static + ?Sized> ToContextResult for UnsafePtr<T>

Sets an arbitrary pointer to the context result.

Auto Trait Implementations§

§

impl<T> Freeze for UnsafePtr<T>
where T: ?Sized,

§

impl<T> RefUnwindSafe for UnsafePtr<T>
where T: RefUnwindSafe + ?Sized,

§

impl<T> !Send for UnsafePtr<T>

§

impl<T> !Sync for UnsafePtr<T>

§

impl<T> Unpin for UnsafePtr<T>
where T: ?Sized,

§

impl<T> UnwindSafe for UnsafePtr<T>
where T: RefUnwindSafe + ?Sized,

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.