diff options
| author | Huon Wilson <dbau.pp+github@gmail.com> | 2013-12-18 01:49:31 +1100 |
|---|---|---|
| committer | Huon Wilson <dbau.pp+github@gmail.com> | 2013-12-19 09:26:13 +1100 |
| commit | 4c79b22ef26a2b846d84c46bc8fea50c953559dd (patch) | |
| tree | 195c5edab1e773dcd7db4454b6d848a47d5a6249 /src/libstd | |
| parent | ac137f6dbe51429de203ea2e900b8bac01f466cb (diff) | |
| download | rust-4c79b22ef26a2b846d84c46bc8fea50c953559dd.tar.gz rust-4c79b22ef26a2b846d84c46bc8fea50c953559dd.zip | |
std::vec: remove .as_imm_buf, replaced by .as_ptr & .len.
There's no need for the restrictions of a closure with the above methods.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/c_str.rs | 17 | ||||
| -rw-r--r-- | src/libstd/io/native/file.rs | 4 | ||||
| -rw-r--r-- | src/libstd/io/native/process.rs | 9 | ||||
| -rw-r--r-- | src/libstd/os.rs | 2 | ||||
| -rw-r--r-- | src/libstd/ptr.rs | 68 | ||||
| -rw-r--r-- | src/libstd/str.rs | 2 | ||||
| -rw-r--r-- | src/libstd/vec.rs | 48 |
7 files changed, 59 insertions, 91 deletions
diff --git a/src/libstd/c_str.rs b/src/libstd/c_str.rs index 1ba6b7b50ca..2f11e93d839 100644 --- a/src/libstd/c_str.rs +++ b/src/libstd/c_str.rs @@ -267,17 +267,16 @@ impl<'a> ToCStr for &'a [u8] { } unsafe fn to_c_str_unchecked(&self) -> CString { - self.as_imm_buf(|self_buf, self_len| { - let buf = libc::malloc(self_len as libc::size_t + 1) as *mut u8; - if buf.is_null() { - fail!("failed to allocate memory!"); - } + let self_len = self.len(); + let buf = libc::malloc(self_len as libc::size_t + 1) as *mut u8; + if buf.is_null() { + fail!("failed to allocate memory!"); + } - ptr::copy_memory(buf, self_buf, self_len); - *ptr::mut_offset(buf, self_len as int) = 0; + ptr::copy_memory(buf, self.as_ptr(), self_len); + *ptr::mut_offset(buf, self_len as int) = 0; - CString::new(buf as *libc::c_char, true) - }) + CString::new(buf as *libc::c_char, true) } fn with_c_str<T>(&self, f: |*libc::c_char| -> T) -> T { diff --git a/src/libstd/io/native/file.rs b/src/libstd/io/native/file.rs index bd618dd6f0f..74d18f11a1d 100644 --- a/src/libstd/io/native/file.rs +++ b/src/libstd/io/native/file.rs @@ -37,8 +37,8 @@ fn keep_going(data: &[u8], f: |*u8, uint| -> i64) -> i64 { #[cfg(windows)] static eintr: int = 0; // doesn't matter #[cfg(not(windows))] static eintr: int = libc::EINTR as int; - let (data, origamt) = data.as_imm_buf(|data, amt| (data, amt)); - let mut data = data; + let origamt = data.len(); + let mut data = data.as_ptr(); let mut amt = origamt; while amt > 0 { let mut ret; diff --git a/src/libstd/io/native/process.rs b/src/libstd/io/native/process.rs index a1f50b15f23..ef972dc4d0a 100644 --- a/src/libstd/io/native/process.rs +++ b/src/libstd/io/native/process.rs @@ -8,7 +8,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use cast; use io; use libc::{pid_t, c_void, c_int}; use libc; @@ -17,6 +16,8 @@ use prelude::*; use ptr; use rt::rtio; use super::file; +#[cfg(windows)] +use cast; use p = io::process; @@ -453,7 +454,7 @@ fn with_argv<T>(prog: &str, args: &[~str], cb: |**libc::c_char| -> T) -> T { // Finally, make sure we add a null pointer. ptrs.push(ptr::null()); - ptrs.as_imm_buf(|buf, _| cb(buf)) + cb(ptrs.as_ptr()) } #[cfg(unix)] @@ -476,7 +477,7 @@ fn with_envp<T>(env: Option<~[(~str, ~str)]>, cb: |*c_void| -> T) -> T { let mut ptrs = tmps.map(|tmp| tmp.with_ref(|buf| buf)); ptrs.push(ptr::null()); - ptrs.as_imm_buf(|buf, _| unsafe { cb(cast::transmute(buf)) }) + cb(ptrs.as_ptr() as *c_void) } _ => cb(ptr::null()) } @@ -499,7 +500,7 @@ fn with_envp<T>(env: Option<~[(~str, ~str)]>, cb: |*mut c_void| -> T) -> T { blk.push(0); - blk.as_imm_buf(|p, _len| unsafe { cb(cast::transmute(p)) }) + cb(blk.as_mut_ptr() as *mut c_void) } _ => cb(ptr::mut_null()) } diff --git a/src/libstd/os.rs b/src/libstd/os.rs index 7abeb34a010..c7ff4510d0d 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -130,7 +130,7 @@ pub mod win32 { let mut t = s.to_utf16(); // Null terminate before passing on. t.push(0u16); - t.as_imm_buf(|buf, _len| f(buf)) + f(t.as_ptr()) } } diff --git a/src/libstd/ptr.rs b/src/libstd/ptr.rs index 162e46c53ae..070884c078c 100644 --- a/src/libstd/ptr.rs +++ b/src/libstd/ptr.rs @@ -511,10 +511,9 @@ pub mod ptr_tests { "there".with_c_str(|p1| { "thing".with_c_str(|p2| { let v = ~[p0, p1, p2, null()]; - v.as_imm_buf(|vp, len| { - assert_eq!(unsafe { buf_len(vp) }, 3u); - assert_eq!(len, 4u); - }) + unsafe { + assert_eq!(buf_len(v.as_ptr()), 3u); + } }) }) }) @@ -623,23 +622,21 @@ pub mod ptr_tests { one, two, three ]; - arr.as_imm_buf(|arr_ptr, arr_len| { - let mut ctr = 0; - let mut iteration_count = 0; - array_each_with_len(arr_ptr, arr_len, |e| { - let actual = str::raw::from_c_str(e); - let expected = expected_arr[ctr].with_ref(|buf| { - str::raw::from_c_str(buf) - }); - debug!( - "test_ptr_array_each_with_len e: {}, a: {}", - expected, actual); - assert_eq!(actual, expected); - ctr += 1; - iteration_count += 1; + let mut ctr = 0; + let mut iteration_count = 0; + array_each_with_len(arr.as_ptr(), arr.len(), |e| { + let actual = str::raw::from_c_str(e); + let expected = expected_arr[ctr].with_ref(|buf| { + str::raw::from_c_str(buf) + }); + debug!( + "test_ptr_array_each_with_len e: {}, a: {}", + expected, actual); + assert_eq!(actual, expected); + ctr += 1; + iteration_count += 1; }); - assert_eq!(iteration_count, 3u); - }) + assert_eq!(iteration_count, 3u); } } @@ -660,23 +657,22 @@ pub mod ptr_tests { one, two, three ]; - arr.as_imm_buf(|arr_ptr, _| { - let mut ctr = 0; - let mut iteration_count = 0; - array_each(arr_ptr, |e| { - let actual = str::raw::from_c_str(e); - let expected = expected_arr[ctr].with_ref(|buf| { - str::raw::from_c_str(buf) - }); - debug!( - "test_ptr_array_each e: {}, a: {}", - expected, actual); - assert_eq!(actual, expected); - ctr += 1; - iteration_count += 1; + let arr_ptr = arr.as_ptr(); + let mut ctr = 0; + let mut iteration_count = 0; + array_each(arr_ptr, |e| { + let actual = str::raw::from_c_str(e); + let expected = expected_arr[ctr].with_ref(|buf| { + str::raw::from_c_str(buf) + }); + debug!( + "test_ptr_array_each e: {}, a: {}", + expected, actual); + assert_eq!(actual, expected); + ctr += 1; + iteration_count += 1; }); - assert_eq!(iteration_count, 3); - }) + assert_eq!(iteration_count, 3); } } diff --git a/src/libstd/str.rs b/src/libstd/str.rs index f1310765130..e785ed74159 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -2502,7 +2502,7 @@ impl<'a> StrSlice<'a> for &'a str { #[inline] fn as_imm_buf<T>(&self, f: |*u8, uint| -> T) -> T { let v: &[u8] = unsafe { cast::transmute(*self) }; - v.as_imm_buf(f) + f(v.as_ptr(), v.len()) } } diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 65be214c14e..08ab6732aec 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -775,7 +775,7 @@ impl<'a, T> Container for &'a [T] { /// Returns the length of a vector #[inline] fn len(&self) -> uint { - self.as_imm_buf(|_p, len| len) + self.repr().len } } @@ -783,7 +783,7 @@ impl<T> Container for ~[T] { /// Returns the length of a vector #[inline] fn len(&self) -> uint { - self.as_imm_buf(|_p, len| len) + self.repr().len } } @@ -985,14 +985,6 @@ pub trait ImmutableVector<'a, T> { fn map<U>(&self, |t: &T| -> U) -> ~[U]; /** - * Work with the buffer of a vector. - * - * Allows for unsafe manipulation of vector contents, which is useful for - * foreign interop. - */ - fn as_imm_buf<U>(&self, f: |*T, uint| -> U) -> U; - - /** * Returns a mutable reference to the first element in this slice * and adjusts the slice in place so that it no longer contains * that element. O(1). @@ -1032,14 +1024,12 @@ impl<'a,T> ImmutableVector<'a, T> for &'a [T] { fn slice(&self, start: uint, end: uint) -> &'a [T] { assert!(start <= end); assert!(end <= self.len()); - self.as_imm_buf(|p, _len| { - unsafe { - cast::transmute(Slice { - data: ptr::offset(p, start as int), + unsafe { + cast::transmute(Slice { + data: self.as_ptr().offset(start as int), len: (end - start) }) - } - }) + } } #[inline] @@ -1197,12 +1187,6 @@ impl<'a,T> ImmutableVector<'a, T> for &'a [T] { self.iter().map(f).collect() } - #[inline] - fn as_imm_buf<U>(&self, f: |*T, uint| -> U) -> U { - let s = self.repr(); - f(s.data, s.len) - } - fn shift_ref(&mut self) -> &'a T { unsafe { let s: &mut Slice<T> = cast::transmute(self); @@ -2206,10 +2190,9 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] { #[inline] unsafe fn copy_memory(self, src: &[T]) { self.as_mut_buf(|p_dst, len_dst| { - src.as_imm_buf(|p_src, len_src| { - assert!(len_dst >= len_src) - ptr::copy_nonoverlapping_memory(p_dst, p_src, len_src) - }) + let len_src = src.len(); + assert!(len_dst >= len_src); + ptr::copy_nonoverlapping_memory(p_dst, src.as_ptr(), len_src) }) } @@ -2369,9 +2352,7 @@ pub mod bytes { dst.reserve_additional(src.len()); unsafe { dst.as_mut_buf(|p_dst, len_dst| { - src.as_imm_buf(|p_src, len_src| { - ptr::copy_memory(p_dst.offset(len_dst as int), p_src, len_src) - }) + ptr::copy_memory(p_dst.offset(len_dst as int), src.as_ptr(), src.len()) }); dst.set_len(old_len + src.len()); } @@ -3555,15 +3536,6 @@ mod tests { #[test] #[should_fail] - fn test_as_imm_buf_fail() { - let v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)]; - v.as_imm_buf(|_buf, _i| { - fail!() - }) - } - - #[test] - #[should_fail] fn test_as_mut_buf_fail() { let mut v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)]; v.as_mut_buf(|_buf, _i| { |
