From 69b3ba755281d9b5e5a5c3bf61002d49ae70f3fe Mon Sep 17 00:00:00 2001 From: Szymon Walter Date: Tue, 20 Mar 2018 10:13:21 +0100 Subject: [PATCH] `impl` `Debug`, `LowerHex`, `Display` for `Address` --- src/block.rs | 25 ++++++++++++++++++++++++- src/lib.rs | 1 + 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/block.rs b/src/block.rs index 357cefb..4c43dc1 100644 --- a/src/block.rs +++ b/src/block.rs @@ -1,5 +1,6 @@ use core::marker::PhantomData; use core::ops::{Add, Sub}; +use core::fmt::{self, Debug, Display, LowerHex}; pub trait Size { // log_block_size = log_2(block_size) - 10 @@ -33,7 +34,7 @@ impl Size for Size8192 { const LOG_SIZE: u32 = 3; } -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[derive(Clone, Copy, PartialEq, Eq, Hash)] pub struct Address { block: usize, offset: usize, @@ -67,6 +68,28 @@ impl Address { } } +impl Debug for Address { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let name = format!("Address<{}>", S::SIZE); + f.debug_struct(&name) + .field("block", &self.block) + .field("offset", &self.offset) + .finish() + } +} + +impl Display for Address { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}:{}", self.block, self.offset) + } +} + +impl LowerHex for Address { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{:x}:{:x}", self.block, self.offset) + } +} + impl From for Address { fn from(idx: usize) -> Address { let block = idx >> (S::LOG_SIZE + 10); diff --git a/src/lib.rs b/src/lib.rs index 4052a75..da79cff 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,7 @@ #![feature(macro_lifetime_matcher)] #![cfg_attr(all(not(test), feature = "no_std"), no_std)] +#[macro_use] extern crate alloc; #[macro_use] extern crate bitflags;