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