Use .copy_from_slice() where applicable

.copy_from_slice() does the same job of .clone_from_slice(), but the
former is explicitly for Copy elements and calls `memcpy` directly, and
thus is it efficient without optimization too.
This commit is contained in:
Ulrik Sverdrup 2016-02-23 19:25:43 +01:00
parent 09130044ce
commit 2d6496dd84
13 changed files with 23 additions and 18 deletions

View file

@ -256,7 +256,7 @@ impl Write for Cursor<Vec<u8>> {
let pos = pos as usize;
let space = self.inner.len() - pos;
let (left, right) = buf.split_at(cmp::min(space, buf.len()));
self.inner[pos..pos + left.len()].clone_from_slice(left);
self.inner[pos..pos + left.len()].copy_from_slice(left);
self.inner.extend_from_slice(right);
}

View file

@ -156,7 +156,7 @@ impl<'a> Read for &'a [u8] {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let amt = cmp::min(buf.len(), self.len());
let (a, b) = self.split_at(amt);
buf[..amt].clone_from_slice(a);
buf[..amt].copy_from_slice(a);
*self = b;
Ok(amt)
}
@ -168,7 +168,7 @@ impl<'a> Read for &'a [u8] {
"failed to fill whole buffer"));
}
let (a, b) = self.split_at(buf.len());
buf.clone_from_slice(a);
buf.copy_from_slice(a);
*self = b;
Ok(())
}
@ -189,7 +189,7 @@ impl<'a> Write for &'a mut [u8] {
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
let amt = cmp::min(data.len(), self.len());
let (a, b) = mem::replace(self, &mut []).split_at_mut(amt);
a.clone_from_slice(&data[..amt]);
a.copy_from_slice(&data[..amt]);
*self = b;
Ok(amt)
}

View file

@ -222,6 +222,7 @@
#![feature(collections)]
#![feature(collections_bound)]
#![feature(const_fn)]
#![feature(copy_from_slice)]
#![feature(core_float)]
#![feature(core_intrinsics)]
#![feature(decode_utf16)]

View file

@ -191,8 +191,8 @@ impl<'a> Parser<'a> {
fn ipv6_addr_from_head_tail(head: &[u16], tail: &[u16]) -> Ipv6Addr {
assert!(head.len() + tail.len() <= 8);
let mut gs = [0; 8];
gs[..head.len()].clone_from_slice(head);
gs[(8 - tail.len()) .. 8].clone_from_slice(tail);
gs[..head.len()].copy_from_slice(head);
gs[(8 - tail.len()) .. 8].copy_from_slice(tail);
Ipv6Addr::new(gs[0], gs[1], gs[2], gs[3], gs[4], gs[5], gs[6], gs[7])
}

View file

@ -341,7 +341,7 @@ impl Wtf8Buf {
Some((surrogate_pos, _)) => {
pos = surrogate_pos + 3;
self.bytes[surrogate_pos..pos]
.clone_from_slice(UTF8_REPLACEMENT_CHARACTER);
.copy_from_slice(UTF8_REPLACEMENT_CHARACTER);
},
None => return unsafe { String::from_utf8_unchecked(self.bytes) }
}