From 142c9da7cd3446211e1d00963260c06729a7637e Mon Sep 17 00:00:00 2001 From: iximeow Date: Sat, 18 Jan 2020 13:49:14 -0800 Subject: finally replace `stringy` with something usable addresses must implement a function that returns a struct that is applicably formattable. particularly because the default Display impl on primitives is not necessarily desirable, if you want hex. additionally this allows meaningful Display for complex (eg, not a single primitive) addresses, such as segmented memory also expose AddressDisplay to name outside this crate. what an oversight --- src/address/mod.rs | 67 +++++++++++++++++++++++++++++++++++++++++++++--------- src/lib.rs | 5 +++- 2 files changed, 60 insertions(+), 12 deletions(-) diff --git a/src/address/mod.rs b/src/address/mod.rs index 7146d01..edda85d 100644 --- a/src/address/mod.rs +++ b/src/address/mod.rs @@ -1,6 +1,6 @@ use core::hash::Hash; -use core::fmt::{self, Debug, Display, Formatter}; +use core::fmt; use core::ops::{Add, Sub, AddAssign, SubAssign}; @@ -11,7 +11,7 @@ use num_traits::{Bounded, WrappingAdd, WrappingSub, CheckedAdd, CheckedSub}; use serde::{Deserialize, Serialize}; pub trait AddressBase where Self: - Debug + Display + AddressDisplay + + AddressDisplay + Copy + Clone + Sized + Hash + Ord + Eq + PartialEq + Bounded + Add + Sub + @@ -69,30 +69,75 @@ impl AddressBase for usize { impl Address for usize {} pub trait AddressDisplay { - fn show(&self, f: &mut Formatter) -> fmt::Result; + type Show: fmt::Display; + fn show(&self) -> Self::Show; } impl AddressDisplay for usize { - fn show(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "{:#x}", self) + type Show = AddressDisplayUsize; + + fn show(&self) -> AddressDisplayUsize { + AddressDisplayUsize(*self) + } +} + +#[repr(transparent)] +pub struct AddressDisplayUsize(usize); + +impl fmt::Display for AddressDisplayUsize { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{:#x}", self.0) } } impl AddressDisplay for u64 { - fn show(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "{:#x}", self) + type Show = AddressDisplayU64; + + fn show(&self) -> AddressDisplayU64 { + AddressDisplayU64(*self) + } +} + +#[repr(transparent)] +pub struct AddressDisplayU64(u64); + +impl fmt::Display for AddressDisplayU64 { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{:#x}", self.0) } } impl AddressDisplay for u32 { - fn show(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "{:#x}", self) + type Show = AddressDisplayU32; + + fn show(&self) -> AddressDisplayU32 { + AddressDisplayU32(*self) + } +} + +#[repr(transparent)] +pub struct AddressDisplayU32(u32); + +impl fmt::Display for AddressDisplayU32 { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{:#x}", self.0) } } impl AddressDisplay for u16 { - fn show(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "{:#x}", self) + type Show = AddressDisplayU16; + + fn show(&self) -> AddressDisplayU16 { + AddressDisplayU16(*self) + } +} + +#[repr(transparent)] +pub struct AddressDisplayU16(u16); + +impl fmt::Display for AddressDisplayU16 { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{:#x}", self.0) } } diff --git a/src/lib.rs b/src/lib.rs index f503741..17115af 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,7 +17,10 @@ extern crate termion; use serde::{Serialize, Deserialize}; mod address; -pub use address::{Address, AddressBase}; +pub use address::{Address, AddressBase, AddressDisplay}; +pub use address::{AddressDisplayUsize, AddressDisplayU64, AddressDisplayU32, AddressDisplayU16}; +#[cfg(feature="address-parse")] +pub use address::AddrParse; mod color; pub use color::{Colorize, NoColors, YaxColors}; -- cgit v1.1