diff options
| author | Huon Wilson <dbau.pp+github@gmail.com> | 2013-12-15 23:35:12 +1100 |
|---|---|---|
| committer | Huon Wilson <dbau.pp+github@gmail.com> | 2013-12-15 23:37:41 +1100 |
| commit | 164f7a290ef98377e4c8c158eca2fbed098a2842 (patch) | |
| tree | d9b2239b59f5d18bb680429595192fc64fdad6eb | |
| parent | f53292f7ee7365fe50ac216efac438ff5569fd06 (diff) | |
| download | rust-164f7a290ef98377e4c8c158eca2fbed098a2842.tar.gz rust-164f7a290ef98377e4c8c158eca2fbed098a2842.zip | |
std::vec: convert to(_mut)_ptr to as_... methods on &[] and &mut [].
| -rw-r--r-- | doc/po/ja/tutorial-ffi.md.po | 10 | ||||
| -rw-r--r-- | doc/po/tutorial-ffi.md.pot | 10 | ||||
| -rw-r--r-- | doc/tutorial-ffi.md | 10 | ||||
| -rw-r--r-- | src/libextra/arena.rs | 7 | ||||
| -rw-r--r-- | src/librustc/back/lto.rs | 5 | ||||
| -rw-r--r-- | src/librustc/middle/trans/builder.rs | 17 | ||||
| -rw-r--r-- | src/librustc/middle/trans/common.rs | 6 | ||||
| -rw-r--r-- | src/librustc/middle/trans/context.rs | 3 | ||||
| -rw-r--r-- | src/librustc/middle/trans/debuginfo.rs | 6 | ||||
| -rw-r--r-- | src/librustc/middle/trans/type_.rs | 12 | ||||
| -rw-r--r-- | src/librustuv/file.rs | 5 | ||||
| -rw-r--r-- | src/librustuv/lib.rs | 3 | ||||
| -rw-r--r-- | src/librustuv/net.rs | 2 | ||||
| -rw-r--r-- | src/librustuv/uvll.rs | 5 | ||||
| -rw-r--r-- | src/libstd/c_str.rs | 2 | ||||
| -rw-r--r-- | src/libstd/io/native/file.rs | 6 | ||||
| -rw-r--r-- | src/libstd/os.rs | 4 | ||||
| -rw-r--r-- | src/libstd/ptr.rs | 26 | ||||
| -rw-r--r-- | src/libstd/rand/isaac.rs | 4 | ||||
| -rw-r--r-- | src/libstd/rt/stack.rs | 4 | ||||
| -rw-r--r-- | src/libstd/str.rs | 4 | ||||
| -rw-r--r-- | src/libstd/vec.rs | 77 |
22 files changed, 115 insertions, 113 deletions
diff --git a/doc/po/ja/tutorial-ffi.md.po b/doc/po/ja/tutorial-ffi.md.po index c5df23b3971..088970bf00d 100644 --- a/doc/po/ja/tutorial-ffi.md.po +++ b/doc/po/ja/tutorial-ffi.md.po @@ -168,7 +168,7 @@ msgid "" "~~~~ {.xfail-test}\n" "pub fn validate_compressed_buffer(src: &[u8]) -> bool {\n" " unsafe {\n" -" snappy_validate_compressed_buffer(vec::raw::to_ptr(src), src.len() as size_t) == 0\n" +" snappy_validate_compressed_buffer(src.as_ptr(), src.len() as size_t) == 0\n" " }\n" "}\n" "~~~~\n" @@ -207,7 +207,7 @@ msgid "" "pub fn compress(src: &[u8]) -> ~[u8] {\n" " unsafe {\n" " let srclen = src.len() as size_t;\n" -" let psrc = vec::raw::to_ptr(src);\n" +" let psrc = src.as_ptr();\n" msgstr "" #. type: Plain text @@ -216,7 +216,7 @@ msgstr "" msgid "" " let mut dstlen = snappy_max_compressed_length(srclen);\n" " let mut dst = vec::with_capacity(dstlen as uint);\n" -" let pdst = vec::raw::to_mut_ptr(dst);\n" +" let pdst = dst.as_mut_ptr();\n" msgstr "" #. type: Plain text @@ -247,7 +247,7 @@ msgid "" "pub fn uncompress(src: &[u8]) -> Option<~[u8]> {\n" " unsafe {\n" " let srclen = src.len() as size_t;\n" -" let psrc = vec::raw::to_ptr(src);\n" +" let psrc = src.as_ptr();\n" msgstr "" #. type: Plain text @@ -263,7 +263,7 @@ msgstr "" #, no-wrap msgid "" " let mut dst = vec::with_capacity(dstlen as uint);\n" -" let pdst = vec::raw::to_mut_ptr(dst);\n" +" let pdst = dst.as_mut_ptr();\n" msgstr "" #. type: Plain text diff --git a/doc/po/tutorial-ffi.md.pot b/doc/po/tutorial-ffi.md.pot index f5a29fd59c3..670e9f32fe6 100644 --- a/doc/po/tutorial-ffi.md.pot +++ b/doc/po/tutorial-ffi.md.pot @@ -168,7 +168,7 @@ msgid "" "~~~~ {.xfail-test}\n" "pub fn validate_compressed_buffer(src: &[u8]) -> bool {\n" " unsafe {\n" -" snappy_validate_compressed_buffer(vec::raw::to_ptr(src), src.len() as size_t) == 0\n" +" snappy_validate_compressed_buffer(src.as_ptr(), src.len() as size_t) == 0\n" " }\n" "}\n" "~~~~\n" @@ -207,7 +207,7 @@ msgid "" "pub fn compress(src: &[u8]) -> ~[u8] {\n" " unsafe {\n" " let srclen = src.len() as size_t;\n" -" let psrc = vec::raw::to_ptr(src);\n" +" let psrc = src.as_ptr();\n" msgstr "" #. type: Plain text @@ -216,7 +216,7 @@ msgstr "" msgid "" " let mut dstlen = snappy_max_compressed_length(srclen);\n" " let mut dst = vec::with_capacity(dstlen as uint);\n" -" let pdst = vec::raw::to_mut_ptr(dst);\n" +" let pdst = dst.as_mut_ptr();\n" msgstr "" #. type: Plain text @@ -247,7 +247,7 @@ msgid "" "pub fn uncompress(src: &[u8]) -> Option<~[u8]> {\n" " unsafe {\n" " let srclen = src.len() as size_t;\n" -" let psrc = vec::raw::to_ptr(src);\n" +" let psrc = src.as_ptr();\n" msgstr "" #. type: Plain text @@ -263,7 +263,7 @@ msgstr "" #, no-wrap msgid "" " let mut dst = vec::with_capacity(dstlen as uint);\n" -" let pdst = vec::raw::to_mut_ptr(dst);\n" +" let pdst = dst.as_mut_ptr();\n" msgstr "" #. type: Plain text diff --git a/doc/tutorial-ffi.md b/doc/tutorial-ffi.md index 7ff82ffcadb..39eff68afd1 100644 --- a/doc/tutorial-ffi.md +++ b/doc/tutorial-ffi.md @@ -79,7 +79,7 @@ the allocated memory. The length is less than or equal to the capacity. ~~~~ {.xfail-test} pub fn validate_compressed_buffer(src: &[u8]) -> bool { unsafe { - snappy_validate_compressed_buffer(vec::raw::to_ptr(src), src.len() as size_t) == 0 + snappy_validate_compressed_buffer(src.as_ptr(), src.len() as size_t) == 0 } } ~~~~ @@ -100,11 +100,11 @@ the true length after compression for setting the length. pub fn compress(src: &[u8]) -> ~[u8] { unsafe { let srclen = src.len() as size_t; - let psrc = vec::raw::to_ptr(src); + let psrc = src.as_ptr(); let mut dstlen = snappy_max_compressed_length(srclen); let mut dst = vec::with_capacity(dstlen as uint); - let pdst = vec::raw::to_mut_ptr(dst); + let pdst = dst.as_mut_ptr(); snappy_compress(psrc, srclen, pdst, &mut dstlen); dst.set_len(dstlen as uint); @@ -120,13 +120,13 @@ format and `snappy_uncompressed_length` will retrieve the exact buffer size requ pub fn uncompress(src: &[u8]) -> Option<~[u8]> { unsafe { let srclen = src.len() as size_t; - let psrc = vec::raw::to_ptr(src); + let psrc = src.as_ptr(); let mut dstlen: size_t = 0; snappy_uncompressed_length(psrc, srclen, &mut dstlen); let mut dst = vec::with_capacity(dstlen as uint); - let pdst = vec::raw::to_mut_ptr(dst); + let pdst = dst.as_mut_ptr(); if snappy_uncompress(psrc, srclen, pdst, &mut dstlen) == 0 { dst.set_len(dstlen as uint); diff --git a/src/libextra/arena.rs b/src/libextra/arena.rs index 8aa89f86642..dc5f3b5d3cf 100644 --- a/src/libextra/arena.rs +++ b/src/libextra/arena.rs @@ -44,7 +44,6 @@ use std::num; use std::ptr; use std::mem; use std::uint; -use std::vec; use std::unstable::intrinsics; use std::unstable::intrinsics::{TyDesc, get_tydesc}; @@ -115,7 +114,7 @@ fn round_up_to(base: uint, align: uint) -> uint { // in it. unsafe fn destroy_chunk(chunk: &Chunk) { let mut idx = 0; - let buf = vec::raw::to_ptr(chunk.data); + let buf = chunk.data.as_ptr(); let fill = chunk.fill; while idx < fill { @@ -179,7 +178,7 @@ impl Arena { //debug!("idx = {}, size = {}, align = {}, fill = {}", // start, n_bytes, align, head.fill); - ptr::offset(vec::raw::to_ptr(this.pod_head.data), start as int) + ptr::offset(this.pod_head.data.as_ptr(), start as int) } } @@ -235,7 +234,7 @@ impl Arena { //debug!("idx = {}, size = {}, align = {}, fill = {}", // start, n_bytes, align, head.fill); - let buf = vec::raw::to_ptr(self.head.data); + let buf = self.head.data.as_ptr(); return (ptr::offset(buf, tydesc_start as int), ptr::offset(buf, start as int)); } } diff --git a/src/librustc/back/lto.rs b/src/librustc/back/lto.rs index 3efaa387358..65ff67975a2 100644 --- a/src/librustc/back/lto.rs +++ b/src/librustc/back/lto.rs @@ -16,7 +16,6 @@ use metadata::cstore; use util::common::time; use std::libc; -use std::vec; pub fn run(sess: session::Session, llmod: ModuleRef, tm: TargetMachineRef, reachable: &[~str]) { @@ -48,7 +47,7 @@ pub fn run(sess: session::Session, llmod: ModuleRef, debug!("reading {}", name); let bc = time(sess.time_passes(), format!("read {}.bc", name), (), |_| archive.read(format!("{}.bc", name))); - let ptr = vec::raw::to_ptr(bc); + let ptr = bc.as_ptr(); debug!("linking {}", name); time(sess.time_passes(), format!("ll link {}", name), (), |()| unsafe { if !llvm::LLVMRustLinkInExternalBitcode(llmod, @@ -62,7 +61,7 @@ pub fn run(sess: session::Session, llmod: ModuleRef, // Internalize everything but the reachable symbols of the current module let cstrs = reachable.map(|s| s.to_c_str()); let arr = cstrs.map(|c| c.with_ref(|p| p)); - let ptr = vec::raw::to_ptr(arr); + let ptr = arr.as_ptr(); unsafe { llvm::LLVMRustRunRestrictionPass(llmod, ptr as **libc::c_char, arr.len() as libc::size_t); diff --git a/src/librustc/middle/trans/builder.rs b/src/librustc/middle/trans/builder.rs index 7541a14fc72..863c567a216 100644 --- a/src/librustc/middle/trans/builder.rs +++ b/src/librustc/middle/trans/builder.rs @@ -20,7 +20,6 @@ use middle::trans::type_::Type; use std::cast; use std::hashmap::HashMap; use std::libc::{c_uint, c_ulonglong, c_char}; -use std::vec; use syntax::codemap::Span; use std::ptr::is_not_null; @@ -118,7 +117,7 @@ impl Builder { pub fn aggregate_ret(&self, ret_vals: &[ValueRef]) { unsafe { llvm::LLVMBuildAggregateRet(self.llbuilder, - vec::raw::to_ptr(ret_vals), + ret_vals.as_ptr(), ret_vals.len() as c_uint); } } @@ -161,7 +160,7 @@ impl Builder { unsafe { let v = llvm::LLVMBuildInvoke(self.llbuilder, llfn, - vec::raw::to_ptr(args), + args.as_ptr(), args.len() as c_uint, then, catch, @@ -500,7 +499,7 @@ impl Builder { pub fn gep(&self, ptr: ValueRef, indices: &[ValueRef]) -> ValueRef { self.count_insn("gep"); unsafe { - llvm::LLVMBuildGEP(self.llbuilder, ptr, vec::raw::to_ptr(indices), + llvm::LLVMBuildGEP(self.llbuilder, ptr, indices.as_ptr(), indices.len() as c_uint, noname()) } } @@ -528,7 +527,7 @@ impl Builder { self.count_insn("inboundsgep"); unsafe { llvm::LLVMBuildInBoundsGEP( - self.llbuilder, ptr, vec::raw::to_ptr(indices), indices.len() as c_uint, noname()) + self.llbuilder, ptr, indices.as_ptr(), indices.len() as c_uint, noname()) } } @@ -716,8 +715,8 @@ impl Builder { let phi = self.empty_phi(ty); self.count_insn("addincoming"); unsafe { - llvm::LLVMAddIncoming(phi, vec::raw::to_ptr(vals), - vec::raw::to_ptr(bbs), + llvm::LLVMAddIncoming(phi, vals.as_ptr(), + bbs.as_ptr(), vals.len() as c_uint); phi } @@ -775,7 +774,7 @@ impl Builder { attributes: &[(uint, lib::llvm::Attribute)]) -> ValueRef { self.count_insn("call"); unsafe { - let v = llvm::LLVMBuildCall(self.llbuilder, llfn, vec::raw::to_ptr(args), + let v = llvm::LLVMBuildCall(self.llbuilder, llfn, args.as_ptr(), args.len() as c_uint, noname()); for &(idx, attr) in attributes.iter() { llvm::LLVMAddInstrAttribute(v, idx as c_uint, attr as c_uint); @@ -885,7 +884,7 @@ impl Builder { let args: &[ValueRef] = []; self.count_insn("trap"); llvm::LLVMBuildCall( - self.llbuilder, T, vec::raw::to_ptr(args), args.len() as c_uint, noname()); + self.llbuilder, T, args.as_ptr(), args.len() as c_uint, noname()); } } diff --git a/src/librustc/middle/trans/common.rs b/src/librustc/middle/trans/common.rs index 5395944ce46..55617067db4 100644 --- a/src/librustc/middle/trans/common.rs +++ b/src/librustc/middle/trans/common.rs @@ -946,7 +946,7 @@ pub fn C_zero_byte_arr(size: uint) -> ValueRef { let mut elts: ~[ValueRef] = ~[]; while i < size { elts.push(C_u8(0u)); i += 1u; } return llvm::LLVMConstArray(Type::i8().to_ref(), - vec::raw::to_ptr(elts), elts.len() as c_uint); + elts.as_ptr(), elts.len() as c_uint); } } @@ -968,13 +968,13 @@ pub fn C_named_struct(T: Type, elts: &[ValueRef]) -> ValueRef { pub fn C_array(ty: Type, elts: &[ValueRef]) -> ValueRef { unsafe { - return llvm::LLVMConstArray(ty.to_ref(), vec::raw::to_ptr(elts), elts.len() as c_uint); + return llvm::LLVMConstArray(ty.to_ref(), elts.as_ptr(), elts.len() as c_uint); } } pub fn C_bytes(bytes: &[u8]) -> ValueRef { unsafe { - let ptr = cast::transmute(vec::raw::to_ptr(bytes)); + let ptr = cast::transmute(bytes.as_ptr()); return llvm::LLVMConstStringInContext(base::task_llcx(), ptr, bytes.len() as c_uint, True); } } diff --git a/src/librustc/middle/trans/context.rs b/src/librustc/middle/trans/context.rs index 3dbe70ecf69..167b17ba95c 100644 --- a/src/librustc/middle/trans/context.rs +++ b/src/librustc/middle/trans/context.rs @@ -31,7 +31,6 @@ use util::sha2::Sha256; use std::c_str::ToCStr; use std::hashmap::{HashMap, HashSet}; use std::local_data; -use std::vec; use std::libc::c_uint; use syntax::ast; @@ -261,7 +260,7 @@ impl CrateContext { indices.iter().map(|i| C_i32(*i as i32)).collect(); unsafe { llvm::LLVMConstInBoundsGEP(pointer, - vec::raw::to_ptr(v), + v.as_ptr(), indices.len() as c_uint) } } diff --git a/src/librustc/middle/trans/debuginfo.rs b/src/librustc/middle/trans/debuginfo.rs index ab0013e73bf..0f83e51cac8 100644 --- a/src/librustc/middle/trans/debuginfo.rs +++ b/src/librustc/middle/trans/debuginfo.rs @@ -860,7 +860,7 @@ pub fn create_function_debug_context(cx: &mut CrateContext, fn create_DIArray(builder: DIBuilderRef, arr: &[DIDescriptor]) -> DIArray { return unsafe { - llvm::LLVMDIBuilderGetOrCreateArray(builder, vec::raw::to_ptr(arr), arr.len() as u32) + llvm::LLVMDIBuilderGetOrCreateArray(builder, arr.as_ptr(), arr.len() as u32) }; } @@ -949,7 +949,7 @@ fn declare_local(bcx: @mut Block, file_metadata, loc.line as c_uint, type_metadata, - vec::raw::to_ptr(address_operations), + address_operations.as_ptr(), address_operations.len() as c_uint, argument_index) } @@ -2133,7 +2133,7 @@ fn set_debug_location(cx: &mut CrateContext, debug_location: DebugLocation) { let elements = [C_i32(line as i32), C_i32(col as i32), scope, ptr::null()]; unsafe { metadata_node = llvm::LLVMMDNodeInContext(debug_context(cx).llcontext, - vec::raw::to_ptr(elements), + elements.as_ptr(), elements.len() as c_uint); } } diff --git a/src/librustc/middle/trans/type_.rs b/src/librustc/middle/trans/type_.rs index 3e0761e214c..8c5b55b73cd 100644 --- a/src/librustc/middle/trans/type_.rs +++ b/src/librustc/middle/trans/type_.rs @@ -150,13 +150,13 @@ impl Type { pub fn func(args: &[Type], ret: &Type) -> Type { let vec : &[TypeRef] = unsafe { cast::transmute(args) }; - ty!(llvm::LLVMFunctionType(ret.to_ref(), vec::raw::to_ptr(vec), + ty!(llvm::LLVMFunctionType(ret.to_ref(), vec.as_ptr(), args.len() as c_uint, False)) } pub fn variadic_func(args: &[Type], ret: &Type) -> Type { let vec : &[TypeRef] = unsafe { cast::transmute(args) }; - ty!(llvm::LLVMFunctionType(ret.to_ref(), vec::raw::to_ptr(vec), + ty!(llvm::LLVMFunctionType(ret.to_ref(), vec.as_ptr(), args.len() as c_uint, True)) } @@ -170,7 +170,7 @@ impl Type { pub fn struct_(els: &[Type], packed: bool) -> Type { let els : &[TypeRef] = unsafe { cast::transmute(els) }; - ty!(llvm::LLVMStructTypeInContext(base::task_llcx(), vec::raw::to_ptr(els), + ty!(llvm::LLVMStructTypeInContext(base::task_llcx(), els.as_ptr(), els.len() as c_uint, packed as Bool)) } @@ -297,7 +297,7 @@ impl Type { pub fn set_struct_body(&mut self, els: &[Type], packed: bool) { unsafe { let vec : &[TypeRef] = cast::transmute(els); - llvm::LLVMStructSetBody(self.to_ref(), vec::raw::to_ptr(vec), + llvm::LLVMStructSetBody(self.to_ref(), vec.as_ptr(), els.len() as c_uint, packed as Bool) } } @@ -311,7 +311,7 @@ impl Type { let num_fields = llvm::LLVMCountStructElementTypes(self.to_ref()) as uint; let mut elems = vec::from_elem(num_fields, 0 as TypeRef); - llvm::LLVMGetStructElementTypes(self.to_ref(), vec::raw::to_mut_ptr(elems)); + llvm::LLVMGetStructElementTypes(self.to_ref(), elems.as_mut_ptr()); Type::from_ref(elems[idx]) } @@ -355,7 +355,7 @@ impl Type { unsafe { let n_args = llvm::LLVMCountParamTypes(self.to_ref()) as uint; let args = vec::from_elem(n_args, 0 as TypeRef); - llvm::LLVMGetParamTypes(self.to_ref(), vec::raw::to_ptr(args)); + llvm::LLVMGetParamTypes(self.to_ref(), args.as_ptr()); cast::transmute(args) } } diff --git a/src/librustuv/file.rs b/src/librustuv/file.rs index 16e2d1adbd0..3a8af71e019 100644 --- a/src/librustuv/file.rs +++ b/src/librustuv/file.rs @@ -20,7 +20,6 @@ use std::io; use std::rt::local::Local; use std::rt::rtio; use std::rt::sched::{Scheduler, SchedHandle}; -use std::vec; use super::{Loop, UvError, uv_error_to_io_error, wait_until_woken_after}; use uvio::HomingIO; @@ -78,7 +77,7 @@ impl FsRequest { { execute_nop(|req, cb| unsafe { uvll::uv_fs_write(loop_.handle, req, - fd, vec::raw::to_ptr(buf) as *c_void, + fd, buf.as_ptr() as *c_void, buf.len() as size_t, offset, cb) }) } @@ -88,7 +87,7 @@ impl FsRequest { { execute(|req, cb| unsafe { uvll::uv_fs_read(loop_.handle, req, - fd, vec::raw::to_ptr(buf) as *c_void, + fd, buf.as_ptr() as *c_void, buf.len() as size_t, offset, cb) }).map(|req| { req.get_result() as int diff --git a/src/librustuv/lib.rs b/src/librustuv/lib.rs index 241db9900cd..d8bf356a099 100644 --- a/src/librustuv/lib.rs +++ b/src/librustuv/lib.rs @@ -60,7 +60,6 @@ use std::str::raw::from_c_str; use std::str; use std::task; use std::unstable::finally::Finally; -use std::vec; use std::io::IoError; @@ -388,7 +387,7 @@ pub fn empty_buf() -> Buf { /// Borrow a slice to a Buf pub fn slice_to_uv_buf(v: &[u8]) -> Buf { - let data = vec::raw::to_ptr(v); + let data = v.as_ptr(); uvll::uv_buf_t { base: data, len: v.len() as uvll::uv_buf_len_t } } diff --git a/src/librustuv/net.rs b/src/librustuv/net.rs index c09383c2d8f..6f1930bc7fe 100644 --- a/src/librustuv/net.rs +++ b/src/librustuv/net.rs @@ -62,7 +62,7 @@ pub fn sockaddr_to_socket_addr(addr: *sockaddr) -> SocketAddr { // apparently there's an off-by-one in libuv? let ip_size = ip_size + 1; let buf = vec::from_elem(ip_size + 1 /*null terminated*/, 0u8); - let buf_ptr = vec::raw::to_ptr(buf); + let buf_ptr = buf.as_ptr(); let ret = if uvll::rust_is_ipv4_sockaddr(addr) == 1 { uvll::uv_ip4_name(addr, buf_ptr as *c_char, ip_size as size_t) } else { diff --git a/src/librustuv/uvll.rs b/src/librustuv/uvll.rs index c1d4d367e25..91be6563343 100644 --- a/src/librustuv/uvll.rs +++ b/src/librustuv/uvll.rs @@ -33,7 +33,6 @@ use std::libc::{size_t, c_int, c_uint, c_void, c_char, c_double}; use std::libc::ssize_t; use std::libc::{malloc, free}; use std::libc; -use std::vec; #[cfg(test)] use std::libc::uintptr_t; @@ -419,7 +418,7 @@ pub unsafe fn uv_write(req: *uv_write_t, cb: uv_write_cb) -> c_int; } - let buf_ptr = vec::raw::to_ptr(buf_in); + let buf_ptr = buf_in.as_ptr(); let buf_cnt = buf_in.len() as i32; return uv_write(req, stream, buf_ptr, buf_cnt, cb); } @@ -435,7 +434,7 @@ pub unsafe fn uv_udp_send(req: *uv_udp_send_t, cb: uv_udp_send_cb) -> c_int; } - let buf_ptr = vec::raw::to_ptr(buf_in); + let buf_ptr = buf_in.as_ptr(); let buf_cnt = buf_in.len() as i32; return uv_udp_send(req, handle, buf_ptr, buf_cnt, addr, cb); } diff --git a/src/libstd/c_str.rs b/src/libstd/c_str.rs index f6f1c538c7b..1ba6b7b50ca 100644 --- a/src/libstd/c_str.rs +++ b/src/libstd/c_str.rs @@ -385,7 +385,7 @@ mod tests { fn test_str_multistring_parsing() { unsafe { let input = bytes!("zero", "\x00", "one", "\x00", "\x00"); - let ptr = vec::raw::to_ptr(input); + let ptr = input.as_ptr(); let expected = ["zero", "one"]; let mut it = expected.iter(); let result = from_c_multistring(ptr as *libc::c_char, None, |c| { diff --git a/src/libstd/io/native/file.rs b/src/libstd/io/native/file.rs index 4e1c6bf982a..bd618dd6f0f 100644 --- a/src/libstd/io/native/file.rs +++ b/src/libstd/io/native/file.rs @@ -133,7 +133,7 @@ impl rtio::RtioFileStream for FileDesc { self.inner_write(buf) } fn pread(&mut self, buf: &mut [u8], offset: u64) -> Result<int, IoError> { - return os_pread(self.fd, vec::raw::to_ptr(buf), buf.len(), offset); + return os_pread(self.fd, buf.as_ptr(), buf.len(), offset); #[cfg(windows)] fn os_pread(fd: c_int, buf: *u8, amt: uint, offset: u64) -> IoResult<int> { @@ -165,7 +165,7 @@ impl rtio::RtioFileStream for FileDesc { } } fn pwrite(&mut self, buf: &[u8], offset: u64) -> Result<(), IoError> { - return os_pwrite(self.fd, vec::raw::to_ptr(buf), buf.len(), offset); + return os_pwrite(self.fd, buf.as_ptr(), buf.len(), offset); #[cfg(windows)] fn os_pwrite(fd: c_int, buf: *u8, amt: uint, offset: u64) -> IoResult<()> { @@ -700,7 +700,7 @@ pub fn readlink(p: &CString) -> IoResult<Path> { } let mut buf = vec::with_capacity::<u8>(len as uint); match unsafe { - libc::readlink(p, vec::raw::to_ptr(buf) as *mut libc::c_char, + libc::readlink(p, buf.as_ptr() as *mut libc::c_char, len as libc::size_t) } { -1 => Err(super::last_error()), diff --git a/src/libstd/os.rs b/src/libstd/os.rs index f82df73908c..7abeb34a010 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -358,13 +358,13 @@ pub fn self_exe_path() -> Option<Path> { KERN_PROC as c_int, KERN_PROC_PATHNAME as c_int, -1 as c_int]; let mut sz: size_t = 0; - let err = sysctl(vec::raw::to_ptr(mib), mib.len() as ::libc::c_uint, + let err = sysctl(mib.as_ptr(), mib.len() as ::libc::c_uint, ptr::mut_null(), &mut sz, ptr::null(), 0u as size_t); 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(vec::raw::to_ptr(mib), mib.len() as ::libc::c_uint, + sysctl(mib.as_ptr(), mib.len() as ::libc::c_uint, buf as *mut c_void, &mut sz, ptr::null(), 0u as size_t) }); if err != 0 { return None; } diff --git a/src/libstd/ptr.rs b/src/libstd/ptr.rs index 9b9636af901..e2dda826765 100644 --- a/src/libstd/ptr.rs +++ b/src/libstd/ptr.rs @@ -449,7 +449,7 @@ pub mod ptr_tests { use cast; use libc; use str; - use vec; + use vec::{ImmutableVector, MutableVector}; #[test] fn test() { @@ -474,15 +474,15 @@ pub mod ptr_tests { let v0 = ~[32000u16, 32001u16, 32002u16]; let mut v1 = ~[0u16, 0u16, 0u16]; - copy_memory(mut_offset(vec::raw::to_mut_ptr(v1), 1), - offset(vec::raw::to_ptr(v0), 1), 1); + copy_memory(mut_offset(v1.as_mut_ptr(), 1), + offset(v0.as_ptr(), 1), 1); assert!((v1[0] == 0u16 && v1[1] == 32001u16 && v1[2] == 0u16)); - copy_memory(vec::raw::to_mut_ptr(v1), - offset(vec::raw::to_ptr(v0), 2), 1); + copy_memory(v1.as_mut_ptr(), + offset(v0.as_ptr(), 2), 1); assert!((v1[0] == 32002u16 && v1[1] == 32001u16 && v1[2] == 0u16)); - copy_memory(mut_offset(vec::raw::to_mut_ptr(v1), 2), - vec::raw::to_ptr(v0), 1u); + copy_memory(mut_offset(v1.as_mut_ptr(), 2), + v0.as_ptr(), 1u); assert!((v1[0] == 32002u16 && v1[1] == 32001u16 && v1[2] == 32000u16)); } @@ -558,7 +558,7 @@ pub mod ptr_tests { unsafe { let xs = ~[5, ..16]; - let mut ptr = to_ptr(xs); + let mut ptr = xs.as_ptr(); let end = ptr.offset(16); while ptr < end { @@ -567,7 +567,7 @@ pub mod ptr_tests { } let mut xs_mut = xs.clone(); - let mut m_ptr = to_mut_ptr(xs_mut); + let mut m_ptr = xs_mut.as_mut_ptr(); let m_end = m_ptr.offset(16); while m_ptr < m_end { @@ -581,12 +581,10 @@ pub mod ptr_tests { #[test] fn test_ptr_subtraction() { - use vec::raw::*; - unsafe { let xs = ~[0,1,2,3,4,5,6,7,8,9]; let mut idx = 9i8; - let ptr = to_ptr(xs); + let ptr = xs.as_ptr(); while idx >= 0i8 { assert_eq!(*(ptr.offset(idx as int)), idx as int); @@ -594,7 +592,7 @@ pub mod ptr_tests { } let mut xs_mut = xs.clone(); - let m_start = to_mut_ptr(xs_mut); + let m_start = xs_mut.as_mut_ptr(); let mut m_ptr = m_start.offset(9); while m_ptr >= m_start { @@ -700,7 +698,7 @@ pub mod ptr_tests { #[test] fn test_set_memory() { let mut xs = [0u8, ..20]; - let ptr = vec::raw::to_mut_ptr(xs); + let ptr = xs.as_mut_ptr(); unsafe { set_memory(ptr, 5u8, xs.len()); } assert_eq!(xs, [5u8, ..20]); } diff --git a/src/libstd/rand/isaac.rs b/src/libstd/rand/isaac.rs index 5ba6994b78d..38d7a683a70 100644 --- a/src/libstd/rand/isaac.rs +++ b/src/libstd/rand/isaac.rs @@ -49,7 +49,7 @@ impl IsaacRng { let mut rng = EMPTY; unsafe { - let ptr = raw::to_mut_ptr(rng.rsl); + let ptr = rng.rsl.as_mut_ptr(); raw::mut_buf_as_slice(ptr as *mut u8, mem::size_of_val(&rng.rsl), |slice| { OSRng::new().fill_bytes(slice); @@ -254,7 +254,7 @@ impl Isaac64Rng { let mut rng = EMPTY_64; unsafe { - let ptr = raw::to_mut_ptr(rng.rsl); + let ptr = rng.rsl.as_mut_ptr(); raw::mut_buf_as_slice(ptr as *mut u8, mem::size_of_val(&rng.rsl), |slice| { OSRng::new().fill_bytes(slice); diff --git a/src/libstd/rt/stack.rs b/src/libstd/rt/stack.rs index 4b3236b76bf..44b60e955d2 100644 --- a/src/libstd/rt/stack.rs +++ b/src/libstd/rt/stack.rs @@ -39,13 +39,13 @@ impl StackSegment { /// Point to the low end of the allocated stack pub fn start(&self) -> *uint { - vec::raw::to_ptr(self.buf) as *uint + self.buf.as_ptr() as *uint } /// Point one word beyond the high end of the allocated stack pub fn end(&self) -> *uint { unsafe { - vec::raw::to_ptr(self.buf).offset(self.buf.len() as int) as *uint + self.buf.as_ptr().offset(self.buf.len() as int) as *uint } } } diff --git a/src/libstd/str.rs b/src/libstd/str.rs index 5a7f6f5dc3f..0c95f527ace 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -1139,7 +1139,7 @@ pub mod raw { fn test_from_buf_len() { unsafe { let a = ~[65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8]; - let b = vec::raw::to_ptr(a); + let b = a.as_ptr(); let c = from_buf_len(b, 3u); assert_eq!(c, ~"AAA"); } @@ -3360,7 +3360,7 @@ mod tests { fn test_raw_from_c_str() { unsafe { let a = ~[65, 65, 65, 65, 65, 65, 65, 0]; - let b = vec::raw::to_ptr(a); + let b = a.as_ptr(); let c = raw::from_c_str(b); assert_eq!(c, ~"AAAAAAA"); } diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 9919c552346..137a176ff41 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -123,7 +123,6 @@ use unstable::finally::Finally; use unstable::intrinsics; use unstable::intrinsics::{get_tydesc, owns_managed}; use unstable::raw::{Box, Repr, Slice, Vec}; -use vec; use util; /** @@ -135,7 +134,7 @@ use util; pub fn from_fn<T>(n_elts: uint, op: |uint| -> T) -> ~[T] { unsafe { let mut v = with_capacity(n_elts); - let p = raw::to_mut_ptr(v); + let p = v.as_mut_ptr(); let mut i: uint = 0u; (|| { while i < n_elts { @@ -162,7 +161,7 @@ pub fn from_elem<T:Clone>(n_elts: uint, t: T) -> ~[T] { // vec::with_capacity/ptr::set_memory for primitive types. unsafe { let mut v = with_capacity(n_elts); - let p = raw::to_mut_ptr(v); + let p = v.as_mut_ptr(); let mut i = 0u; (|| { while i < n_elts { @@ -956,6 +955,17 @@ pub trait ImmutableVector<'a, T> { unsafe fn unsafe_ref(&self, index: uint) -> *T; /** + * Returns an unsafe pointer to the vector's buffer + * + * The caller must ensure that the vector outlives the pointer this + * function returns, or else it will end up pointing to garbage. + * + * Modifying the vector may cause its buffer to be reallocated, which + * would also make any pointers to it invalid. + */ + fn as_ptr(&self) -> *T; + + /** * Binary search a sorted vector with a comparator function. * * The comparator should implement an order consistent with the sort @@ -1043,7 +1053,7 @@ impl<'a,T> ImmutableVector<'a, T> for &'a [T] { #[inline] fn iter(self) -> VecIterator<'a, T> { unsafe { - let p = vec::raw::to_ptr(self); + let p = self.as_ptr(); if mem::size_of::<T>() == 0 { VecIterator{ptr: p, end: (p as uint + self.len()) as *T, @@ -1156,6 +1166,12 @@ impl<'a,T> ImmutableVector<'a, T> for &'a [T] { self.repr().data.offset(index as int) } + #[inline] + fn as_ptr(&self) -> *T { + self.repr().data + } + + fn bsearch(&self, f: |&T| -> Ordering) -> Option<uint> { let mut base : uint = 0; let mut lim : uint = self.len(); @@ -1571,8 +1587,8 @@ impl<T> OwnedVector<T> for ~[T] { let new_len = self_len + rhs_len; self.reserve_additional(rhs.len()); unsafe { // Note: infallible. - let self_p = vec::raw::to_mut_ptr(*self); - let rhs_p = vec::raw::to_ptr(rhs); + let self_p = self.as_mut_ptr(); + let rhs_p = rhs.as_ptr(); ptr::copy_memory(ptr::mut_offset(self_p, self_len as int), rhs_p, rhs_len); self.set_len(new_len); rhs.set_len(0); @@ -1648,7 +1664,7 @@ impl<T> OwnedVector<T> for ~[T] { self.set_len(next_ln); // Swap out the element we want from the end - let vp = raw::to_mut_ptr(*self); + let vp = self.as_mut_ptr(); let vp = ptr::mut_offset(vp, (next_ln - 1) as int); Some(ptr::replace_ptr(vp, work_elt)) @@ -1900,7 +1916,7 @@ impl<T:Eq> OwnedEqVector<T> for ~[T] { if ln < 1 { return; } // Avoid bounds checks by using unsafe pointers. - let p = vec::raw::to_mut_ptr(*self); + let p = self.as_mut_ptr(); let mut r = 1; let mut w = 1; @@ -2038,6 +2054,17 @@ pub trait MutableVector<'a, T> { /// Returns an unsafe mutable pointer to the element in index unsafe fn unsafe_mut_ref(self, index: uint) -> *mut T; + + /// Return an unsafe mutable pointer to the vector's buffer. + /// + /// The caller must ensure that the vector outlives the pointer this + /// function returns, or else it will end up pointing to garbage. + /// + /// Modifying the vector may cause its buffer to be reallocated, which + /// would also make any pointers to it invalid. + #[inline] + fn as_mut_ptr(self) -> *mut T; + /// Unsafely sets the element in index to the value unsafe fn unsafe_set(self, index: uint, val: T); @@ -2083,7 +2110,7 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] { #[inline] fn mut_iter(self) -> VecMutIterator<'a, T> { unsafe { - let p = vec::raw::to_mut_ptr(self); + let p = self.as_mut_ptr(); if mem::size_of::<T>() == 0 { VecMutIterator{ptr: p, end: (p as uint + self.len()) as *mut T, @@ -2160,6 +2187,11 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] { } #[inline] + fn as_mut_ptr(self) -> *mut T { + self.repr().data as *mut T + } + + #[inline] unsafe fn unsafe_set(self, index: uint, val: T) { *self.unsafe_mut_ref(index) = val; } @@ -2206,32 +2238,11 @@ pub mod raw { use cast; use option::Some; use ptr; - use mem; use unstable::intrinsics; use vec::{with_capacity, ImmutableVector, MutableVector}; use unstable::raw::Slice; /** - * Returns an unsafe pointer to the vector's buffer - * - * The caller must ensure that the vector outlives the pointer this - * function returns, or else it will end up pointing to garbage. - * - * Modifying the vector may cause its buffer to be reallocated, which - * would also make any pointers to it invalid. - */ - #[inline] - pub fn to_ptr<T>(v: &[T]) -> *T { - v.repr().data - } - - /** see `to_ptr()` */ - #[inline] - pub fn to_mut_ptr<T>(v: &mut [T]) -> *mut T { - v.repr().data as *mut T - } - - /** * Form a slice from a pointer and length (as a number of units, * not bytes). */ @@ -2755,7 +2766,7 @@ mod tests { unsafe { // Test on-stack copy-from-buf. let a = ~[1, 2, 3]; - let mut ptr = raw::to_ptr(a); + let mut ptr = a.as_ptr(); let b = from_buf(ptr, 3u); assert_eq!(b.len(), 3u); assert_eq!(b[0], 1); @@ -2764,7 +2775,7 @@ mod tests { // Test on-heap copy-from-buf. let c = ~[1, 2, 3, 4, 5]; - ptr = raw::to_ptr(c); + ptr = c.as_ptr(); let d = from_buf(ptr, 5u); assert_eq!(d.len(), 5u); assert_eq!(d[0], 1); @@ -4291,7 +4302,7 @@ mod bench { bh.iter(|| { let mut v: ~[u8] = vec::with_capacity(1024); unsafe { - let vp = vec::raw::to_mut_ptr(v); + let vp = v.as_mut_ptr(); ptr::set_memory(vp, 0, 1024); v.set_len(1024); } |
