devela::_dep::rkyv::rel_ptr

Function signed_offset

pub fn signed_offset<E>(from: usize, to: usize) -> Result<isize, E> 
where E: Source,
Available on crate feature dep_rkyv only.
Expand description

Calculates the offset between two positions as an isize.

This function exists solely to get the distance between two usizes as an isize with a full range of values.

§Examples

assert!(signed_offset::<Error>(0, 1).is_ok_and(|x| x == 1));
assert!(signed_offset::<Error>(1, 0).is_ok_and(|x| x == -1));
assert!(signed_offset::<Error>(0, isize::MAX as usize)
    .is_ok_and(|x| x == isize::MAX));
assert!(signed_offset::<Error>(isize::MAX as usize, 0)
    .is_ok_and(|x| x == -isize::MAX));
assert!(signed_offset::<Error>(0, isize::MAX as usize + 1).is_err());
assert!(signed_offset::<Error>(isize::MAX as usize + 1, 0)
    .is_ok_and(|x| x == isize::MIN));
assert!(signed_offset::<Error>(0, isize::MAX as usize + 2).is_err());
assert!(signed_offset::<Error>(isize::MAX as usize + 2, 0).is_err());