Skip to content

Commit

Permalink
feat: Nicer debug and display for core types (#628)
Browse files Browse the repository at this point in the history
Adds more dev-friendly outputs for both `Debug` and `Display` for `Node,
Port, IncomingPort, OutgoingPort, Wire, CircuitUnit`.

```
Node(0)
Port(Incoming, 0)
IncomingPort(0)
OutgoingPort(0)
Wire { node: 0, port: 0 }
```
CircuitUnit variants:
```
WireUnit { node: 0, port: 0 }
LinearUnit(0)
```
  • Loading branch information
aborgna-q committed Oct 26, 2023
1 parent ce15278 commit 0beb165
Showing 1 changed file with 67 additions and 16 deletions.
83 changes: 67 additions & 16 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,7 @@ use crate::hugr::HugrError;

/// A handle to a node in the HUGR.
#[derive(
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Debug,
From,
serde::Serialize,
serde::Deserialize,
Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, From, serde::Serialize, serde::Deserialize,
)]
#[serde(transparent)]
#[cfg_attr(feature = "pyo3", pyclass)]
Expand All @@ -39,7 +29,6 @@ pub struct Node {
Ord,
Hash,
Default,
Debug,
From,
serde::Serialize,
serde::Deserialize,
Expand All @@ -63,21 +52,21 @@ pub trait NodeIndex {
}

/// A port in the incoming direction.
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash, Default, Debug)]
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash, Default)]
pub struct IncomingPort {
index: u16,
}

/// A port in the outgoing direction.
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash, Default, Debug)]
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash, Default)]
pub struct OutgoingPort {
index: u16,
}

/// The direction of a port.
pub type Direction = portgraph::Direction;

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
/// A DataFlow wire, defined by a Value-kind output port of a node
// Stores node and offset to output port
pub struct Wire(Node, usize);
Expand Down Expand Up @@ -249,7 +238,7 @@ impl Wire {
///
/// Falls back to [`Wire`] if the wire is not linear or if it's not possible to
/// track the origin.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum CircuitUnit {
/// Arbitrary input wire.
Wire(Wire),
Expand Down Expand Up @@ -280,3 +269,65 @@ impl From<Wire> for CircuitUnit {
CircuitUnit::Wire(value)
}
}

impl std::fmt::Debug for Node {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("Node").field(&self.index()).finish()
}
}

impl std::fmt::Debug for Port {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("Port")
.field(&self.offset.direction())
.field(&self.index())
.finish()
}
}

impl std::fmt::Debug for IncomingPort {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("IncomingPort").field(&self.index).finish()
}
}

impl std::fmt::Debug for OutgoingPort {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("OutgoingPort").field(&self.index).finish()
}
}

impl std::fmt::Debug for Wire {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Wire")
.field("node", &self.0.index())
.field("port", &self.1)
.finish()
}
}

impl std::fmt::Debug for CircuitUnit {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Wire(w) => f
.debug_struct("WireUnit")
.field("node", &w.0.index())
.field("port", &w.1)
.finish(),
Self::Linear(id) => f.debug_tuple("LinearUnit").field(id).finish(),
}
}
}

macro_rules! impl_display_from_debug {
($($t:ty),*) => {
$(
impl std::fmt::Display for $t {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
<Self as std::fmt::Debug>::fmt(self, f)
}
}
)*
};
}
impl_display_from_debug!(Node, Port, IncomingPort, OutgoingPort, Wire, CircuitUnit);

0 comments on commit 0beb165

Please sign in to comment.