From e19dad01419e8fdc5604fe8e913dfb33ddb04959 Mon Sep 17 00:00:00 2001 From: Xuejie Xiao Date: Fri, 1 Feb 2019 09:39:51 +0800 Subject: [PATCH] Add lifetime to DBPinnableSlice to make sure it cannot outlive DB --- src/db.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/db.rs b/src/db.rs index 40d7c13..dffb520 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1665,11 +1665,12 @@ fn to_cpath>(path: P) -> Result { } } -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, + } } }