std::vec: Add function vec::bytes::push_bytes

`push_bytes` is implemented with `ptr::copy_memory` here since this
function is intended to be used to implement `.push_str()` for str, so
we want to avoid the overhead.
This commit is contained in:
blake2-ppc 2013-09-11 00:52:46 +02:00
parent 8d488f38ed
commit 8fce135326

View File

@ -2244,6 +2244,23 @@ pub mod bytes {
// Bound checks are done at vec::raw::copy_memory.
unsafe { vec::raw::copy_memory(dst, src, count) }
}
/**
* Allocate space in `dst` and append the data in `src`.
*/
#[inline]
pub fn push_bytes(dst: &mut ~[u8], src: &[u8]) {
let old_len = dst.len();
dst.reserve_additional(src.len());
unsafe {
do dst.as_mut_buf |p_dst, len_dst| {
do src.as_imm_buf |p_src, len_src| {
ptr::copy_memory(p_dst.offset(len_dst as int), p_src, len_src)
}
}
vec::raw::set_len(dst, old_len + src.len());
}
}
}
impl<A: Clone> Clone for ~[A] {