From 22eafc213e9de5987f75bc5df8c06dd19c08dbd5 Mon Sep 17 00:00:00 2001 From: puuuuh <54703480+puuuuh@users.noreply.github.com> Date: Fri, 4 Oct 2024 13:33:26 +0300 Subject: [PATCH] Apply suggestions from code review Co-authored-by: Niko Matsakis --- src/interned.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/interned.rs b/src/interned.rs index a15808ec..ff828de8 100644 --- a/src/interned.rs +++ b/src/interned.rs @@ -151,6 +151,7 @@ where let a: &C::Data<'db> = unsafe { std::mem::transmute(a) }; Lookup::eq(&data, a) }) { + // SAFETY: Read lock on map is held during this block return C::struct_from_id(unsafe { *bucket.as_ref().1.get() }); } }; @@ -310,6 +311,19 @@ where } } +/// The `Lookup` trait is a more flexible variant on [`std::borrow::Borrow`] +/// and [`std::borrow::ToOwned`]. It is implemented by "some type that can +/// be used as the lookup key for `O`". This means that `self` +/// can be hashed and compared for equality with values of type `O` +/// without actually creating an owned value. It `self` needs to be interned, +/// it can be converted into an equivalent value of type `O`. +/// +/// The canonical example is `&str: Lookup`. However, this example +/// alone can be handled by [`std::borrow::Borrow`][]. In our case, we may have +/// multiple keys accumulated into a struct, like `ViewStruct: Lookup<(K1, ...)>`, +/// where `struct ViewStruct...>(K1...)`. The `Borrow` trait +/// requires that `&(K1...)` be convertible to `&ViewStruct` which just isn't +/// possible. `Lookup` instead offers direct `hash` and `eq` methods. pub trait Lookup { fn hash(&self, h: &mut H);