Convert vec::{as_imm_buf, as_mut_buf} to methods.

This commit is contained in:
Huon Wilson 2013-07-03 16:34:17 +10:00
parent a732a2daff
commit cdea73cf5b
14 changed files with 90 additions and 86 deletions

View File

@ -45,7 +45,7 @@ static LZ_NORM : c_int = 0x80; // LZ with 128 probes, "normal"
static LZ_BEST : c_int = 0xfff; // LZ with 4095 probes, "best"
pub fn deflate_bytes(bytes: &[u8]) -> ~[u8] {
do vec::as_imm_buf(bytes) |b, len| {
do bytes.as_imm_buf |b, len| {
unsafe {
let mut outsz : size_t = 0;
let res =
@ -63,7 +63,7 @@ pub fn deflate_bytes(bytes: &[u8]) -> ~[u8] {
}
pub fn inflate_bytes(bytes: &[u8]) -> ~[u8] {
do vec::as_imm_buf(bytes) |b, len| {
do bytes.as_imm_buf |b, len| {
unsafe {
let mut outsz : size_t = 0;
let res =

View File

@ -53,7 +53,7 @@ fn map_slices<A:Copy + Send,B:Copy + Send>(
info!("spawning tasks");
while base < len {
let end = uint::min(len, base + items_per_task);
do vec::as_imm_buf(xs) |p, _len| {
do xs.as_imm_buf |p, _len| {
let f = f();
let base = base;
let f = do future_spawn() || {

View File

@ -1046,7 +1046,7 @@ pub unsafe fn ip4_name(src: &sockaddr_in) -> ~str {
// ipv4 addr max size: 15 + 1 trailing null byte
let dst: ~[u8] = ~[0u8,0u8,0u8,0u8,0u8,0u8,0u8,0u8,
0u8,0u8,0u8,0u8,0u8,0u8,0u8,0u8];
do vec::as_imm_buf(dst) |dst_buf, size| {
do dst.as_imm_buf |dst_buf, size| {
rust_uv_ip4_name(to_unsafe_ptr(src),
dst_buf, size as libc::size_t);
// seems that checking the result of uv_ip4_name
@ -1066,7 +1066,7 @@ pub unsafe fn ip6_name(src: &sockaddr_in6) -> ~str {
0u8,0u8,0u8,0u8,0u8,0u8,0u8,0u8,
0u8,0u8,0u8,0u8,0u8,0u8,0u8,0u8,
0u8,0u8,0u8,0u8,0u8,0u8];
do vec::as_imm_buf(dst) |dst_buf, size| {
do dst.as_imm_buf |dst_buf, size| {
let src_unsafe_ptr = to_unsafe_ptr(src);
let result = rust_uv_ip6_name(src_unsafe_ptr,
dst_buf, size as libc::size_t);

View File

@ -565,7 +565,7 @@ pub fn LoadRangeAssert(cx: block, PointerVal: ValueRef, lo: c_ulonglong,
let min = llvm::LLVMConstInt(t, lo, signed);
let max = llvm::LLVMConstInt(t, hi, signed);
do vec::as_imm_buf([min, max]) |ptr, len| {
do [min, max].as_imm_buf |ptr, len| {
llvm::LLVMSetMetadata(value, lib::llvm::MD_range as c_uint,
llvm::LLVMMDNodeInContext(cx.fcx.ccx.llcx,
ptr, len as c_uint));
@ -942,7 +942,7 @@ pub fn Call(cx: block, Fn: ValueRef, Args: &[ValueRef]) -> ValueRef {
cx.val_to_str(Fn),
Args.map(|arg| cx.val_to_str(*arg)));
do vec::as_imm_buf(Args) |ptr, len| {
do Args.as_imm_buf |ptr, len| {
llvm::LLVMBuildCall(B(cx), Fn, ptr, len as c_uint, noname())
}
}

View File

@ -774,7 +774,7 @@ pub fn C_zero_byte_arr(size: uint) -> ValueRef {
pub fn C_struct(elts: &[ValueRef]) -> ValueRef {
unsafe {
do vec::as_imm_buf(elts) |ptr, len| {
do elts.as_imm_buf |ptr, len| {
llvm::LLVMConstStructInContext(base::task_llcx(), ptr, len as c_uint, False)
}
}
@ -782,7 +782,7 @@ pub fn C_struct(elts: &[ValueRef]) -> ValueRef {
pub fn C_packed_struct(elts: &[ValueRef]) -> ValueRef {
unsafe {
do vec::as_imm_buf(elts) |ptr, len| {
do elts.as_imm_buf |ptr, len| {
llvm::LLVMConstStructInContext(base::task_llcx(), ptr, len as c_uint, True)
}
}
@ -790,7 +790,7 @@ pub fn C_packed_struct(elts: &[ValueRef]) -> ValueRef {
pub fn C_named_struct(T: Type, elts: &[ValueRef]) -> ValueRef {
unsafe {
do vec::as_imm_buf(elts) |ptr, len| {
do elts.as_imm_buf |ptr, len| {
llvm::LLVMConstNamedStruct(T.to_ref(), ptr, len as c_uint)
}
}
@ -826,7 +826,7 @@ pub fn get_param(fndecl: ValueRef, param: uint) -> ValueRef {
pub fn const_get_elt(cx: &CrateContext, v: ValueRef, us: &[c_uint])
-> ValueRef {
unsafe {
let r = do vec::as_imm_buf(us) |p, len| {
let r = do us.as_imm_buf |p, len| {
llvm::LLVMConstExtractValue(v, p, len as c_uint)
};

View File

@ -917,7 +917,7 @@ fn convert_whence(whence: SeekStyle) -> i32 {
impl Reader for *libc::FILE {
fn read(&self, bytes: &mut [u8], len: uint) -> uint {
unsafe {
do vec::as_mut_buf(bytes) |buf_p, buf_len| {
do bytes.as_mut_buf |buf_p, buf_len| {
assert!(buf_len >= len);
let count = libc::fread(buf_p as *mut c_void, 1u as size_t,
@ -1152,7 +1152,7 @@ impl<W:Writer,C> Writer for Wrapper<W, C> {
impl Writer for *libc::FILE {
fn write(&self, v: &[u8]) {
unsafe {
do vec::as_imm_buf(v) |vbuf, len| {
do v.as_imm_buf |vbuf, len| {
let nout = libc::fwrite(vbuf as *c_void,
1,
len as size_t,
@ -1203,7 +1203,7 @@ impl Writer for fd_t {
fn write(&self, v: &[u8]) {
unsafe {
let mut count = 0u;
do vec::as_imm_buf(v) |vbuf, len| {
do v.as_imm_buf |vbuf, len| {
while count < len {
let vb = ptr::offset(vbuf, count) as *c_void;
let nout = libc::write(*self, vb, len as size_t);

View File

@ -92,7 +92,7 @@ pub fn as_c_charp<T>(s: &str, f: &fn(*c_char) -> T) -> T {
pub fn fill_charp_buf(f: &fn(*mut c_char, size_t) -> bool)
-> Option<~str> {
let mut buf = vec::from_elem(TMPBUF_SZ, 0u8 as c_char);
do vec::as_mut_buf(buf) |b, sz| {
do buf.as_mut_buf |b, sz| {
if f(b, sz as size_t) {
unsafe {
Some(str::raw::from_buf(b as *u8))
@ -122,7 +122,7 @@ pub mod win32 {
while !done {
let mut k: DWORD = 0;
let mut buf = vec::from_elem(n as uint, 0u16);
do vec::as_mut_buf(buf) |b, _sz| {
do buf.as_mut_buf |b, _sz| {
k = f(b, TMPBUF_SZ as DWORD);
if k == (0 as DWORD) {
done = true;
@ -147,7 +147,7 @@ pub mod win32 {
let mut t = s.to_utf16();
// Null terminate before passing on.
t.push(0u16);
vec::as_imm_buf(t, |buf, _len| f(buf))
t.as_imm_buf(|buf, _len| f(buf))
}
}
@ -937,7 +937,7 @@ pub fn copy_file(from: &Path, to: &Path) -> bool {
let mut done = false;
let mut ok = true;
while !done {
do vec::as_mut_buf(buf) |b, _sz| {
do buf.as_mut_buf |b, _sz| {
let nread = libc::fread(b as *mut c_void, 1u as size_t,
bufsize as size_t,
istream);
@ -1683,7 +1683,7 @@ mod tests {
let s = ~"hello";
let mut buf = s.as_bytes_with_null().to_owned();
let len = buf.len();
do vec::as_mut_buf(buf) |b, _len| {
do buf.as_mut_buf |b, _len| {
assert_eq!(libc::fwrite(b as *c_void, 1u as size_t,
(s.len() + 1u) as size_t, ostream),
len as size_t)

View File

@ -406,7 +406,7 @@ pub mod ptr_tests {
do str::as_c_str(s1) |p1| {
do str::as_c_str(s2) |p2| {
let v = ~[p0, p1, p2, null()];
do vec::as_imm_buf(v) |vp, len| {
do v.as_imm_buf |vp, len| {
assert_eq!(unsafe { buf_len(vp) }, 3u);
assert_eq!(len, 4u);
}

View File

@ -830,7 +830,7 @@ pub fn seed() -> ~[u8] {
unsafe {
let n = rustrt::rand_seed_size() as uint;
let mut s = vec::from_elem(n, 0_u8);
do vec::as_mut_buf(s) |p, sz| {
do s.as_mut_buf |p, sz| {
rustrt::rand_gen_seed(p, sz as size_t)
}
s
@ -1087,7 +1087,7 @@ mod tests {
for 10.times {
unsafe {
let seed = super::seed();
let rt_rng = do vec::as_imm_buf(seed) |p, sz| {
let rt_rng = do seed.as_imm_buf |p, sz| {
rustrt::rand_new_seeded(p, sz as size_t)
};
let mut rng = IsaacRng::new_seeded(seed);

View File

@ -40,6 +40,7 @@ use str::raw::from_c_str;
use to_str::ToStr;
use ptr::RawPtr;
use vec;
use vec::ImmutableVector;
use ptr;
use str;
use libc::{c_void, c_int, size_t, malloc, free};
@ -300,7 +301,7 @@ pub fn vec_to_uv_buf(v: ~[u8]) -> Buf {
unsafe {
let data = malloc(v.len() as size_t) as *u8;
assert!(data.is_not_null());
do vec::as_imm_buf(v) |b, l| {
do v.as_imm_buf |b, l| {
let data = data as *mut u8;
ptr::copy_memory(data, b, l)
}

View File

@ -24,7 +24,7 @@ use prelude::*;
use ptr;
use str;
use task;
use vec;
use vec::ImmutableVector;
/**
* A value representing a child process.
@ -703,7 +703,7 @@ fn with_argv<T>(prog: &str, args: &[~str],
argptrs.push(str::as_c_str(*t, |b| b));
}
argptrs.push(ptr::null());
vec::as_imm_buf(argptrs, |buf, _len| cb(buf))
argptrs.as_imm_buf(|buf, _len| cb(buf))
}
#[cfg(unix)]
@ -722,7 +722,7 @@ fn with_envp<T>(env: Option<&[(~str, ~str)]>, cb: &fn(*c_void) -> T) -> T {
}
ptrs.push(ptr::null());
vec::as_imm_buf(ptrs, |p, _len|
ptrs.as_imm_buf(|p, _len|
unsafe { cb(::cast::transmute(p)) }
)
}
@ -743,7 +743,7 @@ fn with_envp<T>(env: Option<&[(~str, ~str)]>, cb: &fn(*mut c_void) -> T) -> T {
blk.push_all(kv.as_bytes_with_null_consume());
}
blk.push(0);
vec::as_imm_buf(blk, |p, _len|
blk.as_imm_buf(|p, _len|
unsafe { cb(::cast::transmute(p)) }
)
}

View File

@ -826,6 +826,7 @@ pub mod raw {
use str::raw;
use str::{as_buf, is_utf8};
use vec;
use vec::MutableVector;
/// Create a Rust string from a null-terminated *u8 buffer
pub unsafe fn from_buf(buf: *u8) -> ~str {
@ -841,7 +842,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 + 1);
vec::as_mut_buf(v, |vbuf, _len| {
v.as_mut_buf(|vbuf, _len| {
ptr::copy_memory(vbuf, buf as *u8, len)
});
vec::raw::set_len(&mut v, len);
@ -863,7 +864,7 @@ pub mod raw {
/// Converts a vector of bytes to a new owned string.
pub unsafe fn from_bytes(v: &[u8]) -> ~str {
do vec::as_imm_buf(v) |buf, len| {
do v.as_imm_buf |buf, len| {
from_buf_len(buf, len)
}
}
@ -917,7 +918,7 @@ pub mod raw {
assert!((end <= n));
let mut v = vec::with_capacity(end - begin + 1u);
do vec::as_imm_buf(v) |vbuf, _vlen| {
do v.as_imm_buf |vbuf, _vlen| {
let vbuf = ::cast::transmute_mut_unsafe(vbuf);
let src = ptr::offset(sbuf, begin);
ptr::copy_memory(vbuf, src, end - begin);

View File

@ -71,7 +71,7 @@ pub fn same_length<T, U>(xs: &[T], ys: &[U]) -> bool {
pub fn from_fn<T>(n_elts: uint, op: &fn(uint) -> T) -> ~[T] {
unsafe {
let mut v = with_capacity(n_elts);
do as_mut_buf(v) |p, _len| {
do v.as_mut_buf |p, _len| {
let mut i: uint = 0u;
while i < n_elts {
intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i)), op(i));
@ -96,7 +96,7 @@ pub fn from_elem<T:Copy>(n_elts: uint, t: T) -> ~[T] {
// vec::with_capacity/ptr::set_memory for primitive types.
unsafe {
let mut v = with_capacity(n_elts);
do as_mut_buf(v) |p, _len| {
do v.as_mut_buf |p, _len| {
let mut i = 0u;
while i < n_elts {
intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i)), copy t);
@ -494,40 +494,6 @@ impl<'self, T> Iterator<&'self [T]> for VecChunkIter<'self, T> {
}
}
/**
* Work with the buffer of a vector.
*
* Allows for unsafe manipulation of vector contents, which is useful for
* foreign interop.
*/
#[inline]
pub fn as_imm_buf<T,U>(s: &[T],
/* NB---this CANNOT be const, see below */
f: &fn(*T, uint) -> U) -> U {
// NB---Do not change the type of s to `&const [T]`. This is
// unsound. The reason is that we are going to create immutable pointers
// into `s` and pass them to `f()`, but in fact they are potentially
// pointing at *mutable memory*. Use `as_const_buf` or `as_mut_buf`
// instead!
unsafe {
let v : *(*T,uint) = transmute(&s);
let (buf,len) = *v;
f(buf, len / sys::nonzero_size_of::<T>())
}
}
/// Similar to `as_imm_buf` but passing a `*mut T`
#[inline]
pub fn as_mut_buf<T,U>(s: &mut [T], f: &fn(*mut T, uint) -> U) -> U {
unsafe {
let v : *(*mut T,uint) = transmute(&s);
let (buf,len) = *v;
f(buf, len / sys::nonzero_size_of::<T>())
}
}
// Equality
#[cfg(not(test))]
@ -695,13 +661,13 @@ impl<'self, T> Container for &'self [T] {
/// Returns true if a vector contains no elements
#[inline]
fn is_empty(&self) -> bool {
as_imm_buf(*self, |_p, len| len == 0u)
self.as_imm_buf(|_p, len| len == 0u)
}
/// Returns the length of a vector
#[inline]
fn len(&self) -> uint {
as_imm_buf(*self, |_p, len| len)
self.as_imm_buf(|_p, len| len)
}
}
@ -709,13 +675,13 @@ impl<T> Container for ~[T] {
/// Returns true if a vector contains no elements
#[inline]
fn is_empty(&self) -> bool {
as_imm_buf(*self, |_p, len| len == 0u)
self.as_imm_buf(|_p, len| len == 0u)
}
/// Returns the length of a vector
#[inline]
fn len(&self) -> uint {
as_imm_buf(*self, |_p, len| len)
self.as_imm_buf(|_p, len| len)
}
}
@ -765,6 +731,8 @@ pub trait ImmutableVector<'self, T> {
fn bsearch(&self, f: &fn(&T) -> Ordering) -> Option<uint>;
fn map<U>(&self, &fn(t: &T) -> U) -> ~[U];
fn as_imm_buf<U>(&self, f: &fn(*T, uint) -> U) -> U;
}
/// Extension methods for vectors
@ -774,7 +742,7 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] {
fn slice(&self, start: uint, end: uint) -> &'self [T] {
assert!(start <= end);
assert!(end <= self.len());
do as_imm_buf(*self) |p, _len| {
do self.as_imm_buf |p, _len| {
unsafe {
transmute((ptr::offset(p, start),
(end - start) * sys::nonzero_size_of::<T>()))
@ -1007,6 +975,28 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] {
fn map<U>(&self, f: &fn(t: &T) -> U) -> ~[U] {
self.iter().transform(f).collect()
}
/**
* Work with the buffer of a vector.
*
* Allows for unsafe manipulation of vector contents, which is useful for
* foreign interop.
*/
#[inline]
fn as_imm_buf<U>(&self,
/* NB---this CANNOT be const, see below */
f: &fn(*T, uint) -> U) -> U {
// NB---Do not change the type of s to `&const [T]`. This is
// unsound. The reason is that we are going to create immutable pointers
// into `s` and pass them to `f()`, but in fact they are potentially
// pointing at *mutable memory*. Use `as_mut_buf` instead!
unsafe {
let v : *(*T,uint) = transmute(self);
let (buf,len) = *v;
f(buf, len / sys::nonzero_size_of::<T>())
}
}
}
#[allow(missing_doc)]
@ -1280,7 +1270,7 @@ impl<T> OwnedVector<T> for ~[T] {
let new_len = self.len() + rhs.len();
self.reserve(new_len);
unsafe {
do as_mut_buf(rhs) |p, len| {
do rhs.as_mut_buf |p, len| {
for uint::range(0, len) |i| {
let x = ptr::replace_ptr(ptr::mut_offset(p, i),
intrinsics::uninit());
@ -1412,7 +1402,7 @@ impl<T> OwnedVector<T> for ~[T] {
/// Shorten a vector, dropping excess elements.
fn truncate(&mut self, newlen: uint) {
do as_mut_buf(*self) |p, oldlen| {
do self.as_mut_buf |p, oldlen| {
assert!(newlen <= oldlen);
unsafe {
// This loop is optimized out for non-drop types.
@ -1570,7 +1560,7 @@ impl<T:Eq> OwnedEqVector<T> for ~[T] {
if self.len() == 0 { return; }
let mut last_written = 0;
let mut next_to_read = 1;
do as_mut_buf(*self) |p, ln| {
do self.as_mut_buf |p, ln| {
// last_written < next_to_read <= ln
while next_to_read < ln {
// last_written < next_to_read < ln
@ -1624,6 +1614,8 @@ pub trait MutableVector<'self, T> {
unsafe fn unsafe_mut_ref(&self, index: uint) -> *mut T;
unsafe fn unsafe_set(&self, index: uint, val: T);
fn as_mut_buf<U>(&self, f: &fn(*mut T, uint) -> U) -> U;
}
impl<'self,T> MutableVector<'self, T> for &'self mut [T] {
@ -1632,7 +1624,7 @@ impl<'self,T> MutableVector<'self, T> for &'self mut [T] {
fn mut_slice(self, start: uint, end: uint) -> &'self mut [T] {
assert!(start <= end);
assert!(end <= self.len());
do as_mut_buf(self) |p, _len| {
do self.as_mut_buf |p, _len| {
unsafe {
transmute((ptr::mut_offset(p, start),
(end - start) * sys::nonzero_size_of::<T>()))
@ -1705,6 +1697,17 @@ impl<'self,T> MutableVector<'self, T> for &'self mut [T] {
unsafe fn unsafe_set(&self, index: uint, val: T) {
*self.unsafe_mut_ref(index) = val;
}
/// Similar to `as_imm_buf` but passing a `*mut T`
#[inline]
fn as_mut_buf<U>(&self, f: &fn(*mut T, uint) -> U) -> U {
unsafe {
let v : *(*mut T,uint) = transmute(self);
let (buf,len) = *v;
f(buf, len / sys::nonzero_size_of::<T>())
}
}
}
/// Trait for ~[T] where T is Cloneable
@ -1754,7 +1757,7 @@ pub mod raw {
use ptr;
use sys;
use unstable::intrinsics;
use vec::{UnboxedVecRepr, as_imm_buf, as_mut_buf, with_capacity};
use vec::{UnboxedVecRepr, with_capacity, ImmutableVector, MutableVector};
use util;
/// The internal representation of a (boxed) vector
@ -1842,7 +1845,7 @@ pub mod raw {
*/
#[inline]
pub unsafe fn get<T:Copy>(v: &[T], i: uint) -> T {
as_imm_buf(v, |p, _len| copy *ptr::offset(p, i))
v.as_imm_buf(|p, _len| copy *ptr::offset(p, i))
}
/**
@ -1853,7 +1856,7 @@ pub mod raw {
#[inline]
pub unsafe fn init_elem<T>(v: &mut [T], i: uint, val: T) {
let mut box = Some(val);
do as_mut_buf(v) |p, _len| {
do v.as_mut_buf |p, _len| {
let box2 = util::replace(&mut box, None);
intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i)),
box2.unwrap());
@ -1873,7 +1876,7 @@ pub mod raw {
pub unsafe fn from_buf_raw<T>(ptr: *T, elts: uint) -> ~[T] {
let mut dst = with_capacity(elts);
set_len(&mut dst, elts);
as_mut_buf(dst, |p_dst, _len_dst| ptr::copy_memory(p_dst, ptr, elts));
dst.as_mut_buf(|p_dst, _len_dst| ptr::copy_memory(p_dst, ptr, elts));
dst
}
@ -1889,8 +1892,8 @@ pub mod raw {
assert!(dst.len() >= count);
assert!(src.len() >= count);
do as_mut_buf(dst) |p_dst, _len_dst| {
do as_imm_buf(src) |p_src, _len_src| {
do dst.as_mut_buf |p_dst, _len_dst| {
do src.as_imm_buf |p_src, _len_src| {
ptr::copy_memory(p_dst, p_src, count)
}
}
@ -1914,7 +1917,7 @@ pub mod bytes {
impl<'self> MutableByteVector for &'self mut [u8] {
#[inline]
fn set_memory(self, value: u8) {
do vec::as_mut_buf(self) |p, len| {
do self.as_mut_buf |p, len| {
unsafe { ptr::set_memory(p, value, len) };
}
}
@ -2920,7 +2923,7 @@ mod tests {
#[should_fail]
fn test_as_imm_buf_fail() {
let v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)];
do as_imm_buf(v) |_buf, _i| {
do v.as_imm_buf |_buf, _i| {
fail!()
}
}
@ -2930,7 +2933,7 @@ mod tests {
#[should_fail]
fn test_as_mut_buf_fail() {
let mut v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)];
do as_mut_buf(v) |_buf, _i| {
do v.as_mut_buf |_buf, _i| {
fail!()
}
}

View File

@ -11,7 +11,6 @@
// error-pattern:index out of bounds
use std::sys;
use std::vec;
fn main() {
@ -22,7 +21,7 @@ fn main() {
// huge).
let x = ~[1u,2u,3u];
do vec::as_imm_buf(x) |p, _len| {
do x.as_imm_buf |p, _len| {
let base = p as uint;
let idx = base / sys::size_of::<uint>();
error!("ov1 base = 0x%x", base);