about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorKevin Ballard <kevin@sb.org>2014-05-03 18:20:35 -0700
committerKevin Ballard <kevin@sb.org>2014-05-08 12:06:21 -0700
commit189dc5f30b2f4198a6ddbe917e2dd6644799f44e (patch)
tree7903bdd527384354c2c45e8bdf6f80d57b5e5081 /src/libstd
parent3296bd7e4686bf4b76d9b9ac5c9c7f61ef4eb9e7 (diff)
downloadrust-189dc5f30b2f4198a6ddbe917e2dd6644799f44e.tar.gz
rust-189dc5f30b2f4198a6ddbe917e2dd6644799f44e.zip
Move slice::raw::from_buf_raw() to vec::raw::from_buf()
Change from_buf_raw() to return a Vec<T> instead of a ~[T]. As such, it
belongs in vec, in the newly-created vec::raw module.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/slice.rs55
-rw-r--r--src/libstd/vec.rs37
2 files changed, 36 insertions, 56 deletions
diff --git a/src/libstd/slice.rs b/src/libstd/slice.rs
index 17c09b0a427..7dc42a75dff 100644
--- a/src/libstd/slice.rs
+++ b/src/libstd/slice.rs
@@ -722,19 +722,6 @@ impl<'a, T: TotalOrd> MutableTotalOrdVector<T> for &'a mut [T] {
     }
 }
 
-/**
-* Constructs a vector from an unsafe pointer to a buffer
-*
-* # Arguments
-*
-* * ptr - An unsafe pointer to a buffer of `T`
-* * elts - The number of elements in the buffer
-*/
-// Wrapper for fn in raw: needs to be called by net_tcp::on_tcp_read_cb
-pub unsafe fn from_buf<T>(ptr: *T, elts: uint) -> ~[T] {
-    raw::from_buf_raw(ptr, elts)
-}
-
 /// Unsafe operations
 pub mod raw {
     use iter::Iterator;
@@ -744,23 +731,6 @@ pub mod raw {
 
     pub use core::slice::raw::{buf_as_slice, mut_buf_as_slice};
     pub use core::slice::raw::{shift_ptr, pop_ptr};
-
-    /**
-    * Constructs a vector from an unsafe pointer to a buffer
-    *
-    * # Arguments
-    *
-    * * ptr - An unsafe pointer to a buffer of `T`
-    * * elts - The number of elements in the buffer
-    */
-    // Was in raw, but needs to be called by net_tcp::on_tcp_read_cb
-    #[inline]
-    pub unsafe fn from_buf_raw<T>(ptr: *T, elts: uint) -> ~[T] {
-        let mut dst = Vec::with_capacity(elts);
-        dst.set_len(elts);
-        ptr::copy_memory(dst.as_mut_ptr(), ptr, elts);
-        dst.move_iter().collect()
-    }
 }
 
 /// An iterator that moves out of a vector.
@@ -821,31 +791,6 @@ mod tests {
     fn is_odd(n: &uint) -> bool { *n % 2u == 1u }
 
     #[test]
-    fn test_unsafe_ptrs() {
-        unsafe {
-            // Test on-stack copy-from-buf.
-            let a = box [1, 2, 3];
-            let mut ptr = a.as_ptr();
-            let b = from_buf(ptr, 3u);
-            assert_eq!(b.len(), 3u);
-            assert_eq!(b[0], 1);
-            assert_eq!(b[1], 2);
-            assert_eq!(b[2], 3);
-
-            // Test on-heap copy-from-buf.
-            let c = box [1, 2, 3, 4, 5];
-            ptr = c.as_ptr();
-            let d = from_buf(ptr, 5u);
-            assert_eq!(d.len(), 5u);
-            assert_eq!(d[0], 1);
-            assert_eq!(d[1], 2);
-            assert_eq!(d[2], 3);
-            assert_eq!(d[3], 4);
-            assert_eq!(d[4], 5);
-        }
-    }
-
-    #[test]
     fn test_from_fn() {
         // Test on-stack from_fn.
         let mut v = Vec::from_fn(3u, square);
diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs
index 257dcc0fcfe..d220ebd0d1e 100644
--- a/src/libstd/vec.rs
+++ b/src/libstd/vec.rs
@@ -1455,12 +1455,30 @@ pub fn unzip<T, U, V: Iterator<(T, U)>>(mut iter: V) -> (Vec<T>, Vec<U>) {
     (ts, us)
 }
 
+/// Unsafe operations
+pub mod raw {
+    use super::Vec;
+    use ptr;
+
+    /// Constructs a vector from an unsafe pointer to a buffer.
+    ///
+    /// The elements of the buffer are copied into the vector without cloning,
+    /// as if `ptr::read()` were called on them.
+    #[inline]
+    pub unsafe fn from_buf<T>(ptr: *T, elts: uint) -> Vec<T> {
+        let mut dst = Vec::with_capacity(elts);
+        dst.set_len(elts);
+        ptr::copy_nonoverlapping_memory(dst.as_mut_ptr(), ptr, elts);
+        dst
+    }
+}
+
 
 #[cfg(test)]
 mod tests {
     use prelude::*;
     use mem::size_of;
-    use super::unzip;
+    use super::{unzip, raw};
 
     #[test]
     fn test_small_vec_struct() {
@@ -1720,4 +1738,21 @@ mod tests {
         assert_eq!((2, 5), (left[1], right[1]));
         assert_eq!((3, 6), (left[2], right[2]));
     }
+
+    #[test]
+    fn test_unsafe_ptrs() {
+        unsafe {
+            // Test on-stack copy-from-buf.
+            let a = [1, 2, 3];
+            let ptr = a.as_ptr();
+            let b = raw::from_buf(ptr, 3u);
+            assert_eq!(b, vec![1, 2, 3]);
+
+            // Test on-heap copy-from-buf.
+            let c = box [1, 2, 3, 4, 5];
+            let ptr = c.as_ptr();
+            let d = raw::from_buf(ptr, 5u);
+            assert_eq!(d, vec![1, 2, 3, 4, 5]);
+        }
+    }
 }