Skip to content

Commit c6f29b3

Browse files
committed
Change Entry<'a, K, V, S> back to Entry<'a, K, V>
This fixes a breaking change.
1 parent 9ba0591 commit c6f29b3

File tree

2 files changed

+39
-46
lines changed

2 files changed

+39
-46
lines changed

src/libstd/collections/hash/map.rs

Lines changed: 30 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -619,19 +619,24 @@ impl<K, V, S> HashMap<K, V, S>
619619
/// assert_eq!(letters.get(&'y'), None);
620620
/// ```
621621
#[stable(feature = "rust1", since = "1.0.0")]
622-
pub fn entry(&mut self, key: K) -> Entry<K, V, S> {
622+
pub fn entry(&mut self, key: K) -> Entry<K, V> {
623+
// Ideally we would put this in VacantEntry::insert, but Entry is not
624+
// generic over the BuildHasher and adding a generic parameter would be
625+
// a breaking change.
626+
self.reserve(1);
627+
623628
let hash = make_hash(&self.hash_builder, &key);
624629
if let Some(elem) = self.table.find(hash, |q| q.0.eq(&key)) {
625630
Entry::Occupied(OccupiedEntry {
626631
key: Some(key),
627632
elem,
628-
table: self,
633+
table: &mut self.table,
629634
})
630635
} else {
631636
Entry::Vacant(VacantEntry {
632637
hash,
633638
key,
634-
table: self,
639+
table: &mut self.table,
635640
})
636641
}
637642
}
@@ -1734,20 +1739,20 @@ impl<'a, K, V, S> Debug for RawEntryBuilder<'a, K, V, S> {
17341739
/// [`HashMap`]: struct.HashMap.html
17351740
/// [`entry`]: struct.HashMap.html#method.entry
17361741
#[stable(feature = "rust1", since = "1.0.0")]
1737-
pub enum Entry<'a, K: 'a, V: 'a, S: 'a> {
1742+
pub enum Entry<'a, K: 'a, V: 'a> {
17381743
/// An occupied entry.
17391744
#[stable(feature = "rust1", since = "1.0.0")]
17401745
Occupied(#[stable(feature = "rust1", since = "1.0.0")]
1741-
OccupiedEntry<'a, K, V, S>),
1746+
OccupiedEntry<'a, K, V>),
17421747

17431748
/// A vacant entry.
17441749
#[stable(feature = "rust1", since = "1.0.0")]
17451750
Vacant(#[stable(feature = "rust1", since = "1.0.0")]
1746-
VacantEntry<'a, K, V, S>),
1751+
VacantEntry<'a, K, V>),
17471752
}
17481753

17491754
#[stable(feature= "debug_hash_map", since = "1.12.0")]
1750-
impl<'a, K: 'a + Debug, V: 'a + Debug, S: BuildHasher> Debug for Entry<'a, K, V, S> {
1755+
impl<'a, K: 'a + Debug, V: 'a + Debug> Debug for Entry<'a, K, V> {
17511756
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
17521757
match *self {
17531758
Vacant(ref v) => {
@@ -1769,31 +1774,29 @@ impl<'a, K: 'a + Debug, V: 'a + Debug, S: BuildHasher> Debug for Entry<'a, K, V,
17691774
///
17701775
/// [`Entry`]: enum.Entry.html
17711776
#[stable(feature = "rust1", since = "1.0.0")]
1772-
pub struct OccupiedEntry<'a, K: 'a, V: 'a, S: 'a> {
1777+
pub struct OccupiedEntry<'a, K: 'a, V: 'a> {
17731778
key: Option<K>,
17741779
elem: Bucket<(K, V)>,
1775-
table: &'a mut HashMap<K, V, S>,
1780+
table: &'a mut RawTable<(K, V)>,
17761781
}
17771782

17781783
#[stable(feature = "rust1", since = "1.0.0")]
1779-
unsafe impl<'a, K, V, S> Send for OccupiedEntry<'a, K, V, S>
1784+
unsafe impl<'a, K, V> Send for OccupiedEntry<'a, K, V>
17801785
where
17811786
K: Send,
17821787
V: Send,
1783-
S: Send,
17841788
{
17851789
}
17861790
#[stable(feature = "rust1", since = "1.0.0")]
1787-
unsafe impl<'a, K, V, S> Sync for OccupiedEntry<'a, K, V, S>
1791+
unsafe impl<'a, K, V> Sync for OccupiedEntry<'a, K, V>
17881792
where
17891793
K: Sync,
17901794
V: Sync,
1791-
S: Sync,
17921795
{
17931796
}
17941797

17951798
#[stable(feature= "debug_hash_map", since = "1.12.0")]
1796-
impl<'a, K: 'a + Debug, V: 'a + Debug, S> Debug for OccupiedEntry<'a, K, V, S> {
1799+
impl<'a, K: 'a + Debug, V: 'a + Debug> Debug for OccupiedEntry<'a, K, V> {
17971800
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
17981801
f.debug_struct("OccupiedEntry")
17991802
.field("key", self.key())
@@ -1807,14 +1810,14 @@ impl<'a, K: 'a + Debug, V: 'a + Debug, S> Debug for OccupiedEntry<'a, K, V, S> {
18071810
///
18081811
/// [`Entry`]: enum.Entry.html
18091812
#[stable(feature = "rust1", since = "1.0.0")]
1810-
pub struct VacantEntry<'a, K: 'a, V: 'a, S: 'a> {
1813+
pub struct VacantEntry<'a, K: 'a, V: 'a> {
18111814
hash: u64,
18121815
key: K,
1813-
table: &'a mut HashMap<K, V, S>,
1816+
table: &'a mut RawTable<(K, V)>,
18141817
}
18151818

18161819
#[stable(feature= "debug_hash_map", since = "1.12.0")]
1817-
impl<'a, K: 'a + Debug, V: 'a, S> Debug for VacantEntry<'a, K, V, S> {
1820+
impl<'a, K: 'a + Debug, V: 'a> Debug for VacantEntry<'a, K, V> {
18181821
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
18191822
f.debug_tuple("VacantEntry")
18201823
.field(self.key())
@@ -2091,7 +2094,7 @@ impl<'a, K, V> fmt::Debug for Drain<'a, K, V>
20912094
}
20922095
}
20932096

2094-
impl<'a, K, V, S> Entry<'a, K, V, S> {
2097+
impl<'a, K, V> Entry<'a, K, V> {
20952098
#[stable(feature = "rust1", since = "1.0.0")]
20962099
/// Ensures a value is in the entry by inserting the default if empty, and returns
20972100
/// a mutable reference to the value in the entry.
@@ -2109,11 +2112,7 @@ impl<'a, K, V, S> Entry<'a, K, V, S> {
21092112
/// *map.entry("poneyland").or_insert(10) *= 2;
21102113
/// assert_eq!(map["poneyland"], 6);
21112114
/// ```
2112-
pub fn or_insert(self, default: V) -> &'a mut V
2113-
where
2114-
K: Hash,
2115-
S: BuildHasher,
2116-
{
2115+
pub fn or_insert(self, default: V) -> &'a mut V {
21172116
match self {
21182117
Occupied(entry) => entry.into_mut(),
21192118
Vacant(entry) => entry.insert(default),
@@ -2136,11 +2135,7 @@ impl<'a, K, V, S> Entry<'a, K, V, S> {
21362135
///
21372136
/// assert_eq!(map["poneyland"], "hoho".to_string());
21382137
/// ```
2139-
pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V
2140-
where
2141-
K: Hash,
2142-
S: BuildHasher,
2143-
{
2138+
pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
21442139
match self {
21452140
Occupied(entry) => entry.into_mut(),
21462141
Vacant(entry) => entry.insert(default()),
@@ -2200,7 +2195,7 @@ impl<'a, K, V, S> Entry<'a, K, V, S> {
22002195

22012196
}
22022197

2203-
impl<'a, K, V: Default, S> Entry<'a, K, V, S> {
2198+
impl<'a, K, V: Default> Entry<'a, K, V> {
22042199
#[stable(feature = "entry_or_default", since = "1.28.0")]
22052200
/// Ensures a value is in the entry by inserting the default value if empty,
22062201
/// and returns a mutable reference to the value in the entry.
@@ -2217,19 +2212,15 @@ impl<'a, K, V: Default, S> Entry<'a, K, V, S> {
22172212
/// assert_eq!(map["poneyland"], None);
22182213
/// # }
22192214
/// ```
2220-
pub fn or_default(self) -> &'a mut V
2221-
where
2222-
K: Hash,
2223-
S: BuildHasher,
2224-
{
2215+
pub fn or_default(self) -> &'a mut V {
22252216
match self {
22262217
Occupied(entry) => entry.into_mut(),
22272218
Vacant(entry) => entry.insert(Default::default()),
22282219
}
22292220
}
22302221
}
22312222

2232-
impl<'a, K, V, S> OccupiedEntry<'a, K, V, S> {
2223+
impl<'a, K, V> OccupiedEntry<'a, K, V> {
22332224
/// Gets a reference to the key in the entry.
22342225
///
22352226
/// # Examples
@@ -2267,7 +2258,7 @@ impl<'a, K, V, S> OccupiedEntry<'a, K, V, S> {
22672258
#[stable(feature = "map_entry_recover_keys2", since = "1.12.0")]
22682259
pub fn remove_entry(self) -> (K, V) {
22692260
unsafe {
2270-
self.table.table.erase_no_drop(&self.elem);
2261+
self.table.erase_no_drop(&self.elem);
22712262
self.elem.read()
22722263
}
22732264
}
@@ -2461,7 +2452,7 @@ impl<'a, K, V, S> OccupiedEntry<'a, K, V, S> {
24612452
}
24622453
}
24632454

2464-
impl<'a, K: 'a, V: 'a, S: 'a> VacantEntry<'a, K, V, S> {
2455+
impl<'a, K: 'a, V: 'a> VacantEntry<'a, K, V> {
24652456
/// Gets a reference to the key that would be used when inserting a value
24662457
/// through the `VacantEntry`.
24672458
///
@@ -2514,15 +2505,8 @@ impl<'a, K: 'a, V: 'a, S: 'a> VacantEntry<'a, K, V, S> {
25142505
/// assert_eq!(map["poneyland"], 37);
25152506
/// ```
25162507
#[stable(feature = "rust1", since = "1.0.0")]
2517-
pub fn insert(self, value: V) -> &'a mut V
2518-
where
2519-
K: Hash,
2520-
S: BuildHasher,
2521-
{
2522-
let hash_builder = &self.table.hash_builder;
2523-
let bucket = self.table.table.insert(self.hash, (self.key, value), |x| {
2524-
make_hash(hash_builder, &x.0)
2525-
});
2508+
pub fn insert(self, value: V) -> &'a mut V {
2509+
let bucket = self.table.insert_no_grow(self.hash, (self.key, value));
25262510
unsafe { &mut bucket.as_mut().1 }
25272511
}
25282512
}

src/libstd/collections/hash/raw/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,16 @@ impl<T> RawTable<T> {
646646
#[inline]
647647
pub fn insert(&mut self, hash: u64, value: T, hasher: impl Fn(&T) -> u64) -> Bucket<T> {
648648
self.reserve(1, hasher);
649+
self.insert_no_grow(hash, value)
650+
}
649651

652+
/// Inserts a new element into the table, without growing the table.
653+
///
654+
/// There must be enough space in the table to insert the new element.
655+
///
656+
/// This does not check if the given element already exists in the table.
657+
#[inline]
658+
pub fn insert_no_grow(&mut self, hash: u64, value: T) -> Bucket<T> {
650659
unsafe {
651660
let index = self.find_insert_slot(hash);
652661
let bucket = self.bucket(index);

0 commit comments

Comments
 (0)