diff options
| author | bors <bors@rust-lang.org> | 2013-12-15 06:56:27 -0800 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-12-15 06:56:27 -0800 |
| commit | 8d52dfbace05c46754f4f6bb5a25f55906c9d7b0 (patch) | |
| tree | 2fe29e4faba8034176fe5c1f14bd2659ca0c7235 /src/libstd | |
| parent | ef7969e86f0b53e4236ca627b31ac09413c07b82 (diff) | |
| parent | 164f7a290ef98377e4c8c158eca2fbed098a2842 (diff) | |
| download | rust-8d52dfbace05c46754f4f6bb5a25f55906c9d7b0.tar.gz rust-8d52dfbace05c46754f4f6bb5a25f55906c9d7b0.zip | |
auto merge of #10984 : huonw/rust/clean-raw, r=cmr
See commits for details.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/c_str.rs | 4 | ||||
| -rw-r--r-- | src/libstd/io/buffered.rs | 8 | ||||
| -rw-r--r-- | src/libstd/io/comm_adapters.rs | 2 | ||||
| -rw-r--r-- | src/libstd/io/mem.rs | 10 | ||||
| -rw-r--r-- | src/libstd/io/mod.rs | 4 | ||||
| -rw-r--r-- | src/libstd/io/native/file.rs | 8 | ||||
| -rw-r--r-- | src/libstd/os.rs | 8 | ||||
| -rw-r--r-- | src/libstd/ptr.rs | 26 | ||||
| -rw-r--r-- | src/libstd/rand/isaac.rs | 12 | ||||
| -rw-r--r-- | src/libstd/rt/stack.rs | 6 | ||||
| -rw-r--r-- | src/libstd/str.rs | 42 | ||||
| -rw-r--r-- | src/libstd/vec.rs | 239 |
12 files changed, 158 insertions, 211 deletions
diff --git a/src/libstd/c_str.rs b/src/libstd/c_str.rs index 1563b7f99d1..1ba6b7b50ca 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<T>(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 365866872ec..95c313dd282 100644 --- a/src/libstd/io/buffered.rs +++ b/src/libstd/io/buffered.rs @@ -79,7 +79,7 @@ impl<R: Reader> BufferedReader<R> { // 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<R: Reader> Reader for BufferedReader<R> { 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<W: Writer> BufferedWriter<W> { pub fn with_capacity(cap: uint, inner: W) -> BufferedWriter<W> { // 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<W: Writer> Writer for BufferedWriter<W> { 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 a53146f0091..b3e5a9a0c86 100644 --- a/src/libstd/io/comm_adapters.rs +++ b/src/libstd/io/comm_adapters.rs @@ -57,7 +57,7 @@ impl<P: GenericPort<~[u8]>> Reader for PortReader<P> { 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 b6778d89c33..efb55a436ea 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 1024f28f379..c0bdc2a2014 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 7b5104657d9..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,13 +700,13 @@ 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()), 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 bcd353bab7a..7abeb34a010 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -358,18 +358,18 @@ 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; } 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<Path> { _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 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 3dcf97212f5..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); @@ -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 4358390da9f..44b60e955d2 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 caf0b535e05..f1310765130 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -992,7 +992,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; @@ -1001,7 +1001,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) @@ -1108,7 +1108,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; } @@ -1132,23 +1132,13 @@ pub mod raw { /// 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 /// modifing its buffers, so it is up to the caller to ensure that /// the string is actually the specified size. #[test] 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"); } @@ -1338,7 +1328,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<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 + /// 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 fbfd9accd0d..1b238ede049 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 { @@ -143,7 +142,7 @@ pub fn from_fn<T>(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<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 { @@ -170,7 +169,7 @@ pub fn from_elem<T:Clone>(n_elts: uint, t: T) -> ~[T] { i += 1u; } }).finally(|| { - raw::set_len(&mut v, i); + v.set_len(i); }); v } @@ -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 function should implement an order consistent @@ -1044,7 +1054,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, @@ -1157,6 +1167,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(); @@ -1271,8 +1287,6 @@ pub trait ImmutableCopyableVector<T> { * 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. @@ -1296,11 +1310,6 @@ impl<'a,T:Clone> ImmutableCopyableVector<T> for &'a [T] { (lefts, rights) } - #[inline] - unsafe fn unsafe_get(&self, index: uint) -> T { - (*self.unsafe_ref(index)).clone() - } - fn permutations(self) -> Permutations<T> { Permutations{ swaps: ElementSwaps::new(self.len()), @@ -1448,6 +1457,15 @@ pub trait OwnedVector<T> { * 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<T> OwnedVector<T> for ~[T] { @@ -1570,11 +1588,11 @@ 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); - raw::set_len(self, new_len); - raw::set_len(&mut rhs, 0); + self.set_len(new_len); + rhs.set_len(0); } } @@ -1584,7 +1602,7 @@ impl<T> OwnedVector<T> 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)) } } @@ -1624,7 +1642,7 @@ impl<T> OwnedVector<T> 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 @@ -1632,7 +1650,7 @@ impl<T> OwnedVector<T> 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 @@ -1640,15 +1658,14 @@ impl<T> OwnedVector<T> 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)) @@ -1701,7 +1718,7 @@ impl<T> OwnedVector<T> for ~[T] { } } }); - unsafe { raw::set_len(self, newlen); } + unsafe { self.set_len(newlen); } } fn retain(&mut self, f: |t: &T| -> bool) { @@ -1745,6 +1762,16 @@ impl<T> OwnedVector<T> for ~[T] { i += 1u; } } + #[inline] + unsafe fn set_len(&mut self, new_len: uint) { + if owns_managed::<T>() { + let repr: **mut Box<Vec<()>> = cast::transmute(self); + (**repr).data.fill = new_len * mem::nonzero_size_of::<T>(); + } else { + let repr: **mut Vec<()> = cast::transmute(self); + (**repr).fill = new_len * mem::nonzero_size_of::<T>(); + } + } } impl<T> Mutable for ~[T] { @@ -1890,7 +1917,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; @@ -2028,6 +2055,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); @@ -2073,7 +2111,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, @@ -2150,6 +2188,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; } @@ -2194,52 +2237,11 @@ pub unsafe fn from_buf<T>(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<T>(v: &mut ~[T], new_len: uint) { - if owns_managed::<T>() { - let repr: **mut Box<Vec<()>> = cast::transmute(v); - (**repr).data.fill = new_len * mem::nonzero_size_of::<T>(); - } else { - let repr: **mut Vec<()> = cast::transmute(v); - (**repr).fill = new_len * mem::nonzero_size_of::<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. - */ - #[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 - } + use unstable::raw::Slice; /** * Form a slice from a pointer and length (as a number of units, @@ -2272,14 +2274,6 @@ pub mod raw { } /** - * Unchecked vector indexing. - */ - #[inline] - pub unsafe fn get<T:Clone>(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 * is newly allocated. @@ -2305,7 +2299,7 @@ pub mod raw { #[inline] pub unsafe fn from_buf_raw<T>(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 } @@ -2313,18 +2307,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<T>(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<T>(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) }) }) } @@ -2355,15 +2345,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); @@ -2378,59 +2365,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]) { @@ -2442,7 +2391,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()); } } } @@ -2818,7 +2767,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); @@ -2827,7 +2776,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); @@ -3652,7 +3601,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); } } @@ -4354,9 +4303,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); } }); } @@ -4375,7 +4324,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; @@ -4388,7 +4337,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; |
