use std::fmt;
#[repr(transparent)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct AbsoluteRow(u64);
impl AbsoluteRow {
pub const ZERO: Self = Self(0);
#[cfg(test)]
pub const MAX: Self = Self(u64::MAX);
#[inline]
pub const fn new(value: u64) -> Self {
Self(value)
}
#[inline]
pub const fn as_u64(self) -> u64 {
self.0
}
#[inline]
pub const fn checked_add(self, rows: PhysicalRowCount) -> Option<Self> {
match self.0.checked_add(rows.0) {
Some(value) => Some(Self(value)),
None => None,
}
}
#[inline]
pub const fn checked_sub(self, rows: PhysicalRowCount) -> Option<Self> {
match self.0.checked_sub(rows.0) {
Some(value) => Some(Self(value)),
None => None,
}
}
#[inline]
pub const fn checked_offset_from(
self,
origin: AbsoluteRow,
) -> Option<PhysicalRowCount> {
match self.0.checked_sub(origin.0) {
Some(value) => Some(PhysicalRowCount(value)),
None => None,
}
}
}
impl fmt::Display for AbsoluteRow {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
#[repr(transparent)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PhysicalRowCount(u64);
impl PhysicalRowCount {
pub const ZERO: Self = Self(0);
#[inline]
pub const fn new(value: u64) -> Self {
Self(value)
}
#[inline]
pub const fn as_u64(self) -> u64 {
self.0
}
#[inline]
pub const fn checked_add(self, other: Self) -> Option<Self> {
match self.0.checked_add(other.0) {
Some(value) => Some(Self(value)),
None => None,
}
}
}
impl fmt::Display for PhysicalRowCount {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
#[repr(transparent)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PhysicalEndExclusive(AbsoluteRow);
impl PhysicalEndExclusive {
#[inline]
pub const fn new(position: AbsoluteRow) -> Self {
Self(position)
}
#[inline]
pub const fn position(self) -> AbsoluteRow {
self.0
}
}
impl fmt::Display for PhysicalEndExclusive {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct PhysicalExtent {
start: AbsoluteRow,
end: PhysicalEndExclusive,
}
impl PhysicalExtent {
#[inline]
pub const fn try_new(start: AbsoluteRow, end: PhysicalEndExclusive) -> Option<Self> {
if start.as_u64() <= end.position().as_u64() {
Some(Self { start, end })
} else {
None
}
}
#[inline]
pub const fn start(self) -> AbsoluteRow {
self.start
}
#[inline]
pub const fn end(self) -> PhysicalEndExclusive {
self.end
}
}
#[cfg(test)]
mod tests {
use super::{AbsoluteRow, PhysicalEndExclusive, PhysicalExtent, PhysicalRowCount};
#[test]
fn absolute_arithmetic_rejects_overflow_and_underflow() {
assert_eq!(
AbsoluteRow::new(u64::MAX).checked_add(PhysicalRowCount::new(1)),
None
);
assert_eq!(
AbsoluteRow::ZERO.checked_sub(PhysicalRowCount::new(1)),
None
);
assert_eq!(
AbsoluteRow::new(4).checked_offset_from(AbsoluteRow::new(5)),
None
);
}
#[test]
fn physical_extents_validate_order() {
let start = AbsoluteRow::new(10);
let end = PhysicalEndExclusive::new(AbsoluteRow::new(13));
let extent = PhysicalExtent::try_new(start, end)
.expect("10..13 is a valid half-open extent");
assert_eq!(extent.start(), start);
assert_eq!(extent.end(), end);
assert_eq!(extent.end().position(), AbsoluteRow::new(13));
assert!(
PhysicalExtent::try_new(start, PhysicalEndExclusive::new(start)).is_some()
);
assert!(
PhysicalExtent::try_new(
start,
PhysicalEndExclusive::new(AbsoluteRow::new(9)),
)
.is_none()
);
}
}