aboutsummaryrefslogtreecommitdiff
path: root/src/address/mod.rs
diff options
context:
space:
mode:
authoriximeow <me@iximeow.net>2020-01-18 13:49:14 -0800
committeriximeow <me@iximeow.net>2020-01-18 13:49:14 -0800
commit142c9da7cd3446211e1d00963260c06729a7637e (patch)
tree43830627b64f0e0a52e9644e56550e8d8cf98959 /src/address/mod.rs
parent894cba0dde913d86472430eae8b7ba0353711b4e (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/address/mod.rs')
-rw-r--r--src/address/mod.rs67
1 files changed, 56 insertions, 11 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)
}
}