Skip to main content

core/slice/
specialize.rs

1use crate::clone::TrivialClone;
2use crate::ptr;
3
4pub(super) trait SpecFill<T> {
5    fn spec_fill(&mut self, value: T);
6}
7
8impl<T: Clone> SpecFill<T> for [T] {
9    #[ferrocene::prevalidated]
10    default fn spec_fill(&mut self, value: T) {
11        if let Some((last, elems)) = self.split_last_mut() {
12            for el in elems {
13                el.clone_from(&value);
14            }
15
16            *last = value
17        }
18    }
19}
20
21impl<T: TrivialClone> SpecFill<T> for [T] {
22    #[ferrocene::prevalidated]
23    default fn spec_fill(&mut self, value: T) {
24        for item in self.iter_mut() {
25            // SAFETY: `TrivialClone` indicates that this is equivalent to
26            // calling `Clone::clone`
27            *item = unsafe { ptr::read(&value) };
28        }
29    }
30}
31
32impl SpecFill<u8> for [u8] {
33    #[ferrocene::prevalidated]
34    fn spec_fill(&mut self, value: u8) {
35        // SAFETY: The pointer is derived from a reference, so it's writable.
36        unsafe {
37            crate::intrinsics::write_bytes(self.as_mut_ptr(), value, self.len());
38        }
39    }
40}
41
42impl SpecFill<i8> for [i8] {
43    fn spec_fill(&mut self, value: i8) {
44        // SAFETY: The pointer is derived from a reference, so it's writable.
45        unsafe {
46            crate::intrinsics::write_bytes(self.as_mut_ptr(), value.cast_unsigned(), self.len());
47        }
48    }
49}
50
51macro spec_fill_int {
52    ($($type:ty)*) => {$(
53        impl SpecFill<$type> for [$type] {
54            #[inline]
55            fn spec_fill(&mut self, value: $type) {
56                // We always take this fastpath in Miri for long slices as the manual `for`
57                // loop can be prohibitively slow.
58                if (cfg!(miri) && self.len() > 32) || crate::intrinsics::is_val_statically_known(value) {
59                    let bytes = value.to_ne_bytes();
60                    if value == <$type>::from_ne_bytes([bytes[0]; size_of::<$type>()]) {
61                        // SAFETY: The pointer is derived from a reference, so it's writable.
62                        unsafe {
63                            crate::intrinsics::write_bytes(self.as_mut_ptr(), bytes[0], self.len());
64                        }
65                        return;
66                    }
67                }
68                for item in self.iter_mut() {
69                    *item = value;
70                }
71            }
72        }
73    )*}
74}
75
76spec_fill_int! { u16 i16 u32 i32 u64 i64 u128 i128 usize isize }