Skip to content

Commit

Permalink
Stableify
Browse files Browse the repository at this point in the history
  • Loading branch information
ljoonal committed Apr 13, 2023
1 parent f6fba2b commit 3c10453
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
2 changes: 1 addition & 1 deletion deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ db-urls = ["https://github.com/rustsec/advisory-db"]
# The lint level for security vulnerabilities
vulnerability = "deny"
# The lint level for unmaintained crates
unmaintained = "deny"
unmaintained = "warn"
# The lint level for crates that have been yanked from their source registry
yanked = "deny"
# The lint level for crates with security notices. Note that as of
Expand Down
46 changes: 36 additions & 10 deletions onlivfe_cache_store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,26 @@
#![allow(clippy::multiple_crate_versions)]
// The warnings are a bit too aggressive
#![allow(clippy::significant_drop_tightening)]
#![feature(drain_filter)]

// Pain, https://github.com/rust-lang/rust/issues/43244
/// Drains items from the slice if they match the condition,
/// returns the drained items
fn drain_vec<I, F>(slice: &mut Vec<I>, cond: F) -> Vec<I>
where
F: Fn(&I) -> bool,
{
let mut removed = vec![];
let mut i = 0;
while i < slice.len() {
if cond(&mut slice[i]) {
removed.push(slice.remove(i));
} else {
i += 1;
}
}

removed
}

use directories::ProjectDirs;
use onlivfe::{
Expand Down Expand Up @@ -161,11 +180,12 @@ impl OnlivfeStore for OnlivfeCacheStorageBackend {
&self, account_id: PlatformAccountId, profile_ids: Vec<ProfileId>,
) -> Result<(), Self::Err> {
let mut profiles_to_accounts = self.profiles_to_accounts.write().await;
let removed_profile_ids = profiles_to_accounts
.drain_filter(|(acc_id, _)| acc_id == &account_id)
.filter(|(_, prof_id)| profile_ids.contains(prof_id))
.map(|(_, prof_id)| prof_id)
.collect::<Vec<ProfileId>>();
let removed_profile_ids =
drain_vec(&mut profiles_to_accounts, |(acc_id, _)| acc_id == &account_id)
.into_iter()
.filter(|(_, prof_id)| profile_ids.contains(prof_id))
.map(|(_, prof_id)| prof_id)
.collect::<Vec<ProfileId>>();
for profile_id in profile_ids {
profiles_to_accounts.push((account_id.clone(), profile_id));
}
Expand Down Expand Up @@ -342,8 +362,11 @@ impl OnlivfeStore for OnlivfeCacheStorageBackend {
&self, profile_id: ProfileId, account_ids: Vec<PlatformAccountId>,
) -> Result<(), Self::Err> {
let mut profiles_to_accounts = self.profiles_to_accounts.write().await;
let removed_account_ids = profiles_to_accounts
.drain_filter(|(_, prof_id)| prof_id == &profile_id)
let removed_account_ids =
drain_vec(&mut profiles_to_accounts, |(_, prof_id)| {
prof_id == &profile_id
})
.into_iter()
.filter(|(acc_id, _)| account_ids.contains(acc_id))
.map(|(acc_id, _)| acc_id)
.collect::<Vec<PlatformAccountId>>();
Expand Down Expand Up @@ -415,8 +438,11 @@ impl OnlivfeStore for OnlivfeCacheStorageBackend {
&self, profile_id: ProfileId,
) -> Result<(), Self::Err> {
let mut profiles_to_accounts = self.profiles_to_accounts.write().await;
let removed_account_ids = profiles_to_accounts
.drain_filter(|(_, prof_id)| prof_id == &profile_id)
let removed_account_ids =
drain_vec(&mut profiles_to_accounts, |(_, prof_id)| {
prof_id == &profile_id
})
.into_iter()
.map(|(acc_id, _)| acc_id)
.collect::<Vec<PlatformAccountId>>();

Expand Down

0 comments on commit 3c10453

Please sign in to comment.