Skip to content

Commit

Permalink
add custom Debug implementation for concrete::Address
Browse files Browse the repository at this point in the history
part of #7
  • Loading branch information
benmaddison committed May 5, 2022
1 parent 4a8f5a7 commit 1f0ff07
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
11 changes: 7 additions & 4 deletions src/concrete/addr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,18 @@ impl<A: Afi> FromStr for Address<A> {
}
}

impl<A: Afi> fmt::Display for Address<A>
where
A::Primitive: AddressDisplay<A>,
{
impl<A: Afi> fmt::Display for Address<A> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.into_primitive().fmt_addr(f)
}
}

impl<A: Afi> fmt::Debug for Address<A> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Address<{:?}>({})", A::as_afi(), self)
}
}

#[cfg(any(test, feature = "arbitrary"))]
use proptest::{
arbitrary::{any_with, Arbitrary, ParamsFor, StrategyFor},
Expand Down
2 changes: 1 addition & 1 deletion src/concrete/addr/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
};

/// An IP address.
#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Clone, Copy, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Address<A: Afi>(A::Primitive);

impl<A: Afi> Address<A> {
Expand Down
1 change: 1 addition & 0 deletions src/concrete/af.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ impl<A: traits::Afi> traits::AfiClass for A {
}
}

#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum Afi {
Ipv4,
Ipv6,
Expand Down
4 changes: 2 additions & 2 deletions src/traits/af.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::cmp::Ord;
use core::fmt::Debug;
use core::hash::Hash;

use crate::{any, concrete};
use crate::{any, concrete, fmt};

use super::{Address, Mask, Prefix, PrefixLength};

Expand All @@ -14,7 +14,7 @@ pub trait Afi: Copy + Debug + Hash + Ord {
// This bound required to satisfy coherence rules when implementing
// `From<A::Octets> for Address<A>`
type Octets: Borrow<[u8]>;
type Primitive: primitive::Address<Self>;
type Primitive: primitive::Address<Self> + fmt::AddressDisplay<Self>;

/// Get the [`concrete::Afi`] variant associated with `Self`.
fn as_afi() -> concrete::Afi;
Expand Down

0 comments on commit 1f0ff07

Please sign in to comment.