pub(crate) const CLOSED_BIT: usize = 1 << (usize::BITS - 1);
pub(crate) const POS_MASK: usize = !CLOSED_BIT;
pub(crate) const MAX_CAPACITY: usize = 1 << (usize::BITS - 3);
#[inline]
pub(crate) const fn pos_add(pos: usize, n: usize) -> usize {
pos.wrapping_add(n) & POS_MASK
}
#[inline]
pub(crate) const fn pos_diff(a: usize, b: usize) -> isize {
(((a.wrapping_sub(b)) & POS_MASK) << 1) as isize >> 1
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn add_wraps_without_touching_closed_bit() {
assert_eq!(pos_add(POS_MASK, 1), 0);
assert_eq!(pos_add(POS_MASK - 1, 3), 1);
assert_eq!(pos_add(5, 3) & CLOSED_BIT, 0);
}
#[test]
fn diff_is_signed_across_the_wrap() {
assert_eq!(pos_diff(5, 3), 2);
assert_eq!(pos_diff(3, 5), -2);
assert_eq!(pos_diff(1, POS_MASK), 2);
assert_eq!(pos_diff(POS_MASK, 1), -2);
assert_eq!(pos_diff(0, 0), 0);
}
#[test]
fn diff_ignores_closed_bit_on_either_side() {
assert_eq!(pos_diff(7 | CLOSED_BIT, 3), 4);
assert_eq!(pos_diff(7, 3 | CLOSED_BIT), 4);
}
}