Skip to content

Commit

Permalink
Merge #754
Browse files Browse the repository at this point in the history
754: Better defmt for Instant, Duration and Ipv6Address r=Dirbaio a=thvdveld

This changes the defmt formatting of Instant, Duration and Ipv6Address. They now have the same display as fmt::Display.

Co-authored-by: Thibaut Vandervelden <thvdveld@vub.be>
  • Loading branch information
bors[bot] and thvdveld committed Mar 20, 2023
2 parents bc12398 + 91fc759 commit 9eb8292
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 8 deletions.
16 changes: 14 additions & 2 deletions src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use core::{fmt, ops};
/// * A value less than `0` indicates a time before the starting
/// point.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Instant {
micros: i64,
}
Expand Down Expand Up @@ -134,6 +133,13 @@ impl fmt::Display for Instant {
}
}

#[cfg(feature = "defmt")]
impl defmt::Format for Instant {
fn format(&self, f: defmt::Formatter) {
defmt::write!(f, "{}.{:03}s", self.secs(), self.millis());
}
}

impl ops::Add<Duration> for Instant {
type Output = Instant;

Expand Down Expand Up @@ -172,7 +178,6 @@ impl ops::Sub<Instant> for Instant {

/// A relative amount of time.
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Duration {
micros: u64,
}
Expand Down Expand Up @@ -230,6 +235,13 @@ impl fmt::Display for Duration {
}
}

#[cfg(feature = "defmt")]
impl defmt::Format for Duration {
fn format(&self, f: defmt::Formatter) {
defmt::write!(f, "{}.{:03}s", self.secs(), self.millis());
}
}

impl ops::Add<Duration> for Duration {
type Output = Duration;

Expand Down
27 changes: 24 additions & 3 deletions src/wire/ieee802154.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,34 @@ impl Pan {

/// A IEEE 802.15.4 address.
#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Address {
Absent,
Short([u8; 2]),
Extended([u8; 8]),
}

#[cfg(feature = "defmt")]
impl defmt::Format for Address {
fn format(&self, f: defmt::Formatter) {
match self {
Self::Absent => defmt::write!(f, "not-present"),
Self::Short(bytes) => defmt::write!(f, "{:02x}:{:02x}", bytes[0], bytes[1]),
Self::Extended(bytes) => defmt::write!(
f,
"{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
bytes[0],
bytes[1],
bytes[2],
bytes[3],
bytes[4],
bytes[5],
bytes[6],
bytes[7]
),
}
}
}

#[cfg(test)]
impl Default for Address {
fn default() -> Self {
Expand Down Expand Up @@ -171,10 +192,10 @@ impl fmt::Display for Address {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Absent => write!(f, "not-present"),
Self::Short(bytes) => write!(f, "{:02x}-{:02x}", bytes[0], bytes[1]),
Self::Short(bytes) => write!(f, "{:02x}:{:02x}", bytes[0], bytes[1]),
Self::Extended(bytes) => write!(
f,
"{:02x}-{:02x}-{:02x}-{:02x}-{:02x}-{:02x}-{:02x}-{:02x}",
"{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7]
),
}
Expand Down
2 changes: 1 addition & 1 deletion src/wire/ipv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ impl fmt::Display for Cidr {
#[cfg(feature = "defmt")]
impl defmt::Format for Cidr {
fn format(&self, f: defmt::Formatter) {
defmt::write!(f, "{:?}/{=u8}", self.address, self.prefix_len);
defmt::write!(f, "{}/{=u8}", self.address, self.prefix_len);
}
}

Expand Down
70 changes: 68 additions & 2 deletions src/wire/ipv6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ pub const IPV4_MAPPED_PREFIX_SIZE: usize = ADDR_SIZE - 4; // 4 == ipv4::ADDR_SIZ

/// A sixteen-octet IPv6 address.
#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Default)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Address(pub [u8; ADDR_SIZE]);

impl Address {
Expand Down Expand Up @@ -305,6 +304,67 @@ impl fmt::Display for Address {
}
}

#[cfg(feature = "defmt")]
impl defmt::Format for Address {
fn format(&self, f: defmt::Formatter) {
if self.is_ipv4_mapped() {
return defmt::write!(
f,
"::ffff:{}.{}.{}.{}",
self.0[IPV4_MAPPED_PREFIX_SIZE + 0],
self.0[IPV4_MAPPED_PREFIX_SIZE + 1],
self.0[IPV4_MAPPED_PREFIX_SIZE + 2],
self.0[IPV4_MAPPED_PREFIX_SIZE + 3]
);
}

// The string representation of an IPv6 address should
// collapse a series of 16 bit sections that evaluate
// to 0 to "::"
//
// See https://tools.ietf.org/html/rfc4291#section-2.2
// for details.
enum State {
Head,
HeadBody,
Tail,
TailBody,
}
let mut words = [0u16; 8];
self.write_parts(&mut words);
let mut state = State::Head;
for word in words.iter() {
state = match (*word, &state) {
// Once a u16 equal to zero write a double colon and
// skip to the next non-zero u16.
(0, &State::Head) | (0, &State::HeadBody) => {
defmt::write!(f, "::");
State::Tail
}
// Continue iterating without writing any characters until
// we hit a non-zero value.
(0, &State::Tail) => State::Tail,
// When the state is Head or Tail write a u16 in hexadecimal
// without the leading colon if the value is not 0.
(_, &State::Head) => {
defmt::write!(f, "{:x}", word);
State::HeadBody
}
(_, &State::Tail) => {
defmt::write!(f, "{:x}", word);
State::TailBody
}
// Write the u16 with a leading colon when parsing a value
// that isn't the first in a section
(_, &State::HeadBody) | (_, &State::TailBody) => {
defmt::write!(f, ":{:x}", word);
state
}
}
}
}
}

#[cfg(feature = "proto-ipv4")]
/// Convert the given IPv4 address into a IPv4-mapped IPv6 address
impl From<ipv4::Address> for Address {
Expand All @@ -319,7 +379,6 @@ impl From<ipv4::Address> for Address {
/// A specification of an IPv6 CIDR block, containing an address and a variable-length
/// subnet masking prefix length.
#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Default)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Cidr {
address: Address,
prefix_len: u8,
Expand Down Expand Up @@ -384,6 +443,13 @@ impl fmt::Display for Cidr {
}
}

#[cfg(feature = "defmt")]
impl defmt::Format for Cidr {
fn format(&self, f: defmt::Formatter) {
defmt::write!(f, "{}/{=u8}", self.address, self.prefix_len);
}
}

/// A read/write wrapper around an Internet Protocol version 6 packet buffer.
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
Expand Down

0 comments on commit 9eb8292

Please sign in to comment.