From 8fce135326707264d99102cdb514b00d6aa38081 Mon Sep 17 00:00:00 2001 From: blake2-ppc Date: Wed, 11 Sep 2013 00:52:46 +0200 Subject: 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. --- src/libstd/vec.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/libstd') diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 50f194e1f4c..b7274d58e1b 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -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 Clone for ~[A] { -- cgit 1.4.1-3-g733a5