From 989a6eb0e63e5aa6442612949e85779dfc6ba665 Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Sat, 29 Sep 2018 11:39:19 -0500 Subject: [PATCH] mac: impl Debug and Display for MacAddress Adding implementations of the `Debug` and `Display` traits to the `MacAddress` struct allows creating string representations of these objects. --- src/mac.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/mac.rs b/src/mac.rs index b87ae88..8f61141 100644 --- a/src/mac.rs +++ b/src/mac.rs @@ -37,6 +37,7 @@ impl error::Error for ParseError { } +#[derive(Debug)] pub struct MacAddress { addr: [u8; 6], } @@ -67,6 +68,17 @@ impl MacAddress { } +impl fmt::Display for MacAddress { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let s = self.addr.iter() + .map(|b| format!("{:x}", b)) + .collect::>() + .join(":"); + write!(f, "{}", s) + } +} + + #[cfg(test)] mod test { use super::*;