diff options
| author | iximeow <me@iximeow.net> | 2020-01-18 13:49:14 -0800 | 
|---|---|---|
| committer | iximeow <me@iximeow.net> | 2020-01-18 13:49:14 -0800 | 
| commit | 142c9da7cd3446211e1d00963260c06729a7637e (patch) | |
| tree | 43830627b64f0e0a52e9644e56550e8d8cf98959 /src | |
| parent | 894cba0dde913d86472430eae8b7ba0353711b4e (diff) | |
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
Diffstat (limited to 'src')
| -rw-r--r-- | src/address/mod.rs | 67 | ||||
| -rw-r--r-- | 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<Output=Self> + Sub<Output=Self> + @@ -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)      }  } @@ -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}; | 
