diff --git a/src/lib.rs b/src/lib.rs index 0848a26..eacaf3d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,8 +9,8 @@ extern crate rand; mod bench; -mod table; mod map; +mod table; pub use map::*; pub use table::{make_hash, SafeHash}; diff --git a/src/map.rs b/src/map.rs index 05e5fd3..4c13d9c 100644 --- a/src/map.rs +++ b/src/map.rs @@ -11,8 +11,9 @@ 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)] @@ -20,10 +21,9 @@ 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 @@ -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); @@ -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() { @@ -3595,7 +3596,6 @@ mod test_map { panic!("Adaptive early resize failed"); } - #[test] fn test_remove_at_index() { let mut m = HashMap::new(); diff --git a/src/table.rs b/src/table.rs index 14428cf..46c8938 100644 --- a/src/table.rs +++ b/src/table.rs @@ -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; @@ -679,7 +679,7 @@ impl RawTable { unsafe fn new_uninitialized_internal( capacity: usize, fallibility: Fallibility, - ) -> Result, CollectionAllocErr> { + ) -> Result, TryReserveError> { if capacity == 0 { return Ok(RawTable { size: 0, @@ -694,10 +694,16 @@ impl RawTable { // we just allocate a single array, and then have the subarrays // point into it. let (layout, _) = calculate_layout::(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), @@ -711,8 +717,8 @@ impl RawTable { /// at the very least, set every hash to EMPTY_BUCKET. unsafe fn new_uninitialized(capacity: usize) -> RawTable { 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, } } @@ -733,7 +739,7 @@ impl RawTable { fn new_internal( capacity: usize, fallibility: Fallibility, - ) -> Result, CollectionAllocErr> { + ) -> Result, TryReserveError> { unsafe { let ret = RawTable::new_uninitialized_internal(capacity, fallibility)?; if capacity > 0 { @@ -744,9 +750,9 @@ impl RawTable { } /// 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, CollectionAllocErr> { + pub fn try_new(capacity: usize) -> Result, TryReserveError> { Self::new_internal(capacity, Fallible) } @@ -754,8 +760,8 @@ impl RawTable { /// initially empty. pub fn new(capacity: usize) -> RawTable { 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, } }