From 81091a05ba74721a9cb1e20a290428bf296d399e Mon Sep 17 00:00:00 2001 From: Xuejie Xiao Date: Sat, 2 Feb 2019 07:32:15 +0800 Subject: [PATCH] Ergonomic changes via AsRef --- src/db.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/db.rs b/src/db.rs index dffb520..430609b 100644 --- a/src/db.rs +++ b/src/db.rs @@ -938,7 +938,7 @@ impl DB { self.get_cf_opt(cf, key.as_ref(), &ReadOptions::default()) } - pub fn get_pinned_opt(&self, key: &[u8], readopts: &ReadOptions) -> Result, Error> { + pub fn get_pinned_opt>(&self, key: T, readopts: &ReadOptions) -> Result, Error> { if readopts.inner.is_null() { return Err(Error::new( "Unable to create RocksDB read options. \ @@ -950,6 +950,7 @@ impl DB { )); } + let key = key.as_ref(); unsafe { let val = ffi_try!(ffi::rocksdb_get_pinned( self.inner, @@ -965,14 +966,14 @@ impl DB { } } - pub fn get_pinned(&self, key: &[u8]) -> Result, Error> { + pub fn get_pinned>(&self, key: T) -> Result, Error> { self.get_pinned_opt(key, &ReadOptions::default()) } - pub fn get_pinned_cf_opt( + pub fn get_pinned_cf_opt>( &self, cf: ColumnFamily, - key: &[u8], + key: T, readopts: &ReadOptions, ) -> Result, Error> { if readopts.inner.is_null() { @@ -986,6 +987,7 @@ impl DB { )); } + let key = key.as_ref(); unsafe { let val = ffi_try!(ffi::rocksdb_get_pinned_cf( self.inner, @@ -1002,7 +1004,7 @@ impl DB { } } - pub fn get_pinned_cf(&self, cf: ColumnFamily, key: &[u8]) -> Result, Error> { + pub fn get_pinned_cf>(&self, cf: ColumnFamily, key: T) -> Result, Error> { self.get_pinned_cf_opt(cf, key, &ReadOptions::default()) } @@ -1670,6 +1672,13 @@ pub struct DBPinnableSlice<'a> { db: PhantomData<&'a DB> } +impl<'a> AsRef<[u8]> for DBPinnableSlice<'a> { + fn as_ref(&self) -> &[u8] { + // Implement this via Deref so as not to repeat ourselves + &*self + } +} + impl<'a> Deref for DBPinnableSlice<'a> { type Target = [u8];