Skip to content

Commit 8fce135

Browse files
author
blake2-ppc
committed
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.
1 parent 8d488f3 commit 8fce135

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/libstd/vec.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2244,6 +2244,23 @@ pub mod bytes {
22442244
// Bound checks are done at vec::raw::copy_memory.
22452245
unsafe { vec::raw::copy_memory(dst, src, count) }
22462246
}
2247+
2248+
/**
2249+
* Allocate space in `dst` and append the data in `src`.
2250+
*/
2251+
#[inline]
2252+
pub fn push_bytes(dst: &mut ~[u8], src: &[u8]) {
2253+
let old_len = dst.len();
2254+
dst.reserve_additional(src.len());
2255+
unsafe {
2256+
do dst.as_mut_buf |p_dst, len_dst| {
2257+
do src.as_imm_buf |p_src, len_src| {
2258+
ptr::copy_memory(p_dst.offset(len_dst as int), p_src, len_src)
2259+
}
2260+
}
2261+
vec::raw::set_len(dst, old_len + src.len());
2262+
}
2263+
}
22472264
}
22482265

22492266
impl<A: Clone> Clone for ~[A] {

0 commit comments

Comments
 (0)