about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorAmanieu d'Antras <amanieu@gmail.com>2019-02-07 12:28:27 +0100
committerAmanieu d'Antras <amanieu@gmail.com>2019-04-24 06:54:14 +0800
commita533504ca12ed93fec3cfb1d42add1a32bbc27cf (patch)
tree6786f1974d9a68761e213a3aaefc7431e78c5d85 /src/libstd
parent185ed988d240e380c63c7ff809622a270f670637 (diff)
downloadrust-a533504ca12ed93fec3cfb1d42add1a32bbc27cf.tar.gz
rust-a533504ca12ed93fec3cfb1d42add1a32bbc27cf.zip
Add try_reserve to HashSet
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/collections/hash/set.rs24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs
index 0fbf374fa49..f8a93b680f7 100644
--- a/src/libstd/collections/hash/set.rs
+++ b/src/libstd/collections/hash/set.rs
@@ -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 @@ impl<T, S> HashSet<T, S>
         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.