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 for 61780 #6

Merged
merged 2 commits into from
Aug 30, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
extern crate rand;

mod bench;
mod table;
mod map;
mod table;

pub use map::*;
pub use table::{make_hash, SafeHash};
Expand Down
14 changes: 7 additions & 7 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
use self::Entry::*;
use self::VacantEntryState::*;

use std::cell::Cell;
use rand::{self, Rng};
use std::borrow::Borrow;
use std::cell::Cell;
use std::cmp::max;
use std::fmt::{self, Debug};
#[allow(deprecated)]
use std::hash::{BuildHasher, Hash, Hasher, SipHasher13};
use std::iter::{FromIterator, FusedIterator};
use std::mem::{self, replace};
use std::ops::{Deref, Index};
use rand::{self, Rng};

use super::table::{self, Bucket, EmptyBucket, FullBucket, FullBucketMut, RawTable, SafeHash};
use super::table::BucketState::{Empty, Full};
use super::table::{self, Bucket, EmptyBucket, FullBucket, FullBucketMut, RawTable, SafeHash};

const MIN_NONZERO_RAW_CAPACITY: usize = 32; // must be a power of two

Expand Down Expand Up @@ -812,7 +812,8 @@ where
pub fn reserve(&mut self, additional: usize) {
let remaining = self.capacity() - self.len(); // this can't overflow
if remaining < additional {
let min_cap = self.len()
let min_cap = self
.len()
.checked_add(additional)
.expect("reserve overflow");
let raw_cap = self.resize_policy.raw_capacity(min_cap);
Expand Down Expand Up @@ -2663,11 +2664,11 @@ fn assert_covariance() {

#[cfg(test)]
mod test_map {
use super::HashMap;
use super::Entry::{Occupied, Vacant};
use super::HashMap;
use super::RandomState;
use std::cell::RefCell;
use rand::{thread_rng, Rng};
use std::cell::RefCell;

#[test]
fn test_zero_capacities() {
Expand Down Expand Up @@ -3595,7 +3596,6 @@ mod test_map {
panic!("Adaptive early resize failed");
}


#[test]
fn test_remove_at_index() {
let mut m = HashMap::new();
Expand Down
32 changes: 19 additions & 13 deletions src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// except according to those terms.

use std::alloc::{handle_alloc_error, Alloc, Global, Layout, LayoutErr};
use std::collections::CollectionAllocErr;
use std::collections::TryReserveError;
use std::hash::{BuildHasher, Hash, Hasher};
use std::marker;
use std::mem;
Expand Down Expand Up @@ -679,7 +679,7 @@ impl<K, V> RawTable<K, V> {
unsafe fn new_uninitialized_internal(
capacity: usize,
fallibility: Fallibility,
) -> Result<RawTable<K, V>, CollectionAllocErr> {
) -> Result<RawTable<K, V>, TryReserveError> {
if capacity == 0 {
return Ok(RawTable {
size: 0,
Expand All @@ -694,10 +694,16 @@ impl<K, V> RawTable<K, V> {
// we just allocate a single array, and then have the subarrays
// point into it.
let (layout, _) = calculate_layout::<K, V>(capacity)?;
let buffer = Global.alloc(layout).map_err(|e| match fallibility {
Infallible => handle_alloc_error(layout),
Fallible => e,
})?;
let buffer = Global
.alloc(layout)
.map_err(|e| match fallibility {
Infallible => handle_alloc_error(layout),
Fallible => e,
})
.map_err(|_alloc_err| TryReserveError::AllocError {
layout: layout,
non_exhaustive: (),
})?;

Ok(RawTable {
capacity_mask: capacity.wrapping_sub(1),
Expand All @@ -711,8 +717,8 @@ impl<K, V> RawTable<K, V> {
/// at the very least, set every hash to EMPTY_BUCKET.
unsafe fn new_uninitialized(capacity: usize) -> RawTable<K, V> {
match Self::new_uninitialized_internal(capacity, Infallible) {
Err(CollectionAllocErr::CapacityOverflow) => panic!("capacity overflow"),
Err(CollectionAllocErr::AllocErr) => unreachable!(),
Err(TryReserveError::CapacityOverflow) => panic!("capacity overflow"),
Err(TryReserveError::AllocError { .. }) => unreachable!(),
Ok(table) => table,
}
}
Expand All @@ -733,7 +739,7 @@ impl<K, V> RawTable<K, V> {
fn new_internal(
capacity: usize,
fallibility: Fallibility,
) -> Result<RawTable<K, V>, CollectionAllocErr> {
) -> Result<RawTable<K, V>, TryReserveError> {
unsafe {
let ret = RawTable::new_uninitialized_internal(capacity, fallibility)?;
if capacity > 0 {
Expand All @@ -744,18 +750,18 @@ impl<K, V> RawTable<K, V> {
}

/// Tries to create a new raw table from a given capacity. If it cannot allocate,
/// it returns with AllocErr.
/// it returns with AllocError.
#[allow(unused)]
pub fn try_new(capacity: usize) -> Result<RawTable<K, V>, CollectionAllocErr> {
pub fn try_new(capacity: usize) -> Result<RawTable<K, V>, TryReserveError> {
Self::new_internal(capacity, Fallible)
}

/// Creates a new raw table from a given capacity. All buckets are
/// initially empty.
pub fn new(capacity: usize) -> RawTable<K, V> {
match Self::new_internal(capacity, Infallible) {
Err(CollectionAllocErr::CapacityOverflow) => panic!("capacity overflow"),
Err(CollectionAllocErr::AllocErr) => unreachable!(),
Err(TryReserveError::CapacityOverflow) => panic!("capacity overflow"),
Err(TryReserveError::AllocError { .. }) => unreachable!(),
Ok(table) => table,
}
}
Expand Down