Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix soundness bug #153

Merged
merged 1 commit into from Sep 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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() {}