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.
This commit is contained in:
Huon Wilson
2013-12-18 01:49:31 +11:00
parent ac137f6dbe
commit 4c79b22ef2
16 changed files with 119 additions and 167 deletions

View File

@@ -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
}
}
@@ -984,14 +984,6 @@ pub trait ImmutableVector<'a, T> {
/// of a vector and return the results.
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
@@ -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());
}
@@ -3553,15 +3534,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() {