From cc0d984118e345c72341ff12d74b1c35deb20ae8 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Fri, 3 Jun 2022 11:14:24 +0200 Subject: Fix most clippy lints --- src/db/lib.rs | 54 ++++++++++++++++++++++++------------------------ src/db/sled_adapter.rs | 42 +++++++++++++++++++------------------ src/db/sqlite_adapter.rs | 52 ++++++++++++++++++++++++---------------------- src/db/test.rs | 4 ++-- 4 files changed, 78 insertions(+), 74 deletions(-) (limited to 'src/db') diff --git a/src/db/lib.rs b/src/db/lib.rs index 4cda121f..95d2c16b 100644 --- a/src/db/lib.rs +++ b/src/db/lib.rs @@ -29,14 +29,14 @@ pub type Exporter<'a> = Box(pub(crate) Box + 'a>); -pub trait IValue<'a>: AsRef<[u8]> { - fn into_vec(&mut self) -> Vec; +pub trait IValue<'a>: AsRef<[u8]> + core::borrow::Borrow<[u8]> { + fn take_maybe(&mut self) -> Vec; } impl<'a> Value<'a> { #[inline] pub fn into_vec(mut self) -> Vec { - self.0.into_vec() + self.0.take_maybe() } } @@ -50,7 +50,7 @@ impl<'a> AsRef<[u8]> for Value<'a> { impl<'a> std::borrow::Borrow<[u8]> for Value<'a> { #[inline] fn borrow(&self) -> &[u8] { - self.0.as_ref().as_ref() + self.0.as_ref().borrow() } } @@ -82,7 +82,7 @@ impl<'a> std::fmt::Debug for Value<'a> { } impl<'a> IValue<'a> for Vec { - fn into_vec(&mut self) -> Vec { + fn take_maybe(&mut self) -> Vec { std::mem::take(self) } } @@ -106,7 +106,7 @@ impl<'a> From<&'a [u8]> for Value<'a> { } impl<'a> IValue<'a> for &'a [u8] { - fn into_vec(&mut self) -> Vec { + fn take_maybe(&mut self) -> Vec { self.to_vec() } } @@ -175,21 +175,22 @@ impl Db { } } - pub fn export<'a>(&'a self) -> Result> { + pub fn export(&self) -> Result> { self.0.export() } - pub fn import<'a>(&self, ex: Exporter<'a>) -> Result<()> { + pub fn import(&self, ex: Exporter<'_>) -> Result<()> { self.0.import(ex) } } +#[allow(clippy::len_without_is_empty)] impl Tree { pub fn db(&self) -> Db { Db(self.0.clone()) } - pub fn get<'a, T: AsRef<[u8]>>(&'a self, key: T) -> Result>> { + pub fn get>(&self, key: T) -> Result>> { self.0.get(self.1, key.as_ref()) } pub fn len(&self) -> Result { @@ -199,18 +200,18 @@ impl Tree { pub fn insert, U: AsRef<[u8]>>(&self, key: T, value: U) -> Result<()> { self.0.insert(self.1, key.as_ref(), value.as_ref()) } - pub fn remove<'a, T: AsRef<[u8]>>(&'a self, key: T) -> Result { + pub fn remove>(&self, key: T) -> Result { self.0.remove(self.1, key.as_ref()) } - pub fn iter<'a>(&'a self) -> Result> { + pub fn iter(&self) -> Result> { self.0.iter(self.1) } - pub fn iter_rev<'a>(&'a self) -> Result> { + pub fn iter_rev(&self) -> Result> { self.0.iter_rev(self.1) } - pub fn range<'a, K, R>(&'a self, range: R) -> Result> + pub fn range(&self, range: R) -> Result> where K: AsRef<[u8]>, R: RangeBounds, @@ -219,7 +220,7 @@ impl Tree { let eb = range.end_bound(); self.0.range(self.1, get_bound(sb), get_bound(eb)) } - pub fn range_rev<'a, K, R>(&'a self, range: R) -> Result> + pub fn range_rev(&self, range: R) -> Result> where K: AsRef<[u8]>, R: RangeBounds, @@ -230,6 +231,7 @@ impl Tree { } } +#[allow(clippy::len_without_is_empty)] impl<'a> Transaction<'a> { pub fn get>(&self, tree: &Tree, key: T) -> Result>> { self.0.get(tree.1, key.as_ref()) @@ -278,12 +280,10 @@ impl<'a> Transaction<'a> { // ---- - #[must_use] pub fn abort(self, e: E) -> TxResult { Err(TxError::Abort(e)) } - #[must_use] pub fn commit(self, r: R) -> TxResult { Ok(r) } @@ -294,32 +294,32 @@ impl<'a> Transaction<'a> { pub(crate) trait IDb: Send + Sync { fn open_tree(&self, name: &str) -> Result; - fn get<'a>(&'a self, tree: usize, key: &[u8]) -> Result>>; + fn get(&self, tree: usize, key: &[u8]) -> Result>>; fn len(&self, tree: usize) -> Result; fn insert(&self, tree: usize, key: &[u8], value: &[u8]) -> Result<()>; fn remove(&self, tree: usize, key: &[u8]) -> Result; - fn iter<'a>(&'a self, tree: usize) -> Result>; - fn iter_rev<'a>(&'a self, tree: usize) -> Result>; + fn iter(&self, tree: usize) -> Result>; + fn iter_rev(&self, tree: usize) -> Result>; - fn range<'a, 'r>( - &'a self, + fn range<'r>( + &self, tree: usize, low: Bound<&'r [u8]>, high: Bound<&'r [u8]>, - ) -> Result>; - fn range_rev<'a, 'r>( - &'a self, + ) -> Result>; + fn range_rev<'r>( + &self, tree: usize, low: Bound<&'r [u8]>, high: Bound<&'r [u8]>, - ) -> Result>; + ) -> Result>; fn transaction(&self, f: &dyn ITxFn) -> TxResult<(), ()>; - fn export<'a>(&'a self) -> Result>; - fn import<'a>(&self, ex: Exporter<'a>) -> Result<()>; + fn export(&self) -> Result>; + fn import(&self, ex: Exporter<'_>) -> Result<()>; } pub(crate) trait ITx<'a> { diff --git a/src/db/sled_adapter.rs b/src/db/sled_adapter.rs index acee305b..7382776f 100644 --- a/src/db/sled_adapter.rs +++ b/src/db/sled_adapter.rs @@ -27,7 +27,7 @@ impl From for Error { // -- val impl<'a> IValue<'a> for sled::IVec { - fn into_vec(&mut self) -> Vec { + fn take_maybe(&mut self) -> Vec { self.to_vec() } } @@ -52,7 +52,7 @@ pub struct SledDb { } impl SledDb { - pub fn new(db: sled::Db) -> Db { + pub fn init(db: sled::Db) -> Db { let s = Self { db, trees: RwLock::new((Vec::new(), HashMap::new())), @@ -67,7 +67,7 @@ impl SledDb { .0 .get(i) .cloned() - .ok_or(Error("invalid tree id".into())) + .ok_or_else(|| Error("invalid tree id".into())) } } @@ -87,7 +87,7 @@ impl IDb for SledDb { // ---- - fn get<'a>(&'a self, tree: usize, key: &[u8]) -> Result>> { + fn get(&self, tree: usize, key: &[u8]) -> Result>> { let tree = self.get_tree(tree)?; Ok(tree.get(key)?.map(From::from)) } @@ -108,37 +108,37 @@ impl IDb for SledDb { Ok(()) } - fn iter<'a>(&'a self, tree: usize) -> Result> { + fn iter(&self, tree: usize) -> Result> { let tree = self.get_tree(tree)?; Ok(Box::new(tree.iter().map(|v| { v.map(|(x, y)| (x.into(), y.into())).map_err(Into::into) }))) } - fn iter_rev<'a>(&'a self, tree: usize) -> Result> { + fn iter_rev(&self, tree: usize) -> Result> { let tree = self.get_tree(tree)?; Ok(Box::new(tree.iter().rev().map(|v| { v.map(|(x, y)| (x.into(), y.into())).map_err(Into::into) }))) } - fn range<'a, 'r>( - &'a self, + fn range<'r>( + &self, tree: usize, low: Bound<&'r [u8]>, high: Bound<&'r [u8]>, - ) -> Result> { + ) -> Result> { let tree = self.get_tree(tree)?; Ok(Box::new(tree.range::<&'r [u8], _>((low, high)).map(|v| { v.map(|(x, y)| (x.into(), y.into())).map_err(Into::into) }))) } - fn range_rev<'a, 'r>( - &'a self, + fn range_rev<'r>( + &self, tree: usize, low: Bound<&'r [u8]>, high: Bound<&'r [u8]>, - ) -> Result> { + ) -> Result> { let tree = self.get_tree(tree)?; Ok(Box::new(tree.range::<&'r [u8], _>((low, high)).rev().map( |v| v.map(|(x, y)| (x.into(), y.into())).map_err(Into::into), @@ -178,7 +178,7 @@ impl IDb for SledDb { // ---- - fn export<'a>(&'a self) -> Result> { + fn export(&self) -> Result> { let mut trees = vec![]; for name in self.db.tree_names() { let name = std::str::from_utf8(&name) @@ -188,17 +188,17 @@ impl IDb for SledDb { let tree = self.trees.read().unwrap().0.get(tree).unwrap().clone(); trees.push((name, tree)); } - let trees_exporter: Exporter<'a> = Box::new(trees.into_iter().map(|(name, tree)| { - let iter: ValueIter<'a> = Box::new( + let trees_exporter: Exporter<'_> = Box::new(trees.into_iter().map(|(name, tree)| { + let iter: ValueIter<'_> = Box::new( tree.iter() .map(|v| v.map(|(x, y)| (x.into(), y.into())).map_err(Into::into)), ); - Ok((name.to_string(), iter)) + Ok((name, iter)) })); Ok(trees_exporter) } - fn import<'a>(&self, ex: Exporter<'a>) -> Result<()> { + fn import(&self, ex: Exporter<'_>) -> Result<()> { for ex_tree in ex { let (name, data) = ex_tree?; @@ -234,9 +234,11 @@ struct SledTx<'a> { impl<'a> SledTx<'a> { fn get_tree(&self, i: usize) -> Result<&TransactionalTree> { - self.trees.get(i).ok_or(Error( - "invalid tree id (it might have been openned after the transaction started)".into(), - )) + self.trees.get(i).ok_or_else(|| { + Error( + "invalid tree id (it might have been openned after the transaction started)".into(), + ) + }) } fn save_error(&self, v: std::result::Result) -> Result { diff --git a/src/db/sqlite_adapter.rs b/src/db/sqlite_adapter.rs index 701639dc..31d20553 100644 --- a/src/db/sqlite_adapter.rs +++ b/src/db/sqlite_adapter.rs @@ -35,7 +35,7 @@ pub struct SqliteDb { } impl SqliteDb { - pub fn new(db: rusqlite::Connection) -> Db { + pub fn init(db: rusqlite::Connection) -> Db { let s = Self { db: Mutex::new(db), trees: RwLock::new(Vec::new()), @@ -49,7 +49,7 @@ impl SqliteDb { .unwrap() .get(i) .cloned() - .ok_or(Error("invalid tree id".into())) + .ok_or_else(|| Error("invalid tree id".into())) } } @@ -77,7 +77,7 @@ impl IDb for SqliteDb { // ---- - fn get<'a>(&'a self, tree: usize, key: &[u8]) -> Result>> { + fn get(&self, tree: usize, key: &[u8]) -> Result>> { let tree = self.get_tree(tree)?; let db = self.db.lock().unwrap(); let mut stmt = db.prepare(&format!("SELECT v FROM {} WHERE k = ?1", tree))?; @@ -102,7 +102,7 @@ impl IDb for SqliteDb { let mut res_iter = stmt.query([])?; match res_iter.next()? { None => Ok(0), - Some(v) => Ok(v.get::<_, usize>(0)?.into()), + Some(v) => Ok(v.get::<_, usize>(0)?), } } @@ -116,24 +116,24 @@ impl IDb for SqliteDb { Ok(()) } - fn iter<'a>(&'a self, tree: usize) -> Result> { + fn iter(&self, tree: usize) -> Result> { let tree = self.get_tree(tree)?; let sql = format!("SELECT k, v FROM {} ORDER BY k ASC", tree); - DbValueIterator::new(self.db.lock().unwrap(), &sql, []) + DbValueIterator::make(self.db.lock().unwrap(), &sql, []) } - fn iter_rev<'a>(&'a self, tree: usize) -> Result> { + fn iter_rev(&self, tree: usize) -> Result> { let tree = self.get_tree(tree)?; let sql = format!("SELECT k, v FROM {} ORDER BY k DESC", tree); - DbValueIterator::new(self.db.lock().unwrap(), &sql, []) + DbValueIterator::make(self.db.lock().unwrap(), &sql, []) } - fn range<'a, 'r>( - &'a self, + fn range<'r>( + &self, tree: usize, low: Bound<&'r [u8]>, high: Bound<&'r [u8]>, - ) -> Result> { + ) -> Result> { let tree = self.get_tree(tree)?; let (bounds_sql, params) = bounds_sql(low, high); @@ -143,18 +143,18 @@ impl IDb for SqliteDb { .iter() .map(|x| x as &dyn rusqlite::ToSql) .collect::>(); - DbValueIterator::new::<&[&dyn rusqlite::ToSql]>( + DbValueIterator::make::<&[&dyn rusqlite::ToSql]>( self.db.lock().unwrap(), &sql, params.as_ref(), ) } - fn range_rev<'a, 'r>( - &'a self, + fn range_rev<'r>( + &self, tree: usize, low: Bound<&'r [u8]>, high: Bound<&'r [u8]>, - ) -> Result> { + ) -> Result> { let tree = self.get_tree(tree)?; let (bounds_sql, params) = bounds_sql(low, high); @@ -164,7 +164,7 @@ impl IDb for SqliteDb { .iter() .map(|x| x as &dyn rusqlite::ToSql) .collect::>(); - DbValueIterator::new::<&[&dyn rusqlite::ToSql]>( + DbValueIterator::make::<&[&dyn rusqlite::ToSql]>( self.db.lock().unwrap(), &sql, params.as_ref(), @@ -200,11 +200,11 @@ impl IDb for SqliteDb { // ---- - fn export<'a>(&'a self) -> Result> { + fn export(&self) -> Result> { unimplemented!() } - fn import<'a>(&self, ex: Exporter<'a>) -> Result<()> { + fn import(&self, ex: Exporter<'_>) -> Result<()> { unimplemented!() } @@ -220,9 +220,11 @@ struct SqliteTx<'a> { impl<'a> SqliteTx<'a> { fn get_tree(&self, i: usize) -> Result { - self.trees.get(i).cloned().ok_or(Error( - "invalid tree id (it might have been openned after the transaction started)".into(), - )) + self.trees.get(i).cloned().ok_or_else(|| { + Error( + "invalid tree id (it might have been openned after the transaction started)".into(), + ) + }) } } @@ -244,7 +246,7 @@ impl<'a> ITx<'a> for SqliteTx<'a> { let mut res_iter = stmt.query([])?; match res_iter.next()? { None => Ok(0), - Some(v) => Ok(v.get::<_, usize>(0)?.into()), + Some(v) => Ok(v.get::<_, usize>(0)?), } } @@ -299,13 +301,13 @@ struct DbValueIterator<'a> { } impl<'a> DbValueIterator<'a> { - fn new( + fn make( db: MutexGuard<'a, Connection>, sql: &str, args: P, ) -> Result> { let res = DbValueIterator { - db: db, + db, stmt: None, iter: None, _pin: PhantomPinned, @@ -314,7 +316,7 @@ impl<'a> DbValueIterator<'a> { unsafe { let db = NonNull::from(&boxed.db); - let stmt = db.as_ref().prepare(&sql)?; + let stmt = db.as_ref().prepare(sql)?; let mut_ref: Pin<&mut DbValueIterator<'a>> = Pin::as_mut(&mut boxed); Pin::get_unchecked_mut(mut_ref).stmt = Some(stmt); diff --git a/src/db/test.rs b/src/db/test.rs index 2f5e4c46..75200cf1 100644 --- a/src/db/test.rs +++ b/src/db/test.rs @@ -81,13 +81,13 @@ fn test_suite(db: Db) { #[test] fn test_sled_db() { let path = mktemp::Temp::new_dir().unwrap(); - let db = SledDb::new(sled::open(path.to_path_buf()).unwrap()); + let db = SledDb::init(sled::open(path.to_path_buf()).unwrap()); test_suite(db); drop(path); } #[test] fn test_sqlite_db() { - let db = SqliteDb::new(rusqlite::Connection::open_in_memory().unwrap()); + let db = SqliteDb::init(rusqlite::Connection::open_in_memory().unwrap()); test_suite(db); } -- cgit v1.2.3