Skip to content

Commit f12698c

Browse files
committed
Add const cloning of slices and tests
1 parent e821cb8 commit f12698c

File tree

3 files changed

+94
-9
lines changed

3 files changed

+94
-9
lines changed

library/core/src/slice/mod.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use crate::cmp::Ordering::{self, Equal, Greater, Less};
1010
use crate::intrinsics::{exact_div, unchecked_sub};
11+
use crate::marker::Destruct;
1112
use crate::mem::{self, MaybeUninit, SizedTypeProperties};
1213
use crate::num::NonZero;
1314
use crate::ops::{OneSidedRange, OneSidedRangeBound, Range, RangeBounds, RangeInclusive};
@@ -3786,9 +3787,10 @@ impl<T> [T] {
37863787
/// [`split_at_mut`]: slice::split_at_mut
37873788
#[stable(feature = "clone_from_slice", since = "1.7.0")]
37883789
#[track_caller]
3789-
pub fn clone_from_slice(&mut self, src: &[T])
3790+
#[rustc_const_unstable(feature = "const_clone", issue = "142757")]
3791+
pub const fn clone_from_slice(&mut self, src: &[T])
37903792
where
3791-
T: Clone,
3793+
T: [const] Clone + [const] Destruct,
37923794
{
37933795
self.spec_clone_from(src);
37943796
}
@@ -5089,13 +5091,18 @@ impl [f64] {
50895091
}
50905092
}
50915093

5094+
#[const_trait]
5095+
#[rustc_const_unstable(feature = "const_clone", issue = "142757")]
50925096
trait CloneFromSpec<T> {
5093-
fn spec_clone_from(&mut self, src: &[T]);
5097+
fn spec_clone_from(&mut self, src: &[T])
5098+
where
5099+
T: [const] Destruct;
50945100
}
50955101

5096-
impl<T> CloneFromSpec<T> for [T]
5102+
#[rustc_const_unstable(feature = "const_clone", issue = "142757")]
5103+
impl<T> const CloneFromSpec<T> for [T]
50975104
where
5098-
T: Clone,
5105+
T: [const] Clone + [const] Destruct,
50995106
{
51005107
#[track_caller]
51015108
default fn spec_clone_from(&mut self, src: &[T]) {
@@ -5105,15 +5112,19 @@ where
51055112
// But since it can't be relied on we also have an explicit specialization for T: Copy.
51065113
let len = self.len();
51075114
let src = &src[..len];
5108-
for i in 0..len {
5109-
self[i].clone_from(&src[i]);
5115+
// FIXME(const_hack): make this a `for idx in 0..self.len()` loop.
5116+
let mut idx = 0;
5117+
while idx < self.len() {
5118+
self[idx].clone_from(&src[idx]);
5119+
idx += 1;
51105120
}
51115121
}
51125122
}
51135123

5114-
impl<T> CloneFromSpec<T> for [T]
5124+
#[rustc_const_unstable(feature = "const_clone", issue = "142757")]
5125+
impl<T> const CloneFromSpec<T> for [T]
51155126
where
5116-
T: Copy,
5127+
T: Copy + [const] Clone + [const] Destruct,
51175128
{
51185129
#[track_caller]
51195130
fn spec_clone_from(&mut self, src: &[T]) {

library/coretests/tests/clone.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,74 @@ fn cstr_metadata_is_length_with_nul() {
121121
let bytes: *const [u8] = p as *const [u8];
122122
assert_eq!(s.to_bytes_with_nul().len(), bytes.len());
123123
}
124+
125+
#[test]
126+
fn test_const_clone() {
127+
const {
128+
let bool: bool = Default::default();
129+
let char: char = Default::default();
130+
let ascii_char: std::ascii::Char = Default::default();
131+
let usize: usize = Default::default();
132+
let u8: u8 = Default::default();
133+
let u16: u16 = Default::default();
134+
let u32: u32 = Default::default();
135+
let u64: u64 = Default::default();
136+
let u128: u128 = Default::default();
137+
let i8: i8 = Default::default();
138+
let i16: i16 = Default::default();
139+
let i32: i32 = Default::default();
140+
let i64: i64 = Default::default();
141+
let i128: i128 = Default::default();
142+
let f16: f16 = Default::default();
143+
let f32: f32 = Default::default();
144+
let f64: f64 = Default::default();
145+
let f128: f128 = Default::default();
146+
147+
let bool_clone: bool = bool.clone();
148+
let char_clone: char = char.clone();
149+
let ascii_char_clone: std::ascii::Char = ascii_char.clone();
150+
151+
let usize_clone: usize = usize.clone();
152+
let u8_clone: u8 = u8.clone();
153+
let u16_clone: u16 = u16.clone();
154+
let u32_clone: u32 = u32.clone();
155+
let u64_clone: u64 = u64.clone();
156+
let u128_clone: u128 = u128.clone();
157+
let i8_clone: i8 = i8.clone();
158+
let i16_clone: i16 = i16.clone();
159+
let i32_clone: i32 = i32.clone();
160+
let i64_clone: i64 = i64.clone();
161+
let i128_clone: i128 = i128.clone();
162+
let f16_clone: f16 = f16.clone();
163+
let f32_clone: f32 = f32.clone();
164+
let f64_clone: f64 = f64.clone();
165+
let f128_clone: f128 = f128.clone();
166+
167+
assert!(bool == bool_clone);
168+
assert!(char == char_clone);
169+
assert!(ascii_char == ascii_char_clone);
170+
assert!(usize == usize_clone);
171+
assert!(u8 == u8_clone);
172+
assert!(u16 == u16_clone);
173+
assert!(u32 == u32_clone);
174+
assert!(u64 == u64_clone);
175+
assert!(u128 == u128_clone);
176+
assert!(i8 == i8_clone);
177+
assert!(i16 == i16_clone);
178+
assert!(i32 == i32_clone);
179+
assert!(i64 == i64_clone);
180+
assert!(i128 == i128_clone);
181+
assert!(f16 == f16_clone);
182+
assert!(f32 == f32_clone);
183+
assert!(f64 == f64_clone);
184+
assert!(f128 == f128_clone);
185+
186+
let src: [i32; 4] = [1, 2, 3, 4];
187+
let mut dst: [i32; 2] = [0, 0];
188+
189+
dst.clone_from_slice(&src[2..]);
190+
191+
assert!(src == [1, 2, 3, 4]);
192+
assert!(dst == [3, 4]);
193+
}
194+
}

library/coretests/tests/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
#![feature(char_internals)]
1717
#![feature(char_max_len)]
1818
#![feature(clone_to_uninit)]
19+
#![feature(const_clone)]
1920
#![feature(const_cmp)]
2021
#![feature(const_convert)]
22+
#![feature(const_default)]
2123
#![feature(const_destruct)]
2224
#![feature(const_drop_in_place)]
2325
#![feature(const_eval_select)]
26+
#![feature(const_index)]
2427
#![feature(const_mul_add)]
2528
#![feature(const_ops)]
2629
#![feature(const_option_ops)]

0 commit comments

Comments
 (0)