Skip to main content

rust_rocksdb/
raw_ptr.rs

1use crate::{
2    db::{DB, DBInner, ThreadMode},
3    db_options::IngestExternalFileOptions,
4    db_options::Options,
5    env::Env,
6    transactions::OptimisticTransactionDB,
7};
8use rust_librocksdb_sys as ffi;
9
10/// Trait for accessing raw pointers to underlying RocksDB objects.
11///
12/// This trait is only available when the `raw-ptr` feature is enabled.
13/// It provides access to the underlying C API pointers for advanced use cases.
14///
15/// # Examples
16///
17/// ```rust,ignore
18/// use rust_rocksdb::{DB, Options, AsRawPtr};
19///
20/// let db = DB::open_default("path")?;
21/// let raw_db_ptr = unsafe { db.as_raw_ptr() }; // *mut rocksdb_t
22///
23/// let opts = Options::default();
24/// let raw_opts_ptr = unsafe { opts.as_raw_ptr() }; // *mut rocksdb_options_t
25///
26/// // You can now use these pointers with the C API directly
27/// // unsafe { rocksdb_some_c_function(raw_db_ptr, raw_opts_ptr); }
28/// ```
29///
30/// # Safety
31///
32/// The returned pointers are only valid as long as the Rust objects are alive.
33/// You must ensure proper lifetime management and avoid using the pointers
34/// after the objects have been dropped.
35pub trait AsRawPtr<T> {
36    /// Returns a raw pointer to the underlying RocksDB object.
37    ///
38    /// # Safety
39    ///
40    /// The returned pointer is only valid as long as the object implementing
41    /// this trait is alive. The caller must ensure proper lifetime management
42    /// and avoid using the pointer after the object has been dropped.
43    unsafe fn as_raw_ptr(&self) -> *mut T;
44}
45
46impl AsRawPtr<ffi::rocksdb_t> for DB {
47    /// Returns a raw pointer to the underlying `rocksdb_t` object.
48    ///
49    /// This allows direct access to the RocksDB C API for advanced use cases.
50    unsafe fn as_raw_ptr(&self) -> *mut ffi::rocksdb_t {
51        self.inner.inner()
52    }
53}
54
55impl AsRawPtr<ffi::rocksdb_options_t> for Options {
56    /// Returns a raw pointer to the underlying `rocksdb_options_t` object.
57    ///
58    /// This allows direct access to the RocksDB options C API for advanced use cases.
59    unsafe fn as_raw_ptr(&self) -> *mut ffi::rocksdb_options_t {
60        self.inner
61    }
62}
63
64impl AsRawPtr<ffi::rocksdb_env_t> for Env {
65    /// Returns a raw pointer to the underlying `rocksdb_env_t` object.
66    ///
67    /// This allows direct access to the RocksDB environment C API for advanced use cases.
68    unsafe fn as_raw_ptr(&self) -> *mut ffi::rocksdb_env_t {
69        self.0.inner
70    }
71}
72
73impl AsRawPtr<ffi::rocksdb_ingestexternalfileoptions_t> for IngestExternalFileOptions {
74    /// Returns a raw pointer to the underlying `rocksdb_ingestexternalfileoptions_t` object.
75    ///
76    /// This allows direct access to the RocksDB Ingest External File Options C API for advanced use cases.
77    unsafe fn as_raw_ptr(&self) -> *mut ffi::rocksdb_ingestexternalfileoptions_t {
78        self.inner
79    }
80}
81
82impl<T: ThreadMode> AsRawPtr<ffi::rocksdb_t> for OptimisticTransactionDB<T> {
83    /// Returns a raw pointer to the underlying `rocksdb_t` (base DB) object.
84    ///
85    /// This allows direct access to the RocksDB C API for advanced use cases
86    /// such as verifying file checksums.
87    unsafe fn as_raw_ptr(&self) -> *mut ffi::rocksdb_t {
88        self.inner.inner()
89    }
90}