Skip to content

Commit 27b92c9

Browse files
committed
Implement support in wtf8
Signed-off-by: Xuanwo <github@xuanwo.io>
1 parent 013fbc6 commit 27b92c9

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

library/std/src/sys_common/wtf8.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use core::str::next_code_point;
2222

2323
use crate::borrow::Cow;
2424
use crate::char;
25+
use crate::collections::TryReserveError;
2526
use crate::fmt;
2627
use crate::hash::{Hash, Hasher};
2728
use crate::iter::FromIterator;
@@ -231,11 +232,47 @@ impl Wtf8Buf {
231232
self.bytes.reserve(additional)
232233
}
233234

235+
/// Tries to reserve capacity for at least `additional` more elements to be inserted
236+
/// in the given `Wtf8Buf`. The collection may reserve more space to avoid
237+
/// frequent reallocations. After calling `try_reserve`, capacity will be
238+
/// greater than or equal to `self.len() + additional`. Does nothing if
239+
/// capacity is already sufficient.
240+
///
241+
/// # Errors
242+
///
243+
/// If the capacity overflows, or the allocator reports a failure, then an error
244+
/// is returned.
245+
#[inline]
246+
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
247+
self.bytes.try_reserve(additional)
248+
}
249+
234250
#[inline]
235251
pub fn reserve_exact(&mut self, additional: usize) {
236252
self.bytes.reserve_exact(additional)
237253
}
238254

255+
/// Tries to reserve the minimum capacity for exactly `additional`
256+
/// elements to be inserted in the given `Wtf8Buf`. After calling
257+
/// `try_reserve_exact`, capacity will be greater than or equal to
258+
/// `self.len() + additional` if it returns `Ok(())`.
259+
/// Does nothing if the capacity is already sufficient.
260+
///
261+
/// Note that the allocator may give the collection more space than it
262+
/// requests. Therefore, capacity can not be relied upon to be precisely
263+
/// minimal. Prefer [`try_reserve`] if future insertions are expected.
264+
///
265+
/// [`try_reserve`]: Wtf8Buf::try_reserve
266+
///
267+
/// # Errors
268+
///
269+
/// If the capacity overflows, or the allocator reports a failure, then an error
270+
/// is returned.
271+
#[inline]
272+
pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
273+
self.bytes.try_reserve_exact(additional)
274+
}
275+
239276
#[inline]
240277
pub fn shrink_to_fit(&mut self) {
241278
self.bytes.shrink_to_fit()

0 commit comments

Comments
 (0)