Skip to content

Commit

Permalink
"cargo fmt" all changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lufte committed Jul 30, 2024
1 parent dfd93ce commit 5a84462
Showing 1 changed file with 37 additions and 21 deletions.
58 changes: 37 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,11 @@ impl<K: Hash + Eq, V, S: BuildHasher> LruCache<K, V, S> {
/// assert_eq!(Rc::strong_count(&key2), 2); // key2 was only cloned once even though we
/// // queried it 3 times
/// ```
pub fn try_get_or_insert_mut_ref<'a, Q, F, E>(&'a mut self, k: &'_ Q, f: F) -> Result<&'a mut V, E>
pub fn try_get_or_insert_mut_ref<'a, Q, F, E>(
&'a mut self,
k: &'_ Q,
f: F,
) -> Result<&'a mut V, E>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized + alloc::borrow::ToOwned<Owned = K>,
Expand Down Expand Up @@ -1723,8 +1727,8 @@ mod tests {
use super::LruCache;
use core::{fmt::Debug, num::NonZeroUsize};
use scoped_threadpool::Pool;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::rc::Rc;
use std::sync::atomic::{AtomicUsize, Ordering};

fn assert_opt_eq<V: PartialEq + Debug>(opt: Option<&V>, v: V) {
assert!(opt.is_some());
Expand Down Expand Up @@ -1815,19 +1819,25 @@ mod tests {

#[test]
fn test_get_or_insert_ref() {
use alloc::string::String;
use alloc::borrow::ToOwned;
use alloc::string::String;

let key1 = Rc::new("1".to_owned());
let key2 = Rc::new("2".to_owned());
let mut cache = LruCache::<Rc<String>, String>::new(NonZeroUsize::new(2).unwrap());
assert!(cache.is_empty());
assert_eq!(cache.get_or_insert_ref(&key1, ||"One".to_owned()), "One");
assert_eq!(cache.get_or_insert_ref(&key2, ||"Two".to_owned()), "Two");
assert_eq!(cache.get_or_insert_ref(&key1, || "One".to_owned()), "One");
assert_eq!(cache.get_or_insert_ref(&key2, || "Two".to_owned()), "Two");
assert_eq!(cache.len(), 2);
assert!(!cache.is_empty());
assert_eq!(cache.get_or_insert_ref(&key2, ||"Not two".to_owned()), "Two");
assert_eq!(cache.get_or_insert_ref(&key2, ||"Again not two".to_owned()), "Two");
assert_eq!(
cache.get_or_insert_ref(&key2, || "Not two".to_owned()),
"Two"
);
assert_eq!(
cache.get_or_insert_ref(&key2, || "Again not two".to_owned()),
"Two"
);
assert_eq!(Rc::strong_count(&key1), 2);
assert_eq!(Rc::strong_count(&key2), 2);
}
Expand Down Expand Up @@ -1860,15 +1870,15 @@ mod tests {

#[test]
fn test_try_get_or_insert_ref() {
use alloc::string::String;
use alloc::borrow::ToOwned;
use alloc::string::String;

let key1 = Rc::new("1".to_owned());
let key2 = Rc::new("2".to_owned());
let mut cache = LruCache::<Rc<String>, String>::new(NonZeroUsize::new(2).unwrap());
let f = ||->Result<String, ()> {Err(())};
let a = ||->Result<String, ()> {Ok("One".to_owned())};
let b = ||->Result<String, ()> {Ok("Two".to_owned())};
let f = || -> Result<String, ()> { Err(()) };
let a = || -> Result<String, ()> { Ok("One".to_owned()) };
let b = || -> Result<String, ()> { Ok("Two".to_owned()) };
assert_eq!(cache.try_get_or_insert_ref(&key1, a), Ok(&"One".to_owned()));
assert_eq!(cache.try_get_or_insert_ref(&key2, f), Err(()));
assert_eq!(cache.try_get_or_insert_ref(&key2, b), Ok(&"Two".to_owned()));
Expand Down Expand Up @@ -1901,16 +1911,16 @@ mod tests {

#[test]
fn test_get_or_insert_mut_ref() {
use alloc::string::String;
use alloc::borrow::ToOwned;
use alloc::string::String;

let key1 = Rc::new("1".to_owned());
let key2 = Rc::new("2".to_owned());
let mut cache = LruCache::<Rc<String>, &'static str>::new(NonZeroUsize::new(2).unwrap());
assert_eq!(cache.get_or_insert_mut_ref(&key1, ||"One"), &mut "One");
let v = cache.get_or_insert_mut_ref(&key2, ||"Two");
assert_eq!(cache.get_or_insert_mut_ref(&key1, || "One"), &mut "One");
let v = cache.get_or_insert_mut_ref(&key2, || "Two");
*v = "New two";
assert_eq!(cache.get_or_insert_mut_ref(&key2, ||"Two"), &mut "New two");
assert_eq!(cache.get_or_insert_mut_ref(&key2, || "Two"), &mut "New two");
assert_eq!(Rc::strong_count(&key1), 2);
assert_eq!(Rc::strong_count(&key2), 2);
}
Expand All @@ -1937,22 +1947,28 @@ mod tests {

#[test]
fn test_try_get_or_insert_mut_ref() {
use alloc::string::String;
use alloc::borrow::ToOwned;
use alloc::string::String;

let key1 = Rc::new("1".to_owned());
let key2 = Rc::new("2".to_owned());
let mut cache = LruCache::<Rc<String>, String>::new(NonZeroUsize::new(2).unwrap());
let f = ||->Result<String, ()> {Err(())};
let a = ||->Result<String, ()> {Ok("One".to_owned())};
let b = ||->Result<String, ()> {Ok("Two".to_owned())};
assert_eq!(cache.try_get_or_insert_mut_ref(&key1, a), Ok(&mut "One".to_owned()));
let f = || -> Result<String, ()> { Err(()) };
let a = || -> Result<String, ()> { Ok("One".to_owned()) };
let b = || -> Result<String, ()> { Ok("Two".to_owned()) };
assert_eq!(
cache.try_get_or_insert_mut_ref(&key1, a),
Ok(&mut "One".to_owned())
);
assert_eq!(cache.try_get_or_insert_mut_ref(&key2, f), Err(()));
if let Ok(v) = cache.try_get_or_insert_mut_ref(&key2, b) {
assert_eq!(v, &mut "Two");
*v = "New two".to_owned();
}
assert_eq!(cache.try_get_or_insert_mut_ref(&key2, a), Ok(&mut "New two".to_owned()));
assert_eq!(
cache.try_get_or_insert_mut_ref(&key2, a),
Ok(&mut "New two".to_owned())
);
assert_eq!(Rc::strong_count(&key1), 2);
assert_eq!(Rc::strong_count(&key2), 2);
}
Expand Down

0 comments on commit 5a84462

Please sign in to comment.