std::vec: remove unnecessary count parameter on {bytes,

raw}::copy_memory.

Slices carry their length with them, so we can just use that
information.
This commit is contained in:
Huon Wilson
2013-12-15 22:23:11 +11:00
parent 8f6df87c1d
commit f97040a93b
9 changed files with 33 additions and 52 deletions

View File

@@ -175,7 +175,7 @@ impl Uuid {
pub fn new_v4() -> Uuid { pub fn new_v4() -> Uuid {
let ub = rand::task_rng().gen_vec(16); let ub = rand::task_rng().gen_vec(16);
let mut uuid = Uuid{ bytes: [0, .. 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_variant(VariantRFC4122);
uuid.set_version(Version4Random); uuid.set_version(Version4Random);
uuid uuid
@@ -202,7 +202,7 @@ impl Uuid {
fields.data1 = to_be32(d1 as i32) as u32; fields.data1 = to_be32(d1 as i32) as u32;
fields.data2 = to_be16(d2 as i16) as u16; fields.data2 = to_be16(d2 as i16) as u16;
fields.data3 = to_be16(d3 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 { unsafe {
transmute(fields) transmute(fields)
@@ -220,7 +220,7 @@ impl Uuid {
let mut uuid = Uuid{ bytes: [0, .. 16] }; let mut uuid = Uuid{ bytes: [0, .. 16] };
unsafe { unsafe {
vec::raw::copy_memory(uuid.bytes, b, 16); vec::raw::copy_memory(uuid.bytes, b);
} }
Some(uuid) Some(uuid)
} }
@@ -442,11 +442,7 @@ impl Zero for Uuid {
impl Clone for Uuid { impl Clone for Uuid {
/// Returns a copy of the UUID /// Returns a copy of the UUID
fn clone(&self) -> Uuid { fn clone(&self) -> Uuid { *self }
let mut clone = Uuid{ bytes: [0, .. 16] };
vec::bytes::copy_memory(clone.bytes, self.bytes, 16);
clone
}
} }
impl FromStr for Uuid { impl FromStr for Uuid {
@@ -509,7 +505,7 @@ impl rand::Rand for Uuid {
fn rand<R: rand::Rng>(rng: &mut R) -> Uuid { fn rand<R: rand::Rng>(rng: &mut R) -> Uuid {
let ub = rng.gen_vec(16); let ub = rng.gen_vec(16);
let mut uuid = Uuid{ bytes: [0, .. 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_variant(VariantRFC4122);
uuid.set_version(Version4Random); uuid.set_version(Version4Random);
uuid uuid

View File

@@ -136,16 +136,14 @@ impl FixedBuffer for FixedBuffer64 {
if input.len() >= buffer_remaining { if input.len() >= buffer_remaining {
copy_memory( copy_memory(
self.buffer.mut_slice(self.buffer_idx, size), self.buffer.mut_slice(self.buffer_idx, size),
input.slice_to(buffer_remaining), input.slice_to(buffer_remaining));
buffer_remaining);
self.buffer_idx = 0; self.buffer_idx = 0;
func(self.buffer); func(self.buffer);
i += buffer_remaining; i += buffer_remaining;
} else { } else {
copy_memory( copy_memory(
self.buffer.mut_slice(self.buffer_idx, self.buffer_idx + input.len()), self.buffer.mut_slice(self.buffer_idx, self.buffer_idx + input.len()),
input, input);
input.len());
self.buffer_idx += input.len(); self.buffer_idx += input.len();
return; return;
} }
@@ -164,8 +162,7 @@ impl FixedBuffer for FixedBuffer64 {
let input_remaining = input.len() - i; let input_remaining = input.len() - i;
copy_memory( copy_memory(
self.buffer.mut_slice(0, input_remaining), self.buffer.mut_slice(0, input_remaining),
input.slice_from(i), input.slice_from(i));
input.len() - i);
self.buffer_idx += input_remaining; self.buffer_idx += input_remaining;
} }

View File

@@ -149,16 +149,14 @@ impl FixedBuffer for FixedBuffer64 {
if input.len() >= buffer_remaining { if input.len() >= buffer_remaining {
copy_memory( copy_memory(
self.buffer.mut_slice(self.buffer_idx, size), self.buffer.mut_slice(self.buffer_idx, size),
input.slice_to(buffer_remaining), input.slice_to(buffer_remaining));
buffer_remaining);
self.buffer_idx = 0; self.buffer_idx = 0;
func(self.buffer); func(self.buffer);
i += buffer_remaining; i += buffer_remaining;
} else { } else {
copy_memory( copy_memory(
self.buffer.mut_slice(self.buffer_idx, self.buffer_idx + input.len()), self.buffer.mut_slice(self.buffer_idx, self.buffer_idx + input.len()),
input, input);
input.len());
self.buffer_idx += input.len(); self.buffer_idx += input.len();
return; return;
} }
@@ -177,8 +175,7 @@ impl FixedBuffer for FixedBuffer64 {
let input_remaining = input.len() - i; let input_remaining = input.len() - i;
copy_memory( copy_memory(
self.buffer.mut_slice(0, input_remaining), self.buffer.mut_slice(0, input_remaining),
input.slice_from(i), input.slice_from(i));
input.len() - i);
self.buffer_idx += input_remaining; self.buffer_idx += input_remaining;
} }

View File

@@ -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 { unsafe fn with_c_str<T>(v: &[u8], checked: bool, f: |*libc::c_char| -> T) -> T {
if v.len() < BUF_LEN { if v.len() < BUF_LEN {
let mut buf: [u8, .. BUF_LEN] = intrinsics::uninit(); 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[v.len()] = 0;
buf.as_mut_buf(|buf, _| { buf.as_mut_buf(|buf, _| {

View File

@@ -122,7 +122,7 @@ impl<R: Reader> Reader for BufferedReader<R> {
return None; return None;
} }
let nread = num::min(available.len(), buf.len()); 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 nread
}; };
self.pos += nread; self.pos += nread;
@@ -185,7 +185,7 @@ impl<W: Writer> Writer for BufferedWriter<W> {
self.inner.write(buf); self.inner.write(buf);
} else { } else {
let dst = self.buf.mut_slice_from(self.pos); 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(); self.pos += buf.len();
} }
} }

View File

@@ -57,7 +57,7 @@ impl<P: GenericPort<~[u8]>> Reader for PortReader<P> {
let dst = buf.mut_slice_from(num_read); let dst = buf.mut_slice_from(num_read);
let src = prev.slice_from(self.pos); let src = prev.slice_from(self.pos);
let count = cmp::min(dst.len(), src.len()); 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; num_read += count;
self.pos += count; self.pos += count;
}, },

View File

@@ -58,8 +58,7 @@ impl Writer for MemWriter {
// Do the necessary writes // Do the necessary writes
if left.len() > 0 { if left.len() > 0 {
vec::bytes::copy_memory(self.buf.mut_slice_from(self.pos), vec::bytes::copy_memory(self.buf.mut_slice_from(self.pos), left);
left, left.len());
} }
if right.len() > 0 { if right.len() > 0 {
self.buf.push_all(right); 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 input = self.buf.slice(self.pos, self.pos + write_len);
let output = buf.mut_slice(0, write_len); let output = buf.mut_slice(0, write_len);
assert_eq!(input.len(), output.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; self.pos += write_len;
assert!(self.pos <= self.buf.len()); assert!(self.pos <= self.buf.len());
@@ -175,8 +174,7 @@ impl<'a> Writer for BufWriter<'a> {
return; return;
} }
vec::bytes::copy_memory(self.buf.mut_slice_from(self.pos), vec::bytes::copy_memory(self.buf.mut_slice_from(self.pos), buf);
buf, buf.len());
self.pos += buf.len(); 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 input = self.buf.slice(self.pos, self.pos + write_len);
let output = buf.mut_slice(0, write_len); let output = buf.mut_slice(0, write_len);
assert_eq!(input.len(), output.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; self.pos += write_len;
assert!(self.pos <= self.buf.len()); assert!(self.pos <= self.buf.len());

View File

@@ -1631,7 +1631,7 @@ impl<T> OwnedVector<T> for ~[T] {
{ {
let first_slice = self.slice(0, 1); let first_slice = self.slice(0, 1);
let last_slice = self.slice(next_ln, ln); 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 // Memcopy everything to the left one element
@@ -1639,8 +1639,7 @@ impl<T> OwnedVector<T> for ~[T] {
let init_slice = self.slice(0, next_ln); let init_slice = self.slice(0, next_ln);
let tail_slice = self.slice(1, ln); let tail_slice = self.slice(1, ln);
raw::copy_memory(cast::transmute(init_slice), raw::copy_memory(cast::transmute(init_slice),
tail_slice, tail_slice);
next_ln);
} }
// Set the new length. Now the vector is back to normal // Set the new length. Now the vector is back to normal
@@ -2312,18 +2311,14 @@ pub mod raw {
/** /**
* Copies data from one vector to another. * Copies data from one vector to another.
* *
* Copies `count` bytes from `src` to `dst`. The source and destination * Copies `src` to `dst`. The source and destination may overlap.
* may overlap.
*/ */
#[inline] #[inline]
pub unsafe fn copy_memory<T>(dst: &mut [T], src: &[T], pub unsafe fn copy_memory<T>(dst: &mut [T], src: &[T]) {
count: uint) { dst.as_mut_buf(|p_dst, len_dst| {
assert!(dst.len() >= count); src.as_imm_buf(|p_src, len_src| {
assert!(src.len() >= count); assert!(len_dst >= len_src)
ptr::copy_memory(p_dst, p_src, len_src)
dst.as_mut_buf(|p_dst, _len_dst| {
src.as_imm_buf(|p_src, _len_src| {
ptr::copy_memory(p_dst, p_src, count)
}) })
}) })
} }
@@ -2419,13 +2414,12 @@ pub mod bytes {
/** /**
* Copies data from one vector to another. * Copies data from one vector to another.
* *
* Copies `count` bytes from `src` to `dst`. The source and destination * Copies `src` to `dst`. The source and destination may overlap.
* may overlap.
*/ */
#[inline] #[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. // Bound checks are done at vec::raw::copy_memory.
unsafe { vec::raw::copy_memory(dst, src, count) } unsafe { vec::raw::copy_memory(dst, src) }
} }
/** /**
@@ -3651,7 +3645,7 @@ mod tests {
unsafe { unsafe {
let mut a = [1, 2, 3, 4]; let mut a = [1, 2, 3, 4];
let b = [1, 2, 3, 4, 5]; let b = [1, 2, 3, 4, 5];
raw::copy_memory(a, b, 5); raw::copy_memory(a, b);
} }
} }

View File

@@ -95,11 +95,10 @@ impl RepeatFasta {
let mut buf = vec::from_elem(alu_len + LINE_LEN, 0u8); let mut buf = vec::from_elem(alu_len + LINE_LEN, 0u8);
let alu: &[u8] = self.alu.as_bytes(); let alu: &[u8] = self.alu.as_bytes();
copy_memory(buf, alu, alu_len); copy_memory(buf, alu);
let buf_len = buf.len(); let buf_len = buf.len();
copy_memory(buf.mut_slice(alu_len, buf_len), copy_memory(buf.mut_slice(alu_len, buf_len),
alu, alu.slice_to(LINE_LEN));
LINE_LEN);
let mut pos = 0; let mut pos = 0;
let mut bytes; let mut bytes;