Skip to content

Commit

Permalink
Fix following rust-lang/rust#61780
Browse files Browse the repository at this point in the history
  • Loading branch information
jonhoo committed Aug 30, 2019
1 parent 1dcddda commit 1b4fe4e
Showing 1 changed file with 19 additions and 13 deletions.
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

0 comments on commit 1b4fe4e

Please sign in to comment.