test(no_crate_inject, attr(allow(unused_variables), deny(warnings))))]
#![cfg_attr(test, allow(deprecated))] // rand
+#![cfg_attr(not(test), feature(copy_from_slice))] // impl [T]
#![cfg_attr(not(stage0), deny(warnings))]
#![feature(alloc)]
core_slice::SliceExt::clone_from_slice(self, src)
}
+ /// Copies all elements from `src` into `self`, using a memcpy.
+ ///
+ /// The length of `src` must be the same as `self`.
+ ///
+ /// # Panics
+ ///
+ /// This function will panic if the two slices have different lengths.
+ ///
+ /// # Example
+ ///
+ /// ```rust
+ /// #![feature(copy_from_slice)]
+ /// let mut dst = [0, 0, 0];
+ /// let src = [1, 2, 3];
+ ///
+ /// dst.copy_from_slice(&src);
+ /// assert_eq!(src, dst);
+ /// ```
+ #[unstable(feature = "copy_from_slice", issue = "31755")]
+ pub fn copy_from_slice(&mut self, src: &[T]) where T: Copy {
+ core_slice::SliceExt::copy_from_slice(self, src)
+ }
+
+
/// Copies `self` into a new `Vec`.
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
#![feature(btree_range)]
#![feature(collections)]
#![feature(collections_bound)]
+#![feature(copy_from_slice)]
#![feature(const_fn)]
#![feature(fn_traits)]
#![feature(enumset)]
assert_eq!(drop_count.load(Ordering::SeqCst), 8);
}
+#[test]
+fn test_copy_from_slice() {
+ let src = [0, 1, 2, 3, 4, 5];
+ let mut dst = [0; 6];
+ dst.copy_from_slice(&src);
+ assert_eq!(src, dst)
+}
+
+#[test]
+#[should_panic(expected = "destination and source slices have different lengths")]
+fn test_copy_from_slice_dst_longer() {
+ let src = [0, 1, 2, 3];
+ let mut dst = [0; 5];
+ dst.copy_from_slice(&src);
+}
+
+#[test]
+#[should_panic(expected = "destination and source slices have different lengths")]
+fn test_copy_from_slice_dst_shorter() {
+ let src = [0, 1, 2, 3];
+ let mut dst = [0; 3];
+ dst.copy_from_slice(&src);
+}
+
mod bench {
use std::{mem, ptr};
use std::__rand::{Rng, thread_rng};
use result::Result::{Ok, Err};
use ptr;
use mem;
-use marker::{Send, Sync, self};
+use marker::{Copy, Send, Sync, self};
use raw::Repr;
// Avoid conflicts with *both* the Slice trait (buggy) and the `slice::raw` module.
use raw::Slice as RawSlice;
#[stable(feature = "clone_from_slice", since = "1.7.0")]
fn clone_from_slice(&mut self, &[Self::Item]) where Self::Item: Clone;
+ #[unstable(feature = "copy_from_slice", issue = "31755")]
+ fn copy_from_slice(&mut self, src: &[Self::Item]) where Self::Item: Copy;
}
// Use macros to be generic over const/mut
self[i].clone_from(&src[i]);
}
}
+
+ #[inline]
+ fn copy_from_slice(&mut self, src: &[T]) where T: Copy {
+ assert!(self.len() == src.len(),
+ "destination and source slices have different lengths");
+ unsafe {
+ ptr::copy_nonoverlapping(
+ src.as_ptr(), self.as_mut_ptr(), self.len());
+ }
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]