about summary refs log tree commit diff
path: root/library/std/src
diff options
context:
space:
mode:
authorXuanwo <github@xuanwo.io>2021-12-28 11:53:14 +0800
committerXuanwo <github@xuanwo.io>2021-12-28 11:53:14 +0800
commit27b92c9f98a0ea3070b988819bb3aa1111ed0382 (patch)
tree145d5739b2d4d102b48131947760f96e6fa537cf /library/std/src
parent013fbc61877c8b1ca964274f171bd79952247fc3 (diff)
Implement support in wtf8
Signed-off-by: Xuanwo <github@xuanwo.io>
Diffstat (limited to 'library/std/src')
-rw-r--r--library/std/src/sys_common/wtf8.rs37
1 files changed, 37 insertions, 0 deletions
diff --git a/library/std/src/sys_common/wtf8.rs b/library/std/src/sys_common/wtf8.rs
index 6e29bc61454..3f3f4abd214 100644
--- a/library/std/src/sys_common/wtf8.rs
+++ b/library/std/src/sys_common/wtf8.rs
@@ -22,6 +22,7 @@ use core::str::next_code_point;
 
 use crate::borrow::Cow;
 use crate::char;
+use crate::collections::TryReserveError;
 use crate::fmt;
 use crate::hash::{Hash, Hasher};
 use crate::iter::FromIterator;
@@ -231,11 +232,47 @@ impl Wtf8Buf {
         self.bytes.reserve(additional)
     }
 
+    /// Tries to reserve capacity for at least `additional` more elements to be inserted
+    /// in the given `Wtf8Buf`. The collection may reserve more space to avoid
+    /// frequent reallocations. After calling `try_reserve`, capacity will be
+    /// greater than or equal to `self.len() + additional`. Does nothing if
+    /// capacity is already sufficient.
+    ///
+    /// # Errors
+    ///
+    /// If the capacity overflows, or the allocator reports a failure, then an error
+    /// is returned.
+    #[inline]
+    pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
+        self.bytes.try_reserve(additional)
+    }
+
     #[inline]
     pub fn reserve_exact(&mut self, additional: usize) {
         self.bytes.reserve_exact(additional)
     }
 
+    /// Tries to reserve the minimum capacity for exactly `additional`
+    /// elements to be inserted in the given `Wtf8Buf`. After calling
+    /// `try_reserve_exact`, capacity will be greater than or equal to
+    /// `self.len() + additional` if it returns `Ok(())`.
+    /// Does nothing if the capacity is already sufficient.
+    ///
+    /// Note that the allocator may give the collection more space than it
+    /// requests. Therefore, capacity can not be relied upon to be precisely
+    /// minimal. Prefer [`try_reserve`] if future insertions are expected.
+    ///
+    /// [`try_reserve`]: Wtf8Buf::try_reserve
+    ///
+    /// # Errors
+    ///
+    /// If the capacity overflows, or the allocator reports a failure, then an error
+    /// is returned.
+    #[inline]
+    pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
+        self.bytes.try_reserve_exact(additional)
+    }
+
     #[inline]
     pub fn shrink_to_fit(&mut self) {
         self.bytes.shrink_to_fit()