Add lifetime to DBPinnableSlice to make sure it cannot outlive DB

master
Xuejie Xiao 6 years ago
parent dbd2ca6e4f
commit e19dad0141
  1. 16
      src/db.rs

@ -1665,11 +1665,12 @@ fn to_cpath<P: AsRef<Path>>(path: P) -> Result<CString, Error> {
}
}
pub struct DBPinnableSlice {
pub struct DBPinnableSlice<'a> {
ptr: *mut ffi::rocksdb_pinnableslice_t,
db: PhantomData<&'a DB>
}
impl Deref for DBPinnableSlice {
impl<'a> Deref for DBPinnableSlice<'a> {
type Target = [u8];
fn deref(&self) -> &[u8] {
@ -1681,7 +1682,7 @@ impl Deref for DBPinnableSlice {
}
}
impl Drop for DBPinnableSlice {
impl<'a> Drop for DBPinnableSlice<'a> {
fn drop(&mut self) {
unsafe {
ffi::rocksdb_pinnableslice_destroy(self.ptr,);
@ -1689,13 +1690,16 @@ impl Drop for DBPinnableSlice {
}
}
impl DBPinnableSlice {
impl<'a> DBPinnableSlice<'a> {
/// Used to wrap a PinnableSlice from rocksdb to avoid unnecessary memcpy
///
/// # Unsafe
/// Requires that the pointer must be generated by rocksdb_get_pinned
pub unsafe fn from_c(ptr: *mut ffi::rocksdb_pinnableslice_t) -> DBPinnableSlice {
DBPinnableSlice { ptr, }
pub unsafe fn from_c(ptr: *mut ffi::rocksdb_pinnableslice_t) -> DBPinnableSlice<'a> {
DBPinnableSlice {
ptr,
db: PhantomData,
}
}
}

Loading…
Cancel
Save