Skip to content

Commit

Permalink
Auto merge of rust-lang#31834 - ubsan:copy_from_slice, r=alexcrichton
Browse files Browse the repository at this point in the history
implements rust-lang/rfcs#1419

r? alexcrichton
  • Loading branch information
bors committed Feb 26, 2016
2 parents 9c6a008 + e12b1f9 commit 0913004
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/libcollections/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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)]
Expand Down
24 changes: 24 additions & 0 deletions src/libcollections/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,30 @@ impl<T> [T] {
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]
Expand Down
1 change: 1 addition & 0 deletions src/libcollectionstest/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#![feature(btree_range)]
#![feature(collections)]
#![feature(collections_bound)]
#![feature(copy_from_slice)]
#![feature(const_fn)]
#![feature(fn_traits)]
#![feature(enumset)]
Expand Down
24 changes: 24 additions & 0 deletions src/libcollectionstest/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,30 @@ fn test_box_slice_clone_panics() {
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};
Expand Down
14 changes: 13 additions & 1 deletion src/libcore/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use result::Result;
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;
Expand Down Expand Up @@ -152,6 +152,8 @@ pub trait SliceExt {

#[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
Expand Down Expand Up @@ -488,6 +490,16 @@ impl<T> SliceExt for [T] {
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")]
Expand Down

0 comments on commit 0913004

Please sign in to comment.