diff --git a/src/lib.rs b/src/lib.rs index 825d42d..5046bb7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,7 +29,7 @@ mod alloc { #[cfg(not(feature = "disable"))] mod collections { - pub use alloc_crate::collections::CollectionAllocErr; + pub use alloc_crate::collections::TryReserveError; } #[cfg(not(feature = "disable"))] diff --git a/src/map.rs b/src/map.rs index 97cdfc3..89f41f0 100644 --- a/src/map.rs +++ b/src/map.rs @@ -11,7 +11,7 @@ use self::Entry::*; use self::VacantEntryState::*; -use collections::CollectionAllocErr; +use collections::TryReserveError; use core::borrow::Borrow; use core::cmp::max; use core::fmt::{self, Debug}; @@ -28,6 +28,7 @@ use super::table::Fallibility::{Fallible, Infallible}; use super::table::{ self, Bucket, EmptyBucket, Fallibility, FullBucket, FullBucketMut, RawTable, SafeHash, }; +use alloc::Layout; const MIN_NONZERO_RAW_CAPACITY: usize = 32; // must be a power of two @@ -46,7 +47,7 @@ impl DefaultResizePolicy { /// provide that capacity, accounting for maximum loading. The raw capacity /// is always zero or a power of two. #[inline] - fn try_raw_capacity(&self, len: usize) -> Result { + fn try_raw_capacity(&self, len: usize) -> Result { if len == 0 { Ok(0) } else { @@ -56,7 +57,7 @@ impl DefaultResizePolicy { let mut raw_cap = len.checked_mul(11) .map(|l| l / 10) .and_then(|l| l.checked_next_power_of_two()) - .ok_or(CollectionAllocErr::CapacityOverflow)?; + .ok_or(TryReserveError::CapacityOverflow)?; raw_cap = max(MIN_NONZERO_RAW_CAPACITY, raw_cap); Ok(raw_cap) @@ -786,9 +787,10 @@ where /// map.reserve(10); /// ``` pub fn reserve(&mut self, additional: usize) { + let layout = Layout::from_size_align(additional, 4).unwrap(); match self.reserve_internal(additional, Infallible) { - Err(CollectionAllocErr::CapacityOverflow) => panic!("capacity overflow"), - Err(CollectionAllocErr::AllocErr) => unreachable!(), + Err(TryReserveError::CapacityOverflow) => panic!("capacity overflow"), + Err(TryReserveError::AllocError { .. }) => unreachable!(), Ok(()) => { /* yay */ } } } @@ -810,18 +812,18 @@ where /// let mut map: HashMap<&str, isize> = HashMap::new(); /// map.try_reserve(10).expect("why is the test harness OOMing on 10 bytes?"); /// ``` - pub fn try_reserve(&mut self, additional: usize) -> Result<(), CollectionAllocErr> { + pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> { self.reserve_internal(additional, Fallible) } fn reserve_internal(&mut self, additional: usize, fallibility: Fallibility) - -> Result<(), CollectionAllocErr> { + -> Result<(), TryReserveError> { let remaining = self.capacity() - self.len(); // this can't overflow if remaining < additional { let min_cap = self.len() .checked_add(additional) - .ok_or(CollectionAllocErr::CapacityOverflow)?; + .ok_or(TryReserveError::CapacityOverflow)?; let raw_cap = self.resize_policy.try_raw_capacity(min_cap)?; self.try_resize(raw_cap, fallibility)?; } else if self.table.tag() && remaining <= self.len() { @@ -844,7 +846,7 @@ where &mut self, new_raw_cap: usize, fallibility: Fallibility, - ) -> Result<(), CollectionAllocErr> { + ) -> Result<(), TryReserveError> { assert!(self.table.size() <= new_raw_cap); assert!(new_raw_cap.is_power_of_two() || new_raw_cap == 0); diff --git a/src/table.rs b/src/table.rs index 44d113c..c40b133 100644 --- a/src/table.rs +++ b/src/table.rs @@ -9,7 +9,7 @@ // except according to those terms. use alloc::{handle_alloc_error, Alloc, Global, Layout, LayoutErr}; -use collections::CollectionAllocErr; +use collections::TryReserveError; use core::hash::{BuildHasher, Hash, Hasher}; use core::hint; use core::marker; @@ -689,7 +689,7 @@ impl RawTable { unsafe fn new_uninitialized_internal( capacity: usize, fallibility: Fallibility, - ) -> Result, CollectionAllocErr> { + ) -> Result, TryReserveError> { if capacity == 0 { return Ok(RawTable { size: 0, @@ -704,9 +704,12 @@ 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 { + let buffer = Global.alloc(layout).map_err(|_| match fallibility { Infallible => handle_alloc_error(layout), - Fallible => e, + Fallible => TryReserveError::AllocError { + layout, + non_exhaustive: () + }, })?; Ok(RawTable { @@ -720,9 +723,10 @@ impl RawTable { /// Does not initialize the buckets. The caller should ensure they, /// at the very least, set every hash to EMPTY_BUCKET. unsafe fn new_uninitialized(capacity: usize) -> RawTable { + let layout = Layout::from_size_align(capacity, 4).unwrap(); 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, } } @@ -744,7 +748,7 @@ impl RawTable { fn new_internal( capacity: usize, fallibility: Fallibility, - ) -> Result, CollectionAllocErr> { + ) -> Result, TryReserveError> { unsafe { let ret = RawTable::new_uninitialized_internal(capacity, fallibility)?; ptr::write_bytes(ret.hashes.ptr(), 0, capacity); @@ -754,16 +758,17 @@ impl RawTable { /// Tries to create a new raw table from a given capacity. If it cannot allocate, /// it returns with AllocErr. - pub fn try_new(capacity: usize) -> Result, CollectionAllocErr> { + pub fn try_new(capacity: usize) -> Result, 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 { + let layout = Layout::from_size_align(capacity, 4).unwrap(); 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, } }