Skip to content

Commit c40ac57

Browse files
committed
Add try_reserve for OsString
Signed-off-by: Xuanwo <[email protected]>
1 parent 4ee34f3 commit c40ac57

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

library/std/src/ffi/os_str.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ mod tests;
33

44
use crate::borrow::{Borrow, Cow};
55
use crate::cmp;
6+
use crate::collections::TryReserveError;
67
use crate::fmt;
78
use crate::hash::{Hash, Hasher};
89
use crate::iter::{Extend, FromIterator};
@@ -265,6 +266,43 @@ impl OsString {
265266
self.inner.reserve(additional)
266267
}
267268

269+
/// Tries to reserve capacity for at least `additional` more elements to be inserted
270+
/// in the given `OsString`. The collection may reserve more space to avoid
271+
/// frequent reallocations. After calling `try_reserve`, capacity will be
272+
/// greater than or equal to `self.len() + additional`. Does nothing if
273+
/// capacity is already sufficient.
274+
///
275+
/// # Errors
276+
///
277+
/// If the capacity overflows, or the allocator reports a failure, then an error
278+
/// is returned.
279+
///
280+
/// # Examples
281+
///
282+
/// ```
283+
/// #![feature(try_reserve_2)]
284+
/// use std::ffi::OsString;
285+
/// use std::collections::TryReserveError;
286+
///
287+
/// fn find_max_slow(data: &str) -> Result<OsString, TryReserveError> {
288+
/// let mut s = OsString::new();
289+
///
290+
/// // Pre-reserve the memory, exiting if we can't
291+
/// s.try_reserve(data.len())?;
292+
///
293+
/// // Now we know this can't OOM in the middle of our complex work
294+
/// s.push(data);
295+
///
296+
/// Ok(s)
297+
/// }
298+
/// # find_max_slow("123").expect("why is the test harness OOMing on 12 bytes?");
299+
/// ```
300+
#[unstable(feature = "try_reserve_2", issue = "91789")]
301+
#[inline]
302+
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
303+
self.inner.try_reserve(additional)
304+
}
305+
268306
/// Reserves the minimum capacity for exactly `additional` more capacity to
269307
/// be inserted in the given `OsString`. Does nothing if the capacity is
270308
/// already sufficient.
@@ -290,6 +328,49 @@ impl OsString {
290328
self.inner.reserve_exact(additional)
291329
}
292330

331+
/// Tries to reserve the minimum capacity for exactly `additional`
332+
/// elements to be inserted in the given `OsString`. After calling
333+
/// `try_reserve_exact`, capacity will be greater than or equal to
334+
/// `self.len() + additional` if it returns `Ok(())`.
335+
/// Does nothing if the capacity is already sufficient.
336+
///
337+
/// Note that the allocator may give the collection more space than it
338+
/// requests. Therefore, capacity can not be relied upon to be precisely
339+
/// minimal. Prefer [`try_reserve`] if future insertions are expected.
340+
///
341+
/// [`try_reserve`]: OsString::try_reserve
342+
///
343+
/// # Errors
344+
///
345+
/// If the capacity overflows, or the allocator reports a failure, then an error
346+
/// is returned.
347+
///
348+
/// # Examples
349+
///
350+
/// ```
351+
/// #![feature(try_reserve_2)]
352+
/// use std::ffi::OsString;
353+
/// use std::collections::TryReserveError;
354+
///
355+
/// fn find_max_slow(data: &str) -> Result<OsString, TryReserveError> {
356+
/// let mut s = OsString::from(data);
357+
///
358+
/// // Pre-reserve the memory, exiting if we can't
359+
/// s.try_reserve_exact(data.len())?;
360+
///
361+
/// // Now we know this can't OOM in the middle of our complex work
362+
/// s.push(data);
363+
///
364+
/// Ok(s)
365+
/// }
366+
/// # find_max_slow("123").expect("why is the test harness OOMing on 12 bytes?");
367+
/// ```
368+
#[unstable(feature = "try_reserve_2", issue = "91789")]
369+
#[inline]
370+
pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
371+
self.inner.try_reserve_exact(additional)
372+
}
373+
293374
/// Shrinks the capacity of the `OsString` to match its length.
294375
///
295376
/// # Examples

library/std/src/sys/unix/os_str.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//! systems: just a `Vec<u8>`/`[u8]`.
33
44
use crate::borrow::Cow;
5+
use crate::collections::TryReserveError;
56
use crate::fmt;
67
use crate::fmt::Write;
78
use crate::mem;
@@ -112,11 +113,21 @@ impl Buf {
112113
self.inner.reserve(additional)
113114
}
114115

116+
#[inline]
117+
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
118+
self.inner.try_reserve(additional)
119+
}
120+
115121
#[inline]
116122
pub fn reserve_exact(&mut self, additional: usize) {
117123
self.inner.reserve_exact(additional)
118124
}
119125

126+
#[inline]
127+
pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
128+
self.inner.try_reserve_exact(additional)
129+
}
130+
120131
#[inline]
121132
pub fn shrink_to_fit(&mut self) {
122133
self.inner.shrink_to_fit()

0 commit comments

Comments
 (0)