]> git.lizzy.rs Git - rust.git/commitdiff
Convert vec::{as_imm_buf, as_mut_buf} to methods.
authorHuon Wilson <dbau.pp+github@gmail.com>
Wed, 3 Jul 2013 06:34:17 +0000 (16:34 +1000)
committerHuon Wilson <dbau.pp+github@gmail.com>
Wed, 3 Jul 2013 14:46:50 +0000 (00:46 +1000)
14 files changed:
src/libextra/flate.rs
src/libextra/par.rs
src/libextra/uv_ll.rs
src/librustc/middle/trans/build.rs
src/librustc/middle/trans/common.rs
src/libstd/io.rs
src/libstd/os.rs
src/libstd/ptr.rs
src/libstd/rand.rs
src/libstd/rt/uv/mod.rs
src/libstd/run.rs
src/libstd/str.rs
src/libstd/vec.rs
src/test/run-fail/bug-2470-bounds-check-overflow.rs

index f249feeb4403d460e691a2d4287db490eff4dd51..88c61e60d86b4186cf8127ef3ae1494062640f56 100644 (file)
@@ -45,7 +45,7 @@ unsafe fn tinfl_decompress_mem_to_heap(psrc_buf: *const c_void,
 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 =
index da046f6b5ce705db905a348e7803795f94d77c5a..d56f7b32ae7d86ab96e08c9d9ea8e2e67c346517 100644 (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() || {
index db960f334fdc9cfde270d16c7cc64e7afd407634..69ff100784077f93c323b9fb1ec16d3cd7659473 100644 (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);
index 8535c84c5cb5e30d4e839dab3bc4216b860671dd..811138c6dbddfb0110213e4f3cc01d2cb2eac6fc 100644 (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())
         }
     }
index 5b3052a1e1f4c47d632bafe17b72c79136f50dbf..865fb26b94558da93879e0e8596268170fc7dbfb 100644 (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)
         };
 
index bdcad15f45c444fd9ad8bc70dbab1ec7fc869b6c..38826dd411b681f7653c08321123051abd501297 100644 (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 @@ fn get_type(&self) -> WriterType { File }
 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);
index c994bbf6fa49f096def3ab3a67473725d4533d97..be0c504885b312906d377a5bc0ed7144abdfee40 100644 (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 fn fill_utf16_buf_and_decode(f: &fn(*mut u16, DWORD) -> DWORD)
             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 fn as_utf16_p<T>(s: &str, f: &fn(*u16) -> T) -> T {
         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 @@ fn do_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 @@ fn copy_file_ok() {
           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)
index 473f56ddd7984f1bfea0846210ff7a31f1aaea8e..aee6f1bd204e3a6070d06d5013ffac9b74943f21 100644 (file)
@@ -406,7 +406,7 @@ fn test_buf_len() {
             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);
                     }
index 5782822bc2b770aca56ae537476681a621e42890..5054763d742b05cf3e1f7a66bc005ebd0660d336 100644 (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 @@ pub enum rust_rng {}
         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);
index f50efc079a7c7b6a753e7a50262eefd2a6d1fd3e..092d736620268db78c049814d6a297fc4d30a103 100644 (file)
@@ -40,6 +40,7 @@
 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)
         }
index 9e5def253c7ed1770b221fa02fa6e73faf4e1760..df15111a91f4287940f2824dff39fcdf33ed9f4e 100644 (file)
@@ -24,7 +24,7 @@
 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)) }
         )
       }
index 28162cf5117ca7f2a2d241ee69d13ff5b42eacfc..564c58f7097ee90080eaa8e7d605709c02591f76 100644 (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 unsafe fn from_buf(buf: *u8) -> ~str {
     /// 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 unsafe fn from_c_str_len(c_str: *libc::c_char, len: uint) -> ~str {
 
     /// 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 unsafe fn slice_bytes_owned(s: &str, begin: uint, end: uint) -> ~str {
             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);
index f19f917ee4749f971008983be08f56389160cb44..fb9c47b43733b1db42bc766d7e87122638ae1853 100644 (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 @@ fn next(&mut self) -> Option<&'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 @@ fn bsearch(&self, f: &fn(&T) -> Ordering) -> Option<uint> {
     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 @@ fn push_all_move(&mut self, mut rhs: ~[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 @@ fn swap_remove(&mut self, index: uint) -> 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 @@ pub fn dedup(&mut self) {
             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 @@ unsafe fn unsafe_mut_ref(&self, index: uint) -> *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 unsafe fn mut_buf_as_slice<T,U>(p: *mut T,
      */
     #[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 unsafe fn get<T:Copy>(v: &[T], i: uint) -> T {
     #[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 unsafe fn init_elem<T>(v: &mut [T], i: uint, val: T) {
     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 unsafe fn copy_memory<T>(dst: &mut [T], src: &[T],
         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 trait MutableByteVector {
     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 @@ fn test_permute_fail() {
     #[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 @@ fn test_as_imm_buf_fail() {
     #[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!()
         }
     }
index bd7d86d72953146ec307b07a6b6c51556661589f..0fdaf31c59361a13adb460e4a98878331cca8d0c 100644 (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);