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.master
parent
ff8316895a
commit
989a6eb0e6
12
src/mac.rs
12
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::<Vec<_>>()
|
||||
.join(":");
|
||||
write!(f, "{}", s)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
|
Loading…
Reference in New Issue