Skip to content

Commit 0aa9342

Browse files
committed
Remove KeyRef from the public api
1 parent 69d703e commit 0aa9342

File tree

1 file changed

+42
-53
lines changed

1 file changed

+42
-53
lines changed

src/lib.rs

Lines changed: 42 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
//! ```
5959
6060
#![no_std]
61-
#![cfg_attr(feature = "nightly", feature(negative_impls, auto_traits))]
6261

6362
#[cfg(feature = "hashbrown")]
6463
extern crate hashbrown;
@@ -88,8 +87,7 @@ use std::collections::HashMap;
8887
extern crate alloc;
8988

9089
// Struct used to hold a reference to a key
91-
#[doc(hidden)]
92-
pub struct KeyRef<K> {
90+
struct KeyRef<K> {
9391
k: *const K,
9492
}
9593

@@ -107,49 +105,40 @@ impl<K: PartialEq> PartialEq for KeyRef<K> {
107105

108106
impl<K: Eq> Eq for KeyRef<K> {}
109107

110-
#[cfg(feature = "nightly")]
111-
#[doc(hidden)]
112-
pub auto trait NotKeyRef {}
108+
// This type exists to allow a "blanket" Borrow impl for KeyRef without conflicting with the
109+
// stdlib blanket impl
110+
#[repr(transparent)]
111+
struct KeyWrapper<K: ?Sized>(K);
113112

114-
#[cfg(feature = "nightly")]
115-
impl<K> !NotKeyRef for KeyRef<K> {}
116-
117-
#[cfg(feature = "nightly")]
118-
impl<K, D> Borrow<D> for KeyRef<K>
119-
where
120-
K: Borrow<D>,
121-
D: NotKeyRef + ?Sized,
122-
{
123-
fn borrow(&self) -> &D {
124-
unsafe { &*self.k }.borrow()
113+
impl<K: ?Sized> KeyWrapper<K> {
114+
fn from_ref(key: &K) -> &Self {
115+
// safety: KeyWrapper is transparent, so casting the ref like this is allowable
116+
unsafe { &*(key as *const K as *const KeyWrapper<K>) }
125117
}
126118
}
127119

128-
#[cfg(not(feature = "nightly"))]
129-
impl<K> Borrow<K> for KeyRef<K> {
130-
fn borrow(&self) -> &K {
131-
unsafe { &*self.k }
120+
impl<K: ?Sized + Hash> Hash for KeyWrapper<K> {
121+
fn hash<H: Hasher>(&self, state: &mut H) {
122+
self.0.hash(state)
132123
}
133124
}
134125

135-
#[cfg(not(feature = "nightly"))]
136-
impl Borrow<str> for KeyRef<alloc::string::String> {
137-
fn borrow(&self) -> &str {
138-
unsafe { &*self.k }
126+
impl<K: ?Sized + PartialEq> PartialEq for KeyWrapper<K> {
127+
fn eq(&self, other: &Self) -> bool {
128+
self.0.eq(&other.0)
139129
}
140130
}
141131

142-
#[cfg(not(feature = "nightly"))]
143-
impl<T: ?Sized> Borrow<T> for KeyRef<Box<T>> {
144-
fn borrow(&self) -> &T {
145-
unsafe { &*self.k }
146-
}
147-
}
132+
impl<K: ?Sized + Eq> Eq for KeyWrapper<K> {}
148133

149-
#[cfg(not(feature = "nightly"))]
150-
impl<T> Borrow<[T]> for KeyRef<alloc::vec::Vec<T>> {
151-
fn borrow(&self) -> &[T] {
152-
unsafe { &*self.k }
134+
impl<K, Q> Borrow<KeyWrapper<Q>> for KeyRef<K>
135+
where
136+
K: Borrow<Q>,
137+
Q: ?Sized,
138+
{
139+
fn borrow(&self) -> &KeyWrapper<Q> {
140+
let key = unsafe { &*self.k }.borrow();
141+
KeyWrapper::from_ref(key)
153142
}
154143
}
155144

@@ -422,10 +411,10 @@ impl<K: Hash + Eq, V, S: BuildHasher> LruCache<K, V, S> {
422411
/// ```
423412
pub fn get<'a, Q>(&'a mut self, k: &Q) -> Option<&'a V>
424413
where
425-
KeyRef<K>: Borrow<Q>,
414+
K: Borrow<Q>,
426415
Q: Hash + Eq + ?Sized,
427416
{
428-
if let Some(node) = self.map.get_mut(k) {
417+
if let Some(node) = self.map.get_mut(KeyWrapper::from_ref(k)) {
429418
let node_ptr: *mut LruEntry<K, V> = node.as_ptr();
430419

431420
self.detach(node_ptr);
@@ -458,10 +447,10 @@ impl<K: Hash + Eq, V, S: BuildHasher> LruCache<K, V, S> {
458447
/// ```
459448
pub fn get_mut<'a, Q>(&'a mut self, k: &Q) -> Option<&'a mut V>
460449
where
461-
KeyRef<K>: Borrow<Q>,
450+
K: Borrow<Q>,
462451
Q: Hash + Eq + ?Sized,
463452
{
464-
if let Some(node) = self.map.get_mut(k) {
453+
if let Some(node) = self.map.get_mut(KeyWrapper::from_ref(k)) {
465454
let node_ptr: *mut LruEntry<K, V> = node.as_ptr();
466455

467456
self.detach(node_ptr);
@@ -584,11 +573,11 @@ impl<K: Hash + Eq, V, S: BuildHasher> LruCache<K, V, S> {
584573
/// ```
585574
pub fn peek<'a, Q>(&'a self, k: &Q) -> Option<&'a V>
586575
where
587-
KeyRef<K>: Borrow<Q>,
576+
K: Borrow<Q>,
588577
Q: Hash + Eq + ?Sized,
589578
{
590579
self.map
591-
.get(k)
580+
.get(KeyWrapper::from_ref(k))
592581
.map(|node| unsafe { &*node.as_ref().val.as_ptr() })
593582
}
594583

@@ -611,10 +600,10 @@ impl<K: Hash + Eq, V, S: BuildHasher> LruCache<K, V, S> {
611600
/// ```
612601
pub fn peek_mut<'a, Q>(&'a mut self, k: &Q) -> Option<&'a mut V>
613602
where
614-
KeyRef<K>: Borrow<Q>,
603+
K: Borrow<Q>,
615604
Q: Hash + Eq + ?Sized,
616605
{
617-
match self.map.get_mut(k) {
606+
match self.map.get_mut(KeyWrapper::from_ref(k)) {
618607
None => None,
619608
Some(node) => Some(unsafe { &mut *(*node.as_ptr()).val.as_mut_ptr() }),
620609
}
@@ -671,10 +660,10 @@ impl<K: Hash + Eq, V, S: BuildHasher> LruCache<K, V, S> {
671660
/// ```
672661
pub fn contains<Q>(&self, k: &Q) -> bool
673662
where
674-
KeyRef<K>: Borrow<Q>,
663+
K: Borrow<Q>,
675664
Q: Hash + Eq + ?Sized,
676665
{
677-
self.map.contains_key(k)
666+
self.map.contains_key(KeyWrapper::from_ref(k))
678667
}
679668

680669
/// Removes and returns the value corresponding to the key from the cache or
@@ -696,10 +685,10 @@ impl<K: Hash + Eq, V, S: BuildHasher> LruCache<K, V, S> {
696685
/// ```
697686
pub fn pop<Q>(&mut self, k: &Q) -> Option<V>
698687
where
699-
KeyRef<K>: Borrow<Q>,
688+
K: Borrow<Q>,
700689
Q: Hash + Eq + ?Sized,
701690
{
702-
match self.map.remove(k) {
691+
match self.map.remove(KeyWrapper::from_ref(k)) {
703692
None => None,
704693
Some(old_node) => {
705694
let mut old_node = unsafe {
@@ -738,10 +727,10 @@ impl<K: Hash + Eq, V, S: BuildHasher> LruCache<K, V, S> {
738727
/// ```
739728
pub fn pop_entry<Q>(&mut self, k: &Q) -> Option<(K, V)>
740729
where
741-
KeyRef<K>: Borrow<Q>,
730+
K: Borrow<Q>,
742731
Q: Hash + Eq + ?Sized,
743732
{
744-
match self.map.remove(k) {
733+
match self.map.remove(KeyWrapper::from_ref(k)) {
745734
None => None,
746735
Some(old_node) => {
747736
let mut old_node = unsafe { *Box::from_raw(old_node.as_ptr()) };
@@ -806,10 +795,10 @@ impl<K: Hash + Eq, V, S: BuildHasher> LruCache<K, V, S> {
806795
/// ```
807796
pub fn promote<'a, Q>(&'a mut self, k: &Q)
808797
where
809-
KeyRef<K>: Borrow<Q>,
798+
K: Borrow<Q>,
810799
Q: Hash + Eq + ?Sized,
811800
{
812-
if let Some(node) = self.map.get_mut(k) {
801+
if let Some(node) = self.map.get_mut(KeyWrapper::from_ref(k)) {
813802
let node_ptr: *mut LruEntry<K, V> = node.as_ptr();
814803
self.detach(node_ptr);
815804
self.attach(node_ptr);
@@ -842,10 +831,10 @@ impl<K: Hash + Eq, V, S: BuildHasher> LruCache<K, V, S> {
842831
/// ```
843832
pub fn demote<'a, Q>(&'a mut self, k: &Q)
844833
where
845-
KeyRef<K>: Borrow<Q>,
834+
K: Borrow<Q>,
846835
Q: Hash + Eq + ?Sized,
847836
{
848-
if let Some(node) = self.map.get_mut(k) {
837+
if let Some(node) = self.map.get_mut(KeyWrapper::from_ref(k)) {
849838
let node_ptr: *mut LruEntry<K, V> = node.as_ptr();
850839
self.detach(node_ptr);
851840
self.attach_last(node_ptr);

0 commit comments

Comments
 (0)