mirror of
https://github.com/Zenithsiz/dcb.git
synced 2026-02-08 19:34:27 +00:00
Added dcb_util::DisplayWrapper and now using it in dcb-exe.
This commit is contained in:
@@ -52,21 +52,12 @@ impl Kind {
|
||||
/// Returns a displayable with the value of this kind
|
||||
#[must_use]
|
||||
pub fn value_fmt(self) -> impl fmt::Display {
|
||||
/// Display wrapper
|
||||
struct FmtValue(Kind);
|
||||
|
||||
impl fmt::Display for FmtValue {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self.0 {
|
||||
// Signed
|
||||
Kind::Add(rhs) | Kind::AddUnsigned(rhs) | Kind::SetLessThan(rhs) => write!(f, "{:#}", SignedHex(rhs)),
|
||||
// Unsigned
|
||||
Kind::SetLessThanUnsigned(rhs) | Kind::And(rhs) | Kind::Or(rhs) | Kind::Xor(rhs) => write!(f, "{rhs:#x}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FmtValue(self)
|
||||
dcb_util::DisplayWrapper::new(move |f| match self {
|
||||
// Signed
|
||||
Self::Add(rhs) | Self::AddUnsigned(rhs) | Self::SetLessThan(rhs) => write!(f, "{:#}", SignedHex(rhs)),
|
||||
// Unsigned
|
||||
Self::SetLessThanUnsigned(rhs) | Self::And(rhs) | Self::Or(rhs) | Self::Xor(rhs) => write!(f, "{rhs:#x}"),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -39,19 +39,10 @@ impl Kind {
|
||||
/// Returns a displayable with the value of this kind
|
||||
#[must_use]
|
||||
pub fn value_fmt(self) -> impl fmt::Display {
|
||||
/// Display wrapper
|
||||
struct FmtValue(Kind);
|
||||
|
||||
impl fmt::Display for FmtValue {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self.0 {
|
||||
Kind::Imm { kind } => write!(f, "{}", kind.value_fmt()),
|
||||
Kind::Reg { rhs, .. } => write!(f, "{}", rhs),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FmtValue(self)
|
||||
dcb_util::DisplayWrapper::new(move |f| match self {
|
||||
Self::Imm { kind } => write!(f, "{}", kind.value_fmt()),
|
||||
Self::Reg { rhs, .. } => write!(f, "{}", rhs),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -45,21 +45,13 @@ impl LoadImmKind {
|
||||
|
||||
/// Returns a displayable with the value of this load kind formatted.
|
||||
pub fn value_fmt(self) -> impl fmt::Display {
|
||||
struct FmtValue(Self);
|
||||
|
||||
impl fmt::Display for FmtValue {
|
||||
#[rustfmt::skip]
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self.0 {
|
||||
LoadImmKind::Address(address) => write!(f, "{address:#x}"),
|
||||
LoadImmKind::Word(value) => write!(f, "{value:#x}"),
|
||||
LoadImmKind::HalfWordUnsigned(value) => write!(f, "{value:#x}"),
|
||||
LoadImmKind::HalfWordSigned(value) => write!(f, "{}", SignedHex(value)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FmtValue(self)
|
||||
#[rustfmt::skip]
|
||||
dcb_util::DisplayWrapper::new(move |f| match self {
|
||||
Self::Address(address) => write!(f, "{address:#x}"),
|
||||
Self::Word(value) => write!(f, "{value:#x}"),
|
||||
Self::HalfWordUnsigned(value) => write!(f, "{value:#x}"),
|
||||
Self::HalfWordSigned(value) => write!(f, "{}", SignedHex(value)),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
22
dcb-util/src/display_wrapper.rs
Normal file
22
dcb-util/src/display_wrapper.rs
Normal file
@@ -0,0 +1,22 @@
|
||||
//! Display wrapper.
|
||||
|
||||
// Imports
|
||||
use std::{cell::RefCell, fmt};
|
||||
|
||||
/// A display wrapper using `F`
|
||||
pub struct DisplayWrapper<F: FnMut(&mut fmt::Formatter) -> fmt::Result>(RefCell<F>);
|
||||
|
||||
impl<F: FnMut(&mut fmt::Formatter) -> fmt::Result> DisplayWrapper<F> {
|
||||
/// Creates a new display wrapper
|
||||
pub fn new(func: F) -> Self {
|
||||
Self(RefCell::new(func))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl<F: FnMut(&mut fmt::Formatter) -> fmt::Result> fmt::Display for DisplayWrapper<F> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
// Note: `f` cannot be re-entrant, so this cannot fail
|
||||
self.0.borrow_mut()(f)
|
||||
}
|
||||
}
|
||||
@@ -91,6 +91,7 @@ pub mod null_ascii_string;
|
||||
#[macro_use]
|
||||
pub mod impl_bytes;
|
||||
pub mod discarding_sorted_merge_iter;
|
||||
pub mod display_wrapper;
|
||||
pub mod next_from_bytes;
|
||||
pub mod peekable_iter;
|
||||
pub mod signed_hex;
|
||||
@@ -99,6 +100,7 @@ pub mod signed_hex;
|
||||
//pub use array_split::{array_split, array_split_mut};
|
||||
pub use ascii_str_arr::AsciiStrArr;
|
||||
pub use discarding_sorted_merge_iter::DiscardingSortedMergeIter;
|
||||
pub use display_wrapper::DisplayWrapper;
|
||||
pub use next_from_bytes::NextFromBytes;
|
||||
pub use peekable_iter::PeekableIter;
|
||||
pub use signed_hex::SignedHex;
|
||||
|
||||
Reference in New Issue
Block a user