Unnamed repository; edit this file 'description' to name the repository.
Use the number of remaining elements in the char iter for allocation
When collecting from an iterator of chars, when expanding past
INLINE_CAP, include extra space for at least one byte per char for any
remaining known size.
| -rw-r--r-- | lib/smol_str/src/lib.rs | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/lib/smol_str/src/lib.rs b/lib/smol_str/src/lib.rs index 60fc81e227..c449f3611c 100644 --- a/lib/smol_str/src/lib.rs +++ b/lib/smol_str/src/lib.rs @@ -138,12 +138,18 @@ impl SmolStr { } fn from_char_iter<I: iter::Iterator<Item = char>>(mut iter: I) -> SmolStr { + let (min_size, _) = iter.size_hint(); + if min_size > INLINE_CAP { + let heap: String = iter.collect(); + return SmolStr(Repr::Heap(heap.into_boxed_str().into())); + } let mut len = 0; let mut buf = [0u8; INLINE_CAP]; while let Some(ch) = iter.next() { let size = ch.len_utf8(); if size + len > INLINE_CAP { - let mut heap = String::with_capacity(size + len); + let (min_remaining, _) = iter.size_hint(); + let mut heap = String::with_capacity(size + len + min_remaining); heap.push_str(std::str::from_utf8(&buf[..len]).unwrap()); heap.push(ch); heap.extend(iter); |