Revised dcb_exe::exe::pos's impls.

Ran `cargo fmt` on the whole project.
This commit is contained in:
Filipe Rodrigues 2021-01-09 14:37:15 +00:00
parent 21a0d7a4cb
commit 764628e691
5 changed files with 58 additions and 61 deletions

View File

@ -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.

View File

@ -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}")
}
}

View File

@ -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 {
<u32 as fmt::LowerHex>::fmt(&self.0, f)
}
}
impl ops::Sub<u32> for Pos {
type Output = Self;
fn sub(self, rhs: u32) -> Self::Output {
Self(self.0 - rhs)
}
}
impl ops::Sub<Pos> for Pos {
type Output = i64;
fn sub(self, rhs: Self) -> Self::Output {
self.0.as_signed().sign_extended::<i64>() - rhs.0.as_signed().sign_extended::<i64>()
}
}
// `Pos + u32 = Pos`
impl ops::Add<u32> for Pos {
type Output = Self;
@ -60,6 +38,7 @@ impl ops::Add<u32> for Pos {
}
}
// `Pos + i32 = Pos`
impl ops::Add<i32> for Pos {
type Output = Self;
@ -68,6 +47,7 @@ impl ops::Add<i32> for Pos {
}
}
// `Pos + i16 = Pos`
impl ops::Add<i16> for Pos {
type Output = Self;
@ -76,6 +56,35 @@ impl ops::Add<i16> for Pos {
}
}
// `Pos - u32 = Pos`
impl ops::Sub<u32> for Pos {
type Output = Self;
fn sub(self, rhs: u32) -> Self::Output {
Self(self.0 - rhs)
}
}
// `Pos - Pos = i64`
impl ops::Sub<Pos> for Pos {
type Output = i64;
fn sub(self, rhs: Self) -> Self::Output {
self.0.as_signed().sign_extended::<i64>() - rhs.0.as_signed().sign_extended::<i64>()
}
}
// `Pos & u32 = Pos`
impl ops::BitAnd<u32> for Pos {
type Output = Self;
fn bitand(self, rhs: u32) -> Self::Output {
Self(self.0 & rhs)
}
}
// `Pos += T <=> Pos = Pos + T`
impl<T> ops::AddAssign<T> for Pos
where
Pos: ops::Add<T, Output = Self>,
@ -85,33 +94,27 @@ where
}
}
impl ops::BitAnd<u32> for Pos {
type Output = Self;
fn bitand(self, rhs: u32) -> Self::Output {
Self(self.0 & rhs)
}
}
impl<'a, T> ops::Sub<T> for &'_ Pos
// `&Pos + T`
impl<T> ops::Add<T> for &'_ Pos
where
Pos: ops::Sub<T, Output = Pos>,
Pos: ops::Add<T>,
{
type Output = Pos;
fn sub(self, rhs: T) -> Self::Output {
<Pos as ops::Sub<T>>::sub(Pos(self.0), rhs)
}
}
impl<'a, T> ops::Add<T> for &'_ Pos
where
Pos: ops::Add<T, Output = Pos>,
{
type Output = Pos;
type Output = <Pos as ops::Add<T>>::Output;
fn add(self, rhs: T) -> Self::Output {
<Pos as ops::Add<T>>::add(Pos(self.0), rhs)
<Pos as ops::Add<T>>::add(*self, rhs)
}
}
// `&Pos - T`
impl<T> ops::Sub<T> for &'_ Pos
where
Pos: ops::Sub<T>,
{
type Output = <Pos as ops::Sub<T>>::Output;
fn sub(self, rhs: T) -> Self::Output {
<Pos as ops::Sub<T>>::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))
}

View File

@ -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`.
///

View File

@ -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)]