Skip to content
This repository has been archived by the owner on Aug 19, 2019. It is now read-only.

CollectionAllocErr was renamed to TryReserveError #11

Closed
wants to merge 1 commit into from
Closed
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 @@ -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"))]
Expand Down
20 changes: 11 additions & 9 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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

Expand All @@ -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<usize, CollectionAllocErr> {
fn try_raw_capacity(&self, len: usize) -> Result<usize, TryReserveError> {
if len == 0 {
Ok(0)
} else {
Expand All @@ -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)
Expand Down Expand Up @@ -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 { layout, non_exhaustive: () }) => unreachable!(),
Ok(()) => { /* yay */ }
}
}
Expand All @@ -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() {
Expand All @@ -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);

Expand Down
20 changes: 11 additions & 9 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 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;
Expand Down Expand Up @@ -689,7 +689,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 @@ -706,7 +706,7 @@ impl<K, V> RawTable<K, V> {
let (layout, _) = calculate_layout::<K, V>(capacity)?;
let buffer = Global.alloc(layout).map_err(|e| match fallibility {
Infallible => handle_alloc_error(layout),
Fallible => e,
Fallible => TryReserveError::CapacityOverflow,
})?;

Ok(RawTable {
Expand All @@ -720,9 +720,10 @@ impl<K, V> RawTable<K, V> {
/// 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<K, V> {
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 { layout, non_exhaustive: () }) => unreachable!(),
Ok(table) => table,
}
}
Expand All @@ -744,7 +745,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)?;
ptr::write_bytes(ret.hashes.ptr(), 0, capacity);
Expand All @@ -754,16 +755,17 @@ 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.
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> {
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 { layout, non_exhaustive: () }) => unreachable!(),
Ok(table) => table,
}
}
Expand Down