1#![allow(unsafe_code)]
2#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
3extern crate libsqlite3_sys as ffi;
45#[cfg(all(target_family = "wasm", target_os = "unknown"))]
6use sqlite_wasm_rs::export as ffi;
78use std::ops::Deref;
910/// `SerializedDatabase` is a wrapper for a serialized database that is dynamically allocated by calling `sqlite3_serialize`.
11/// This RAII wrapper is necessary to deallocate the memory when it goes out of scope with `sqlite3_free`.
12#[derive(Debug)]
13pub struct SerializedDatabase {
14 data: *mut u8,
15 len: usize,
16}
1718impl SerializedDatabase {
19/// Creates a new `SerializedDatabase` with the given data pointer and length.
20 ///
21 /// SAFETY: The data pointer needs to be returned by sqlite
22 /// and the length must match the underlying buffer pointer
23pub(crate) unsafe fn new(data: *mut u8, len: usize) -> Self {
24Self { data, len }
25 }
2627/// Returns a slice of the serialized database.
28pub fn as_slice(&self) -> &[u8] {
29// The pointer is never null because we don't pass the NO_COPY flag
30unsafe { std::slice::from_raw_parts(self.data, self.len) }
31 }
32}
3334impl Deref for SerializedDatabase {
35type Target = [u8];
3637fn deref(&self) -> &Self::Target {
38self.as_slice()
39 }
40}
4142impl Drop for SerializedDatabase {
43/// Deallocates the memory of the serialized database when it goes out of scope.
44fn drop(&mut self) {
45unsafe {
46 ffi::sqlite3_free(self.data as _);
47 }
48 }
49}