Try out alternate way of implementing `distance_between` per Ralf Jung's suggestion
@@ -7,11 +7,11 @@ use crate::StaticVec;
/// An internal function for calculating pointer offsets as usizes, while accounting
/// directly for possible ZSTs. This is used specifically in the iterator implementations.
#[inline(always)]
-pub(crate) /*const*/ fn distance_between<T>(dest: *const T, origin: *const T) -> usize {
- match size_of::<T>() {
- 0 => (dest as usize).wrapping_sub(origin as usize),
+pub(crate) const fn distance_between<T>(dest: *const T, origin: *const T) -> usize {
// Safety: this function is used strictly with linear inputs
- // where dest is known to come after origin.
+ // where `dest` is known to come after `origin`.
+ match size_of::<T>() {
+ 0 => unsafe { ptr_offset_from(dest as *const u8, origin as *const u8) as usize },
_ => unsafe { ptr_offset_from(dest, origin) as usize },
}
}
