2023-06-24 17:18:31 -05:00
|
|
|
//! HoleyBytes register value definition
|
|
|
|
|
2023-07-24 11:48:42 -05:00
|
|
|
use sealed::sealed;
|
2023-06-20 19:07:48 -05:00
|
|
|
|
2023-06-24 17:16:14 -05:00
|
|
|
/// Define [`Value`] union
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
/// Union variants have to be sound to byte-reinterpretate
|
|
|
|
/// between each other. Otherwise the behaviour is undefined.
|
2023-06-20 19:07:48 -05:00
|
|
|
macro_rules! value_def {
|
|
|
|
($($ty:ident),* $(,)?) => {
|
2023-06-24 17:16:14 -05:00
|
|
|
/// HBVM register value
|
2023-06-20 19:07:48 -05:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
#[repr(packed)]
|
|
|
|
pub union Value {
|
|
|
|
$(pub $ty: $ty),*
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$(
|
|
|
|
impl From<$ty> for Value {
|
|
|
|
#[inline]
|
|
|
|
fn from(value: $ty) -> Self {
|
|
|
|
Self { $ty: value }
|
|
|
|
}
|
|
|
|
}
|
2023-07-24 11:48:42 -05:00
|
|
|
|
|
|
|
static_assertions::const_assert_eq!(
|
|
|
|
core::mem::size_of::<$ty>(),
|
|
|
|
core::mem::size_of::<Value>(),
|
|
|
|
);
|
|
|
|
|
|
|
|
#[sealed]
|
|
|
|
unsafe impl ValueVariant for $ty {}
|
2023-06-20 19:07:48 -05:00
|
|
|
)*
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-07-24 11:48:42 -05:00
|
|
|
impl Value {
|
|
|
|
#[inline]
|
|
|
|
pub fn cast<Variant: ValueVariant>(self) -> Variant {
|
|
|
|
union Transmute<Variant: ValueVariant> {
|
|
|
|
src: Value,
|
|
|
|
variant: Variant,
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe { Transmute { src: self }.variant }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// # Safety
|
|
|
|
/// - N/A, not to be implemented manually
|
|
|
|
#[sealed]
|
|
|
|
pub unsafe trait ValueVariant: Copy + Into<Value> {}
|
|
|
|
|
2023-06-20 19:07:48 -05:00
|
|
|
value_def!(u64, i64, f64);
|
2023-06-24 17:16:14 -05:00
|
|
|
static_assertions::const_assert_eq!(core::mem::size_of::<Value>(), 8);
|
2023-06-20 19:07:48 -05:00
|
|
|
|
2023-07-24 11:48:42 -05:00
|
|
|
impl core::fmt::Debug for Value {
|
2023-06-20 19:07:48 -05:00
|
|
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
2023-06-24 17:16:14 -05:00
|
|
|
// Print formatted as hexadecimal, unsigned integer
|
2023-07-24 11:48:42 -05:00
|
|
|
write!(f, "{:x}", self.cast::<u64>())
|
2023-06-20 19:07:48 -05:00
|
|
|
}
|
|
|
|
}
|