std::vec: remove .as_muf_buf, replaced by .as_mut_ptr & .len.

This commit is contained in:
Huon Wilson 2013-12-18 02:13:20 +11:00
parent 4c79b22ef2
commit 9177f7ecb4
6 changed files with 71 additions and 112 deletions

View file

@ -295,13 +295,12 @@ unsafe fn with_c_str<T>(v: &[u8], checked: bool, f: |*libc::c_char| -> T) -> T {
vec::bytes::copy_memory(buf, v);
buf[v.len()] = 0;
buf.as_mut_buf(|buf, _| {
if checked {
check_for_null(v, buf as *mut libc::c_char);
}
let buf = buf.as_mut_ptr();
if checked {
check_for_null(v, buf as *mut libc::c_char);
}
f(buf as *libc::c_char)
})
f(buf as *libc::c_char)
} else if checked {
v.to_c_str().with_ref(f)
} else {

View file

@ -59,15 +59,12 @@ static BUF_BYTES : uint = 2048u;
#[cfg(unix)]
pub fn getcwd() -> Path {
let mut buf = [0 as libc::c_char, ..BUF_BYTES];
buf.as_mut_buf(|buf, len| {
unsafe {
if libc::getcwd(buf, len as size_t).is_null() {
fail!()
}
Path::new(CString::new(buf as *c_char, false))
unsafe {
if libc::getcwd(buf.as_mut_ptr(), buf.len() as size_t).is_null() {
fail!()
}
})
Path::new(CString::new(buf.as_ptr(), false))
}
}
#[cfg(windows)]
@ -103,20 +100,17 @@ pub mod win32 {
let mut res = None;
let mut done = false;
while !done {
let mut k: DWORD = 0;
let mut buf = vec::from_elem(n as uint, 0u16);
buf.as_mut_buf(|b, _sz| {
k = f(b, TMPBUF_SZ as DWORD);
if k == (0 as DWORD) {
done = true;
} else if (k == n &&
libc::GetLastError() ==
libc::ERROR_INSUFFICIENT_BUFFER as DWORD) {
n *= (2 as DWORD);
} else {
done = true;
}
});
let k = f(buf.as_mut_ptr(), TMPBUF_SZ as DWORD);
if k == (0 as DWORD) {
done = true;
} else if (k == n &&
libc::GetLastError() ==
libc::ERROR_INSUFFICIENT_BUFFER as DWORD) {
n *= (2 as DWORD);
} else {
done = true;
}
if k != 0 && done {
let sub = buf.slice(0, k as uint);
res = option::Some(str::from_utf16(sub));
@ -363,10 +357,8 @@ pub fn self_exe_path() -> Option<Path> {
if err != 0 { return None; }
if sz == 0 { return None; }
let mut v: ~[u8] = vec::with_capacity(sz as uint);
let err = v.as_mut_buf(|buf,_| {
sysctl(mib.as_ptr(), mib.len() as ::libc::c_uint,
buf as *mut c_void, &mut sz, ptr::null(), 0u as size_t)
});
let err = sysctl(mib.as_ptr(), mib.len() as ::libc::c_uint,
v.as_mut_ptr() as *mut c_void, &mut sz, ptr::null(), 0u as size_t);
if err != 0 { return None; }
if sz == 0 { return None; }
v.set_len(sz as uint - 1); // chop off trailing NUL
@ -394,9 +386,7 @@ pub fn self_exe_path() -> Option<Path> {
_NSGetExecutablePath(ptr::mut_null(), &mut sz);
if sz == 0 { return None; }
let mut v: ~[u8] = vec::with_capacity(sz as uint);
let err = v.as_mut_buf(|buf, _| {
_NSGetExecutablePath(buf as *mut i8, &mut sz)
});
let err = _NSGetExecutablePath(v.as_mut_ptr() as *mut i8, &mut sz);
if err != 0 { return None; }
v.set_len(sz as uint - 1); // chop off trailing NUL
Some(v)
@ -628,15 +618,14 @@ pub fn last_os_error() -> ~str {
let mut buf = [0 as c_char, ..TMPBUF_SZ];
buf.as_mut_buf(|buf, len| {
unsafe {
if strerror_r(errno() as c_int, buf, len as size_t) < 0 {
fail!("strerror_r failure");
}
str::raw::from_c_str(buf as *c_char)
let p = buf.as_mut_ptr();
unsafe {
if strerror_r(errno() as c_int, p, buf.len() as size_t) < 0 {
fail!("strerror_r failure");
}
})
str::raw::from_c_str(p as *c_char)
}
}
#[cfg(windows)]
@ -669,19 +658,17 @@ pub fn last_os_error() -> ~str {
let mut buf = [0 as WCHAR, ..TMPBUF_SZ];
unsafe {
buf.as_mut_buf(|buf, len| {
let res = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
ptr::mut_null(),
err,
langId,
buf,
len as DWORD,
ptr::null());
if res == 0 {
fail!("[{}] FormatMessage failure", errno());
}
});
let res = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
ptr::mut_null(),
err,
langId,
buf.as_mut_ptr(),
buf.len() as DWORD,
ptr::null());
if res == 0 {
fail!("[{}] FormatMessage failure", errno());
}
str::from_utf16(buf)
}

View file

@ -111,9 +111,7 @@ impl Rng for OSRng {
pbBuffer: *mut BYTE);
}
v.as_mut_buf(|ptr, len| {
unsafe {rust_win32_rand_gen(self.hcryptprov, len as DWORD, ptr)}
})
unsafe {rust_win32_rand_gen(self.hcryptprov, v.len() as DWORD, v.as_mut_ptr())}
}
}

View file

@ -914,13 +914,11 @@ fn new_sched_rng() -> XorShiftRng {
let mut seeds = [0u32, .. 4];
let size = mem::size_of_val(&seeds);
loop {
let nbytes = seeds.as_mut_buf(|buf, _| {
unsafe {
libc::read(fd,
buf as *mut libc::c_void,
size as libc::size_t)
}
});
let nbytes = unsafe {
libc::read(fd,
seeds.as_mut_ptr() as *mut libc::c_void,
size as libc::size_t)
};
rtassert!(nbytes as uint == size);
if !seeds.iter().all(|x| *x == 0) {

View file

@ -1000,7 +1000,7 @@ pub mod raw {
/// Create a Rust string from a *u8 buffer of the given length
pub unsafe fn from_buf_len(buf: *u8, len: uint) -> ~str {
let mut v: ~[u8] = vec::with_capacity(len);
v.as_mut_buf(|vbuf, _len| ptr::copy_memory(vbuf, buf as *u8, len));
ptr::copy_memory(v.as_mut_ptr(), buf as *u8, len);
v.set_len(len);
assert!(is_utf8(v));
@ -2282,7 +2282,7 @@ impl<'a> StrSlice<'a> for &'a str {
unsafe {
let mut v = vec::with_capacity(len);
v.as_mut_buf(|dst, _| ptr::copy_memory(dst, src, len));
ptr::copy_memory(v.as_mut_ptr(), src, len);
v.set_len(len);
::cast::transmute(v)
}
@ -2697,7 +2697,8 @@ impl OwnedStr for ~str {
#[inline]
fn as_mut_buf<T>(&mut self, f: |*mut u8, uint| -> T) -> T {
unsafe {
raw::as_owned_vec(self).as_mut_buf(f)
let v = raw::as_owned_vec(self);
f(v.as_mut_ptr(), v.len())
}
}

View file

@ -1681,15 +1681,16 @@ impl<T> OwnedVector<T> for ~[T] {
self.pop()
}
fn truncate(&mut self, newlen: uint) {
self.as_mut_buf(|p, oldlen| {
assert!(newlen <= oldlen);
unsafe {
// This loop is optimized out for non-drop types.
for i in range(newlen, oldlen) {
ptr::read_and_zero_ptr(ptr::mut_offset(p, i as int));
}
let oldlen = self.len();
assert!(newlen <= oldlen);
unsafe {
let p = self.as_mut_ptr();
// This loop is optimized out for non-drop types.
for i in range(newlen, oldlen) {
ptr::read_and_zero_ptr(p.offset(i as int));
}
});
}
unsafe { self.set_len(newlen); }
}
@ -2053,9 +2054,6 @@ pub trait MutableVector<'a, T> {
/// `self` and `src` must not overlap. Fails if `self` is
/// shorter than `src`.
unsafe fn copy_memory(self, src: &[T]);
/// Similar to `as_imm_buf` but passing a `*mut T`
fn as_mut_buf<U>(self, f: |*mut T, uint| -> U) -> U;
}
impl<'a,T> MutableVector<'a, T> for &'a mut [T] {
@ -2063,14 +2061,12 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] {
fn mut_slice(self, start: uint, end: uint) -> &'a mut [T] {
assert!(start <= end);
assert!(end <= self.len());
self.as_mut_buf(|p, _len| {
unsafe {
cast::transmute(Slice {
data: ptr::mut_offset(p, start as int) as *T,
unsafe {
cast::transmute(Slice {
data: self.as_mut_ptr().offset(start as int) as *T,
len: (end - start)
})
}
})
}
}
#[inline]
@ -2189,17 +2185,9 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] {
#[inline]
unsafe fn copy_memory(self, src: &[T]) {
self.as_mut_buf(|p_dst, len_dst| {
let len_src = src.len();
assert!(len_dst >= len_src);
ptr::copy_nonoverlapping_memory(p_dst, src.as_ptr(), len_src)
})
}
#[inline]
fn as_mut_buf<U>(self, f: |*mut T, uint| -> U) -> U {
let Slice{ data, len } = self.repr();
f(data as *mut T, len)
let len_src = src.len();
assert!(self.len() >= len_src);
ptr::copy_nonoverlapping_memory(self.as_mut_ptr(), src.as_ptr(), len_src)
}
}
@ -2283,7 +2271,7 @@ pub mod raw {
pub unsafe fn from_buf_raw<T>(ptr: *T, elts: uint) -> ~[T] {
let mut dst = with_capacity(elts);
dst.set_len(elts);
dst.as_mut_buf(|p_dst, _len_dst| ptr::copy_memory(p_dst, ptr, elts));
ptr::copy_memory(dst.as_mut_ptr(), ptr, elts);
dst
}
@ -2315,6 +2303,7 @@ pub mod raw {
/// Operations on `[u8]`.
pub mod bytes {
use container::Container;
use vec::MutableVector;
use ptr;
@ -2327,9 +2316,7 @@ pub mod bytes {
impl<'a> MutableByteVector for &'a mut [u8] {
#[inline]
fn set_memory(self, value: u8) {
self.as_mut_buf(|p, len| {
unsafe { ptr::set_memory(p, value, len) };
})
unsafe { ptr::set_memory(self.as_mut_ptr(), value, self.len()) };
}
}
@ -2351,9 +2338,7 @@ pub mod bytes {
let old_len = dst.len();
dst.reserve_additional(src.len());
unsafe {
dst.as_mut_buf(|p_dst, len_dst| {
ptr::copy_memory(p_dst.offset(len_dst as int), src.as_ptr(), src.len())
});
ptr::copy_memory(dst.as_mut_ptr().offset(old_len as int), src.as_ptr(), src.len());
dst.set_len(old_len + src.len());
}
}
@ -3534,15 +3519,6 @@ mod tests {
}
}
#[test]
#[should_fail]
fn test_as_mut_buf_fail() {
let mut v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)];
v.as_mut_buf(|_buf, _i| {
fail!()
})
}
#[test]
#[should_fail]
fn test_copy_memory_oob() {