|
1 | 1 | #[cfg(not(feature = "std"))] |
2 | 2 | use alloc::vec::Vec; |
| 3 | +use core::num::NonZeroUsize; |
3 | 4 | use core::ptr::NonNull; |
4 | 5 | use std::mem; |
5 | 6 | use std::mem::MaybeUninit; |
@@ -36,22 +37,21 @@ impl<A> Array<A, Ix0> |
36 | 37 | /// ``` |
37 | 38 | pub fn into_scalar(self) -> A |
38 | 39 | { |
39 | | - let size = mem::size_of::<A>(); |
40 | | - if size == 0 { |
| 40 | + let size = match NonZeroUsize::new(mem::size_of::<A>()) { |
| 41 | + Some(size) => size, |
41 | 42 | // Any index in the `Vec` is fine since all elements are identical. |
42 | | - self.data.into_vec().remove(0) |
43 | | - } else { |
44 | | - // Find the index in the `Vec` corresponding to `self.ptr`. |
45 | | - // (This is necessary because the element in the array might not be |
46 | | - // the first element in the `Vec`, such as if the array was created |
47 | | - // by `array![1, 2, 3, 4].slice_move(s![2])`.) |
48 | | - let first = self.parts.ptr.as_ptr() as usize; |
49 | | - let base = self.data.as_ptr() as usize; |
50 | | - let index = (first - base) / size; |
51 | | - debug_assert_eq!((first - base) % size, 0); |
52 | | - // Remove the element at the index and return it. |
53 | | - self.data.into_vec().remove(index) |
54 | | - } |
| 43 | + None => return self.data.into_vec().remove(0), |
| 44 | + }; |
| 45 | + // Find the index in the `Vec` corresponding to `self.ptr`. |
| 46 | + // (This is necessary because the element in the array might not be |
| 47 | + // the first element in the `Vec`, such as if the array was created |
| 48 | + // by `array![1, 2, 3, 4].slice_move(s![2])`.) |
| 49 | + let first = self.parts.ptr.as_ptr() as usize; |
| 50 | + let base = self.data.as_ptr() as usize; |
| 51 | + let index = (first - base) / size; |
| 52 | + debug_assert_eq!((first - base) % size.get(), 0); |
| 53 | + // Remove the element at the index and return it. |
| 54 | + self.data.into_vec().remove(index) |
55 | 55 | } |
56 | 56 | } |
57 | 57 |
|
|
0 commit comments