1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//! UTF-16 string allocation helpers on [`Arena`].
//!
//! Public docs live on [`Arena`] itself.
use core::ptr::NonNull;
use allocator_api2::alloc::{AllocError, Allocator};
use super::{AllocFlavor, Arena, expect_alloc};
use crate::internal::owned_in_chunk::{OwnedInLocalChunk, OwnedInSharedChunk};
#[cfg_attr(docsrs, doc(cfg(feature = "utf16")))]
impl<A: Allocator + Clone> Arena<A> {
/// Allocates `[len_prefix: usize][u16 elements...]` in the current
/// local chunk, copies elements, and bumps refcount per `flavor`.
fn try_alloc_utf16_prefixed_local(&self, src: &[u16], flavor: AllocFlavor) -> Result<NonNull<u16>, AllocError> {
let len = src.len();
let payload_bytes = len.checked_mul(core::mem::size_of::<u16>()).ok_or(AllocError)?;
let total = core::mem::size_of::<usize>().checked_add(payload_bytes).ok_or(AllocError)?;
if total > self.provider.max_normal_alloc {
debug_assert!(matches!(flavor, AllocFlavor::Rc | AllocFlavor::Box));
// SAFETY: src is a valid slice of `len` u16s.
return unsafe { self.try_alloc_prefixed_local_oversized::<u16>(src.as_ptr(), len, payload_bytes) };
}
try_alloc_prefixed!(
self = self,
src_ptr = src.as_ptr(),
len = len,
payload_bytes = payload_bytes,
elem_ty = u16,
slot = current_local,
refill = refill_local,
accounting = {
// SAFETY: callers only invoke with `Rc` or `Box`; the `SimpleRef`
// flavor goes through a different code path.
debug_assert!(matches!(flavor, AllocFlavor::Rc | AllocFlavor::Box));
self.current_local.bump_smart_pointers_issued();
},
)
}
/// Local owned sibling for the safe smart-pointer constructors.
fn try_alloc_utf16_prefixed_local_owned(&self, src: &[u16], flavor: AllocFlavor) -> Result<OwnedInLocalChunk<u16, A>, AllocError> {
let raw = self.try_alloc_utf16_prefixed_local(src, flavor)?;
// SAFETY: helper returned a fresh local UTF-16 allocation with one caller-owned `+1`.
Ok(unsafe { OwnedInLocalChunk::from_raw_alloc(raw) })
}
/// Allocates `[len_prefix: usize][u16 elements...]` in the current
/// shared chunk, accounting via `arcs_issued`.
fn try_alloc_utf16_prefixed_shared(&self, src: &[u16]) -> Result<NonNull<u16>, AllocError> {
let len = src.len();
let payload_bytes = len.checked_mul(core::mem::size_of::<u16>()).ok_or(AllocError)?;
let total = core::mem::size_of::<usize>().checked_add(payload_bytes).ok_or(AllocError)?;
if total > self.provider.max_normal_alloc {
// SAFETY: src is a valid slice of `len` u16s.
return unsafe { self.try_alloc_prefixed_shared_oversized::<u16>(src.as_ptr(), len, payload_bytes) };
}
try_alloc_prefixed!(
self = self,
src_ptr = src.as_ptr(),
len = len,
payload_bytes = payload_bytes,
elem_ty = u16,
slot = current_shared,
refill = refill_shared,
accounting = {
self.current_shared.bump_smart_pointers_issued();
},
)
}
/// Shared owned sibling of [`Self::try_alloc_utf16_prefixed_local_owned`].
fn try_alloc_utf16_prefixed_shared_owned(&self, src: &[u16]) -> Result<OwnedInSharedChunk<u16, A>, AllocError> {
let raw = self.try_alloc_utf16_prefixed_shared(src)?;
// SAFETY: helper returned a fresh shared UTF-16 allocation with one caller-owned `+1`.
Ok(unsafe { OwnedInSharedChunk::from_raw_alloc(raw) })
}
/// Copy `s` into the arena and return an [`RcUtf16Str`](crate::strings::RcUtf16Str).
#[must_use]
#[inline]
pub fn alloc_utf16_str_rc(&self, s: impl AsRef<widestring::Utf16Str>) -> crate::strings::RcUtf16Str<A> {
expect_alloc(self.try_alloc_utf16_str_rc(s))
}
/// Fallible variant of [`Self::alloc_utf16_str_rc`].
///
/// # Errors
///
/// Returns [`AllocError`] if the backing allocator fails (chunk
/// exhaustion or oversize-cutover budget) or if the source string
/// length would overflow the inline length-prefix accounting.
#[inline]
pub fn try_alloc_utf16_str_rc(&self, s: impl AsRef<widestring::Utf16Str>) -> Result<crate::strings::RcUtf16Str<A>, AllocError> {
let owned = self.try_alloc_utf16_prefixed_local_owned(s.as_ref().as_slice(), AllocFlavor::Rc)?;
Ok(crate::strings::RcUtf16Str::from_owned_in_chunk(owned))
}
/// Copy `s` into a `Shared`-flavor chunk and return an [`ArcUtf16Str`](crate::strings::ArcUtf16Str).
#[must_use]
#[inline]
pub fn alloc_utf16_str_arc(&self, s: impl AsRef<widestring::Utf16Str>) -> crate::strings::ArcUtf16Str<A>
where
A: Send + Sync,
{
expect_alloc(self.try_alloc_utf16_str_arc(s))
}
/// Fallible variant of [`Self::alloc_utf16_str_arc`].
///
/// # Errors
///
/// Returns [`AllocError`] if the backing allocator fails (chunk
/// exhaustion or oversize-cutover budget) or if the source string
/// length would overflow the inline length-prefix accounting.
#[inline]
pub fn try_alloc_utf16_str_arc(&self, s: impl AsRef<widestring::Utf16Str>) -> Result<crate::strings::ArcUtf16Str<A>, AllocError>
where
A: Send + Sync,
{
let owned = self.try_alloc_utf16_prefixed_shared_owned(s.as_ref().as_slice())?;
Ok(crate::strings::ArcUtf16Str::from_owned_in_chunk(owned))
}
/// Copy `s` into the arena and return a [`BoxUtf16Str`](crate::strings::BoxUtf16Str).
#[must_use]
#[inline]
pub fn alloc_utf16_str_box(&self, s: impl AsRef<widestring::Utf16Str>) -> crate::strings::BoxUtf16Str<A> {
expect_alloc(self.try_alloc_utf16_str_box(s))
}
/// Fallible variant of [`Self::alloc_utf16_str_box`].
///
/// # Errors
///
/// Returns [`AllocError`] if the backing allocator fails (chunk
/// exhaustion or oversize-cutover budget) or if the source string
/// length would overflow the inline length-prefix accounting.
#[inline]
pub fn try_alloc_utf16_str_box(&self, s: impl AsRef<widestring::Utf16Str>) -> Result<crate::strings::BoxUtf16Str<A>, AllocError> {
let owned = self.try_alloc_utf16_prefixed_local_owned(s.as_ref().as_slice(), AllocFlavor::Box)?;
Ok(crate::strings::BoxUtf16Str::from_owned_in_chunk(owned))
}
/// Transcode `s` from UTF-8 to UTF-16 and return an [`RcUtf16Str`](crate::strings::RcUtf16Str).
#[must_use]
#[inline]
pub fn alloc_utf16_str_rc_from_str(&self, s: impl AsRef<str>) -> crate::strings::RcUtf16Str<A> {
expect_alloc(self.try_alloc_utf16_str_rc_from_str(s))
}
/// Fallible variant of [`Self::alloc_utf16_str_rc_from_str`].
///
/// # Errors
///
/// Returns [`AllocError`] if the backing allocator fails (chunk
/// exhaustion or oversize-cutover budget) or if the source string
/// length would overflow the inline length-prefix accounting.
#[inline]
pub fn try_alloc_utf16_str_rc_from_str(&self, s: impl AsRef<str>) -> Result<crate::strings::RcUtf16Str<A>, AllocError> {
let buf: alloc::vec::Vec<u16> = s.as_ref().encode_utf16().collect();
let owned = self.try_alloc_utf16_prefixed_local_owned(&buf, AllocFlavor::Rc)?;
Ok(crate::strings::RcUtf16Str::from_owned_in_chunk(owned))
}
/// Transcode `s` from UTF-8 to UTF-16 and return an [`ArcUtf16Str`](crate::strings::ArcUtf16Str).
#[must_use]
#[inline]
pub fn alloc_utf16_str_arc_from_str(&self, s: impl AsRef<str>) -> crate::strings::ArcUtf16Str<A>
where
A: Send + Sync,
{
expect_alloc(self.try_alloc_utf16_str_arc_from_str(s))
}
/// Fallible variant of [`Self::alloc_utf16_str_arc_from_str`].
///
/// # Errors
///
/// Returns [`AllocError`] if the backing allocator fails (chunk
/// exhaustion or oversize-cutover budget) or if the source string
/// length would overflow the inline length-prefix accounting.
#[inline]
pub fn try_alloc_utf16_str_arc_from_str(&self, s: impl AsRef<str>) -> Result<crate::strings::ArcUtf16Str<A>, AllocError>
where
A: Send + Sync,
{
let buf: alloc::vec::Vec<u16> = s.as_ref().encode_utf16().collect();
let owned = self.try_alloc_utf16_prefixed_shared_owned(&buf)?;
Ok(crate::strings::ArcUtf16Str::from_owned_in_chunk(owned))
}
/// Transcode `s` from UTF-8 to UTF-16 and return a [`BoxUtf16Str`](crate::strings::BoxUtf16Str).
#[must_use]
#[inline]
pub fn alloc_utf16_str_box_from_str(&self, s: impl AsRef<str>) -> crate::strings::BoxUtf16Str<A> {
expect_alloc(self.try_alloc_utf16_str_box_from_str(s))
}
/// Fallible variant of [`Self::alloc_utf16_str_box_from_str`].
///
/// # Errors
///
/// Returns [`AllocError`] if the backing allocator fails (chunk
/// exhaustion or oversize-cutover budget) or if the source string
/// length would overflow the inline length-prefix accounting.
#[inline]
pub fn try_alloc_utf16_str_box_from_str(&self, s: impl AsRef<str>) -> Result<crate::strings::BoxUtf16Str<A>, AllocError> {
let buf: alloc::vec::Vec<u16> = s.as_ref().encode_utf16().collect();
let owned = self.try_alloc_utf16_prefixed_local_owned(&buf, AllocFlavor::Box)?;
Ok(crate::strings::BoxUtf16Str::from_owned_in_chunk(owned))
}
/// Create a new, empty growable [`Utf16String`](crate::strings::Utf16String) backed by this arena.
#[must_use]
#[inline]
pub const fn alloc_utf16_string(&self) -> crate::strings::Utf16String<'_, A> {
crate::strings::Utf16String::new_in(self)
}
/// Create a new growable arena-backed [`Utf16String`](crate::strings::Utf16String) with capacity.
///
/// At least `cap` `u16` elements are pre-allocated.
///
/// # Panics
///
/// Panics if the backing allocator fails. Use
/// [`Self::try_alloc_utf16_string_with_capacity`] for a fallible variant.
#[must_use]
#[inline]
pub fn alloc_utf16_string_with_capacity(&self, cap: usize) -> crate::strings::Utf16String<'_, A> {
crate::strings::Utf16String::with_capacity_in(cap, self)
}
/// Fallible variant of [`Self::alloc_utf16_string_with_capacity`].
///
/// # Errors
///
/// Returns [`AllocError`] if the backing allocator fails (chunk
/// exhaustion or oversize-cutover budget) or if the source string
/// length would overflow the inline length-prefix accounting.
#[inline]
pub fn try_alloc_utf16_string_with_capacity(&self, cap: usize) -> Result<crate::strings::Utf16String<'_, A>, AllocError> {
crate::strings::Utf16String::try_with_capacity_in(cap, self)
}
}