Skip to content

Commit 2c16341

Browse files
committed
Auto merge of #89 - SimonSapin:layout, r=Amanieu
Expose the Layout of the failed allocation in CollectionAllocErr::AllocErr This a breaking change to a public API. This enables a similar change proposed in `std`: rust-lang/rust#48043 (comment) There is precedent for this in the `hashglobe` crate, which is Firefox’s (pre-hashbrown) fork of `std::collections::HashMap` with stable `try_reserve`. * https://github.com/servo/servo/blob/b3eed5b5bd/components/hashglobe/src/lib.rs#L29-L42 * https://github.com/servo/servo/blob/b3eed5b5bd/components/hashglobe/src/hash_map.rs#L722
2 parents 14bb115 + c2e26e7 commit 2c16341

File tree

4 files changed

+9
-6
lines changed

4 files changed

+9
-6
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "hashbrown"
3-
version = "0.4.0"
3+
version = "0.5.0"
44
authors = ["Amanieu d'Antras <[email protected]>"]
55
description = "A Rust port of Google's SwissTable hash map"
66
license = "Apache-2.0/MIT"

src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ pub enum CollectionAllocErr {
8888
/// Error due to the computed capacity exceeding the collection's maximum
8989
/// (usually `isize::MAX` bytes).
9090
CapacityOverflow,
91-
/// Error due to the allocator (see the `AllocErr` type's docs).
92-
AllocErr,
91+
/// Error due to the allocator.
92+
AllocErr {
93+
/// The layout of the allocation request that failed.
94+
layout: alloc::alloc::Layout,
95+
},
9396
}

src/map.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3402,12 +3402,12 @@ mod test_map {
34023402
panic!("usize::MAX should trigger an overflow!");
34033403
}
34043404

3405-
if let Err(AllocErr) = empty_bytes.try_reserve(MAX_USIZE / 8) {
3405+
if let Err(AllocErr { .. }) = empty_bytes.try_reserve(MAX_USIZE / 8) {
34063406
} else {
34073407
// This may succeed if there is enough free memory. Attempt to
34083408
// allocate a second hashmap to ensure the allocation will fail.
34093409
let mut empty_bytes2: HashMap<u8, u8> = HashMap::new();
3410-
if let Err(AllocErr) = empty_bytes2.try_reserve(MAX_USIZE / 8) {
3410+
if let Err(AllocErr { .. }) = empty_bytes2.try_reserve(MAX_USIZE / 8) {
34113411
} else {
34123412
panic!("usize::MAX / 8 should trigger an OOM!");
34133413
}

src/raw/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl Fallibility {
8383
#[inline]
8484
fn alloc_err(self, layout: Layout) -> CollectionAllocErr {
8585
match self {
86-
Fallibility::Fallible => CollectionAllocErr::AllocErr,
86+
Fallibility::Fallible => CollectionAllocErr::AllocErr { layout },
8787
Fallibility::Infallible => handle_alloc_error(layout),
8888
}
8989
}

0 commit comments

Comments
 (0)