diff --git a/doc/po/ja/tutorial-ffi.md.po b/doc/po/ja/tutorial-ffi.md.po index ff28357cabf13..088970bf00d28 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 @@ -224,7 +224,7 @@ msgstr "" #, no-wrap msgid "" " snappy_compress(psrc, srclen, pdst, &mut dstlen);\n" -" vec::raw::set_len(&mut dst, dstlen as uint);\n" +" dst.set_len(dstlen as uint);\n" " dst\n" " }\n" "}\n" @@ -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 @@ -271,7 +271,7 @@ msgstr "" #, no-wrap msgid "" " if snappy_uncompress(psrc, srclen, pdst, &mut dstlen) == 0 {\n" -" vec::raw::set_len(&mut dst, dstlen as uint);\n" +" dst.set_len(dstlen as uint);\n" " Some(dst)\n" " } else {\n" " None // SNAPPY_INVALID_INPUT\n" diff --git a/doc/po/tutorial-ffi.md.pot b/doc/po/tutorial-ffi.md.pot index c3116dea87274..670e9f32fe65f 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 @@ -224,7 +224,7 @@ msgstr "" #, no-wrap msgid "" " snappy_compress(psrc, srclen, pdst, &mut dstlen);\n" -" vec::raw::set_len(&mut dst, dstlen as uint);\n" +" dst.set_len(dstlen as uint);\n" " dst\n" " }\n" "}\n" @@ -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 @@ -271,7 +271,7 @@ msgstr "" #, no-wrap msgid "" " if snappy_uncompress(psrc, srclen, pdst, &mut dstlen) == 0 {\n" -" vec::raw::set_len(&mut dst, dstlen as uint);\n" +" dst.set_len(dstlen as uint);\n" " Some(dst)\n" " } else {\n" " None // SNAPPY_INVALID_INPUT\n" diff --git a/doc/tutorial-ffi.md b/doc/tutorial-ffi.md index 0746728a0f5b3..39eff68afd1f6 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,14 +100,14 @@ 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); - vec::raw::set_len(&mut dst, dstlen as uint); + dst.set_len(dstlen as uint); dst } } @@ -120,16 +120,16 @@ 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 { - vec::raw::set_len(&mut dst, dstlen as uint); + dst.set_len(dstlen as uint); Some(dst) } else { None // SNAPPY_INVALID_INPUT diff --git a/src/libextra/arena.rs b/src/libextra/arena.rs index 8aa89f86642a5..dc5f3b5d3cfd4 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/libextra/uuid.rs b/src/libextra/uuid.rs index 0fbac7771dc2a..1b72b240b674b 100644 --- a/src/libextra/uuid.rs +++ b/src/libextra/uuid.rs @@ -175,7 +175,7 @@ impl Uuid { pub fn new_v4() -> Uuid { let ub = rand::task_rng().gen_vec(16); let mut uuid = Uuid{ bytes: [0, .. 16] }; - vec::bytes::copy_memory(uuid.bytes, ub, 16); + vec::bytes::copy_memory(uuid.bytes, ub); uuid.set_variant(VariantRFC4122); uuid.set_version(Version4Random); uuid @@ -202,7 +202,7 @@ impl Uuid { fields.data1 = to_be32(d1 as i32) as u32; fields.data2 = to_be16(d2 as i16) as u16; fields.data3 = to_be16(d3 as i16) as u16; - vec::bytes::copy_memory(fields.data4, d4, 8); + vec::bytes::copy_memory(fields.data4, d4); unsafe { transmute(fields) @@ -220,7 +220,7 @@ impl Uuid { let mut uuid = Uuid{ bytes: [0, .. 16] }; unsafe { - vec::raw::copy_memory(uuid.bytes, b, 16); + vec::raw::copy_memory(uuid.bytes, b); } Some(uuid) } @@ -442,11 +442,7 @@ impl Zero for Uuid { impl Clone for Uuid { /// Returns a copy of the UUID - fn clone(&self) -> Uuid { - let mut clone = Uuid{ bytes: [0, .. 16] }; - vec::bytes::copy_memory(clone.bytes, self.bytes, 16); - clone - } + fn clone(&self) -> Uuid { *self } } impl FromStr for Uuid { @@ -509,7 +505,7 @@ impl rand::Rand for Uuid { fn rand(rng: &mut R) -> Uuid { let ub = rng.gen_vec(16); let mut uuid = Uuid{ bytes: [0, .. 16] }; - vec::bytes::copy_memory(uuid.bytes, ub, 16); + vec::bytes::copy_memory(uuid.bytes, ub); uuid.set_variant(VariantRFC4122); uuid.set_version(Version4Random); uuid diff --git a/src/librustc/back/lto.rs b/src/librustc/back/lto.rs index 3efaa387358d0..65ff67975a2f5 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 7541a14fc720c..863c567a21651 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 5395944ce4605..55617067db437 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 3dbe70ecf690d..167b17ba95c6c 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 ab0013e73bf38..0f83e51cac8bc 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 3e0761e214ce8..8c5b55b73cda5 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/librustc/util/sha2.rs b/src/librustc/util/sha2.rs index 1ac5b73192164..b3c4fbf110563 100644 --- a/src/librustc/util/sha2.rs +++ b/src/librustc/util/sha2.rs @@ -136,16 +136,14 @@ impl FixedBuffer for FixedBuffer64 { if input.len() >= buffer_remaining { copy_memory( self.buffer.mut_slice(self.buffer_idx, size), - input.slice_to(buffer_remaining), - buffer_remaining); + input.slice_to(buffer_remaining)); self.buffer_idx = 0; func(self.buffer); i += buffer_remaining; } else { copy_memory( self.buffer.mut_slice(self.buffer_idx, self.buffer_idx + input.len()), - input, - input.len()); + input); self.buffer_idx += input.len(); return; } @@ -164,8 +162,7 @@ impl FixedBuffer for FixedBuffer64 { let input_remaining = input.len() - i; copy_memory( self.buffer.mut_slice(0, input_remaining), - input.slice_from(i), - input.len() - i); + input.slice_from(i)); self.buffer_idx += input_remaining; } diff --git a/src/librustpkg/sha1.rs b/src/librustpkg/sha1.rs index 396ef756c7610..06970561fac4f 100644 --- a/src/librustpkg/sha1.rs +++ b/src/librustpkg/sha1.rs @@ -149,16 +149,14 @@ impl FixedBuffer for FixedBuffer64 { if input.len() >= buffer_remaining { copy_memory( self.buffer.mut_slice(self.buffer_idx, size), - input.slice_to(buffer_remaining), - buffer_remaining); + input.slice_to(buffer_remaining)); self.buffer_idx = 0; func(self.buffer); i += buffer_remaining; } else { copy_memory( self.buffer.mut_slice(self.buffer_idx, self.buffer_idx + input.len()), - input, - input.len()); + input); self.buffer_idx += input.len(); return; } @@ -177,8 +175,7 @@ impl FixedBuffer for FixedBuffer64 { let input_remaining = input.len() - i; copy_memory( self.buffer.mut_slice(0, input_remaining), - input.slice_from(i), - input.len() - i); + input.slice_from(i)); self.buffer_idx += input_remaining; } diff --git a/src/librustuv/file.rs b/src/librustuv/file.rs index 16e2d1adbd0c3..3a8af71e019ef 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 241db9900cd4c..d8bf356a099b4 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 c09383c2d8f5d..6f1930bc7fe31 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/process.rs b/src/librustuv/process.rs index 8098ea653bc79..0410d58bd8b38 100644 --- a/src/librustuv/process.rs +++ b/src/librustuv/process.rs @@ -49,7 +49,7 @@ impl Process { let mut stdio = vec::with_capacity::(io.len()); let mut ret_io = vec::with_capacity(io.len()); unsafe { - vec::raw::set_len(&mut stdio, io.len()); + stdio.set_len(io.len()); for (slot, other) in stdio.iter().zip(io.iter()) { let io = set_stdio(slot as *uvll::uv_stdio_container_t, other, loop_); diff --git a/src/librustuv/uvll.rs b/src/librustuv/uvll.rs index c1d4d367e25f2..91be656334392 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 1563b7f99d193..1ba6b7b50cabf 100644 --- a/src/libstd/c_str.rs +++ b/src/libstd/c_str.rs @@ -293,7 +293,7 @@ impl<'a> ToCStr for &'a [u8] { unsafe fn with_c_str(v: &[u8], checked: bool, f: |*libc::c_char| -> T) -> T { if v.len() < BUF_LEN { let mut buf: [u8, .. BUF_LEN] = intrinsics::uninit(); - vec::bytes::copy_memory(buf, v, v.len()); + vec::bytes::copy_memory(buf, v); buf[v.len()] = 0; buf.as_mut_buf(|buf, _| { @@ -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/buffered.rs b/src/libstd/io/buffered.rs index 04f4d50036fdd..fb06a4dfdb422 100644 --- a/src/libstd/io/buffered.rs +++ b/src/libstd/io/buffered.rs @@ -79,7 +79,7 @@ impl BufferedReader { // to be very cheap (large mallocs are not nearly as expensive as large // callocs). let mut buf = vec::with_capacity(cap); - unsafe { vec::raw::set_len(&mut buf, cap); } + unsafe { buf.set_len(cap); } BufferedReader { inner: inner, buf: buf, @@ -122,7 +122,7 @@ impl Reader for BufferedReader { return None; } let nread = num::min(available.len(), buf.len()); - vec::bytes::copy_memory(buf, available, nread); + vec::bytes::copy_memory(buf, available.slice_to(nread)); nread }; self.pos += nread; @@ -154,7 +154,7 @@ impl BufferedWriter { pub fn with_capacity(cap: uint, inner: W) -> BufferedWriter { // See comments in BufferedReader for why this uses unsafe code. let mut buf = vec::with_capacity(cap); - unsafe { vec::raw::set_len(&mut buf, cap); } + unsafe { buf.set_len(cap); } BufferedWriter { inner: inner, buf: buf, @@ -185,7 +185,7 @@ impl Writer for BufferedWriter { self.inner.write(buf); } else { let dst = self.buf.mut_slice_from(self.pos); - vec::bytes::copy_memory(dst, buf, buf.len()); + vec::bytes::copy_memory(dst, buf); self.pos += buf.len(); } } diff --git a/src/libstd/io/comm_adapters.rs b/src/libstd/io/comm_adapters.rs index a53146f009191..b3e5a9a0c86f1 100644 --- a/src/libstd/io/comm_adapters.rs +++ b/src/libstd/io/comm_adapters.rs @@ -57,7 +57,7 @@ impl> Reader for PortReader

