Skip to content

Commit

Permalink
style(rust): add explicit lifetime
Browse files Browse the repository at this point in the history
  • Loading branch information
henryhchchc committed Jun 26, 2024
1 parent 4a3fa56 commit 2044a1d
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl<K, V> Cache<K, V> {
}
}

pub fn get_or_try_put<G, E, Q>(&self, key: &Q, generator: G) -> Result<&V, E>
pub fn get_or_try_put<'c, G, E, Q>(&'c self, key: &Q, generator: G) -> Result<&'c V, E>
where
Q: ?Sized + Eq + Hash + ToOwned<Owned = K>,
K: Eq + Hash + Borrow<Q>,
Expand All @@ -32,7 +32,7 @@ impl<K, V> Cache<K, V> {
// SAFETY: We never remove elements from the cache so the `Box` is not dropped until
// `self.cache` gets dropped, which is when `self` gets dropped.
// Therefore, it is ok to extend the lifetime of the reference to the lifetime of `self`.
unsafe { transmute::<&V, &V>(b.as_ref()) }
unsafe { transmute::<&V, &'c V>(b.as_ref()) }
} else {
drop(cache);
let mut cache = match self.inner.write() {
Expand All @@ -54,7 +54,7 @@ impl<K, V> Cache<K, V> {
// SAFETY: We never remove elements from the cache so the `Box` is not dropped until
// `self.cache` gets dropped, which is when `self` gets dropped.
// Therefore, it is ok to extend the lifetime of the reference to the lifetime of `self`.
unsafe { transmute::<&V, &V>(item_box.as_ref()) }
unsafe { transmute::<&V, &'c V>(item_box.as_ref()) }
};
Ok(item)
}
Expand Down

0 comments on commit 2044a1d

Please sign in to comment.