Skip to content

Commit c6a4e5f

Browse files
committed
Use Vec::try_with_capacity where convenient
1 parent 8beb1a0 commit c6a4e5f

File tree

6 files changed

+13
-8
lines changed

6 files changed

+13
-8
lines changed

library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@
357357
#![feature(slice_concat_trait)]
358358
#![feature(thin_box)]
359359
#![feature(try_reserve_kind)]
360+
#![feature(try_with_capacity)]
360361
#![feature(vec_into_raw_parts)]
361362
// tidy-alphabetical-end
362363
//

library/std/src/sys/pal/uefi/helpers.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ pub(crate) fn locate_handles(mut guid: Guid) -> io::Result<Vec<NonNull<crate::ff
6262
// The returned buf_len is in bytes
6363
assert_eq!(buf_len % size_of::<r_efi::efi::Handle>(), 0);
6464
let num_of_handles = buf_len / size_of::<r_efi::efi::Handle>();
65-
let mut buf: Vec<r_efi::efi::Handle> = Vec::with_capacity(num_of_handles);
65+
let mut buf: Vec<r_efi::efi::Handle> =
66+
Vec::try_with_capacity(num_of_handles).map_err(|_| io::ErrorKind::OutOfMemory)?;
6667
match inner(&mut guid, boot_services, &mut buf_len, buf.as_mut_ptr()) {
6768
Ok(()) => {
6869
// This is safe because the call will succeed only if buf_len >= required length.

library/std/src/sys/pal/unix/os.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ pub fn getcwd() -> io::Result<PathBuf> {
154154

155155
#[cfg(not(target_os = "espidf"))]
156156
pub fn getcwd() -> io::Result<PathBuf> {
157-
let mut buf = Vec::with_capacity(512);
157+
let mut buf = Vec::try_with_capacity(512).map_err(|_| io::ErrorKind::OutOfMemory)?;
158158
loop {
159159
unsafe {
160160
let ptr = buf.as_mut_ptr() as *mut libc::c_char;
@@ -352,7 +352,8 @@ pub fn current_exe() -> io::Result<PathBuf> {
352352
"KERN_PROC_PATHNAME sysctl returned zero-length string",
353353
));
354354
}
355-
let mut path: Vec<u8> = Vec::with_capacity(path_len);
355+
let mut path: Vec<u8> =
356+
Vec::try_with_capacity(path_len).map_err(|_| io::ErrorKind::OutOfMemory)?;
356357
cvt(libc::sysctl(
357358
mib.as_ptr(),
358359
mib.len() as libc::c_uint,
@@ -438,7 +439,8 @@ pub fn current_exe() -> io::Result<PathBuf> {
438439
if sz == 0 {
439440
return Err(io::Error::last_os_error());
440441
}
441-
let mut v: Vec<u8> = Vec::with_capacity(sz as usize);
442+
let mut v: Vec<u8> =
443+
Vec::try_with_capacity(sz as usize).map_err(|_| io::ErrorKind::OutOfMemory)?;
442444
let err = libc::_NSGetExecutablePath(v.as_mut_ptr() as *mut i8, &mut sz);
443445
if err != 0 {
444446
return Err(io::Error::last_os_error());
@@ -726,7 +728,7 @@ pub fn home_dir() -> Option<PathBuf> {
726728
n if n < 0 => 512 as usize,
727729
n => n as usize,
728730
};
729-
let mut buf = Vec::with_capacity(amt);
731+
let mut buf = Vec::try_with_capacity(amt).ok()?;
730732
let mut passwd: libc::passwd = mem::zeroed();
731733
let mut result = ptr::null_mut();
732734
match libc::getpwuid_r(

library/std/src/sys/pal/unix/thread.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ mod cgroups {
498498
}
499499

500500
let _: Option<()> = try {
501-
let mut buf = Vec::with_capacity(128);
501+
let mut buf = Vec::try_with_capacity(128).ok()?;
502502
// find our place in the cgroup hierarchy
503503
File::open("/proc/self/cgroup").ok()?.read_to_end(&mut buf).ok()?;
504504
let (cgroup_path, version) =

library/std/src/sys/pal/wasi/os.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub fn error_string(errno: i32) -> String {
6969
}
7070

7171
pub fn getcwd() -> io::Result<PathBuf> {
72-
let mut buf = Vec::with_capacity(512);
72+
let mut buf = Vec::try_with_capacity(512).map_err(|_| io::ErrorKind::OutOfMemory)?;
7373
loop {
7474
unsafe {
7575
let ptr = buf.as_mut_ptr() as *mut libc::c_char;

library/std/src/sys/pal/windows/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ pub fn to_u16s<S: AsRef<OsStr>>(s: S) -> crate::io::Result<Vec<u16>> {
181181
// in the OsStr plus one for the null-terminating character. We are not
182182
// wasting bytes here as paths created by this function are primarily used
183183
// in an ephemeral fashion.
184-
let mut maybe_result = Vec::with_capacity(s.len() + 1);
184+
let mut maybe_result =
185+
Vec::try_with_capacity(s.len() + 1).map_err(|_| ErrorKind::OutOfMemory)?;
185186
maybe_result.extend(s.encode_wide());
186187

187188
if unrolled_find_u16s(0, &maybe_result).is_some() {

0 commit comments

Comments
 (0)