]> git.lizzy.rs Git - rust.git/commitdiff
Add try_reserve to HashSet
authorAmanieu d'Antras <amanieu@gmail.com>
Thu, 7 Feb 2019 11:28:27 +0000 (12:28 +0100)
committerAmanieu d'Antras <amanieu@gmail.com>
Tue, 23 Apr 2019 22:54:14 +0000 (06:54 +0800)
src/libstd/collections/hash/set.rs

index 0fbf374fa496a6fc6dfa38d481207a262bcad812..f8a93b680f7626d93b1c3779332a0c422e967f17 100644 (file)
@@ -1,4 +1,5 @@
 use crate::borrow::Borrow;
+use crate::collections::CollectionAllocErr;
 use crate::fmt;
 use crate::hash::{Hash, BuildHasher};
 use crate::iter::{Chain, FromIterator, FusedIterator};
@@ -357,6 +358,29 @@ pub fn reserve(&mut self, additional: usize) {
         self.map.reserve(additional)
     }
 
+    /// Tries to reserve capacity for at least `additional` more elements to be inserted
+    /// in the given `HashSet<K,V>`. The collection may reserve more space to avoid
+    /// frequent reallocations.
+    ///
+    /// # Errors
+    ///
+    /// If the capacity overflows, or the allocator reports a failure, then an error
+    /// is returned.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(try_reserve)]
+    /// use std::collections::HashSet;
+    /// let mut set: HashSet<i32> = HashSet::new();
+    /// set.try_reserve(10).expect("why is the test harness OOMing on 10 bytes?");
+    /// ```
+    #[inline]
+    #[unstable(feature = "try_reserve", reason = "new API", issue="48043")]
+    pub fn try_reserve(&mut self, additional: usize) -> Result<(), CollectionAllocErr> {
+        self.map.try_reserve(additional)
+    }
+
     /// Shrinks the capacity of the set as much as possible. It will drop
     /// down as much as possible while maintaining the internal rules
     /// and possibly leaving some space in accordance with the resize policy.