Implement Clone for Box<[T]> where T: Clone

Closes #25097
This commit is contained in:
Jonathan Reem
2015-07-26 22:12:00 -07:00
parent 184267cac6
commit e24423091f
4 changed files with 109 additions and 10 deletions

View File

@@ -1270,6 +1270,59 @@ fn test_to_vec() {
assert_eq!(ys, [1, 2, 3]);
}
#[test]
fn test_box_slice_clone() {
let data = vec![vec![0, 1], vec![0], vec![1]];
let data2 = data.clone().into_boxed_slice().clone().to_vec();
assert_eq!(data, data2);
}
#[test]
fn test_box_slice_clone_panics() {
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::thread::spawn;
struct Canary {
count: Arc<AtomicUsize>,
panics: bool
}
impl Drop for Canary {
fn drop(&mut self) {
self.count.fetch_add(1, Ordering::SeqCst);
}
}
impl Clone for Canary {
fn clone(&self) -> Self {
if self.panics { panic!() }
Canary {
count: self.count.clone(),
panics: self.panics
}
}
}
let drop_count = Arc::new(AtomicUsize::new(0));
let canary = Canary { count: drop_count.clone(), panics: false };
let panic = Canary { count: drop_count.clone(), panics: true };
spawn(move || {
// When xs is dropped, +5.
let xs = vec![canary.clone(), canary.clone(), canary.clone(),
panic, canary].into_boxed_slice();
// When panic is cloned, +3.
xs.clone();
}).join().unwrap_err();
// Total = 8
assert_eq!(drop_count.load(Ordering::SeqCst), 8);
}
mod bench {
use std::iter::repeat;
use std::{mem, ptr};