7
7
#![ stable( feature = "rust1" , since = "1.0.0" ) ]
8
8
9
9
use crate :: cmp:: Ordering :: { self , Equal , Greater , Less } ;
10
- use crate :: intrinsics:: { exact_div, select_unpredictable, unchecked_sub} ;
10
+ use crate :: intrinsics:: { const_eval_select , exact_div, select_unpredictable, unchecked_sub} ;
11
11
use crate :: mem:: { self , SizedTypeProperties } ;
12
12
use crate :: num:: NonZero ;
13
13
use crate :: ops:: { Bound , OneSidedRange , Range , RangeBounds } ;
@@ -3671,8 +3671,9 @@ impl<T> [T] {
3671
3671
/// [`split_at_mut`]: slice::split_at_mut
3672
3672
#[ doc( alias = "memcpy" ) ]
3673
3673
#[ stable( feature = "copy_from_slice" , since = "1.9.0" ) ]
3674
+ #[ rustc_const_unstable( feature = "const_copy_from_slice" , issue = "131415" ) ]
3674
3675
#[ track_caller]
3675
- pub fn copy_from_slice ( & mut self , src : & [ T ] )
3676
+ pub const fn copy_from_slice ( & mut self , src : & [ T ] )
3676
3677
where
3677
3678
T : Copy ,
3678
3679
{
@@ -3681,11 +3682,21 @@ impl<T> [T] {
3681
3682
#[ cfg_attr( not( feature = "panic_immediate_abort" ) , inline( never) , cold) ]
3682
3683
#[ cfg_attr( feature = "panic_immediate_abort" , inline) ]
3683
3684
#[ track_caller]
3684
- fn len_mismatch_fail ( dst_len : usize , src_len : usize ) -> ! {
3685
- panic ! (
3686
- "source slice length ({}) does not match destination slice length ({})" ,
3687
- src_len, dst_len,
3688
- ) ;
3685
+ const fn len_mismatch_fail ( dst_len : usize , src_len : usize ) -> ! {
3686
+ const fn panic_at_const ( _dst_len : usize , _src_len : usize ) -> ! {
3687
+ // Note that we cannot format in constant expressions.
3688
+ panic ! (
3689
+ "copy_from_slice: source slice length does not match destination slice length"
3690
+ ) ;
3691
+ }
3692
+ fn panic_at_rt ( dst_len : usize , src_len : usize ) -> ! {
3693
+ panic ! (
3694
+ "source slice length ({}) does not match destination slice length ({})" ,
3695
+ src_len, dst_len,
3696
+ ) ;
3697
+ }
3698
+ // FIXME(const-hack): We would prefer to have streamlined panics when formatters become const-friendly.
3699
+ const_eval_select ( ( dst_len, src_len) , panic_at_const, panic_at_rt)
3689
3700
}
3690
3701
3691
3702
if self . len ( ) != src. len ( ) {
0 commit comments