Skip to content

Commit

Permalink
feat(network): impl display for [Ipv4Addr]
Browse files Browse the repository at this point in the history
Signed-off-by: Anhad Singh <[email protected]>
  • Loading branch information
Andy-Python-Programmer committed Sep 29, 2023
1 parent c1787ac commit a2e4f27
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/data_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,16 @@ impl<U: Protocol> Stack<U> for Arp {
crate::impl_stack!(@make Arp {
fn write_stage2(&self, _mem: NonNull<u8>, _payload_len: usize) {}
});

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

use alloc::string::ToString;

#[test]
fn mac_addr_fmt() {
let mac = MacAddr([0x00, 0x01, 0x02, 0x03, 0x04, 0x05]);
assert_eq!(mac.to_string(), "00:01:02:03:04:05");
}
}
21 changes: 21 additions & 0 deletions src/network.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::fmt;

use byte_endian::BigEndian;
use static_assertions::const_assert_eq;

Expand Down Expand Up @@ -31,6 +33,13 @@ impl From<[u8; Ipv4Addr::ADDR_SIZE]> for Ipv4Addr {
}
}

impl fmt::Display for Ipv4Addr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}.{}.{}.{}", self.0[0], self.0[1], self.0[2], self.0[3])?;
Ok(())
}
}

#[derive(Debug, Copy, Clone, PartialEq)]
#[repr(u8)]
pub enum Ipv4Type {
Expand Down Expand Up @@ -110,3 +119,15 @@ crate::impl_stack!(@make Ipv4 {
ipv4.checksum = checksum::make(checksum::calculate(ipv4));
}
});

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

use alloc::string::ToString;

#[test]
fn ipv4_addr_fmt() {
assert_eq!(Ipv4Addr::new([127, 0, 0, 1]).to_string(), "127.0.0.1");
}
}

0 comments on commit a2e4f27

Please sign in to comment.