Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some slightly performance & style updates #370

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ impl CompactString {
/// ```
#[inline]
pub fn as_bytes(&self) -> &[u8] {
&self.0.as_slice()[..self.len()]
self.0.as_slice()
}

// TODO: Implement a `try_as_mut_slice(...)` that will fail if it results in cloning?
Expand Down
9 changes: 8 additions & 1 deletion compact_str/src/repr/capacity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const HEAP_MARKER: usize = {
///
/// All bytes `255`, with the last being [`LastUtf8Char::Heap`], using the same amount of bytes
/// as `usize`. Example (64-bit): `[255, 255, 255, 255, 255, 255, 255, 216]`
#[cfg(not(target_pointer_width = "64"))]
const CAPACITY_IS_ON_THE_HEAP: Capacity = Capacity(VALID_MASK | HEAP_MARKER);

/// The maximum value we're able to store, e.g. on 64-bit arch this is 2^56 - 2.
Expand Down Expand Up @@ -101,7 +102,13 @@ impl Capacity {
/// stored on the heap
#[inline(always)]
pub fn is_heap(self) -> bool {
self == CAPACITY_IS_ON_THE_HEAP
cfg_if::cfg_if! {
if #[cfg(target_pointer_width = "64")] {
false
} else {
self == CAPACITY_IS_ON_THE_HEAP
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion compact_str/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1914,7 +1914,7 @@ fn test_from_string_buffer_inlines_on_clone() {
#[should_panic = "Cannot allocate memory to hold CompactString"]
fn test_alloc_excessively_long_string() {
// 2**56 - 2 bytes, the maximum number `Capacity` can hold
CompactString::with_capacity((1 << 56) - 2);
std::hint::black_box(CompactString::with_capacity((1 << 56) - 2));
}

// This feature was enabled by <https://github.com/rust-lang/rust/pull/94075> which was first
Expand Down