@@ -760,6 +760,57 @@ impl<T, const CAP: usize> From<[T; CAP]> for ArrayVec<T, CAP> {
760
760
}
761
761
}
762
762
763
+ // Generic version of from_slice_const not possible at this time
764
+ macro_rules! impl_from_const {
765
+ ( $( $t: ty) +) => {
766
+ $(
767
+ impl <const CAP : usize > ArrayVec <$t, CAP > {
768
+ /// Create a new `ArrayVec` from a slice, suitable for const context
769
+ ///
770
+ /// Capacity is inferred from the type parameter.
771
+ ///
772
+ /// **Panics** or causes a **const error** if the backing array is not large enough to fit the
773
+ /// slice.
774
+ ///
775
+ /// ```
776
+ /// use arrayvec::ArrayVec;
777
+ ///
778
+ /// const V: ArrayVec<u8, 3> = ArrayVec::<u8, 3>::from_slice_const(&[1, 2, 3]);
779
+ /// ```
780
+ ///
781
+ /// A compile-time error will occur - in constants - if the input is too long:
782
+ ///
783
+ /// ```compile_fail
784
+ /// # use arrayvec::ArrayVec;
785
+ /// const S1: ArrayVec<u8, 3> = ArrayVec::<u8, 3>::from_slice_const(&[1, 2, 3, 4]);
786
+ /// ```
787
+ pub const fn from_slice_const( values: & [ $t] ) -> Self {
788
+ let len = values. len( ) ;
789
+ assert_length_lt_capacity_const!( len, CAP ) ;
790
+
791
+ let mut vec = Self :: new_const( ) ;
792
+ let mut i = 0 ;
793
+ while i < len {
794
+ vec. xs[ i] = MaybeUninit :: new( values[ i] ) ;
795
+ i += 1 ;
796
+ }
797
+
798
+ // Safety: we know len <= CAP and elements < len are initialized
799
+ vec. len = len as u32 ;
800
+ vec
801
+ }
802
+ }
803
+ ) +
804
+
805
+ } ;
806
+ }
807
+
808
+ impl_from_const ! ( u8 u16 u32 u64 usize i8 i16 i32 i64 isize char ) ;
809
+ #[ cfg( feature = "floats" ) ]
810
+ impl_from_const ! ( f32 f64 ) ;
811
+ #[ cfg( feature = "u128" ) ]
812
+ impl_from_const ! ( u128 i128 ) ;
813
+
763
814
764
815
/// Try to create an `ArrayVec` from a slice. This will return an error if the slice was too big to
765
816
/// fit.
0 commit comments