|
1 | | -use crate::thin_str::ThinStr; |
2 | 1 | use core::hash; |
3 | 2 | use core::ops::Deref; |
4 | | -use libdd_alloc::{ChainAllocator, VirtualAllocator}; |
| 3 | +use libdd_alloc::{AllocError, Allocator, ChainAllocator, VirtualAllocator}; |
| 4 | +use libdd_profiling::profiles::collections::ThinStr; |
5 | 5 |
|
6 | 6 | type Hasher = hash::BuildHasherDefault<rustc_hash::FxHasher>; |
7 | 7 | type HashSet<K> = std::collections::HashSet<K, Hasher>; |
8 | 8 |
|
| 9 | +/// Allocates and constructs a [`ThinStr`] in one step. |
| 10 | +/// |
| 11 | +/// This combines [`ThinStr::try_allocate_for`] and [`ThinStr::try_from_str_in`] |
| 12 | +/// for convenience. The returned [`ThinStr`] borrows from the allocation made |
| 13 | +/// by `alloc`, so the allocator must outlive the returned reference. |
| 14 | +fn try_new_thin_str_in<'a, A: Allocator>(s: &str, alloc: &'a A) -> Result<ThinStr<'a>, AllocError> { |
| 15 | + let obj = ThinStr::try_allocate_for(s, alloc)?; |
| 16 | + // SAFETY: `obj` was just allocated by us, no other references exist, |
| 17 | + // so we can safely create a `&mut [MaybeUninit<u8>]` from it. |
| 18 | + let uninit = unsafe { &mut *obj.as_ptr() }; |
| 19 | + ThinStr::try_from_str_in(s, uninit) |
| 20 | +} |
| 21 | + |
9 | 22 | /// Holds unique strings and provides [StringId]s that correspond to the order |
10 | 23 | /// that the strings were inserted. |
11 | 24 | pub struct StringSet { |
@@ -77,7 +90,7 @@ impl StringSet { |
77 | 90 | // No match. Make a new string in the arena, and fudge its |
78 | 91 | // lifetime to appease the borrow checker. |
79 | 92 | let new_str = { |
80 | | - let s = ThinStr::try_from_str_in(str, &self.arena) |
| 93 | + let s = try_new_thin_str_in(str, &self.arena) |
81 | 94 | .expect("allocation for StringSet::insert to succeed"); |
82 | 95 |
|
83 | 96 | // SAFETY: all references to this value get re-narrowed to |
|
0 commit comments