still awesome

This commit is contained in:
nora 2023-02-16 20:56:28 +01:00
parent 0df129d612
commit d849e07c58
3 changed files with 128 additions and 45 deletions

View file

@ -1,13 +1,13 @@
use std::{
fmt::{Debug, Display},
ops::Add,
ops::{Add, AddAssign, Sub},
};
use crate::idx::ToIdxUsize;
use bytemuck::{Pod, Zeroable};
/// A _run time_ address inside an object file.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Zeroable, Pod)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Zeroable, Pod)]
#[repr(transparent)]
pub struct Addr {
value: u64,
@ -47,18 +47,99 @@ impl Add<u64> for Addr {
}
/// An offset into an object file. Either absolut or relative to a particular section.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Zeroable, Pod)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Zeroable, Pod)]
#[repr(transparent)]
pub struct Offset(pub u64);
pub struct Offset {
value: u64,
}
#[allow(non_snake_case)]
pub const fn Offset(value: u64) -> Offset {
Offset { value }
}
impl Offset {
pub fn usize(self) -> usize {
self.value.try_into().unwrap()
}
pub fn u64(self) -> u64 {
self.value
}
}
impl ToIdxUsize for Offset {
fn to_idx_usize(self) -> usize {
self.0.to_idx_usize()
self.value.to_idx_usize()
}
}
impl Debug for Offset {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "0x{:x}", self.value)
}
}
impl Display for Offset {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "0x{:x}", self.0)
write!(f, "0x{:x}", self.value)
}
}
impl Add<Self> for Offset {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
self + rhs.value
}
}
impl Add<u64> for Offset {
type Output = Self;
fn add(self, rhs: u64) -> Self::Output {
Offset(self.value + rhs)
}
}
impl Add<usize> for Offset {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Offset(self.value + rhs as u64)
}
}
impl Sub<usize> for Offset {
type Output = Self;
fn sub(self, rhs: usize) -> Self::Output {
Offset(self.value - rhs as u64)
}
}
impl Sub<Self> for Offset {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Offset(self.value - rhs.u64())
}
}
impl AddAssign<usize> for Offset {
fn add_assign(&mut self, rhs: usize) {
*self = *self + rhs;
}
}
impl From<Offset> for u64 {
fn from(value: Offset) -> Self {
value.value
}
}
impl From<u64> for Offset {
fn from(value: u64) -> Self {
Offset(value)
}
}