|
1 | 1 | use core::fmt::{self, Debug}; |
| 2 | +#[cfg(any(target_os = "uefi", target_os = "windows"))] |
2 | 3 | use core::hint; |
3 | 4 | use core::mem; |
| 5 | +use core::num::NonZeroUsize; |
4 | 6 | use core::ops::Deref; |
5 | 7 | use core::slice; |
6 | 8 |
|
@@ -131,6 +133,7 @@ use crate::__private::Slice; |
131 | 133 | /// ``` |
132 | 134 | pub struct DistributedSlice<T: ?Sized + Slice> { |
133 | 135 | name: &'static str, |
| 136 | + stride: NonZeroUsize, |
134 | 137 | section_start: StaticPtr<T::Element>, |
135 | 138 | section_stop: StaticPtr<T::Element>, |
136 | 139 | dupcheck_start: StaticPtr<isize>, |
@@ -163,12 +166,13 @@ impl<T> DistributedSlice<[T]> { |
163 | 166 | dupcheck_start: *const (), |
164 | 167 | dupcheck_stop: *const (), |
165 | 168 | ) -> Self { |
166 | | - if mem::size_of::<T>() == 0 { |
| 169 | + let Some(stride) = NonZeroUsize::new(mem::size_of::<T>()) else { |
167 | 170 | panic!("#[distributed_slice] requires that the slice element type has nonzero size"); |
168 | | - } |
| 171 | + }; |
169 | 172 |
|
170 | 173 | DistributedSlice { |
171 | 174 | name, |
| 175 | + stride, |
172 | 176 | section_start: StaticPtr { |
173 | 177 | ptr: section_start.cast::<T>(), |
174 | 178 | }, |
@@ -230,15 +234,10 @@ impl<T> DistributedSlice<[T]> { |
230 | 234 | panic!("duplicate #[distributed_slice] with name \"{}\"", self.name); |
231 | 235 | } |
232 | 236 |
|
233 | | - let stride = mem::size_of::<T>(); |
234 | 237 | let start = self.section_start.ptr; |
235 | 238 | let stop = self.section_stop.ptr; |
236 | 239 | let byte_offset = stop as usize - start as usize; |
237 | | - let Some(len) = byte_offset.checked_div(stride) else { |
238 | | - // The #[distributed_slice] call checks `size_of::<T>() > 0` before |
239 | | - // using the unsafe `private_new`. |
240 | | - unsafe { hint::unreachable_unchecked() } |
241 | | - }; |
| 240 | + let len = byte_offset / self.stride; |
242 | 241 |
|
243 | 242 | // On Windows, the implementation involves growing a &[T; 0] to |
244 | 243 | // encompass elements that we have asked the linker to place immediately |
|
0 commit comments