use core::num::NonZeroU64;
use std::error::Error;
use std::{fmt, io};
use bstack::{BStackRange, BStackSlice};
use bytemuck::{Pod, Zeroable};
use crate::util::{io_error, io_errorfn};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord, Pod, Zeroable)]
#[repr(transparent)]
pub struct Offset(u64);
impl Offset {
pub const NULL: Offset = Offset(0);
pub const fn is_null(self) -> bool {
self.0 == 0
}
pub const fn get(self) -> u64 {
self.0
}
pub const fn from_raw(v: u64) -> Self {
Offset(v)
}
pub fn checked_add(self, delta: u64) -> io::Result<Offset> {
self.0
.checked_add(delta)
.map(Offset)
.ok_or_else(offset_overflow)
}
pub fn checked_add_mul(self, index: u64, stride: u64) -> io::Result<Offset> {
let delta = index.checked_mul(stride).ok_or_else(offset_overflow)?;
self.checked_add(delta)
}
pub const fn to_non_null(self) -> Option<NonNullOffset> {
NonNullOffset::new(self)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[repr(transparent)]
pub struct NonNullOffset(NonZeroU64);
impl NonNullOffset {
pub const fn new(offset: Offset) -> Option<Self> {
match NonZeroU64::new(offset.0) {
Some(nz) => Some(NonNullOffset(nz)),
None => None,
}
}
pub const unsafe fn new_unchecked(offset: Offset) -> Self {
NonNullOffset(unsafe { NonZeroU64::new_unchecked(offset.0) })
}
pub const fn get(self) -> Offset {
Offset(self.0.get())
}
pub const fn as_u64(self) -> u64 {
self.0.get()
}
pub fn checked_add(self, delta: u64) -> io::Result<NonNullOffset> {
self.0
.checked_add(delta)
.map(NonNullOffset)
.ok_or_else(offset_overflow)
}
#[inline]
pub fn from_range(range: BStackRange) -> Option<NonNullOffset> {
Self::new(range.into())
}
#[inline]
pub fn from_slice(slice: BStackSlice<'_>) -> Option<NonNullOffset> {
Self::new(slice.into())
}
#[inline]
pub fn from_field(off: u64) -> io::Result<NonNullOffset> {
Self::new(Offset::from_raw(off)).ok_or_else(|| io_error!("field offset resolved to null"))
}
}
impl From<NonNullOffset> for Offset {
#[inline]
fn from(o: NonNullOffset) -> Offset {
o.get()
}
}
impl From<NonNullOffset> for u64 {
#[inline]
fn from(o: NonNullOffset) -> u64 {
o.as_u64()
}
}
impl TryFrom<Offset> for NonNullOffset {
type Error = NullOffsetError;
#[inline]
fn try_from(offset: Offset) -> Result<Self, NullOffsetError> {
NonNullOffset::new(offset).ok_or(NullOffsetError)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct NullOffsetError;
impl fmt::Display for NullOffsetError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("offset is null")
}
}
impl Error for NullOffsetError {}
impl From<BStackRange> for Offset {
#[inline]
fn from(range: BStackRange) -> Offset {
Offset::from_raw(range.start())
}
}
impl From<BStackSlice<'_>> for Offset {
#[inline]
fn from(slice: BStackSlice<'_>) -> Offset {
Offset::from_raw(slice.start())
}
}
io_errorfn!(
offset_overflow,
InvalidData,
"on-disk offset arithmetic overflow"
);
#[inline(always)]
pub fn checked_off(base: u64, delta: u64) -> io::Result<u64> {
base.checked_add(delta)
.ok_or_else(|| io_error!("block offset overflow"))
}
#[inline(always)]
pub fn checked_off_mul(base: u64, index: u64, stride: u64) -> io::Result<u64> {
Offset::from_raw(base)
.checked_add_mul(index, stride)
.map(Offset::get)
}