Skip to content

Commit

Permalink
feat(bitfield): generate fmt::UpperHex and LowerHex (#292)
Browse files Browse the repository at this point in the history
This adds generated implementations of the `UpperHex` and `LowerHex`
formatting traits to bitfield types.
  • Loading branch information
hawkw committed Aug 8, 2022
1 parent 0488696 commit b6138c8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 64 deletions.
25 changes: 25 additions & 0 deletions bitfield/src/bitfield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
/// |:--|:--|
/// | [`fmt::Debug`] | The `Debug` implementation prints the bitfield as a "struct", with a "field" for each packing spec in the bitfield. If any of the bitfield's packing specs pack typed values, that type's [`fmt::Debug`] implementation is used rather than printing the value as an integer. |
/// | [`fmt::Binary`] | Prints the raw bits of this bitfield as a binary number. |
/// | [`fmt::UpperHex`] and [`fmt::LowerHex`] | Prints the raw bits of this bitfield in hexadecimal. |
/// | [`fmt::Display`] | Pretty-prints the bitfield in a very nice-looking multi-line format which I'm rather proud of. See [here](#example-display-output) for examples of this format. |
/// | [`Copy`] | Behaves identically as the [`Copy`] implementation for the underlying integer type. |
/// | [`Clone`] | Behaves identically as the [`Clone`] implementation for the underlying integer type. |
Expand Down Expand Up @@ -259,6 +260,8 @@
/// [`fmt::Debug`]: core::fmt::Debug
/// [`fmt::Display`]: core::fmt::Display
/// [`fmt::Binary`]: core::fmt::Binary
/// [`fmt::UpperHex`]: core::fmt::UpperHex
/// [`fmt::LowerHex`]: core::fmt::LowerHex
/// [transparent]: https://doc.rust-lang.org/reference/type-layout.html#the-transparent-representation
/// [`example`]: crate::example
/// [`ExampleBitfield`]: crate::example::ExampleBitfield
Expand Down Expand Up @@ -488,6 +491,28 @@ macro_rules! bitfield {
}
}
}

#[automatically_derived]
impl core::fmt::UpperHex for $Name {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
if f.alternate() {
f.debug_tuple(stringify!($Name)).field(&format_args!("{:#X}", self.0)).finish()
} else {
f.debug_tuple(stringify!($Name)).field(&format_args!("{:X}", self.0)).finish()
}
}
}

#[automatically_derived]
impl core::fmt::LowerHex for $Name {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
if f.alternate() {
f.debug_tuple(stringify!($Name)).field(&format_args!("{:#x}", self.0)).finish()
} else {
f.debug_tuple(stringify!($Name)).field(&format_args!("{:x}", self.0)).finish()
}
}
}
};
(@field<$T:ident>, prev: $Prev:ident:
$(#[$meta:meta])*
Expand Down
36 changes: 0 additions & 36 deletions hal-x86_64/src/interrupt/idt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,42 +214,6 @@ impl Attrs {
}
}

// impl fmt::Debug for Attrs {
// fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
// f.debug_struct("Attrs")
// .field("gate_kind", &self.gate_kind())
// .field("ring", &self.ring())
// .field("is_32_bit", &self.is_32_bit())
// .field("is_present", &self.is_present())
// .field("bits", &format_args!("{:b}", self))
// .finish()
// }
// }

// impl fmt::Binary for Attrs {
// fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
// f.debug_tuple("Attrs")
// .field(&format_args!("{:#08b}", self.0))
// .finish()
// }
// }

impl fmt::UpperHex for Attrs {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_tuple("Attrs")
.field(&format_args!("{:#X}", self.0))
.finish()
}
}

impl fmt::LowerHex for Attrs {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_tuple("Attrs")
.field(&format_args!("{:#x}", self.0))
.finish()
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
29 changes: 1 addition & 28 deletions hal-x86_64/src/segment.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::cpu;
use core::{arch::asm, fmt};
use core::arch::asm;
/// Returns the current code segment selector in `%cs`.
pub fn code_segment() -> Selector {
let value: u16;
Expand Down Expand Up @@ -91,33 +91,6 @@ impl Selector {
}
}

// impl fmt::Debug for Selector {
// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// f.debug_struct("segment::Selector")
// .field("ring", &self.ring())
// .field("index", &self.index())
// .field("is_gdt", &self.is_gdt())
// .field("bits", &format_args!("{:#b}", self.0))
// .finish()
// }
// }

impl fmt::UpperHex for Selector {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("segment::Selector")
.field(&format_args!("{:#X}", self.0))
.finish()
}
}

impl fmt::LowerHex for Selector {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("segment::Selector")
.field(&format_args!("{:#x}", self.0))
.finish()
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit b6138c8

Please sign in to comment.