Skip to main content

libdoltlite_sys/
lib.rs

1#![expect(non_snake_case, non_camel_case_types)]
2#![cfg_attr(not(test), no_std)]
3pub use self::error::*;
4
5use core::mem;
6#[cfg(not(feature = "loadable_extension"))]
7use core::sync::atomic::{AtomicI32, Ordering};
8
9mod error;
10
11#[must_use]
12pub fn SQLITE_STATIC() -> sqlite3_destructor_type {
13    None
14}
15
16#[must_use]
17pub fn SQLITE_TRANSIENT() -> sqlite3_destructor_type {
18    Some(unsafe { mem::transmute::<isize, unsafe extern "C" fn(*mut core::ffi::c_void)>(-1_isize) })
19}
20
21#[allow(dead_code, clippy::all)]
22mod bindings {
23    include!(concat!(env!("OUT_DIR"), "/bindgen.rs"));
24}
25pub use bindings::*;
26
27#[cfg(all(feature = "remote", not(target_arch = "wasm32")))]
28mod remote {
29    use core::ffi::{c_char, c_int};
30
31    #[repr(C)]
32    pub struct DoltliteServer {
33        _private: [u8; 0],
34    }
35
36    #[repr(C)]
37    pub struct DoltliteServeOpts {
38        pub zDir: *const c_char,
39        pub port: c_int,
40        pub zBindAddr: *const c_char,
41        pub certFile: *const c_char,
42        pub keyFile: *const c_char,
43        pub authKeysDir: *const c_char,
44        pub audience: *const c_char,
45    }
46
47    unsafe extern "C" {
48        pub fn doltliteServe(
49            directory: *const c_char,
50            port: c_int,
51            bind_address: *const c_char,
52        ) -> c_int;
53
54        pub fn doltliteServeAsync(
55            directory: *const c_char,
56            port: c_int,
57            bind_address: *const c_char,
58        ) -> *mut DoltliteServer;
59
60        pub fn doltliteServeOpts(options: *const DoltliteServeOpts) -> c_int;
61
62        pub fn doltliteServeAsyncOpts(options: *const DoltliteServeOpts) -> *mut DoltliteServer;
63
64        pub fn doltliteServerStop(server: *mut DoltliteServer);
65
66        pub fn doltliteServerPort(server: *mut DoltliteServer) -> c_int;
67    }
68}
69
70#[cfg(all(feature = "remote", not(target_arch = "wasm32")))]
71pub use remote::*;
72
73#[cfg(not(feature = "loadable_extension"))]
74unsafe extern "C" {
75    fn doltliteInstallAutoExt() -> core::ffi::c_int;
76}
77
78#[cfg(not(feature = "loadable_extension"))]
79static DOLTLITE_INIT_RESULT: AtomicI32 = AtomicI32::new(i32::MIN);
80
81#[cfg(feature = "loadable_extension")]
82pub fn initialize_doltlite() -> core::ffi::c_int {
83    // A loadable extension runs inside an already-initialized host process;
84    // do not load DoltLite's process-wide auto-extension a second time.
85    SQLITE_OK
86}
87
88#[cfg(not(feature = "loadable_extension"))]
89pub fn initialize_doltlite() -> core::ffi::c_int {
90    let existing = DOLTLITE_INIT_RESULT.load(Ordering::Acquire);
91    if existing != i32::MIN {
92        return existing;
93    }
94
95    let result = unsafe { doltliteInstallAutoExt() };
96    match DOLTLITE_INIT_RESULT.compare_exchange(
97        i32::MIN,
98        result,
99        Ordering::AcqRel,
100        Ordering::Acquire,
101    ) {
102        Ok(_) => result,
103        Err(previous) => previous,
104    }
105}
106
107impl Default for sqlite3_vtab {
108    fn default() -> Self {
109        unsafe { mem::zeroed() }
110    }
111}
112
113impl Default for sqlite3_vtab_cursor {
114    fn default() -> Self {
115        unsafe { mem::zeroed() }
116    }
117}