From 9a54153104b4880e09a10574c5d355b79002daca Mon Sep 17 00:00:00 2001 From: vincent-herlemont Date: Sat, 12 Oct 2024 09:08:49 +0200 Subject: [PATCH] refactor: clippy fix --- benches/all.rs | 8 ++-- benches/setup.rs | 11 ++--- justfile | 6 ++- native_db_macro/src/keys.rs | 11 +---- native_db_macro/src/model_attributes.rs | 4 +- src/database.rs | 19 +++----- src/database_builder.rs | 8 +++- src/db_type/key/inner_key_value_redb1.rs | 5 +-- src/db_type/key/key.rs | 6 +-- src/db_type/key/mod.rs | 1 + src/metadata/mod.rs | 1 + src/model.rs | 9 ++-- src/snapshot.rs | 5 ++- src/table_definition.rs | 12 +---- .../internal/private_readable_transaction.rs | 2 +- src/transaction/internal/r_transaction.rs | 4 +- src/transaction/internal/rw_transaction.rs | 32 ++++++-------- src/transaction/query/drain.rs | 2 +- src/transaction/query/scan/primary_scan.rs | 8 ++-- src/transaction/query/scan/secondary_scan.rs | 8 ++-- src/transaction/rw_transaction.rs | 2 +- src/upgrade/mod.rs | 2 +- src/upgrade/redb1_to_redb2.rs | 2 +- src/upgrade/secondary_index_table_multimap.rs | 2 +- src/watch/mod.rs | 14 +++--- src/watch/query/internal.rs | 2 +- src/watch/query/scan.rs | 6 +-- src/watch/sender.rs | 16 +++---- tests/custom_type/custom.rs | 2 +- tests/macro_def/secondary_key.rs | 8 ++-- tests/native_model.rs | 1 - tests/scan.rs | 44 +++++++++---------- tests/util.rs | 10 ++--- 33 files changed, 126 insertions(+), 147 deletions(-) diff --git a/benches/all.rs b/benches/all.rs index 2ff174ef..368d0c9e 100644 --- a/benches/all.rs +++ b/benches/all.rs @@ -182,7 +182,7 @@ fn bench_select_range( bench_display: BenchDisplay, cfg: BenchSelectRangeRandomDataCfg, ) { - let mut group = c.benchmark_group(format!("Select Range Secondary Key")); + let mut group = c.benchmark_group("Select Range Secondary Key".to_string()); group.plot_config( criterion::PlotConfiguration::default().summary_scale(criterion::AxisScale::Linear), ); @@ -280,7 +280,7 @@ fn bench_select_range( let transaction = db .transaction_with_behavior(TransactionBehavior::Immediate) .unwrap(); - let sql = T::generate_select_range_sk(&"sk_1"); + let sql = T::generate_select_range_sk("sk_1"); let mut stmt = transaction.prepare(&sql).unwrap(); let rows = stmt.query_map(&[(":from_sk", &from_sk), (":to_sk", &to_sk)], |row| { @@ -301,7 +301,7 @@ fn bench_get( c: &mut Criterion, bench_display: BenchDisplay, ) { - let mut group = c.benchmark_group(format!("Get")); + let mut group = c.benchmark_group("Get".to_string()); group.plot_config( criterion::PlotConfiguration::default().summary_scale(criterion::AxisScale::Linear), ); @@ -392,7 +392,7 @@ fn bench_delete( c: &mut Criterion, bench_display: BenchDisplay, ) { - let mut group = c.benchmark_group(format!("Delete")); + let mut group = c.benchmark_group("Delete".to_string()); group.plot_config( criterion::PlotConfiguration::default().summary_scale(criterion::AxisScale::Linear), ); diff --git a/benches/setup.rs b/benches/setup.rs index 17aee021..fb167c91 100644 --- a/benches/setup.rs +++ b/benches/setup.rs @@ -27,6 +27,7 @@ macro_rules! define_item_struct { #[derive(Serialize, Deserialize, Clone, Default, Debug)] #[native_model(id = $id, version = 1)] #[native_db] + #[allow(non_camel_case_types)] pub struct $struct_name { #[primary_key] pub pk: i64, @@ -220,7 +221,7 @@ impl BenchDisplay { } pub fn display_1_by_tranaction(&self) -> String { - format!("{}", self.display('1')) + self.display('1').to_string() } pub fn display_n_by_tranaction(&self) -> String { @@ -229,10 +230,10 @@ impl BenchDisplay { pub fn display_read(&self) -> String { match self { - BenchDisplay::SK_1 => format!("1:SK"), - BenchDisplay::SK_10 => format!("10:SK"), - BenchDisplay::SK_50 => format!("50:SK"), - BenchDisplay::SK_100 => format!("100:SK"), + BenchDisplay::SK_1 => "1:SK".to_string(), + BenchDisplay::SK_10 => "10:SK".to_string(), + BenchDisplay::SK_50 => "50:SK".to_string(), + BenchDisplay::SK_100 => "100:SK".to_string(), } } diff --git a/justfile b/justfile index cb07b78b..7e254504 100644 --- a/justfile +++ b/justfile @@ -89,4 +89,8 @@ expand test_file_name="util": RUSTFLAGS="-Zmacro-backtrace" cargo expand --test {{test_file_name}} | save -f --raw src/{{test_file_name}}_expanded.rs expand_clean: - rm -f src/*_expanded.rs \ No newline at end of file + rm -f src/*_expanded.rs + +format: + cargo clippy; \ + cargo fmt --all \ No newline at end of file diff --git a/native_db_macro/src/keys.rs b/native_db_macro/src/keys.rs index 7a3dc402..74ff688f 100644 --- a/native_db_macro/src/keys.rs +++ b/native_db_macro/src/keys.rs @@ -75,7 +75,7 @@ impl ToTokenStream for KeyDefinition { } } -#[derive(Clone)] +#[derive(Clone, Default)] pub(crate) struct KeyOptions { pub(crate) unique: bool, pub(crate) optional: bool, @@ -100,15 +100,6 @@ impl ToTokenStream for () { } } -impl Default for KeyOptions { - fn default() -> Self { - Self { - unique: false, - optional: false, - } - } -} - impl KeyDefinition { pub(crate) fn name(&self) -> String { if let Some(field_name) = &self.field_name { diff --git a/native_db_macro/src/model_attributes.rs b/native_db_macro/src/model_attributes.rs index a4124ae6..781f216a 100644 --- a/native_db_macro/src/model_attributes.rs +++ b/native_db_macro/src/model_attributes.rs @@ -76,7 +76,7 @@ impl ModelAttributes { } else { panic!( "Unknown attribute: {}", - meta.path.get_ident().expect("Expected ident").to_string() + meta.path.get_ident().expect("Expected ident") ); } Ok(()) @@ -102,7 +102,7 @@ impl ModelAttributes { field.ty.to_tokens(&mut field_type_token_stream); let field_type = field_type_token_stream.to_string(); let mut secondary_options = KeyOptions::default(); - if let Ok(_) = attr.meta.require_list() { + if attr.meta.require_list().is_ok() { attr.parse_nested_meta(|meta| { if meta.path.is_ident("unique") { secondary_options.unique = true; diff --git a/src/database.rs b/src/database.rs index 54bd7f86..6ac4fc00 100644 --- a/src/database.rs +++ b/src/database.rs @@ -14,7 +14,6 @@ use std::cell::RefCell; use std::collections::HashMap; use std::sync::atomic::AtomicU64; use std::sync::{Arc, RwLock}; -use std::u64; /// The database instance. Allows you to create [rw_transaction](database/struct.Database.html#method.rw_transaction) and [r_transaction](database/struct.Database.html#method.r_transaction), [watch](database/struct.Database.html#method.watch) queries, and [unwatch](database/struct.Database.html#method.unwatch) etc. /// @@ -120,18 +119,14 @@ impl<'a> Database<'a> { (model_builder, main_table_definition).into(); let rw = self.instance.redb_database()?.begin_write()?; - rw.open_table(primary_table_definition.redb.clone())?; + rw.open_table(primary_table_definition.redb)?; for secondary_key in model_builder.model.secondary_keys.iter() { primary_table_definition.secondary_tables.insert( secondary_key.clone(), redb::MultimapTableDefinition::new(secondary_key.unique_table_name.as_str()).into(), ); - rw.open_multimap_table( - primary_table_definition.secondary_tables[&secondary_key] - .redb - .clone(), - )?; + rw.open_multimap_table(primary_table_definition.secondary_tables[secondary_key].redb)?; } rw.commit()?; @@ -191,7 +186,7 @@ impl<'a> Database<'a> { use semver::VersionReq; let metadata = self.metadata(); let comparator = VersionReq::parse(selector) - .expect(format!("Invalid version selector: {}", selector).as_str()); + .unwrap_or_else(|_| panic!("Invalid version selector: {}", selector)); let previous_version = if let Some(previous_version) = metadata.previous_version() { previous_version @@ -200,9 +195,9 @@ impl<'a> Database<'a> { }; let previous_version = Version::parse(previous_version) - .expect(format!("Invalid previous version: {}", previous_version).as_str()); + .unwrap_or_else(|_| panic!("Invalid previous version: {}", previous_version)); let current_version = Version::parse(metadata.current_version()) - .expect(format!("Invalid current version: {}", metadata.current_version()).as_str()); + .unwrap_or_else(|_| panic!("Invalid current version: {}", metadata.current_version())); // If the previous version is the same as the current version, the database is not upgrading if previous_version == current_version { @@ -216,7 +211,7 @@ impl<'a> Database<'a> { let rx = self.instance.redb_database()?.begin_read()?; let mut stats_primary_tables = vec![]; for primary_table in self.primary_table_definitions.values() { - let result_table_open = rx.open_table(primary_table.redb.clone()); + let result_table_open = rx.open_table(primary_table.redb); let stats_table = match result_table_open { Err(redb::TableError::TableDoesNotExist(_)) => StatsTable { name: primary_table.redb.name().to_string(), @@ -238,7 +233,7 @@ impl<'a> Database<'a> { let mut stats_secondary_tables = vec![]; for primary_table in self.primary_table_definitions.values() { for secondary_table in primary_table.secondary_tables.values() { - let result_table_open = rx.open_multimap_table(secondary_table.redb.clone()); + let result_table_open = rx.open_multimap_table(secondary_table.redb); let stats_table = match result_table_open { Err(redb::TableError::TableDoesNotExist(_)) => StatsTable { name: secondary_table.redb.name().to_string(), diff --git a/src/database_builder.rs b/src/database_builder.rs index 099da9dd..3605df6c 100644 --- a/src/database_builder.rs +++ b/src/database_builder.rs @@ -56,7 +56,7 @@ impl Builder { }; for (_, model_builder) in models.models_builder.iter() { - database.seed_model(&model_builder)?; + database.seed_model(model_builder)?; } // TODO: Maybe we can do some migration with models here. @@ -65,6 +65,12 @@ impl Builder { } } +impl Default for Builder { + fn default() -> Self { + Self::new() + } +} + impl Builder { /// Construct a new [Builder] with sensible defaults. pub fn new() -> Self { diff --git a/src/db_type/key/inner_key_value_redb1.rs b/src/db_type/key/inner_key_value_redb1.rs index 3930accf..b04a13ad 100644 --- a/src/db_type/key/inner_key_value_redb1.rs +++ b/src/db_type/key/inner_key_value_redb1.rs @@ -257,7 +257,7 @@ impl Value for DatabaseInnerKeyValue { impl Key for DatabaseInnerKeyValue { fn compare(data1: &[u8], data2: &[u8]) -> std::cmp::Ordering { - data1.cmp(&data2) + data1.cmp(data2) } } @@ -347,8 +347,7 @@ mod tests { use std::ops::RangeBounds; fn range>(range: R) -> DatabaseInnerKeyValueRange { - let range = DatabaseInnerKeyValueRange::new(range); - range + DatabaseInnerKeyValueRange::new(range) } #[test] diff --git a/src/db_type/key/key.rs b/src/db_type/key/key.rs index 71735e8e..8249e2fc 100644 --- a/src/db_type/key/key.rs +++ b/src/db_type/key/key.rs @@ -1,7 +1,6 @@ use redb::{Key as RedbKey, TypeName, Value as RedbValue}; use std::fmt::Debug; use std::ops::{Bound, Range, RangeBounds, RangeFrom, RangeInclusive, RangeTo, RangeToInclusive}; -use std::u8; #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Key(Vec); @@ -455,7 +454,7 @@ impl RedbValue for Key { impl RedbKey for Key { fn compare(data1: &[u8], data2: &[u8]) -> std::cmp::Ordering { - data1.cmp(&data2) + data1.cmp(data2) } } @@ -533,8 +532,7 @@ mod tests { use std::ops::RangeBounds; fn range>(range: R) -> KeyRange { - let range = KeyRange::new(range); - range + KeyRange::new(range) } #[test] diff --git a/src/db_type/key/mod.rs b/src/db_type/key/mod.rs index 2ac3aab5..19baddb7 100644 --- a/src/db_type/key/mod.rs +++ b/src/db_type/key/mod.rs @@ -1,3 +1,4 @@ +#[allow(clippy::module_inception)] mod key; mod key_definition; mod key_value; diff --git a/src/metadata/mod.rs b/src/metadata/mod.rs index 2a99ba63..80592331 100644 --- a/src/metadata/mod.rs +++ b/src/metadata/mod.rs @@ -1,4 +1,5 @@ mod current_version; +#[allow(clippy::module_inception)] mod metadata; mod table; diff --git a/src/model.rs b/src/model.rs index b93921b7..3df00852 100644 --- a/src/model.rs +++ b/src/model.rs @@ -17,13 +17,12 @@ impl Model { where F: Fn(KeyOptions) -> bool, { - let key = self - .secondary_keys - .get(secondary_key.into()) - .ok_or_else(|| Error::SecondaryKeyDefinitionNotFound { + let key = self.secondary_keys.get(secondary_key).ok_or_else(|| { + Error::SecondaryKeyDefinitionNotFound { table: self.primary_key.unique_table_name.to_string(), key: secondary_key.unique_table_name.clone(), - })?; + } + })?; if check(key.options.clone()) { Ok(()) diff --git a/src/snapshot.rs b/src/snapshot.rs index 23bdf062..88b19f01 100644 --- a/src/snapshot.rs +++ b/src/snapshot.rs @@ -11,7 +11,7 @@ impl Database<'_> { let w = new_db.instance.redb_database()?.begin_write()?; { // Copy primary tables - for (_, primary_table_definition) in &self.primary_table_definitions { + for primary_table_definition in self.primary_table_definitions.values() { let table = r.open_table(primary_table_definition.redb)?; let mut new_table = w.open_table(primary_table_definition.redb)?; for result in table.iter()? { @@ -20,7 +20,8 @@ impl Database<'_> { } // Copy secondary tables - for (_, secondary_table_definition) in &primary_table_definition.secondary_tables { + for secondary_table_definition in primary_table_definition.secondary_tables.values() + { let table = r.open_multimap_table(secondary_table_definition.redb)?; let mut new_table = w.open_multimap_table(secondary_table_definition.redb)?; for result in table.iter()? { diff --git a/src/table_definition.rs b/src/table_definition.rs index 3338be56..690b01cc 100644 --- a/src/table_definition.rs +++ b/src/table_definition.rs @@ -13,7 +13,7 @@ pub struct PrimaryTableDefinition<'a> { pub(crate) native_model_options: NativeModelOptions, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Default)] pub struct NativeModelOptions { pub(crate) native_model_id: u32, pub(crate) native_model_version: u32, @@ -23,16 +23,6 @@ pub struct NativeModelOptions { pub(crate) native_model_legacy: bool, } -impl Default for NativeModelOptions { - fn default() -> Self { - Self { - native_model_id: 0, - native_model_version: 0, - native_model_legacy: false, - } - } -} - impl<'a> From<(&ModelBuilder, RedbPrimaryTableDefinition<'a>)> for PrimaryTableDefinition<'a> { fn from(input: (&ModelBuilder, RedbPrimaryTableDefinition<'a>)) -> Self { let (builder, redb) = input; diff --git a/src/transaction/internal/private_readable_transaction.rs b/src/transaction/internal/private_readable_transaction.rs index 2b30089b..f9fa95d2 100644 --- a/src/transaction/internal/private_readable_transaction.rs +++ b/src/transaction/internal/private_readable_transaction.rs @@ -40,7 +40,7 @@ pub trait PrivateReadableTransaction<'db, 'txn> { ) -> Result> { let secondary_key = key_def.key_definition(); // Provide a better error for the test of unicity of the secondary key - model.check_secondary_options(&secondary_key, |options| options.unique == true)?; + model.check_secondary_options(&secondary_key, |options| options.unique)?; let table = self.get_secondary_table(&model, &secondary_key)?; diff --git a/src/transaction/internal/r_transaction.rs b/src/transaction/internal/r_transaction.rs index 49b271bb..37bbea93 100644 --- a/src/transaction/internal/r_transaction.rs +++ b/src/transaction/internal/r_transaction.rs @@ -20,7 +20,7 @@ where type RedbTransaction<'db_bis> = redb::ReadTransaction where Self: 'db_bis; fn table_definitions(&self) -> &HashMap { - &self.table_definitions + self.table_definitions } fn get_primary_table(&'txn self, model: &Model) -> Result { @@ -47,7 +47,7 @@ where })?; let secondary_table_definition = main_table_definition .secondary_tables - .get(&secondary_key) + .get(secondary_key) .ok_or_else(|| Error::TableDefinitionNotFound { table: secondary_key.unique_table_name.to_string(), })?; diff --git a/src/transaction/internal/rw_transaction.rs b/src/transaction/internal/rw_transaction.rs index 534c8a3a..d6eb2462 100644 --- a/src/transaction/internal/rw_transaction.rs +++ b/src/transaction/internal/rw_transaction.rs @@ -26,7 +26,7 @@ where type RedbTransaction<'db_bis> = redb::WriteTransaction where Self: 'db_bis; fn table_definitions(&self) -> &HashMap { - &self.primary_table_definitions + self.primary_table_definitions } fn get_primary_table(&'txn self, model: &Model) -> Result { @@ -53,7 +53,7 @@ where })?; let secondary_table_definition = main_table_definition .secondary_tables - .get(&secondary_key) + .get(secondary_key) .ok_or_else(|| Error::TableDefinitionNotFound { table: secondary_key.unique_table_name.to_string(), })?; @@ -76,11 +76,10 @@ impl<'db> InternalRwTransaction<'db> { item: Input, ) -> Result<(WatcherRequest, Output)> { let mut table = self.get_primary_table(&model)?; - if let Some(_) = table.get(&item.primary_key)? { + if table.get(&item.primary_key)?.is_some() { return Err(Error::DuplicateKey { key_name: model.primary_key.unique_table_name.to_string(), - } - .into()); + }); } table.insert(&item.primary_key, item.value.as_slice())?; @@ -125,8 +124,8 @@ impl<'db> InternalRwTransaction<'db> { /// This method insert secondary keys and check conflicts. /// It is used by [`concrete_insert`](Self::concrete_insert) and [`concrete_upsert`](Self::concrete_upsert). pub(crate) fn util_insert_secondary_keys(&self, item: &Input, model: &Model) -> Result<()> { - for (secondary_key_def, _value) in &item.secondary_keys { - let mut secondary_table = self.get_secondary_table(&model, secondary_key_def)?; + for secondary_key_def in item.secondary_keys.keys() { + let mut secondary_table = self.get_secondary_table(model, secondary_key_def)?; let secondary_key = match item.secondary_key_value(secondary_key_def)? { KeyEntry::Default(secondary_key) => secondary_key, KeyEntry::Optional(secondary_key) => { @@ -141,13 +140,12 @@ impl<'db> InternalRwTransaction<'db> { if secondary_key_def.options.unique { let check = { let primary_keys = secondary_table.get(&secondary_key)?; - primary_keys.len() > 0 + !primary_keys.is_empty() }; if check { return Err(Error::DuplicateKey { key_name: secondary_key_def.unique_table_name.to_string(), - } - .into()); + }); } } @@ -168,7 +166,7 @@ impl<'db> InternalRwTransaction<'db> { table.remove(&item.primary_key)?; } - for (secondary_key_def, _value) in keys { + for secondary_key_def in keys.keys() { let mut secondary_table = self.get_secondary_table(&model, secondary_key_def)?; match &item.secondary_key_value(secondary_key_def)? { KeyEntry::Default(secondary_key) => { @@ -203,7 +201,7 @@ impl<'db> InternalRwTransaction<'db> { Ok((watcher_request, old_binary_value, new_binary_value)) } - pub(crate) fn concrete_primary_drain<'a>(&self, model: Model) -> Result> { + pub(crate) fn concrete_primary_drain(&self, model: Model) -> Result> { let mut items = vec![]; let mut key_items = HashSet::new(); @@ -225,8 +223,7 @@ impl<'db> InternalRwTransaction<'db> { table: model.primary_key.unique_table_name.to_string(), })? .secondary_tables - .iter() - .map(|(key, _)| key) + .keys() .collect(); // Drain secondary tables @@ -290,18 +287,17 @@ impl<'db> InternalRwTransaction<'db> { // Find the old model table with data for new_primary_table_definition in model_table_definitions { // check if table exists, if the table does not exist continue - if self + if !self .redb_transaction .list_tables()? - .find(|table| table.name() == new_primary_table_definition.redb.name()) - .is_none() + .any(|table| table.name() == new_primary_table_definition.redb.name()) { continue; } let table = self .redb_transaction - .open_table(new_primary_table_definition.redb.clone())?; + .open_table(new_primary_table_definition.redb)?; let len = table.len()?; if len > 0 && old_table_definition.is_some() { panic!( diff --git a/src/transaction/query/drain.rs b/src/transaction/query/drain.rs index b5297fbf..5c2877b4 100644 --- a/src/transaction/query/drain.rs +++ b/src/transaction/query/drain.rs @@ -22,7 +22,7 @@ impl<'db, 'txn> RwDrain<'db, 'txn> { /// Drain all items with a given secondary key. /// /// **TODO: needs to be implemented** - pub fn secondary(&self, _key_def: impl ToKeyDefinition) -> () { + pub fn secondary(&self, _key_def: impl ToKeyDefinition) { todo!() } } diff --git a/src/transaction/query/scan/primary_scan.rs b/src/transaction/query/scan/primary_scan.rs index f27ea28d..ac04574f 100644 --- a/src/transaction/query/scan/primary_scan.rs +++ b/src/transaction/query/scan/primary_scan.rs @@ -19,7 +19,7 @@ where pub(crate) fn new(table: PrimaryTable) -> Self { Self { primary_table: table, - _marker: PhantomData::default(), + _marker: PhantomData, } } @@ -57,7 +57,7 @@ where let range = self.primary_table.range::(..)?; Ok(PrimaryScanIterator { range, - _marker: PhantomData::default(), + _marker: PhantomData, }) } @@ -100,7 +100,7 @@ where .range::(database_inner_key_value_range)?; Ok(PrimaryScanIterator { range, - _marker: PhantomData::default(), + _marker: PhantomData, }) } @@ -143,7 +143,7 @@ where Ok(PrimaryScanIteratorStartWith { range, start_with, - _marker: PhantomData::default(), + _marker: PhantomData, }) } } diff --git a/src/transaction/query/scan/secondary_scan.rs b/src/transaction/query/scan/secondary_scan.rs index eb96ebf4..f398e18b 100644 --- a/src/transaction/query/scan/secondary_scan.rs +++ b/src/transaction/query/scan/secondary_scan.rs @@ -33,7 +33,7 @@ where primary_table, secondary_table, key_def: key_def.key_definition(), - _marker: PhantomData::default(), + _marker: PhantomData, } } @@ -87,7 +87,7 @@ where Ok(SecondaryScanIterator { primary_table: &self.primary_table, primary_keys: primary_keys.into_iter(), - _marker: PhantomData::default(), + _marker: PhantomData, }) } @@ -146,7 +146,7 @@ where Ok(SecondaryScanIterator { primary_table: &self.primary_table, primary_keys: primary_keys.into_iter(), - _marker: PhantomData::default(), + _marker: PhantomData, }) } @@ -209,7 +209,7 @@ where Ok(SecondaryScanIterator { primary_table: &self.primary_table, primary_keys: primary_keys.into_iter(), - _marker: PhantomData::default(), + _marker: PhantomData, }) } } diff --git a/src/transaction/rw_transaction.rs b/src/transaction/rw_transaction.rs index 0d980e06..795469c7 100644 --- a/src/transaction/rw_transaction.rs +++ b/src/transaction/rw_transaction.rs @@ -82,7 +82,7 @@ impl<'db, 'txn> RwTransaction<'db> { self.internal.commit()?; // Send batch to watchers after commit succeeds let batch = self.batch.into_inner(); - watch::push_batch(Arc::clone(&self.watcher), batch)?; + watch::push_batch(Arc::clone(self.watcher), batch)?; Ok(()) } diff --git a/src/upgrade/mod.rs b/src/upgrade/mod.rs index cf34f7e7..68810daf 100644 --- a/src/upgrade/mod.rs +++ b/src/upgrade/mod.rs @@ -13,7 +13,7 @@ pub(crate) fn upgrade_redb( _model_builder: &HashMap, ) -> Result { #[cfg(feature = "redb1")] - redb1_to_redb2::upgrade_redb1_to_redb2(&database_configuration, &path, _model_builder)?; + redb1_to_redb2::upgrade_redb1_to_redb2(database_configuration, &path, _model_builder)?; let redb_builder = database_configuration.new_rdb_builder(); let database_instance = DatabaseInstance::open_on_disk(redb_builder, &path)?; diff --git a/src/upgrade/redb1_to_redb2.rs b/src/upgrade/redb1_to_redb2.rs index 315b2de2..b5502a73 100644 --- a/src/upgrade/redb1_to_redb2.rs +++ b/src/upgrade/redb1_to_redb2.rs @@ -100,7 +100,7 @@ pub(crate) fn upgrade_redb1_to_redb2( let db1 = redb1_builder.open(&redb1_path)?; let mut db2 = redb2_builder.create(&redb2_path)?; - for (_, model_builder) in model_builder { + for model_builder in model_builder.values() { let exist = upgrade_primary_table( model_builder.model.primary_key.unique_table_name.as_str(), &db1, diff --git a/src/upgrade/secondary_index_table_multimap.rs b/src/upgrade/secondary_index_table_multimap.rs index 28de9b18..9918e091 100644 --- a/src/upgrade/secondary_index_table_multimap.rs +++ b/src/upgrade/secondary_index_table_multimap.rs @@ -12,7 +12,7 @@ pub(crate) fn upgrade_secondary_index_table_multimap( model_builder: &HashMap, ) -> Result<()> { // List secondary index tables - for (_, model_builder) in model_builder { + for model_builder in model_builder.values() { for secondary_key in model_builder.model.secondary_keys.iter() { let secondary_table_name = secondary_key.unique_table_name.as_str(); let secondary_table_name_tmp = secondary_table_name.to_string() + "_tmp"; diff --git a/src/watch/mod.rs b/src/watch/mod.rs index 4c762d82..b516c29c 100644 --- a/src/watch/mod.rs +++ b/src/watch/mod.rs @@ -49,9 +49,9 @@ pub(crate) fn push_batch( senders: Arc>, batch: Batch, ) -> Result<(), WatchEventError> { - let watchers = senders.read().map_err(|err| match err { - _ => WatchEventError::LockErrorPoisoned, - })?; + let watchers = senders + .read() + .map_err(|_| WatchEventError::LockErrorPoisoned)?; let mut unused_watchers = vec![]; for (watcher_request, event) in batch { @@ -66,10 +66,10 @@ pub(crate) fn push_batch( drop(watchers); // Remove unused watchers - if unused_watchers.len() > 0 { - let mut w = senders.write().map_err(|err| match err { - _ => WatchEventError::LockErrorPoisoned, - })?; + if !unused_watchers.is_empty() { + let mut w = senders + .write() + .map_err(|_| WatchEventError::LockErrorPoisoned)?; for id in unused_watchers { w.remove_sender(id); } diff --git a/src/watch/query/internal.rs b/src/watch/query/internal.rs index 3526e7cb..88d73e54 100644 --- a/src/watch/query/internal.rs +++ b/src/watch/query/internal.rs @@ -30,7 +30,7 @@ impl InternalWatch<'_> { .watchers_counter_id .fetch_add(1, std::sync::atomic::Ordering::SeqCst); if value == u64::MAX { - Err(Error::MaxWatcherReached.into()) + Err(Error::MaxWatcherReached) } else { Ok(value) } diff --git a/src/watch/query/scan.rs b/src/watch/query/scan.rs index 6e7fe127..47a38109 100644 --- a/src/watch/query/scan.rs +++ b/src/watch/query/scan.rs @@ -21,7 +21,7 @@ impl WatchScan<'_, '_> { /// - [`range`](crate::watch::query::WatchScanPrimary::range) - Watch items with a primary key in a given range. pub fn primary(&self) -> WatchScanPrimary { WatchScanPrimary { - internal: &self.internal, + internal: self.internal, } } @@ -33,7 +33,7 @@ impl WatchScan<'_, '_> { pub fn secondary(&self, key_def: impl ToKeyDefinition) -> WatchScanSecondary { WatchScanSecondary { key_def: key_def.key_definition(), - internal: &self.internal, + internal: self.internal, } } } @@ -162,7 +162,7 @@ impl WatchScanSecondary<'_, '_> { /// Ok(()) /// } /// ``` - pub fn all<'ws, T: ToInput>(&'ws self) -> Result<(MpscReceiver, u64)> { + pub fn all(&self) -> Result<(MpscReceiver, u64)> { self.internal.watch_secondary_all::(&self.key_def) } diff --git a/src/watch/sender.rs b/src/watch/sender.rs index 8109cdc7..0094222f 100644 --- a/src/watch/sender.rs +++ b/src/watch/sender.rs @@ -84,18 +84,18 @@ impl Watchers { { match request_secondary_key { KeyEntry::Default(value) => { - if key_def == request_secondary_key_def { - if value.as_slice().starts_with(key_prefix.as_slice()) { - event_senders.push((*id, Arc::clone(event_sender))); - } + if key_def == request_secondary_key_def + && value.as_slice().starts_with(key_prefix.as_slice()) + { + event_senders.push((*id, Arc::clone(event_sender))); } } KeyEntry::Optional(value) => { if let Some(value) = value { - if key_def == request_secondary_key_def { - if value.as_slice().starts_with(key_prefix.as_slice()) { - event_senders.push((*id, Arc::clone(event_sender))); - } + if key_def == request_secondary_key_def + && value.as_slice().starts_with(key_prefix.as_slice()) + { + event_senders.push((*id, Arc::clone(event_sender))); } } } diff --git a/tests/custom_type/custom.rs b/tests/custom_type/custom.rs index 9f0a0ff7..e2ee1477 100644 --- a/tests/custom_type/custom.rs +++ b/tests/custom_type/custom.rs @@ -46,7 +46,7 @@ fn insert_item_fields() { let mut models = Models::new(); models.define::().unwrap(); let db = Builder::new() - .create(&&models, tf.path("test").as_std_path()) + .create(&models, tf.path("test").as_std_path()) .unwrap(); let rw = db.rw_transaction().unwrap(); diff --git a/tests/macro_def/secondary_key.rs b/tests/macro_def/secondary_key.rs index 5bfa0679..5c5c3629 100644 --- a/tests/macro_def/secondary_key.rs +++ b/tests/macro_def/secondary_key.rs @@ -112,11 +112,9 @@ impl ItemSecondaryOptional { format!("{}", self.id) } pub fn compute_secondary_key(&self) -> Option { - if let Some(name) = &self.name { - Some(format!("{}-{}", name, self.id)) - } else { - None - } + self.name + .as_ref() + .map(|name| format!("{}-{}", name, self.id)) } } diff --git a/tests/native_model.rs b/tests/native_model.rs index 18f01587..1a20748e 100644 --- a/tests/native_model.rs +++ b/tests/native_model.rs @@ -1,4 +1,3 @@ -use bincode; use bincode::{config, Decode, Encode}; use native_db::*; use native_db_macro::native_db; diff --git a/tests/scan.rs b/tests/scan.rs index 0ac01fc0..06efe940 100644 --- a/tests/scan.rs +++ b/tests/scan.rs @@ -29,10 +29,10 @@ impl Item { self.id } pub fn secondary_key_1(&self) -> String { - format!("{}", self.id).into() + format!("{}", self.id) } pub fn secondary_key_2(&self) -> String { - format!("{}", self.name) + self.name.to_string() } } @@ -62,7 +62,7 @@ fn test_iter() { .unwrap(); assert_eq!(result.len(), 2); - let obj1 = result.get(0).unwrap(); + let obj1 = result.first().unwrap(); assert_eq!(obj1.id, 1); assert_eq!(obj1.name, "test"); @@ -102,7 +102,7 @@ fn test_iter_many_items_to_be_bytes() { .unwrap(); assert_eq!(iter.len(), 257); - let obj1 = iter.get(0).unwrap(); + let obj1 = iter.first().unwrap(); assert_eq!(obj1.id, 0); assert_eq!(obj1.name, "test_0"); @@ -138,7 +138,7 @@ fn test_double_ended_iter() { assert_eq!(result.len(), 2); - let obj1 = result.get(0).unwrap(); + let obj1 = result.first().unwrap(); assert_eq!(obj1.id, 2); assert_eq!(obj1.name, "test2"); @@ -175,7 +175,7 @@ fn test_iter_range() { .unwrap(); assert_eq!(result.len(), 1); - let obj1 = result.get(0).unwrap(); + let obj1 = result.first().unwrap(); assert_eq!(obj1.id, 1); assert_eq!(obj1.name, "test"); @@ -189,7 +189,7 @@ fn test_iter_range() { .unwrap(); assert_eq!(result.len(), 2); - let obj1 = result.get(0).unwrap(); + let obj1 = result.first().unwrap(); assert_eq!(obj1.id, 2); assert_eq!(obj1.name, "test2"); @@ -207,7 +207,7 @@ fn test_iter_range() { .unwrap(); assert_eq!(result.len(), 1); - let obj1 = result.get(0).unwrap(); + let obj1 = result.first().unwrap(); assert_eq!(obj1.id, 2); assert_eq!(obj1.name, "test2"); } @@ -239,7 +239,7 @@ fn test_iter_by_key() { assert_eq!(result.len(), 2); - let obj1 = result.get(0).unwrap(); + let obj1 = result.first().unwrap(); assert_eq!(obj1.id, 1); assert_eq!(obj1.name, "test"); @@ -270,7 +270,7 @@ fn test_double_ended_iter_by_key() { assert_eq!(result.len(), 2); - let obj1 = result.get(0).unwrap(); + let obj1 = result.first().unwrap(); assert_eq!(obj1.id, 2); assert_eq!(obj1.name, "test2"); @@ -286,7 +286,7 @@ fn test_double_ended_iter_by_key_range() { let mut models = Models::new(); models.define::().unwrap(); let db = Builder::new() - .create(&&models, tf.path("test").as_std_path()) + .create(&models, tf.path("test").as_std_path()) .unwrap(); let rw = db.rw_transaction().unwrap(); @@ -302,7 +302,7 @@ fn test_double_ended_iter_by_key_range() { assert_eq!(result.len(), 1); - let obj1 = result.get(0).unwrap(); + let obj1 = result.first().unwrap(); assert_eq!(obj1.id, 1); assert_eq!(obj1.name, "test"); @@ -311,7 +311,7 @@ fn test_double_ended_iter_by_key_range() { let result: Vec = iter.rev().try_collect().unwrap(); assert_eq!(result.len(), 2); - let obj1 = result.get(0).unwrap(); + let obj1 = result.first().unwrap(); assert_eq!(obj1.id, 3); assert_eq!(obj1.name, "test3"); @@ -325,7 +325,7 @@ fn test_double_ended_iter_by_key_range() { assert_eq!(result.len(), 1); - let obj1 = result.get(0).unwrap(); + let obj1 = result.first().unwrap(); assert_eq!(obj1.id, 2); assert_eq!(obj1.name, "test2"); } @@ -387,7 +387,7 @@ fn test_start_with_scenario() { .unwrap(); assert_eq!(result.len(), 3); - let obj1 = result.get(0).unwrap(); + let obj1 = result.first().unwrap(); assert_eq!(obj1.name, format!("{}1", p)); let obj2 = result.get(1).unwrap(); @@ -462,7 +462,7 @@ fn test_start_with_by_key_scenario_write_txn() { .unwrap(); assert_eq!(result.len(), 3); - let obj1 = result.get(0).unwrap(); + let obj1 = result.first().unwrap(); assert_eq!(format!("{}:", obj1.flag), format!("{}", p)); let obj2 = result.get(1).unwrap(); @@ -510,7 +510,7 @@ fn test_start_with_by_key_scenario_readonly_txn() { .unwrap(); assert_eq!(result.len(), 3); - let obj1 = result.get(0).unwrap(); + let obj1 = result.first().unwrap(); assert_eq!(format!("{}:", obj1.flag), format!("{}", p)); let obj2 = result.get(1).unwrap(); @@ -547,7 +547,7 @@ fn test_txn_write_iter() { .unwrap(); assert_eq!(result.len(), 2); - let obj1 = result.get(0).unwrap(); + let obj1 = result.first().unwrap(); assert_eq!(obj1.id, 1); assert_eq!(obj1.name, "test"); @@ -583,7 +583,7 @@ fn test_txn_write_iter_range() { .unwrap(); assert_eq!(result.len(), 1); - let obj1 = result.get(0).unwrap(); + let obj1 = result.first().unwrap(); assert_eq!(obj1.id, 1); assert_eq!(obj1.name, "test"); @@ -597,7 +597,7 @@ fn test_txn_write_iter_range() { .unwrap(); assert_eq!(result.len(), 2); - let obj1 = result.get(0).unwrap(); + let obj1 = result.first().unwrap(); assert_eq!(obj1.id, 2); assert_eq!(obj1.name, "test2"); @@ -615,7 +615,7 @@ fn test_txn_write_iter_range() { .unwrap(); assert_eq!(result.len(), 1); - let obj1 = result.get(0).unwrap(); + let obj1 = result.first().unwrap(); assert_eq!(obj1.id, 2); assert_eq!(obj1.name, "test2"); } @@ -658,7 +658,7 @@ fn test_txn_write_start_with_scenario() { .unwrap(); assert_eq!(result.len(), 3); - let obj1 = result.get(0).unwrap(); + let obj1 = result.first().unwrap(); assert_eq!(obj1.name, format!("{}1", p)); let obj2 = result.get(1).unwrap(); diff --git a/tests/util.rs b/tests/util.rs index 48481398..c0c99604 100644 --- a/tests/util.rs +++ b/tests/util.rs @@ -6,7 +6,7 @@ fn test_builder() { let tf = TmpFs::new().unwrap(); // Create without error let mut _db = Builder::new() - .create(&Models::new(), &tf.path("test")) + .create(&Models::new(), tf.path("test")) .unwrap(); } @@ -17,7 +17,7 @@ fn test_builder_with_set_cache_size() { let mut builder = Builder::new(); let _db = builder .set_cache_size(100) - .create(&Models::new(), &tf.path("test")) + .create(&Models::new(), tf.path("test")) .unwrap(); } @@ -26,7 +26,7 @@ fn test_open_unexisting_database() { let tf = TmpFs::new().unwrap(); // Open an unexisting database assert!(Builder::new() - .open(&Models::new(), &tf.path("test")) + .open(&Models::new(), tf.path("test")) .is_err()); } @@ -37,11 +37,11 @@ fn test_open_existing_database() { // Create a database let builder = Builder::new(); let models = Models::new(); - let db = builder.create(&models, &tf.path("test")).unwrap(); + let db = builder.create(&models, tf.path("test")).unwrap(); drop(db); // Open an existing database - let _db = Builder::new().open(&models, &tf.path("test")).unwrap(); + let _db = Builder::new().open(&models, tf.path("test")).unwrap(); } use native_model::{native_model, Model};