Skip to content
Open
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 compact_str/src/repr/capacity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl Capacity {
cfg_if::cfg_if! {
if #[cfg(target_pointer_width = "64")] {
// on 64-bit arches we can always fit the capacity inline
debug_assert!(capacity <= MAX_VALUE);
assert!(capacity <= MAX_VALUE, "Exceeds maximum representable capacity");
// SAFETY: a 64-bit capacity is `< 2^56`, so its `to_le()` form leaves the
// discriminant byte free for `HEAP_MARKER`, letting the compiler fold the dead
// `Err` arm at callers like `From<String>`. (`to_le()`, not the raw value:
Expand Down
8 changes: 8 additions & 0 deletions compact_str/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2077,6 +2077,14 @@ fn test_alloc_excessively_long_string() {
std::hint::black_box(CompactString::with_capacity((1 << 56) - 2));
}

// Regression test case for #495
#[cfg(target_pointer_width = "64")]
#[test]
#[should_panic = "Exceeds maximum representable capacity"]
fn test_alloc_with_unencodable_capacity() {
assert!(CompactString::try_with_capacity(1usize << 56).is_err());
}

// This feature was enabled by <https://github.com/rust-lang/rust/pull/94075> which was first
// released in Rust 1.65.
#[test]
Expand Down
Loading