Skip to content

Commit

Permalink
Merge pull request #153 from matklad/lifetime-bug
Browse files Browse the repository at this point in the history
fix soundness bug
  • Loading branch information
jeromefroe committed Sep 3, 2022
2 parents b491112 + 89af08b commit d5230df
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/main.yml
Expand Up @@ -44,4 +44,5 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: clippy
args: -- -D warnings
# clippy::needless_lifetimes suggest unsound code in this crate
args: -- -D warnings -A clippy::needless_lifetimes
18 changes: 16 additions & 2 deletions src/lib.rs
Expand Up @@ -486,7 +486,7 @@ impl<K: Hash + Eq, V, S: BuildHasher> LruCache<K, V, S> {
/// assert_eq!(cache.get_or_insert(1, ||"a"), &"a");
/// assert_eq!(cache.get_or_insert(1, ||"b"), &"a");
/// ```
pub fn get_or_insert<'a, F>(&mut self, k: K, f: F) -> &'a V
pub fn get_or_insert<'a, F>(&'a mut self, k: K, f: F) -> &'a V
where
F: FnOnce() -> V,
{
Expand Down Expand Up @@ -581,7 +581,7 @@ impl<K: Hash + Eq, V, S: BuildHasher> LruCache<K, V, S> {
///
/// assert_eq!(cache.peek_lru(), Some((&1, &"a")));
/// ```
pub fn peek_lru<'a>(&'_ self) -> Option<(&'a K, &'a V)> {
pub fn peek_lru<'a>(&'a self) -> Option<(&'a K, &'a V)> {
if self.is_empty() {
return None;
}
Expand Down Expand Up @@ -1886,3 +1886,17 @@ mod tests {
assert_eq!(DROP_COUNT.load(Ordering::SeqCst), n * n * 2);
}
}

/// Doctests for what should *not* compile
///
/// ```compile_fail
/// let mut cache = lru::LruCache::<u32, u32>::unbounded();
/// let _: &'static u32 = cache.get_or_insert(0, || 92);
/// ```
///
/// ```compile_fail
/// let mut cache = lru::LruCache::<u32, u32>::unbounded();
/// let _: Option<(&'static u32, _)> = cache.peek_lru();
/// let _: Option<(_, &'static u32)> = cache.peek_lru();
/// ```
fn _test_lifetimes() {}

0 comments on commit d5230df

Please sign in to comment.