|
| 1 | +use crate::ptr::write_bytes; |
| 2 | + |
| 3 | +pub(super) trait SpecFill<T> { |
| 4 | + fn spec_fill(&mut self, value: T); |
| 5 | +} |
| 6 | + |
| 7 | +impl<T: Clone> SpecFill<T> for [T] { |
| 8 | + default fn spec_fill(&mut self, value: T) { |
| 9 | + if let Some((last, elems)) = self.split_last_mut() { |
| 10 | + for el in elems { |
| 11 | + el.clone_from(&value); |
| 12 | + } |
| 13 | + |
| 14 | + *last = value |
| 15 | + } |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +impl<T: Copy> SpecFill<T> for [T] { |
| 20 | + default fn spec_fill(&mut self, value: T) { |
| 21 | + for item in self.iter_mut() { |
| 22 | + *item = value; |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +impl SpecFill<u8> for [u8] { |
| 28 | + fn spec_fill(&mut self, value: u8) { |
| 29 | + // SAFETY: this is slice of u8 |
| 30 | + unsafe { |
| 31 | + let ptr = self.as_mut_ptr(); |
| 32 | + let len = self.len(); |
| 33 | + write_bytes(ptr, value, len); |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +impl SpecFill<i8> for [i8] { |
| 39 | + fn spec_fill(&mut self, value: i8) { |
| 40 | + // SAFETY: this is slice of i8 |
| 41 | + unsafe { |
| 42 | + let ptr = self.as_mut_ptr(); |
| 43 | + let len = self.len(); |
| 44 | + write_bytes(ptr, value as u8, len); |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl SpecFill<bool> for [bool] { |
| 50 | + fn spec_fill(&mut self, value: bool) { |
| 51 | + // SAFETY: this is slice of bool |
| 52 | + unsafe { |
| 53 | + let ptr = self.as_mut_ptr(); |
| 54 | + let len = self.len(); |
| 55 | + write_bytes(ptr, value as u8, len); |
| 56 | + } |
| 57 | + } |
| 58 | +} |
0 commit comments