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§
source§impl<T: ?Sized> UnsafePtr<T>
impl<T: ?Sized> UnsafePtr<T>
sourcepub fn new(ptr: *const T, subtype: u8) -> Self
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.
sourcepub fn from_value_ref(val: &mut ValueRef, subtype: u8) -> Result<Self>
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.
Trait Implementations§
impl<T: 'static + ?Sized> ToContextResult for UnsafePtr<T>
Sets an arbitrary pointer to the context result.