diff --git a/dcb-exe/src/exe/data.rs b/dcb-exe/src/exe/data.rs index a3e234b..2fce239 100644 --- a/dcb-exe/src/exe/data.rs +++ b/dcb-exe/src/exe/data.rs @@ -22,7 +22,7 @@ use std::{borrow::Borrow, cmp::Ordering}; /// /// Two data locations are considered equal if they /// share the same position. -/// +/// /// Their relative order first depends on their position. /// When their positions are equal, the larger one will /// appear first in the order. diff --git a/dcb-exe/src/exe/inst/basic/jmp/imm.rs b/dcb-exe/src/exe/inst/basic/jmp/imm.rs index b3722e2..17348d6 100644 --- a/dcb-exe/src/exe/inst/basic/jmp/imm.rs +++ b/dcb-exe/src/exe/inst/basic/jmp/imm.rs @@ -93,6 +93,6 @@ impl InstFmt for Inst { let mnemonic = self.kind.mnemonic(); let address = self.address(pos); - write!(f, "{mnemonic} {address:#x}") + write!(f, "{mnemonic} {address}") } } diff --git a/dcb-exe/src/exe/pos.rs b/dcb-exe/src/exe/pos.rs index 7616ce7..6e0d92e 100644 --- a/dcb-exe/src/exe/pos.rs +++ b/dcb-exe/src/exe/pos.rs @@ -1,16 +1,14 @@ //! Instruction position -// TODO: More implementations for `Pos` // Imports +use crate::Exe; use int_conv::{SignExtended, Signed}; use std::{convert::TryFrom, fmt, ops}; -use crate::Exe; - /// An instruction position #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash, Debug)] -#[derive(ref_cast::RefCast)] #[derive(derive_more::Display)] +#[derive(ref_cast::RefCast)] #[display(fmt = "{_0:#x}")] #[repr(transparent)] pub struct Pos(pub u32); @@ -30,28 +28,8 @@ impl Pos { } } -impl fmt::LowerHex for Pos { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - ::fmt(&self.0, f) - } -} - -impl ops::Sub for Pos { - type Output = Self; - - fn sub(self, rhs: u32) -> Self::Output { - Self(self.0 - rhs) - } -} - -impl ops::Sub for Pos { - type Output = i64; - - fn sub(self, rhs: Self) -> Self::Output { - self.0.as_signed().sign_extended::() - rhs.0.as_signed().sign_extended::() - } -} +// `Pos + u32 = Pos` impl ops::Add for Pos { type Output = Self; @@ -60,6 +38,7 @@ impl ops::Add for Pos { } } +// `Pos + i32 = Pos` impl ops::Add for Pos { type Output = Self; @@ -68,6 +47,7 @@ impl ops::Add for Pos { } } +// `Pos + i16 = Pos` impl ops::Add for Pos { type Output = Self; @@ -76,6 +56,35 @@ impl ops::Add for Pos { } } + +// `Pos - u32 = Pos` +impl ops::Sub for Pos { + type Output = Self; + + fn sub(self, rhs: u32) -> Self::Output { + Self(self.0 - rhs) + } +} + +// `Pos - Pos = i64` +impl ops::Sub for Pos { + type Output = i64; + + fn sub(self, rhs: Self) -> Self::Output { + self.0.as_signed().sign_extended::() - rhs.0.as_signed().sign_extended::() + } +} + +// `Pos & u32 = Pos` +impl ops::BitAnd for Pos { + type Output = Self; + + fn bitand(self, rhs: u32) -> Self::Output { + Self(self.0 & rhs) + } +} + +// `Pos += T <=> Pos = Pos + T` impl ops::AddAssign for Pos where Pos: ops::Add, @@ -85,33 +94,27 @@ where } } -impl ops::BitAnd for Pos { - type Output = Self; - - fn bitand(self, rhs: u32) -> Self::Output { - Self(self.0 & rhs) - } -} - -impl<'a, T> ops::Sub for &'_ Pos +// `&Pos + T` +impl ops::Add for &'_ Pos where - Pos: ops::Sub, + Pos: ops::Add, { - type Output = Pos; - - fn sub(self, rhs: T) -> Self::Output { - >::sub(Pos(self.0), rhs) - } -} - -impl<'a, T> ops::Add for &'_ Pos -where - Pos: ops::Add, -{ - type Output = Pos; + type Output = >::Output; fn add(self, rhs: T) -> Self::Output { - >::add(Pos(self.0), rhs) + >::add(*self, rhs) + } +} + +// `&Pos - T` +impl ops::Sub for &'_ Pos +where + Pos: ops::Sub, +{ + type Output = >::Output; + + fn sub(self, rhs: T) -> Self::Output { + >::sub(*self, rhs) } } @@ -120,7 +123,7 @@ impl serde::Serialize for Pos { where S: serde::Serializer, { - format_args!("{:#x}", self).serialize(serializer) + format_args!("{}", self).serialize(serializer) } } @@ -129,7 +132,6 @@ impl<'de> serde::Deserialize<'de> for Pos { where D: serde::Deserializer<'de>, { - //deserializer.deserialize_any(PosVisitor) deserializer.deserialize_u32(PosVisitor) } } @@ -137,6 +139,7 @@ impl<'de> serde::Deserialize<'de> for Pos { /// Visitor for deserializing a `Pos`. struct PosVisitor; +#[allow(clippy::map_err_ignore)] // It's clearer to provide a string than the error from `try_from` impl<'de> serde::de::Visitor<'de> for PosVisitor { type Value = Pos; @@ -148,9 +151,7 @@ impl<'de> serde::de::Visitor<'de> for PosVisitor { where E: serde::de::Error, { - #[allow(clippy::map_err_ignore)] // It's clearer to provide a string than the error from `try_from` let pos = u32::try_from(pos).map_err(|_| E::custom("Position must fit within a `u32`"))?; - Ok(Pos(pos)) } @@ -158,9 +159,7 @@ impl<'de> serde::de::Visitor<'de> for PosVisitor { where E: serde::de::Error, { - #[allow(clippy::map_err_ignore)] // It's clearer to provide a string than the error from `try_from` let pos = u32::try_from(pos).map_err(|_| E::custom("Position must fit within a `u32`"))?; - Ok(Pos(pos)) } diff --git a/dcb-util/src/lib.rs b/dcb-util/src/lib.rs index bab40c5..0ff3e9e 100644 --- a/dcb-util/src/lib.rs +++ b/dcb-util/src/lib.rs @@ -91,17 +91,17 @@ pub mod null_ascii_string; #[macro_use] pub mod impl_bytes; pub mod discarding_sorted_merge_iter; +pub mod next_from_bytes; pub mod peekable_iter; pub mod signed_hex; -pub mod next_from_bytes; // Exports //pub use array_split::{array_split, array_split_mut}; pub use ascii_str_arr::AsciiStrArr; pub use discarding_sorted_merge_iter::DiscardingSortedMergeIter; +pub use next_from_bytes::NextFromBytes; pub use peekable_iter::PeekableIter; pub use signed_hex::SignedHex; -pub use next_from_bytes::NextFromBytes; /// Returns the absolute different between `a` and `b`, `a - b` as a `i64`. /// diff --git a/dcb/src/card/table/header.rs b/dcb/src/card/table/header.rs index 2316ad5..d14370b 100644 --- a/dcb/src/card/table/header.rs +++ b/dcb/src/card/table/header.rs @@ -7,11 +7,9 @@ pub mod error; pub use error::FromBytesError; // Imports -use dcb_util::{ - array_split, array_split_mut, -}; use byteorder::{ByteOrder, LittleEndian}; use dcb_bytes::Bytes; +use dcb_util::{array_split, array_split_mut}; /// The header #[derive(PartialEq, Eq, Clone, Copy, Debug)]