impl Debug, LowerHex, Display for Address

This commit is contained in:
Szymon Walter 2018-03-20 10:13:21 +01:00
parent 3dca5904a1
commit 69b3ba7552
2 changed files with 25 additions and 1 deletions

View file

@ -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<S: Size> {
block: usize,
offset: usize,
@ -67,6 +68,28 @@ impl<S: Size> Address<S> {
}
}
impl<S: Size> Debug for Address<S> {
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<S: Size> Display for Address<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}:{}", self.block, self.offset)
}
}
impl<S: Size> LowerHex for Address<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:x}:{:x}", self.block, self.offset)
}
}
impl<S: Size> From<usize> for Address<S> {
fn from(idx: usize) -> Address<S> {
let block = idx >> (S::LOG_SIZE + 10);

View file

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