Remove the generic fill_bytes_buf function

This commit is contained in:
Tobias Bucher 2015-07-10 12:33:10 +02:00
parent d99d4fbf70
commit b83ec47808
2 changed files with 13 additions and 31 deletions
src/libstd/sys/unix

@ -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 {

@ -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<()> {