Skip to content

Commit

Permalink
Use Vec::try_with_capacity where convenient
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Jan 30, 2024
1 parent 8beb1a0 commit c6a4e5f
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 8 deletions.
1 change: 1 addition & 0 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@
#![feature(slice_concat_trait)]
#![feature(thin_box)]
#![feature(try_reserve_kind)]
#![feature(try_with_capacity)]
#![feature(vec_into_raw_parts)]
// tidy-alphabetical-end
//
Expand Down
3 changes: 2 additions & 1 deletion library/std/src/sys/pal/uefi/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ pub(crate) fn locate_handles(mut guid: Guid) -> io::Result<Vec<NonNull<crate::ff
// The returned buf_len is in bytes
assert_eq!(buf_len % size_of::<r_efi::efi::Handle>(), 0);
let num_of_handles = buf_len / size_of::<r_efi::efi::Handle>();
let mut buf: Vec<r_efi::efi::Handle> = Vec::with_capacity(num_of_handles);
let mut buf: Vec<r_efi::efi::Handle> =
Vec::try_with_capacity(num_of_handles).map_err(|_| io::ErrorKind::OutOfMemory)?;
match inner(&mut guid, boot_services, &mut buf_len, buf.as_mut_ptr()) {
Ok(()) => {
// This is safe because the call will succeed only if buf_len >= required length.
Expand Down
10 changes: 6 additions & 4 deletions library/std/src/sys/pal/unix/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ pub fn getcwd() -> io::Result<PathBuf> {

#[cfg(not(target_os = "espidf"))]
pub fn getcwd() -> io::Result<PathBuf> {
let mut buf = Vec::with_capacity(512);
let mut buf = Vec::try_with_capacity(512).map_err(|_| io::ErrorKind::OutOfMemory)?;
loop {
unsafe {
let ptr = buf.as_mut_ptr() as *mut libc::c_char;
Expand Down Expand Up @@ -352,7 +352,8 @@ pub fn current_exe() -> io::Result<PathBuf> {
"KERN_PROC_PATHNAME sysctl returned zero-length string",
));
}
let mut path: Vec<u8> = Vec::with_capacity(path_len);
let mut path: Vec<u8> =
Vec::try_with_capacity(path_len).map_err(|_| io::ErrorKind::OutOfMemory)?;
cvt(libc::sysctl(
mib.as_ptr(),
mib.len() as libc::c_uint,
Expand Down Expand Up @@ -438,7 +439,8 @@ pub fn current_exe() -> io::Result<PathBuf> {
if sz == 0 {
return Err(io::Error::last_os_error());
}
let mut v: Vec<u8> = Vec::with_capacity(sz as usize);
let mut v: Vec<u8> =
Vec::try_with_capacity(sz as usize).map_err(|_| io::ErrorKind::OutOfMemory)?;
let err = libc::_NSGetExecutablePath(v.as_mut_ptr() as *mut i8, &mut sz);
if err != 0 {
return Err(io::Error::last_os_error());
Expand Down Expand Up @@ -726,7 +728,7 @@ pub fn home_dir() -> Option<PathBuf> {
n if n < 0 => 512 as usize,
n => n as usize,
};
let mut buf = Vec::with_capacity(amt);
let mut buf = Vec::try_with_capacity(amt).ok()?;
let mut passwd: libc::passwd = mem::zeroed();
let mut result = ptr::null_mut();
match libc::getpwuid_r(
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/pal/unix/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ mod cgroups {
}

let _: Option<()> = try {
let mut buf = Vec::with_capacity(128);
let mut buf = Vec::try_with_capacity(128).ok()?;
// find our place in the cgroup hierarchy
File::open("/proc/self/cgroup").ok()?.read_to_end(&mut buf).ok()?;
let (cgroup_path, version) =
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/pal/wasi/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub fn error_string(errno: i32) -> String {
}

pub fn getcwd() -> io::Result<PathBuf> {
let mut buf = Vec::with_capacity(512);
let mut buf = Vec::try_with_capacity(512).map_err(|_| io::ErrorKind::OutOfMemory)?;
loop {
unsafe {
let ptr = buf.as_mut_ptr() as *mut libc::c_char;
Expand Down
3 changes: 2 additions & 1 deletion library/std/src/sys/pal/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ pub fn to_u16s<S: AsRef<OsStr>>(s: S) -> crate::io::Result<Vec<u16>> {
// in the OsStr plus one for the null-terminating character. We are not
// wasting bytes here as paths created by this function are primarily used
// in an ephemeral fashion.
let mut maybe_result = Vec::with_capacity(s.len() + 1);
let mut maybe_result =
Vec::try_with_capacity(s.len() + 1).map_err(|_| ErrorKind::OutOfMemory)?;
maybe_result.extend(s.encode_wide());

if unrolled_find_u16s(0, &maybe_result).is_some() {
Expand Down

0 comments on commit c6a4e5f

Please sign in to comment.