use crate::{sigscan::SigScan, traits::MemError};
pub struct Address<'a, T: SigScan> {
at: usize,
owner: &'a T,
}
impl<'a, T: SigScan> Address<'a, T> {
pub const fn new(owner: &'a T, at: usize) -> Self {
Self { at, owner }
}
pub unsafe fn read<V>(&self) -> Result<V, MemError> {
self.owner.read(self.at)
}
pub unsafe fn write<V>(&self, value: &V) -> Result<(), MemError> {
self.owner.write(self.at, &value)
}
#[inline(always)]
pub fn goto(&mut self, to: usize) {
self.at = to;
}
}
impl<'a, T: SigScan> Clone for Address<'a, T> {
fn clone(&self) -> Self {
Self {
at: self.at,
owner: self.owner,
}
}
}
impl<'a, T: SigScan> std::ops::Add<usize> for Address<'a, T> {
type Output = Self;
fn add(mut self, rhs: usize) -> Self::Output {
self.at += rhs;
self
}
}
impl<'a, T: SigScan> std::ops::Div<usize> for Address<'a, T> {
type Output = Self;
fn div(mut self, rhs: usize) -> Self::Output {
self.at /= rhs;
self
}
}
impl<'a, T: SigScan> std::ops::Mul<usize> for Address<'a, T> {
type Output = Self;
fn mul(mut self, rhs: usize) -> Self::Output {
self.at *= rhs;
self
}
}
impl<'a, T: SigScan> std::ops::Sub<usize> for Address<'a, T> {
type Output = Self;
fn sub(mut self, rhs: usize) -> Self::Output {
self.at -= rhs;
self
}
}