about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorblake2-ppc <blake2-ppc>2013-09-11 00:52:46 +0200
committerblake2-ppc <blake2-ppc>2013-09-16 19:13:41 +0200
commit8fce135326707264d99102cdb514b00d6aa38081 (patch)
treef2764ef86a6ca3d83100b1d1fd238d9159d3755a /src
parent8d488f38ed4fad414f9a693b6f4574dfa92b06fd (diff)
downloadrust-8fce135326707264d99102cdb514b00d6aa38081.tar.gz
rust-8fce135326707264d99102cdb514b00d6aa38081.zip
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.
Diffstat (limited to 'src')
-rw-r--r--src/libstd/vec.rs17
1 files changed, 17 insertions, 0 deletions
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<A: Clone> Clone for ~[A] {