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 /src/libstd | |
| parent | f53292f7ee7365fe50ac216efac438ff5569fd06 (diff) | |
| download | rust-164f7a290ef98377e4c8c158eca2fbed098a2842.tar.gz rust-164f7a290ef98377e4c8c158eca2fbed098a2842.zip | |
std::vec: convert to(_mut)_ptr to as_... methods on &[] and &mut [].
Diffstat (limited to 'src/libstd')
| -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 |
8 files changed, 68 insertions, 59 deletions
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); } |
