]> git.lizzy.rs Git - rust.git/commitdiff
auto merge of #11029 : huonw/rust/rm-vec-as-buf, r=cmr
authorbors <bors@rust-lang.org>
Thu, 19 Dec 2013 01:11:42 +0000 (17:11 -0800)
committerbors <bors@rust-lang.org>
Thu, 19 Dec 2013 01:11:42 +0000 (17:11 -0800)
For `str.as_mut_buf`, un-closure-ification is achieved by outright removal (see commit message). The others are replaced by `.as_ptr`, `.as_mut_ptr` and `.len`

20 files changed:
src/libextra/flate.rs
src/librustc/back/link.rs
src/librustc/back/manifest.rs
src/librustc/middle/trans/base.rs
src/librustc/middle/trans/builder.rs
src/librustc/middle/trans/common.rs
src/librustc/middle/trans/foreign.rs
src/librustdoc/html/markdown.rs
src/librustuv/process.rs
src/libstd/c_str.rs
src/libstd/io/native/file.rs
src/libstd/io/native/process.rs
src/libstd/option.rs
src/libstd/os.rs
src/libstd/ptr.rs
src/libstd/rand/os.rs
src/libstd/rt/sched.rs
src/libstd/str.rs
src/libstd/vec.rs
src/test/run-fail/bug-2470-bounds-check-overflow.rs

