From b83ec47808c219cb8e85eeebb370be6466b2d690 Mon Sep 17 00:00:00 2001 From: Tobias Bucher <tobiasbucher5991@gmail.com> Date: Fri, 10 Jul 2015 12:33:10 +0200 Subject: [PATCH] Remove the generic `fill_bytes_buf` function --- src/libstd/sys/unix/mod.rs | 22 ---------------------- src/libstd/sys/unix/os.rs | 22 +++++++++++++--------- 2 files changed, 13 insertions(+), 31 deletions(-) diff --git a/src/libstd/sys/unix/mod.rs b/src/libstd/sys/unix/mod.rs index 5bf8c1e06a6..6fd20b940bb 100644 --- a/src/libstd/sys/unix/mod.rs +++ b/src/libstd/sys/unix/mod.rs @@ -75,28 +75,6 @@ pub fn decode_error_kind(errno: i32) -> ErrorKind { } } -// Some system functions expect the user to pass a appropiately-sized buffer -// without specifying its size. They will only report back whether the buffer -// was large enough or not. -// -// The callback is yielded an uninitialized vector which can be passed to a -// syscall. The closure is expected to return `Err(v)` with the passed vector -// if the space was insufficient and `Ok(r)` if the syscall did not fail due to -// insufficient space. -fn fill_bytes_buf<F, T>(mut f: F) -> io::Result<T> - where F: FnMut(Vec<u8>) -> Result<io::Result<T>,Vec<u8>>, -{ - let mut buf = Vec::new(); - let mut n = os::BUF_BYTES; - loop { - buf.reserve(n); - match f(buf) { - Err(b) => { buf = b; n *= 2; } - Ok(r) => return r, - } - } -} - pub fn cvt<T: One + PartialEq + Neg<Output=T>>(t: T) -> io::Result<T> { let one: T = T::one(); if t == -one { diff --git a/src/libstd/sys/unix/os.rs b/src/libstd/sys/unix/os.rs index ac38d294a6f..2b6b50a1a56 100644 --- a/src/libstd/sys/unix/os.rs +++ b/src/libstd/sys/unix/os.rs @@ -30,7 +30,7 @@ use sys::c; use sys::fd; use vec; -pub const BUF_BYTES: usize = 2048; +const GETCWD_BUF_BYTES: usize = 2048; const TMPBUF_SZ: usize = 128; /// Returns the platform-specific value of errno @@ -94,22 +94,26 @@ pub fn error_string(errno: i32) -> String { } pub fn getcwd() -> io::Result<PathBuf> { - super::fill_bytes_buf(|mut buf| { + let mut buf = Vec::new(); + let mut n = GETCWD_BUF_BYTES; + loop { unsafe { + buf.reserve(n); let ptr = buf.as_mut_ptr() as *mut libc::c_char; - Ok(if !libc::getcwd(ptr, buf.capacity() as libc::size_t).is_null() { + if !libc::getcwd(ptr, buf.capacity() as libc::size_t).is_null() { let len = CStr::from_ptr(buf.as_ptr() as *const libc::c_char).to_bytes().len(); buf.set_len(len); - Ok(PathBuf::from(OsString::from_bytes(buf).unwrap())) + buf.shrink_to_fit(); + return Ok(PathBuf::from(OsString::from_vec(buf))); } else { let error = io::Error::last_os_error(); - if error.raw_os_error().unwrap() == libc::ERANGE { - return Err(buf); + if error.raw_os_error() != Some(libc::ERANGE) { + return Err(error); } - Err(error) - }) + } + n *= 2; } - }) + } } pub fn chdir(p: &path::Path) -> io::Result<()> {