{ let dst = buf.mut_slice_from(num_read); let src = prev.slice_from(self.pos); let count = cmp::min(dst.len(), src.len()); - bytes::copy_memory(dst, src, count); + bytes::copy_memory(dst, src.slice_to(count)); num_read += count; self.pos += count; }, diff --git a/src/libstd/io/mem.rs b/src/libstd/io/mem.rs index b6778d89c3322..efb55a436eaf4 100644 --- a/src/libstd/io/mem.rs +++ b/src/libstd/io/mem.rs @@ -58,8 +58,7 @@ impl Writer for MemWriter { // Do the necessary writes if left.len() > 0 { - vec::bytes::copy_memory(self.buf.mut_slice_from(self.pos), - left, left.len()); + vec::bytes::copy_memory(self.buf.mut_slice_from(self.pos), left); } if right.len() > 0 { self.buf.push_all(right); @@ -116,7 +115,7 @@ impl Reader for MemReader { let input = self.buf.slice(self.pos, self.pos + write_len); let output = buf.mut_slice(0, write_len); assert_eq!(input.len(), output.len()); - vec::bytes::copy_memory(output, input, write_len); + vec::bytes::copy_memory(output, input); } self.pos += write_len; assert!(self.pos <= self.buf.len()); @@ -175,8 +174,7 @@ impl<'a> Writer for BufWriter<'a> { return; } - vec::bytes::copy_memory(self.buf.mut_slice_from(self.pos), - buf, buf.len()); + vec::bytes::copy_memory(self.buf.mut_slice_from(self.pos), buf); self.pos += buf.len(); } } @@ -222,7 +220,7 @@ impl<'a> Reader for BufReader<'a> { let input = self.buf.slice(self.pos, self.pos + write_len); let output = buf.mut_slice(0, write_len); assert_eq!(input.len(), output.len()); - vec::bytes::copy_memory(output, input, write_len); + vec::bytes::copy_memory(output, input); } self.pos += write_len; assert!(self.pos <= self.buf.len()); diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index c5ae645751590..53d79e70c8687 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -523,7 +523,7 @@ pub trait Reader { let mut total_read = 0; buf.reserve_additional(len); - vec::raw::set_len(buf, start_len + len); + buf.set_len(start_len + len); (|| { while total_read < len { @@ -539,7 +539,7 @@ pub trait Reader { } } } - }).finally(|| vec::raw::set_len(buf, start_len + total_read)) + }).finally(|| buf.set_len(start_len + total_read)) } } diff --git a/src/libstd/io/native/file.rs b/src/libstd/io/native/file.rs index 7b5104657d9dc..bd618dd6f0f4a 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 { - 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 { @@ -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,13 +700,13 @@ pub fn readlink(p: &CString) -> IoResult { } let mut buf = vec::with_capacity::(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()), n => { assert!(n > 0); - unsafe { vec::raw::set_len(&mut buf, n as uint); } + unsafe { buf.set_len(n as uint); } Ok(Path::new(buf)) } } diff --git a/src/libstd/os.rs b/src/libstd/os.rs index bcd353bab7af1..7abeb34a010a1 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -358,18 +358,18 @@ pub fn self_exe_path() -> Option { 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; } if sz == 0 { return None; } - vec::raw::set_len(&mut v, sz as uint - 1); // chop off trailing NUL + v.set_len(sz as uint - 1); // chop off trailing NUL Some(v) } } @@ -398,7 +398,7 @@ pub fn self_exe_path() -> Option { _NSGetExecutablePath(buf as *mut i8, &mut sz) }); if err != 0 { return None; } - vec::raw::set_len(&mut v, sz as uint - 1); // chop off trailing NUL + v.set_len(sz as uint - 1); // chop off trailing NUL Some(v) } } diff --git a/src/libstd/ptr.rs b/src/libstd/ptr.rs index 9b9636af901f2..e2dda82676589 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 3dcf97212f5eb..38d7a683a702f 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); @@ -341,7 +341,7 @@ impl Isaac64Rng { static MP_VEC: [(uint, uint), .. 2] = [(0,MIDPOINT), (MIDPOINT, 0)]; macro_rules! ind ( ($x:expr) => { - self.mem.unsafe_get(($x as uint >> 3) & (RAND_SIZE_64 - 1)) + *self.mem.unsafe_ref(($x as uint >> 3) & (RAND_SIZE_64 - 1)) } ); macro_rules! rngstep( @@ -355,8 +355,8 @@ impl Isaac64Rng { let mix = if $j == 0 {!mix} else {mix}; unsafe { - let x = self.mem.unsafe_get(base + mr_offset); - a = mix + self.mem.unsafe_get(base + m2_offset); + let x = *self.mem.unsafe_ref(base + mr_offset); + a = mix + *self.mem.unsafe_ref(base + m2_offset); let y = ind!(x) + a + b; self.mem.unsafe_set(base + mr_offset, y); @@ -395,7 +395,7 @@ impl Rng for Isaac64Rng { self.isaac64(); } self.cnt -= 1; - unsafe { self.rsl.unsafe_get(self.cnt) } + unsafe { *self.rsl.unsafe_ref(self.cnt) } } } diff --git a/src/libstd/rt/stack.rs b/src/libstd/rt/stack.rs index 4358390da9f71..44b60e955d217 100644 --- a/src/libstd/rt/stack.rs +++ b/src/libstd/rt/stack.rs @@ -24,7 +24,7 @@ impl StackSegment { unsafe { // Crate a block of uninitialized values let mut stack = vec::with_capacity(size); - vec::raw::set_len(&mut stack, size); + stack.set_len(size); let mut stk = StackSegment { buf: stack, @@ -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 af381ef3cf007..0c95f527acee3 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -993,7 +993,7 @@ pub mod raw { use cast; use libc; use ptr; - use str::is_utf8; + use str::{is_utf8, OwnedStr}; use vec; use vec::MutableVector; use unstable::raw::Slice; @@ -1002,7 +1002,7 @@ pub mod raw { 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)); - vec::raw::set_len(&mut v, len); + v.set_len(len); assert!(is_utf8(v)); ::cast::transmute(v) @@ -1109,7 +1109,7 @@ pub mod raw { let len = s.len(); assert!((len > 0u)); let b = s[len - 1u]; - set_len(s, len - 1u); + s.set_len(len - 1); return b; } @@ -1130,16 +1130,6 @@ pub mod raw { cast::transmute(s) } - /// Sets the length of a string - /// - /// This will explicitly set the size of the string, without actually - /// modifying its buffers, so it is up to the caller to ensure that - /// the string is actually the specified size. - #[inline] - pub unsafe fn set_len(s: &mut ~str, new_len: uint) { - vec::raw::set_len(as_owned_vec(s), new_len) - } - /// Sets the length of a string /// /// This will explicitly set the size of the string, without actually @@ -1149,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"); } @@ -1339,7 +1329,7 @@ impl Mutable for ~str { #[inline] fn clear(&mut self) { unsafe { - raw::set_len(self, 0) + self.set_len(0) } } } @@ -2293,7 +2283,7 @@ impl<'a> StrSlice<'a> for &'a str { let mut v = vec::with_capacity(len); v.as_mut_buf(|dst, _| ptr::copy_memory(dst, src, len)); - vec::raw::set_len(&mut v, len); + v.set_len(len); ::cast::transmute(v) } }) @@ -2598,6 +2588,13 @@ pub trait OwnedStr { /// The caller must make sure any mutations to this buffer keep the string /// valid UTF-8! fn as_mut_buf(&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 + /// modifying its buffers, so it is up to the caller to ensure that + /// the string is actually the specified size. + unsafe fn set_len(&mut self, new_len: uint); } impl OwnedStr for ~str { @@ -2629,7 +2626,7 @@ impl OwnedStr for ~str { c.encode_utf8(slc) }) }); - raw::set_len(self, cur_len + used); + self.set_len(cur_len + used); } } @@ -2638,7 +2635,7 @@ impl OwnedStr for ~str { let end = self.len(); assert!(end > 0u); let CharRange {ch, next} = self.char_range_at_reverse(end); - unsafe { raw::set_len(self, next); } + unsafe { self.set_len(next); } return ch; } @@ -2689,7 +2686,7 @@ impl OwnedStr for ~str { fn truncate(&mut self, len: uint) { assert!(len <= self.len()); assert!(self.is_char_boundary(len)); - unsafe { raw::set_len(self, len); } + unsafe { self.set_len(len); } } #[inline] @@ -2703,6 +2700,11 @@ impl OwnedStr for ~str { 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) + } } impl Clone for ~str { @@ -3358,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 c31e3b5644432..137a176ff41fa 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(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 { @@ -143,7 +142,7 @@ pub fn from_fn(n_elts: uint, op: |uint| -> T) -> ~[T] { i += 1u; } }).finally(|| { - raw::set_len(&mut v, i); + v.set_len(i); }); v } @@ -162,7 +161,7 @@ pub fn from_elem(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 { @@ -170,7 +169,7 @@ pub fn from_elem(n_elts: uint, t: T) -> ~[T] { i += 1u; } }).finally(|| { - raw::set_len(&mut v, i); + v.set_len(i); }); v } @@ -955,6 +954,17 @@ pub trait ImmutableVector<'a, T> { /// bounds checking. 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. * @@ -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::() == 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 { let mut base : uint = 0; let mut lim : uint = self.len(); @@ -1270,8 +1286,6 @@ pub trait ImmutableCopyableVector { * those that do not. */ fn partitioned(&self, f: |&T| -> bool) -> (~[T], ~[T]); - /// Returns the element at the given index, without doing bounds checking. - unsafe fn unsafe_get(&self, elem: uint) -> T; /// Create an iterator that yields every possible permutation of the /// vector in succession. @@ -1295,11 +1309,6 @@ impl<'a,T:Clone> ImmutableCopyableVector for &'a [T] { (lefts, rights) } - #[inline] - unsafe fn unsafe_get(&self, index: uint) -> T { - (*self.unsafe_ref(index)).clone() - } - fn permutations(self) -> Permutations { Permutations{ swaps: ElementSwaps::new(self.len()), @@ -1447,6 +1456,15 @@ pub trait OwnedVector { * value */ fn grow_fn(&mut self, n: uint, op: |uint| -> T); + + /** + * Sets the length of a vector + * + * This will explicitly set the size of the vector, without actually + * modifying its buffers, so it is up to the caller to ensure that + * the vector is actually the specified size. + */ + unsafe fn set_len(&mut self, new_len: uint); } impl OwnedVector for ~[T] { @@ -1569,11 +1587,11 @@ impl OwnedVector 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); - raw::set_len(self, new_len); - raw::set_len(&mut rhs, 0); + self.set_len(new_len); + rhs.set_len(0); } } @@ -1583,7 +1601,7 @@ impl OwnedVector for ~[T] { ln => { let valptr = ptr::to_mut_unsafe_ptr(&mut self[ln - 1u]); unsafe { - raw::set_len(self, ln - 1u); + self.set_len(ln - 1u); Some(ptr::read_ptr(&*valptr)) } } @@ -1623,7 +1641,7 @@ impl OwnedVector for ~[T] { assert!(self.capacity() >= ln); // Pretend like we have the original length so we can use // the vector copy_memory to overwrite the hole we just made - raw::set_len(self, ln); + self.set_len(ln); // Memcopy the head element (the one we want) to the location we just // popped. For the moment it unsafely exists at both the head and last @@ -1631,7 +1649,7 @@ impl OwnedVector for ~[T] { { let first_slice = self.slice(0, 1); let last_slice = self.slice(next_ln, ln); - raw::copy_memory(cast::transmute(last_slice), first_slice, 1); + raw::copy_memory(cast::transmute(last_slice), first_slice); } // Memcopy everything to the left one element @@ -1639,15 +1657,14 @@ impl OwnedVector for ~[T] { let init_slice = self.slice(0, next_ln); let tail_slice = self.slice(1, ln); raw::copy_memory(cast::transmute(init_slice), - tail_slice, - next_ln); + tail_slice); } // Set the new length. Now the vector is back to normal - raw::set_len(self, next_ln); + 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)) @@ -1700,7 +1717,7 @@ impl OwnedVector for ~[T] { } } }); - unsafe { raw::set_len(self, newlen); } + unsafe { self.set_len(newlen); } } fn retain(&mut self, f: |t: &T| -> bool) { @@ -1744,6 +1761,16 @@ impl OwnedVector for ~[T] { i += 1u; } } + #[inline] + unsafe fn set_len(&mut self, new_len: uint) { + if owns_managed::() { + let repr: **mut Box> = cast::transmute(self); + (**repr).data.fill = new_len * mem::nonzero_size_of::(); + } else { + let repr: **mut Vec<()> = cast::transmute(self); + (**repr).fill = new_len * mem::nonzero_size_of::(); + } + } } impl Mutable for ~[T] { @@ -1889,7 +1916,7 @@ impl OwnedEqVector 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; @@ -2027,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); @@ -2072,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::() == 0 { VecMutIterator{ptr: p, end: (p as uint + self.len()) as *mut T, @@ -2148,6 +2186,11 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] { ptr::mut_offset(self.repr().data as *mut T, index as int) } + #[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; @@ -2193,52 +2236,11 @@ pub unsafe fn from_buf(ptr: *T, elts: uint) -> ~[T] { /// Unsafe operations pub mod raw { use cast; - use clone::Clone; use option::Some; use ptr; - use mem; use unstable::intrinsics; use vec::{with_capacity, ImmutableVector, MutableVector}; - use unstable::raw::{Box, Vec, Slice}; - use unstable::intrinsics::owns_managed; - - /** - * Sets the length of a vector - * - * This will explicitly set the size of the vector, without actually - * modifying its buffers, so it is up to the caller to ensure that - * the vector is actually the specified size. - */ - #[inline] - pub unsafe fn set_len(v: &mut ~[T], new_len: uint) { - if owns_managed::() { - let repr: **mut Box> = cast::transmute(v); - (**repr).data.fill = new_len * mem::nonzero_size_of::(); - } else { - let repr: **mut Vec<()> = cast::transmute(v); - (**repr).fill = new_len * mem::nonzero_size_of::(); - } - } - - /** - * 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(v: &[T]) -> *T { - v.repr().data - } - - /** see `to_ptr()` */ - #[inline] - pub fn to_mut_ptr(v: &mut [T]) -> *mut T { - v.repr().data as *mut T - } + use unstable::raw::Slice; /** * Form a slice from a pointer and length (as a number of units, @@ -2270,14 +2272,6 @@ pub mod raw { })) } - /** - * Unchecked vector indexing. - */ - #[inline] - pub unsafe fn get(v: &[T], i: uint) -> T { - v.as_imm_buf(|p, _len| (*ptr::offset(p, i as int)).clone()) - } - /** * Unchecked vector index assignment. Does not drop the * old value and hence is only suitable when the vector @@ -2304,7 +2298,7 @@ pub mod raw { #[inline] pub unsafe fn from_buf_raw(ptr: *T, elts: uint) -> ~[T] { let mut dst = with_capacity(elts); - set_len(&mut dst, elts); + dst.set_len(elts); dst.as_mut_buf(|p_dst, _len_dst| ptr::copy_memory(p_dst, ptr, elts)); dst } @@ -2312,18 +2306,14 @@ pub mod raw { /** * Copies data from one vector to another. * - * Copies `count` bytes from `src` to `dst`. The source and destination - * may overlap. + * Copies `src` to `dst`. The source and destination may overlap. */ #[inline] - pub unsafe fn copy_memory(dst: &mut [T], src: &[T], - count: uint) { - assert!(dst.len() >= count); - assert!(src.len() >= count); - - dst.as_mut_buf(|p_dst, _len_dst| { - src.as_imm_buf(|p_src, _len_src| { - ptr::copy_memory(p_dst, p_src, count) + pub unsafe fn copy_memory(dst: &mut [T], src: &[T]) { + dst.as_mut_buf(|p_dst, len_dst| { + src.as_imm_buf(|p_src, len_src| { + assert!(len_dst >= len_src) + ptr::copy_memory(p_dst, p_src, len_src) }) }) } @@ -2354,15 +2344,12 @@ pub mod raw { } } -/// Operations on `[u8]` +/// Operations on `[u8]`. pub mod bytes { - use libc; - use num; use vec::raw; - use vec; use ptr; - /// A trait for operations on mutable operations on `[u8]` + /// A trait for operations on mutable `[u8]`s. pub trait MutableByteVector { /// Sets all bytes of the receiver to the given value. fn set_memory(self, value: u8); @@ -2377,59 +2364,21 @@ pub mod bytes { } } - /// Bytewise string comparison - pub fn memcmp(a: &~[u8], b: &~[u8]) -> int { - let a_len = a.len(); - let b_len = b.len(); - let n = num::min(a_len, b_len) as libc::size_t; - let r = unsafe { - libc::memcmp(raw::to_ptr(*a) as *libc::c_void, - raw::to_ptr(*b) as *libc::c_void, n) as int - }; - - if r != 0 { r } else { - if a_len == b_len { - 0 - } else if a_len < b_len { - -1 - } else { - 1 - } - } - } - - /// Bytewise less than or equal - pub fn lt(a: &~[u8], b: &~[u8]) -> bool { memcmp(a, b) < 0 } - - /// Bytewise less than or equal - pub fn le(a: &~[u8], b: &~[u8]) -> bool { memcmp(a, b) <= 0 } - - /// Bytewise equality - pub fn eq(a: &~[u8], b: &~[u8]) -> bool { memcmp(a, b) == 0 } - - /// Bytewise inequality - pub fn ne(a: &~[u8], b: &~[u8]) -> bool { memcmp(a, b) != 0 } - - /// Bytewise greater than or equal - pub fn ge(a: &~[u8], b: &~[u8]) -> bool { memcmp(a, b) >= 0 } - - /// Bytewise greater than - pub fn gt(a: &~[u8], b: &~[u8]) -> bool { memcmp(a, b) > 0 } - /** * Copies data from one vector to another. * - * Copies `count` bytes from `src` to `dst`. The source and destination - * may overlap. + * Copies `src` to `dst`. The source and destination may + * overlap. Fails if the length of `dst` is less than the length + * of `src`. */ #[inline] - pub fn copy_memory(dst: &mut [u8], src: &[u8], count: uint) { + pub fn copy_memory(dst: &mut [u8], src: &[u8]) { // Bound checks are done at vec::raw::copy_memory. - unsafe { vec::raw::copy_memory(dst, src, count) } + unsafe { raw::copy_memory(dst, src) } } /** - * Allocate space in `dst` and append the data in `src`. + * Allocate space in `dst` and append the data to `src`. */ #[inline] pub fn push_bytes(dst: &mut ~[u8], src: &[u8]) { @@ -2441,7 +2390,7 @@ pub mod bytes { ptr::copy_memory(p_dst.offset(len_dst as int), p_src, len_src) }) }); - vec::raw::set_len(dst, old_len + src.len()); + dst.set_len(old_len + src.len()); } } } @@ -2817,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); @@ -2826,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); @@ -3651,7 +3600,7 @@ mod tests { unsafe { let mut a = [1, 2, 3, 4]; let b = [1, 2, 3, 4, 5]; - raw::copy_memory(a, b, 5); + raw::copy_memory(a, b); } } @@ -4353,9 +4302,9 @@ 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); - vec::raw::set_len(&mut v, 1024); + v.set_len(1024); } }); } @@ -4374,7 +4323,7 @@ mod bench { bh.iter(|| { let mut v: ~[u8] = vec::with_capacity(1024); unsafe { - vec::raw::set_len(&mut v, 1024); + v.set_len(1024); } for i in range(0, 1024) { v[i] = 0; @@ -4387,7 +4336,7 @@ mod bench { bh.iter(|| { let mut v: ~[u8] = vec::with_capacity(1024); unsafe { - vec::raw::set_len(&mut v, 1024); + v.set_len(1024); } for x in v.mut_iter() { *x = 0; diff --git a/src/test/bench/shootout-fannkuch-redux.rs b/src/test/bench/shootout-fannkuch-redux.rs index 079ec7c50bbc2..ea57eae22b8e3 100644 --- a/src/test/bench/shootout-fannkuch-redux.rs +++ b/src/test/bench/shootout-fannkuch-redux.rs @@ -37,7 +37,7 @@ fn fannkuch_redux(n: i32) -> i32 { let mut flips_count: i32 = 0; let mut k: i32; loop { - k = perm.unsafe_get(0); + k = *perm.unsafe_ref(0); if k == 0 { break; } @@ -45,8 +45,8 @@ fn fannkuch_redux(n: i32) -> i32 { let k2 = (k+1) >> 1; for i in range(0i32, k2) { let (perm_i, perm_k_i) = { - (perm.unsafe_get(i as uint), - perm.unsafe_get((k-i) as uint)) + (*perm.unsafe_ref(i as uint), + *perm.unsafe_ref((k-i) as uint)) }; perm.unsafe_set(i as uint, perm_k_i); perm.unsafe_set((k-i) as uint, perm_i); @@ -72,15 +72,15 @@ fn fannkuch_redux(n: i32) -> i32 { let mut i: i32 = 0; while i < r { let j = i + 1; - let perm1_j = { perm1.unsafe_get(j as uint) }; + let perm1_j = { *perm1.unsafe_ref(j as uint) }; perm1.unsafe_set(i as uint, perm1_j); i = j; } perm1.unsafe_set(r as uint, perm0); - let count_r = { count.unsafe_get(r as uint) }; + let count_r = { *count.unsafe_ref(r as uint) }; count.unsafe_set(r as uint, count_r - 1); - if count.unsafe_get(r as uint) > 0 { + if *count.unsafe_ref(r as uint) > 0 { break; } r += 1; diff --git a/src/test/bench/shootout-fasta-redux.rs b/src/test/bench/shootout-fasta-redux.rs index 11274905a4ea6..d0f91a358a70c 100644 --- a/src/test/bench/shootout-fasta-redux.rs +++ b/src/test/bench/shootout-fasta-redux.rs @@ -95,11 +95,10 @@ impl RepeatFasta { let mut buf = vec::from_elem(alu_len + LINE_LEN, 0u8); let alu: &[u8] = self.alu.as_bytes(); - copy_memory(buf, alu, alu_len); + copy_memory(buf, alu); let buf_len = buf.len(); copy_memory(buf.mut_slice(alu_len, buf_len), - alu, - LINE_LEN); + alu.slice_to(LINE_LEN)); let mut pos = 0; let mut bytes;