index 5227cb18e6ea2fd6673920bc571818c06c7a5d29..1153c3a6ef300b32433d8cebc532e6df9f816cce 100644 (file)
@@ -44,21 +44,18 @@ pub fn tinfl_decompress_mem_to_heap(psrc_buf: *c_void,
 static TDEFL_WRITE_ZLIB_HEADER : c_int = 0x01000; // write zlib header and adler32 checksum
 
 fn deflate_bytes_internal(bytes: &[u8], flags: c_int) -> ~[u8] {
-    bytes.as_imm_buf(|b, len| {
-        unsafe {
-            let mut outsz : size_t = 0;
-            let res =
-                rustrt::tdefl_compress_mem_to_heap(b as *c_void,
-                                                   len as size_t,
-                                                   &mut outsz,
-                                                   flags);
-            assert!(res as int != 0);
+    unsafe {
+        let mut outsz : size_t = 0;
+        let res = rustrt::tdefl_compress_mem_to_heap(bytes.as_ptr() as *c_void,
+                                                     bytes.len() as size_t,
+                                                     &mut outsz,
+                                                     flags);
+        assert!(res as int != 0);
             let out = vec::raw::from_buf_raw(res as *u8,
                                              outsz as uint);
-            libc::free(res);
-            out
-        }
-    })
+        libc::free(res);
+        out
+    }
 }
 
 pub fn deflate_bytes(bytes: &[u8]) -> ~[u8] {
@@ -70,21 +67,18 @@ pub fn deflate_bytes_zlib(bytes: &[u8]) -> ~[u8] {
 }
 
 fn inflate_bytes_internal(bytes: &[u8], flags: c_int) -> ~[u8] {
-    bytes.as_imm_buf(|b, len| {
-        unsafe {
-            let mut outsz : size_t = 0;
-            let res =
-                rustrt::tinfl_decompress_mem_to_heap(b as *c_void,
-                                                     len as size_t,
-                                                     &mut outsz,
-                                                     flags);
-            assert!(res as int != 0);
-            let out = vec::raw::from_buf_raw(res as *u8,
-                                            outsz as uint);
-            libc::free(res);
-            out
-        }
-    })
+    unsafe {
+        let mut outsz : size_t = 0;
+        let res = rustrt::tinfl_decompress_mem_to_heap(bytes.as_ptr() as *c_void,
+                                                       bytes.len() as size_t,
+                                                       &mut outsz,
+                                                       flags);
+        assert!(res as int != 0);
+        let out = vec::raw::from_buf_raw(res as *u8,
+                                         outsz as uint);
+        libc::free(res);
+        out
+    }
 }
 
 pub fn inflate_bytes(bytes: &[u8]) -> ~[u8] {
index b5f0a5713bd0a3f908478d69905afd0b21adc504..2e9687bb9c1f35b453fbb2d2a33a2fb6e5753b47 100644 (file)
@@ -354,9 +354,7 @@ unsafe fn configure_llvm(sess: Session) {
             add(*arg);
         }
 
-        llvm_args.as_imm_buf(|p, len| {
-            llvm::LLVMRustSetLLVMOptions(len as c_int, p);
-        })
+        llvm::LLVMRustSetLLVMOptions(llvm_args.len() as c_int, llvm_args.as_ptr());
     }
 
     unsafe fn populate_llvm_passes(fpm: lib::llvm::PassManagerRef,
index 1b39935800bab53c5c139fab531fda080d2960d6..8e85603b31cffdc0e22c86c16ac5c4b7779fbc7d 100644 (file)
@@ -55,7 +55,7 @@ fn as_utf16_p<T>(s: &str, f: |*u16| -> T) -> T {
         let mut t = s.to_utf16();
         // Null terminate before passing on.
         t.push(0u16);
-        t.as_imm_buf(|buf, _len| f(buf))
+        f(t.as_ptr())
     }
 
     #[link_name = "kernel32"]
@@ -86,14 +86,12 @@ pub fn embed_manifest(filename: &Path,
                 return Err(format!("failure in BeginUpdateResourceW: {}", os::last_os_error()));
             }
 
-            let ok = manifest.as_imm_buf(|p, len| {
-                UpdateResourceW(hUpdate,
-                                MAKEINTRESOURCEW(24), // RT_MANIFEST
-                                MAKEINTRESOURCEW(1),  // CREATEPROCESS_MANIFEST_RESOURCE_ID
-                                0,                    // LANG_NEUTRAL, SUBLANG_NEUTRAL
-                                p as LPCVOID,
-                                len as u32)
-            });
+            let ok = UpdateResourceW(hUpdate,
+                                     MAKEINTRESOURCEW(24), // RT_MANIFEST
+                                     MAKEINTRESOURCEW(1),  // CREATEPROCESS_MANIFEST_RESOURCE_ID
+                                     0,                    // LANG_NEUTRAL, SUBLANG_NEUTRAL
+                                     manifest.as_ptr() as LPCVOID,
+                                     manifest.len() as u32);
             if ok == FALSE {
                 return Err(format!("failure in UpdateResourceW: {}", os::last_os_error()));
             }
index b3d326d727be503d7e58c79482dd36375796b0f6..7ab3b4fd69eab8e1375664a7b881fc8d2abcf0ea 100644 (file)
@@ -2404,9 +2404,9 @@ fn create_entry_fn(ccx: @mut CrateContext,
                 (rust_main, args)
             };
 
-            let result = args.as_imm_buf(|buf, len| {
-                llvm::LLVMBuildCall(bld, start_fn, buf, len as c_uint, noname())
-            });
+            let result = llvm::LLVMBuildCall(bld, start_fn,
+                                             args.as_ptr(), args.len() as c_uint,
+                                             noname());
 
             llvm::LLVMBuildRet(bld, result);
         }
index 863c567a21651e4ed5b36f0b32e1249dac08cbf6..02e2232bfc8f09f67fb41d1733685a58bc127613 100644 (file)
@@ -464,11 +464,11 @@ pub fn load_range_assert(&self, ptr: ValueRef, lo: c_ulonglong,
             let min = llvm::LLVMConstInt(t, lo, signed);
             let max = llvm::LLVMConstInt(t, hi, signed);
 
-            [min, max].as_imm_buf(|ptr, len| {
-                llvm::LLVMSetMetadata(value, lib::llvm::MD_range as c_uint,
-                                      llvm::LLVMMDNodeInContext(self.ccx.llcx,
-                                                                ptr, len as c_uint));
-            })
+            let v = [min, max];
+
+            llvm::LLVMSetMetadata(value, lib::llvm::MD_range as c_uint,
+                                  llvm::LLVMMDNodeInContext(self.ccx.llcx,
+                                                            v.as_ptr(), v.len() as c_uint));
         }
 
         value
index 55617067db43733f0cc34c5d1904b64f1e822cf3..e64e44695f3e97e64d49e1ec7ff0d4267131a767 100644 (file)
@@ -894,9 +894,9 @@ pub fn C_cstr(cx: &mut CrateContext, s: @str) -> ValueRef {
             None => ()
         }
 
-        let sc = s.as_imm_buf(|buf, buflen| {
-            llvm::LLVMConstStringInContext(cx.llcx, buf as *c_char, buflen as c_uint, False)
-        });
+        let sc = llvm::LLVMConstStringInContext(cx.llcx,
+                                                s.as_ptr() as *c_char, s.len() as c_uint,
+                                                False);
 
         let gsym = token::gensym("str");
         let g = format!("str{}", gsym).with_c_str(|buf| {
@@ -952,17 +952,16 @@ pub fn C_zero_byte_arr(size: uint) -> ValueRef {
 
 pub fn C_struct(elts: &[ValueRef], packed: bool) -> ValueRef {
     unsafe {
-        elts.as_imm_buf(|ptr, len| {
-            llvm::LLVMConstStructInContext(base::task_llcx(), ptr, len as c_uint, packed as Bool)
-        })
+
+        llvm::LLVMConstStructInContext(base::task_llcx(),
+                                       elts.as_ptr(), elts.len() as c_uint,
+                                       packed as Bool)
     }
 }
 
 pub fn C_named_struct(T: Type, elts: &[ValueRef]) -> ValueRef {
     unsafe {
-        elts.as_imm_buf(|ptr, len| {
-            llvm::LLVMConstNamedStruct(T.to_ref(), ptr, len as c_uint)
-        })
+        llvm::LLVMConstNamedStruct(T.to_ref(), elts.as_ptr(), elts.len() as c_uint)
     }
 }
 
@@ -988,9 +987,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 = us.as_imm_buf(|p, len| {
-            llvm::LLVMConstExtractValue(v, p, len as c_uint)
-        });
+        let r = llvm::LLVMConstExtractValue(v, us.as_ptr(), us.len() as c_uint);
 
         debug!("const_get_elt(v={}, us={:?}, r={})",
                cx.tn.val_to_str(v), us, cx.tn.val_to_str(r));
index 2a0840a0ef8ad380b5747144b8605b43b88adc80..5560efacfa5359f50a4ac24f8335a2960de259ab 100644 (file)
@@ -646,11 +646,9 @@ unsafe fn build_wrap_fn(ccx: @mut CrateContext,
         }
 
         // Perform the call itself
-        let llrust_ret_val = llrust_args.as_imm_buf(|ptr, len| {
-            debug!("calling llrustfn = {}", ccx.tn.val_to_str(llrustfn));
-            llvm::LLVMBuildCall(builder, llrustfn, ptr,
-                                len as c_uint, noname())
-        });
+        debug!("calling llrustfn = {}", ccx.tn.val_to_str(llrustfn));
+        let llrust_ret_val = llvm::LLVMBuildCall(builder, llrustfn, llrust_args.as_ptr(),
+                                                 llrust_args.len() as c_uint, noname());
 
         // Get the return value where the foreign fn expects it.
         let llforeign_ret_ty = match tys.fn_ty.ret_ty.cast {
index 97cd7c20f51cbd74825cf941c6d01bb69011ea87..513144ad73132f5f1ded439e94ed698a355e1aeb 100644 (file)
@@ -106,9 +106,8 @@ fn render(w: &mut io::Writer, s: &str) {
         let markdown = sd_markdown_new(extensions, 16, &callbacks,
                                        &options as *html_renderopt as *libc::c_void);
 
-        s.as_imm_buf(|data, len| {
-            sd_markdown_render(ob, data, len as libc::size_t, markdown);
-        });
+
+        sd_markdown_render(ob, s.as_ptr(), s.len() as libc::size_t, markdown);
         sd_markdown_free(markdown);
 
         vec::raw::buf_as_slice((*ob).data, (*ob).size as uint, |buf| {
index 0410d58bd8b3886b781a20ab72a80b70daa3afe7..9e359e26f03d6abddcb2371af62450d89f3925cb 100644 (file)
@@ -70,7 +70,7 @@ pub fn spawn(loop_: &Loop, config: ProcessConfig)
                     },
                     flags: 0,
                     stdio_count: stdio.len() as libc::c_int,
-                    stdio: stdio.as_imm_buf(|p, _| p),
+                    stdio: stdio.as_ptr(),
                     uid: 0,
                     gid: 0,
                 };
@@ -163,7 +163,7 @@ fn with_argv<T>(prog: &str, args: &[~str], f: |**libc::c_char| -> T) -> T {
         c_args.push(s.with_ref(|p| p));
     }
     c_args.push(ptr::null());
-    c_args.as_imm_buf(|buf, _| f(buf))
+    f(c_args.as_ptr())
 }
 
 /// Converts the environment to the env array expected by libuv
@@ -182,7 +182,7 @@ fn with_env<T>(env: Option<&[(~str, ~str)]>, f: |**libc::c_char| -> T) -> T {
         c_envp.push(s.with_ref(|p| p));
     }
     c_envp.push(ptr::null());
-    c_envp.as_imm_buf(|buf, _| f(buf))
+    f(c_envp.as_ptr())
 }
 
 impl HomingIO for Process {
index 1ba6b7b50cabfcc71efe3f1a1c22181762c17afb..592efe6d981b128df194979d6d97d33cd8f92f6a 100644 (file)
@@ -267,17 +267,16 @@ fn to_c_str(&self) -> CString {
     }
 
     unsafe fn to_c_str_unchecked(&self) -> CString {
-        self.as_imm_buf(|self_buf, self_len| {
-            let buf = libc::malloc(self_len as libc::size_t + 1) as *mut u8;
-            if buf.is_null() {
-                fail!("failed to allocate memory!");
-            }
+        let self_len = self.len();
+        let buf = libc::malloc(self_len as libc::size_t + 1) as *mut u8;
+        if buf.is_null() {
+            fail!("failed to allocate memory!");
+        }
 
-            ptr::copy_memory(buf, self_buf, self_len);
-            *ptr::mut_offset(buf, self_len as int) = 0;
+        ptr::copy_memory(buf, self.as_ptr(), self_len);
+        *ptr::mut_offset(buf, self_len as int) = 0;
 
-            CString::new(buf as *libc::c_char, true)
-        })
+        CString::new(buf as *libc::c_char, true)
     }
 
     fn with_c_str<T>(&self, f: |*libc::c_char| -> T) -> T {
@@ -296,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 {
@@ -575,15 +573,14 @@ mod bench {
 
     #[inline]
     fn check(s: &str, c_str: *libc::c_char) {
-        s.as_imm_buf(|s_buf, s_len| {
-            for i in range(0, s_len) {
-                unsafe {
-                    assert_eq!(
-                        *ptr::offset(s_buf, i as int) as libc::c_char,
-                        *ptr::offset(c_str, i as int));
-                }
+        let s_buf = s.as_ptr();
+        for i in range(0, s.len()) {
+            unsafe {
+                assert_eq!(
+                    *ptr::offset(s_buf, i as int) as libc::c_char,
+                    *ptr::offset(c_str, i as int));
             }
-        })
+        }
     }
 
     static s_short: &'static str = "Mary";
index bd618dd6f0f4af897a04bbec21a13684d740f1c1..74d18f11a1d60c9df0ca0fb4892a7382aef5a304 100644 (file)
@@ -37,8 +37,8 @@ fn keep_going(data: &[u8], f: |*u8, uint| -> i64) -> i64 {
     #[cfg(windows)] static eintr: int = 0; // doesn't matter
     #[cfg(not(windows))] static eintr: int = libc::EINTR as int;
 
-    let (data, origamt) = data.as_imm_buf(|data, amt| (data, amt));
-    let mut data = data;
+    let origamt = data.len();
+    let mut data = data.as_ptr();
     let mut amt = origamt;
     while amt > 0 {
         let mut ret;
index a1f50b15f23b9b12184a5218d5007839499f39e4..ef972dc4d0ad115cc29206662fe55f70e100763e 100644 (file)
@@ -8,7 +8,6 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-use cast;
 use io;
 use libc::{pid_t, c_void, c_int};
 use libc;
@@ -17,6 +16,8 @@
 use ptr;
 use rt::rtio;
 use super::file;
+#[cfg(windows)]
+use cast;
 
 use p = io::process;
 
@@ -453,7 +454,7 @@ fn with_argv<T>(prog: &str, args: &[~str], cb: |**libc::c_char| -> T) -> T {
     // Finally, make sure we add a null pointer.
     ptrs.push(ptr::null());
 
-    ptrs.as_imm_buf(|buf, _| cb(buf))
+    cb(ptrs.as_ptr())
 }
 
 #[cfg(unix)]
@@ -476,7 +477,7 @@ fn with_envp<T>(env: Option<~[(~str, ~str)]>, cb: |*c_void| -> T) -> T {
             let mut ptrs = tmps.map(|tmp| tmp.with_ref(|buf| buf));
             ptrs.push(ptr::null());
 
-            ptrs.as_imm_buf(|buf, _| unsafe { cb(cast::transmute(buf)) })
+            cb(ptrs.as_ptr() as *c_void)
         }
         _ => cb(ptr::null())
     }
@@ -499,7 +500,7 @@ fn with_envp<T>(env: Option<~[(~str, ~str)]>, cb: |*mut c_void| -> T) -> T {
 
             blk.push(0);
 
-            blk.as_imm_buf(|p, _len| unsafe { cb(cast::transmute(p)) })
+            cb(blk.as_mut_ptr() as *mut c_void)
         }
         _ => cb(ptr::mut_null())
     }
index 87f6c8608fdfbe8cd2a86d32aae7103d07b06d09..5067f6eb1279b44a986ea146b968d1ebceebae32 100644 (file)
@@ -436,10 +436,10 @@ fn test_get_ptr() {
     #[test]
     fn test_get_str() {
         let x = ~"test";
-        let addr_x = x.as_imm_buf(|buf, _len| buf);
+        let addr_x = x.as_ptr();
         let opt = Some(x);
         let y = opt.unwrap();
-        let addr_y = y.as_imm_buf(|buf, _len| buf);
+        let addr_y = y.as_ptr();
         assert_eq!(addr_x, addr_y);
     }
 
index 7abeb34a010a1c695f81d68aed46a77959b8daf8..8da7c0340f7fe04fc1411063ef33bbe092a90d75 100644 (file)
@@ -59,15 +59,12 @@ pub fn close(fd: c_int) -> c_int {
 #[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)]
@@ -75,13 +72,11 @@ pub fn getcwd() -> Path {
     use libc::DWORD;
     use libc::GetCurrentDirectoryW;
     let mut buf = [0 as u16, ..BUF_BYTES];
-    buf.as_mut_buf(|buf, len| {
-        unsafe {
-            if libc::GetCurrentDirectoryW(len as DWORD, buf) == 0 as DWORD {
-                fail!();
-            }
+    unsafe {
+        if libc::GetCurrentDirectoryW(buf.len() as DWORD, buf.as_mut_ptr()) == 0 as DWORD {
+            fail!();
         }
-    });
+    }
     Path::new(str::from_utf16(buf))
 }
 
@@ -103,20 +98,17 @@ pub fn fill_utf16_buf_and_decode(f: |*mut u16, DWORD| -> DWORD)
             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));
@@ -130,7 +122,7 @@ pub fn as_utf16_p<T>(s: &str, f: |*u16| -> T) -> T {
         let mut t = s.to_utf16();
         // Null terminate before passing on.
         t.push(0u16);
-        t.as_imm_buf(|buf, _len| f(buf))
+        f(t.as_ptr())
     }
 }
 
@@ -363,10 +355,8 @@ fn load_self() -> Option<~[u8]> {
             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 +384,7 @@ fn load_self() -> Option<~[u8]> {
             _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 +616,14 @@ fn __xpg_strerror_r(errnum: c_int,
 
         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 +656,17 @@ fn FormatMessageW(flags: DWORD,
         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)
         }
index 162e46c53aecdade6bd50cae0a39afcce003e112..070884c078c475207e8e6d9a8ec3fb7641b40d21 100644 (file)
@@ -511,10 +511,9 @@ fn test_buf_len() {
             "there".with_c_str(|p1| {
                 "thing".with_c_str(|p2| {
                     let v = ~[p0, p1, p2, null()];
-                    v.as_imm_buf(|vp, len| {
-                        assert_eq!(unsafe { buf_len(vp) }, 3u);
-                        assert_eq!(len, 4u);
-                    })
+                    unsafe {
+                        assert_eq!(buf_len(v.as_ptr()), 3u);
+                    }
                 })
             })
         })
@@ -623,23 +622,21 @@ fn test_ptr_array_each_with_len() {
                 one, two, three
             ];
 
-            arr.as_imm_buf(|arr_ptr, arr_len| {
-                let mut ctr = 0;
-                let mut iteration_count = 0;
-                array_each_with_len(arr_ptr, arr_len, |e| {
-                     let actual = str::raw::from_c_str(e);
-                     let expected = expected_arr[ctr].with_ref(|buf| {
-                         str::raw::from_c_str(buf)
-                     });
-                     debug!(
-                         "test_ptr_array_each_with_len e: {}, a: {}",
-                         expected, actual);
-                     assert_eq!(actual, expected);
-                     ctr += 1;
-                     iteration_count += 1;
+            let mut ctr = 0;
+            let mut iteration_count = 0;
+            array_each_with_len(arr.as_ptr(), arr.len(), |e| {
+                    let actual = str::raw::from_c_str(e);
+                    let expected = expected_arr[ctr].with_ref(|buf| {
+                            str::raw::from_c_str(buf)
+                        });
+                    debug!(
+                        "test_ptr_array_each_with_len e: {}, a: {}",
+                        expected, actual);
+                    assert_eq!(actual, expected);
+                    ctr += 1;
+                    iteration_count += 1;
                 });
-                assert_eq!(iteration_count, 3u);
-            })
+            assert_eq!(iteration_count, 3u);
         }
     }
 
@@ -660,23 +657,22 @@ fn test_ptr_array_each() {
                 one, two, three
             ];
 
-            arr.as_imm_buf(|arr_ptr, _| {
-                let mut ctr = 0;
-                let mut iteration_count = 0;
-                array_each(arr_ptr, |e| {
-                     let actual = str::raw::from_c_str(e);
-                     let expected = expected_arr[ctr].with_ref(|buf| {
-                         str::raw::from_c_str(buf)
-                     });
-                     debug!(
-                         "test_ptr_array_each e: {}, a: {}",
-                         expected, actual);
-                     assert_eq!(actual, expected);
-                     ctr += 1;
-                     iteration_count += 1;
+            let arr_ptr = arr.as_ptr();
+            let mut ctr = 0;
+            let mut iteration_count = 0;
+            array_each(arr_ptr, |e| {
+                    let actual = str::raw::from_c_str(e);
+                    let expected = expected_arr[ctr].with_ref(|buf| {
+                        str::raw::from_c_str(buf)
+                    });
+                    debug!(
+                        "test_ptr_array_each e: {}, a: {}",
+                        expected, actual);
+                    assert_eq!(actual, expected);
+                    ctr += 1;
+                    iteration_count += 1;
                 });
-                assert_eq!(iteration_count, 3);
-            })
+            assert_eq!(iteration_count, 3);
         }
     }
 
index 1eaf1a29fa8940da910110bb90efb6866b36fd27..1eaff3f51a6d775eae22f5495bbe03243d701e11 100644 (file)
@@ -111,9 +111,7 @@ fn rust_win32_rand_gen(hProv: HCRYPTPROV, dwLen: DWORD,
                                    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())}
     }
 }
 
index ac3aeb5a4bb3d8a0d804d7799fd3ab1737f19ce3..cd21cdeb711b720eafe1355f50550cd711aceacb 100644 (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) {
index f13107651305836b35ec0bbf244f41569c8dd164..78a09d459def21359f4a41f01db95f1ede21f5af 100644 (file)
 use vec::{OwnedVector, OwnedCopyableVector, ImmutableVector, MutableVector};
 use default::Default;
 use send_str::{SendStr, SendStrOwned};
+use unstable::raw::Repr;
 
 /*
 Section: Conditions
@@ -382,11 +383,7 @@ impl<'a> Iterator<(uint, char)> for CharOffsetIterator<'a> {
     fn next(&mut self) -> Option<(uint, char)> {
         // Compute the byte offset by using the pointer offset between
         // the original string slice and the iterator's remaining part
-        let offset = self.string.as_imm_buf(|a, _| {
-            self.iter.string.as_imm_buf(|b, _| {
-                b as uint - a as uint
-            })
-        });
+        let offset = self.iter.string.as_ptr() as uint - self.string.as_ptr() as uint;
         self.iter.next().map(|ch| (offset, ch))
     }
 
@@ -400,11 +397,8 @@ impl<'a> DoubleEndedIterator<(uint, char)> for CharOffsetIterator<'a> {
     #[inline]
     fn next_back(&mut self) -> Option<(uint, char)> {
         self.iter.next_back().map(|ch| {
-            let offset = self.string.as_imm_buf(|a, _| {
-                self.iter.string.as_imm_buf(|b, len| {
-                    b as uint - a as uint + len
-                })
-            });
+            let offset = self.iter.string.len() +
+                    self.iter.string.as_ptr() as uint - self.string.as_ptr() as uint;
             (offset, ch)
         })
     }
@@ -748,41 +742,30 @@ pub fn replace(s: &str, from: &str, to: &str) -> ~str {
 Section: Comparing strings
 */
 
+// share the implementation of the lang-item vs. non-lang-item
+// eq_slice.
+#[inline]
+fn eq_slice_(a: &str, b: &str) -> bool {
+    a.len() == b.len() && unsafe {
+        libc::memcmp(a.as_ptr() as *libc::c_void,
+                     b.as_ptr() as *libc::c_void,
+                     a.len() as libc::size_t) == 0
+    }
+}
+
 /// Bytewise slice equality
 #[cfg(not(test))]
 #[lang="str_eq"]
 #[inline]
 pub fn eq_slice(a: &str, b: &str) -> bool {
-    a.as_imm_buf(|ap, alen| {
-        b.as_imm_buf(|bp, blen| {
-            if (alen != blen) { false }
-            else {
-                unsafe {
-                    libc::memcmp(ap as *libc::c_void,
-                                 bp as *libc::c_void,
-                                 alen as libc::size_t) == 0
-                }
-            }
-        })
-    })
+    eq_slice_(a, b)
 }
 
 /// Bytewise slice equality
 #[cfg(test)]
 #[inline]
 pub fn eq_slice(a: &str, b: &str) -> bool {
-    a.as_imm_buf(|ap, alen| {
-        b.as_imm_buf(|bp, blen| {
-            if (alen != blen) { false }
-            else {
-                unsafe {
-                    libc::memcmp(ap as *libc::c_void,
-                                 bp as *libc::c_void,
-                                 alen as libc::size_t) == 0
-                }
-            }
-        })
-    })
+    eq_slice_(a, b)
 }
 
 /// Bytewise string equality
@@ -1000,7 +983,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));
@@ -1080,12 +1063,10 @@ pub unsafe fn slice_bytes<'a>(s: &'a str, begin: uint, end: uint) -> &'a str {
     /// Caller must check slice boundaries!
     #[inline]
     pub unsafe fn slice_unchecked<'a>(s: &'a str, begin: uint, end: uint) -> &'a str {
-        s.as_imm_buf(|sbuf, _n| {
-             cast::transmute(Slice {
-                 data: sbuf.offset(begin as int),
-                 len: end - begin,
-             })
-        })
+        cast::transmute(Slice {
+                data: s.as_ptr().offset(begin as int),
+                len: end - begin,
+            })
     }
 
     /// Appends a byte to a string.
@@ -1309,7 +1290,7 @@ fn into_owned(self) -> ~str { self.to_owned() }
 impl<'a> Container for &'a str {
     #[inline]
     fn len(&self) -> uint {
-        self.as_imm_buf(|_p, n| n)
+        self.repr().len
     }
 }
 
@@ -1997,10 +1978,12 @@ pub trait StrSlice<'a> {
     /// ```
     fn subslice_offset(&self, inner: &str) -> uint;
 
-    /// Work with the byte buffer and length of a slice.
+    /// Return an unsafe pointer to the strings buffer.
     ///
-    /// The buffer does not have a null terminator.
-    fn as_imm_buf<T>(&self, f: |*u8, uint| -> T) -> T;
+    /// The caller must ensure that the string outlives this pointer,
+    /// and that it is not reallocated (e.g. by pushing to the
+    /// string).
+    fn as_ptr(&self) -> *u8;
 }
 
 impl<'a> StrSlice<'a> for &'a str {
@@ -2278,15 +2261,14 @@ fn replace(&self, from: &str, to: &str) -> ~str {
 
     #[inline]
     fn to_owned(&self) -> ~str {
-        self.as_imm_buf(|src, len| {
-            unsafe {
-                let mut v = vec::with_capacity(len);
+        let len = self.len();
+        unsafe {
+            let mut v = vec::with_capacity(len);
 
-                v.as_mut_buf(|dst, _| ptr::copy_memory(dst, src, len));
-                v.set_len(len);
-                ::cast::transmute(v)
-            }
-        })
+            ptr::copy_memory(v.as_mut_ptr(), self.as_ptr(), len);
+            v.set_len(len);
+            ::cast::transmute(v)
+        }
     }
 
     #[inline]
@@ -2482,27 +2464,19 @@ fn lev_distance(&self, t: &str) -> uint {
     }
 
     fn subslice_offset(&self, inner: &str) -> uint {
-        self.as_imm_buf(|a, a_len| {
-            inner.as_imm_buf(|b, b_len| {
-                let a_start: uint;
-                let a_end: uint;
-                let b_start: uint;
-                let b_end: uint;
-                unsafe {
-                    a_start = cast::transmute(a); a_end = a_len + cast::transmute(a);
-                    b_start = cast::transmute(b); b_end = b_len + cast::transmute(b);
-                }
-                assert!(a_start <= b_start);
-                assert!(b_end <= a_end);
-                b_start - a_start
-            })
-        })
+        let a_start = self.as_ptr() as uint;
+        let a_end = a_start + self.len();
+        let b_start = inner.as_ptr() as uint;
+        let b_end = b_start + inner.len();
+
+        assert!(a_start <= b_start);
+        assert!(b_end <= a_end);
+        b_start - a_start
     }
 
     #[inline]
-    fn as_imm_buf<T>(&self, f: |*u8, uint| -> T) -> T {
-        let v: &[u8] = unsafe { cast::transmute(*self) };
-        v.as_imm_buf(f)
+    fn as_ptr(&self) -> *u8 {
+        self.repr().data
     }
 }
 
@@ -2581,14 +2555,6 @@ pub trait OwnedStr {
     /// The buffer does not have a null terminator.
     fn into_bytes(self) -> ~[u8];
 
-    /// Work with the mutable byte buffer and length of a slice.
-    ///
-    /// The buffer does not have a null terminator.
-    ///
-    /// The caller must make sure any mutations to this buffer keep the string
-    /// valid UTF-8!
-    fn as_mut_buf<T>(&mut self, f: |*mut u8, uint| -> T) -> T;
-
     /// Sets the length of a string
     ///
     /// This will explicitly set the size of the string, without actually
@@ -2617,16 +2583,15 @@ fn push_char(&mut self, c: char) {
         let cur_len = self.len();
         // may use up to 4 bytes.
         unsafe {
-            raw::as_owned_vec(self).reserve_additional(4);
+            let v = raw::as_owned_vec(self);
+            v.reserve_additional(4);
 
             // Attempt to not use an intermediate buffer by just pushing bytes
             // directly onto this string.
-            let used = self.as_mut_buf(|buf, _| {
-                vec::raw::mut_buf_as_slice(buf.offset(cur_len as int), 4, |slc| {
-                    c.encode_utf8(slc)
-                })
-            });
-            self.set_len(cur_len + used);
+            let write_ptr = v.as_mut_ptr().offset(cur_len as int);
+            let used = vec::raw::mut_buf_as_slice(write_ptr, 4, |slc| c.encode_utf8(slc));
+
+            v.set_len(cur_len + used);
         }
     }
 
@@ -2694,13 +2659,6 @@ fn into_bytes(self) -> ~[u8] {
         unsafe { cast::transmute(self) }
     }
 
-    #[inline]
-    fn as_mut_buf<T>(&mut self, f: |*mut u8, uint| -> T) -> T {
-        unsafe {
-            raw::as_owned_vec(self).as_mut_buf(f)
-        }
-    }
-
     #[inline]
     unsafe fn set_len(&mut self, new_len: uint) {
         raw::as_owned_vec(self).set_len(new_len)
@@ -3390,19 +3348,15 @@ fn test_as_bytes_fail() {
     }
 
     #[test]
-    fn test_as_imm_buf() {
-        "".as_imm_buf(|_, len| assert_eq!(len, 0));
-
-        "hello".as_imm_buf(|buf, len| {
-            assert_eq!(len, 5);
-            unsafe {
-                assert_eq!(*ptr::offset(buf, 0), 'h' as u8);
-                assert_eq!(*ptr::offset(buf, 1), 'e' as u8);
-                assert_eq!(*ptr::offset(buf, 2), 'l' as u8);
-                assert_eq!(*ptr::offset(buf, 3), 'l' as u8);
-                assert_eq!(*ptr::offset(buf, 4), 'o' as u8);
-            }
-        })
+    fn test_as_ptr() {
+        let buf = "hello".as_ptr();
+        unsafe {
+            assert_eq!(*ptr::offset(buf, 0), 'h' as u8);
+            assert_eq!(*ptr::offset(buf, 1), 'e' as u8);
+            assert_eq!(*ptr::offset(buf, 2), 'l' as u8);
+            assert_eq!(*ptr::offset(buf, 3), 'l' as u8);
+            assert_eq!(*ptr::offset(buf, 4), 'o' as u8);
+        }
     }
 
     #[test]
@@ -3935,10 +3889,10 @@ fn test_str_truncate() {
         assert_eq!(s.as_slice(), "");
 
         let mut s = ~"12345";
-        let p = s.as_imm_buf(|p,_| p);
+        let p = s.as_ptr();
         s.truncate(3);
         s.push_str("6");
-        let p_ = s.as_imm_buf(|p,_| p);
+        let p_ = s.as_ptr();
         assert_eq!(p_, p);
     }
 
index 65be214c14ed5cc3f7c770fdc74cb155a6192830..ad0d8861ee9c4e7e9b477327526858536bc71606 100644 (file)
@@ -775,7 +775,7 @@ impl<'a, T> Container for &'a [T] {
     /// Returns the length of a vector
     #[inline]
     fn len(&self) -> uint {
-        self.as_imm_buf(|_p, len| len)
+        self.repr().len
     }
 }
 
@@ -783,7 +783,7 @@ impl<T> Container for ~[T] {
     /// Returns the length of a vector
     #[inline]
     fn len(&self) -> uint {
-        self.as_imm_buf(|_p, len| len)
+        self.repr().len
     }
 }
 
@@ -984,14 +984,6 @@ pub trait ImmutableVector<'a, T> {
     /// of a vector and return the results.
     fn map<U>(&self, |t: &T| -> U) -> ~[U];
 
-    /**
-     * Work with the buffer of a vector.
-     *
-     * Allows for unsafe manipulation of vector contents, which is useful for
-     * foreign interop.
-     */
-    fn as_imm_buf<U>(&self, f: |*T, uint| -> U) -> U;
-
     /**
      * Returns a mutable reference to the first element in this slice
      * and adjusts the slice in place so that it no longer contains
@@ -1032,14 +1024,12 @@ impl<'a,T> ImmutableVector<'a, T> for &'a [T] {
     fn slice(&self, start: uint, end: uint) -> &'a [T] {
         assert!(start <= end);
         assert!(end <= self.len());
-        self.as_imm_buf(|p, _len| {
-            unsafe {
-                cast::transmute(Slice {
-                    data: ptr::offset(p, start as int),
+        unsafe {
+            cast::transmute(Slice {
+                    data: self.as_ptr().offset(start as int),
                     len: (end - start)
                 })
-            }
-        })
+        }
     }
 
     #[inline]
@@ -1197,12 +1187,6 @@ fn map<U>(&self, f: |t: &T| -> U) -> ~[U] {
         self.iter().map(f).collect()
     }
 
-    #[inline]
-    fn as_imm_buf<U>(&self, f: |*T, uint| -> U) -> U {
-        let s = self.repr();
-        f(s.data, s.len)
-    }
-
     fn shift_ref(&mut self) -> &'a T {
         unsafe {
             let s: &mut Slice<T> = cast::transmute(self);
@@ -1697,15 +1681,16 @@ fn swap_remove(&mut self, index: uint) -> 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); }
     }
 
@@ -2069,9 +2054,6 @@ fn mut_split_at(self, mid: uint) -> (&'a mut [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] {
@@ -2079,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]
@@ -2205,18 +2185,9 @@ unsafe fn init_elem(self, i: uint, val: T) {
 
     #[inline]
     unsafe fn copy_memory(self, src: &[T]) {
-        self.as_mut_buf(|p_dst, len_dst| {
-            src.as_imm_buf(|p_src, len_src| {
-                assert!(len_dst >= len_src)
-                ptr::copy_nonoverlapping_memory(p_dst, p_src, 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)
     }
 }
 
@@ -2300,7 +2271,7 @@ pub unsafe fn mut_buf_as_slice<T,
     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
     }
 
@@ -2332,6 +2303,7 @@ pub unsafe fn pop_ptr<T>(slice: &mut Slice<T>) -> *T {
 
 /// Operations on `[u8]`.
 pub mod bytes {
+    use container::Container;
     use vec::MutableVector;
     use ptr;
 
@@ -2344,9 +2316,7 @@ pub trait MutableByteVector {
     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()) };
         }
     }
 
@@ -2368,11 +2338,7 @@ pub fn push_bytes(dst: &mut ~[u8], src: &[u8]) {
         let old_len = dst.len();
         dst.reserve_additional(src.len());
         unsafe {
-            dst.as_mut_buf(|p_dst, len_dst| {
-                src.as_imm_buf(|p_src, len_src| {
-                    ptr::copy_memory(p_dst.offset(len_dst as int), p_src, len_src)
-                })
-            });
+            ptr::copy_memory(dst.as_mut_ptr().offset(old_len as int), src.as_ptr(), src.len());
             dst.set_len(old_len + src.len());
         }
     }
@@ -3553,24 +3519,6 @@ fn test_permute_fail() {
         }
     }
 
-    #[test]
-    #[should_fail]
-    fn test_as_imm_buf_fail() {
-        let v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)];
-        v.as_imm_buf(|_buf, _i| {
-            fail!()
-        })
-    }
-
-    #[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() {
index a476a9727b3609414fe7e8b063075151c9b424eb..932b9a0d4510656d042b309ee12b34cac45c5abf 100644 (file)
@@ -21,16 +21,15 @@ fn main() {
     // huge).
 
     let x = ~[1u,2u,3u];
-    x.as_imm_buf(|p, _len| {
-        let base = p as uint;
-        let idx = base / mem::size_of::<uint>();
-        error!("ov1 base = 0x{:x}", base);
-        error!("ov1 idx = 0x{:x}", idx);
-        error!("ov1 sizeof::<uint>() = 0x{:x}", mem::size_of::<uint>());
-        error!("ov1 idx * sizeof::<uint>() = 0x{:x}",
-               idx * mem::size_of::<uint>());
 
-        // This should fail.
-        error!("ov1 0x{:x}",  x[idx]);
-    })
+    let base = x.as_ptr() as uint;
+    let idx = base / mem::size_of::<uint>();
+    error!("ov1 base = 0x{:x}", base);
+    error!("ov1 idx = 0x{:x}", idx);
+    error!("ov1 sizeof::<uint>() = 0x{:x}", mem::size_of::<uint>());
+    error!("ov1 idx * sizeof::<uint>() = 0x{:x}",
+           idx * mem::size_of::<uint>());
+
+    // This should fail.
+    error!("ov1 0x{:x}",  x[idx]);
 }