Skip to content

Commit

Permalink
defmt: add impl for core::net
Browse files Browse the repository at this point in the history
  • Loading branch information
newAM committed Mar 5, 2023
1 parent 54f6aae commit e69ed0b
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

- [#733]: `defmt`: Add formatting for `core::net` with the `ip_in_core` feature

[#733]: https:/knurling-rs/defmt/pull/733

## defmt-decoder v0.3.4, defmt-print v0.3.4

- [#729]: Release `defmt-decoder v0.3.4`, `defmt-print v0.3.4`
Expand Down
1 change: 1 addition & 0 deletions defmt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ version = "0.3.2"

[features]
alloc = []
ip_in_core = []

# Encoding feature flags. These should only be set by end-user crates, not by library crates.
#
Expand Down
2 changes: 2 additions & 0 deletions defmt/src/impls/core_/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
mod alloc_;
mod array;
mod cell;
#[cfg(feature = "ip_in_core")]
mod net;
mod num;
mod ops;
mod slice;
Expand Down
69 changes: 69 additions & 0 deletions defmt/src/impls/core_/net.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use core::net;

use super::*;

impl Format for net::AddrParseError {
fn format(&self, fmt: Formatter) {
crate::write!(fmt, "AddrParseError(_)");
}
}

impl Format for net::Ipv4Addr {
fn format(&self, fmt: Formatter) {
crate::write!(
fmt,
"{}.{}.{}.{}",
self.octets()[0],
self.octets()[1],
self.octets()[2],
self.octets()[3]
);
}
}

impl Format for net::SocketAddrV4 {
fn format(&self, fmt: Formatter) {
crate::write!(fmt, "{}:{}", self.ip(), self.port());
}
}

impl Format for net::Ipv6Addr {
fn format(&self, fmt: Formatter) {
crate::write!(
fmt,
"{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}",
self.octets()[0],
self.octets()[1],
self.octets()[2],
self.octets()[3],
self.octets()[4],
self.octets()[5],
self.octets()[6],
self.octets()[7]
);
}
}

impl Format for net::SocketAddrV6 {
fn format(&self, fmt: Formatter) {
crate::write!(fmt, "[{}]:{}", self.ip(), self.port());
}
}

impl Format for net::IpAddr {
fn format(&self, fmt: Formatter) {
match self {
net::IpAddr::V4(a) => crate::write!(fmt, "{}", a),
net::IpAddr::V6(a) => crate::write!(fmt, "{}", a),
}
}
}

impl Format for net::SocketAddr {
fn format(&self, fmt: Formatter) {
match self {
net::SocketAddr::V4(a) => crate::write!(fmt, "{}", a),
net::SocketAddr::V6(a) => crate::write!(fmt, "{}", a),
}
}
}
1 change: 1 addition & 0 deletions defmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// NOTE if you change this URL you'll also need to update all other crates in this repo
#![doc(html_logo_url = "https://knurling.ferrous-systems.com/knurling_logo_light_text.svg")]
#![warn(missing_docs)]
#![cfg_attr(feature = "ip_in_core", feature(ip_in_core))]

#[cfg(feature = "alloc")]
extern crate alloc;
Expand Down

0 comments on commit e69ed0b

Please sign in to comment.