Skip to content

Commit 8322603

Browse files
authored
Rollup merge of #92338 - Xuanwo:try_reserve, r=dtolnay
Add try_reserve and try_reserve_exact for OsString Add `try_reserve` and `try_reserve_exact` for OsString. Part of #91789 I will squash the commits after PR is ready to merge. Signed-off-by: Xuanwo <[email protected]>
2 parents 72e36d4 + d29941e commit 8322603

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

Diff for: library/std/src/ffi/os_str.rs

+81
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 length units
270+
/// in the given `OsString`. The string 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::{OsStr, OsString};
285+
/// use std::collections::TryReserveError;
286+
///
287+
/// fn process_data(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(OsStr::new(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+
/// # process_data("123").expect("why is the test harness OOMing on 3 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+
/// more length units 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 `OsString` 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::{OsStr, OsString};
353+
/// use std::collections::TryReserveError;
354+
///
355+
/// fn process_data(data: &str) -> Result<OsString, TryReserveError> {
356+
/// let mut s = OsString::new();
357+
///
358+
/// // Pre-reserve the memory, exiting if we can't
359+
/// s.try_reserve_exact(OsStr::new(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+
/// # process_data("123").expect("why is the test harness OOMing on 3 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

Diff for: library/std/src/sys/unix/os_str.rs

+11
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()

Diff for: library/std/src/sys/windows/os_str.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/// The underlying OsString/OsStr implementation on Windows is a
22
/// wrapper around the "WTF-8" encoding; see the `wtf8` module for more.
33
use crate::borrow::Cow;
4+
use crate::collections::TryReserveError;
45
use crate::fmt;
56
use crate::mem;
67
use crate::rc::Rc;
@@ -104,10 +105,18 @@ impl Buf {
104105
self.inner.reserve(additional)
105106
}
106107

108+
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
109+
self.inner.try_reserve(additional)
110+
}
111+
107112
pub fn reserve_exact(&mut self, additional: usize) {
108113
self.inner.reserve_exact(additional)
109114
}
110115

116+
pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
117+
self.inner.try_reserve_exact(additional)
118+
}
119+
111120
pub fn shrink_to_fit(&mut self) {
112121
self.inner.shrink_to_fit()
113122
}

Diff for: library/std/src/sys_common/wtf8.rs

+37
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 length units
236+
/// in the given `Wtf8Buf`. The `Wtf8Buf` 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+
/// length units 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 `Wtf8Buf` 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)