From 356bbc547eeb14b5d705db2268ea3ffea65b0292 Mon Sep 17 00:00:00 2001 From: Agustin Borgna Date: Tue, 7 Nov 2023 10:36:56 +0000 Subject: [PATCH 1/2] feat: port.as_directed to match on either direction --- Cargo.toml | 1 + src/core.rs | 34 ++++++++++++++++------------------ 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1a6df4fd6..1643c110a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,6 +50,7 @@ petgraph = { version = "0.6.3", default-features = false } context-iterators = "0.2.0" serde_json = "1.0.97" delegate = "0.10.0" +either = "1.9.0" [features] pyo3 = ["dep:pyo3"] diff --git a/src/core.rs b/src/core.rs index 83bba3ab5..fa9ece839 100644 --- a/src/core.rs +++ b/src/core.rs @@ -4,6 +4,7 @@ use derive_more::From; +use either::Either::{self, Left, Right}; #[cfg(feature = "pyo3")] use pyo3::pyclass; @@ -92,35 +93,32 @@ impl Port { /// [HugrError::InvalidPortDirection] #[inline] pub fn as_incoming(&self) -> Result { - match self.direction() { - Direction::Incoming => Ok(IncomingPort { - index: self.index() as u16, - }), - dir @ Direction::Outgoing => Err(HugrError::InvalidPortDirection(dir)), - } + self.as_directed() + .left() + .ok_or(HugrError::InvalidPortDirection(self.direction())) } /// Converts to an [OutgoingPort] if this port is one; else fails with /// [HugrError::InvalidPortDirection] #[inline] pub fn as_outgoing(&self) -> Result { + self.as_directed() + .right() + .ok_or(HugrError::InvalidPortDirection(self.direction())) + } + + /// Converts to either an [IncomingPort] or an [OutgoingPort], as appropriate. + #[inline] + pub fn as_directed(&self) -> Either { match self.direction() { - Direction::Outgoing => Ok(OutgoingPort { + Direction::Incoming => Left(IncomingPort { + index: self.index() as u16, + }), + Direction::Outgoing => Right(OutgoingPort { index: self.index() as u16, }), - dir @ Direction::Incoming => Err(HugrError::InvalidPortDirection(dir)), } } - /// Creates a new outgoing port. - #[inline] - pub fn try_new_outgoing(port: impl TryInto) -> Result { - let Ok(port) = port.try_into() else { - return Err(HugrError::InvalidPortDirection(Direction::Incoming)); - }; - Ok(Self { - offset: portgraph::PortOffset::new_outgoing(port.index()), - }) - } /// Returns the direction of the port. #[inline] From bac80db6cd6f348126b4a1150be9a383becd247e Mon Sep 17 00:00:00 2001 From: Agustin Borgna Date: Tue, 7 Nov 2023 12:32:49 +0000 Subject: [PATCH 2/2] Use itertool's Either, and reexport it --- Cargo.toml | 1 - src/core.rs | 4 +++- src/lib.rs | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1643c110a..1a6df4fd6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,7 +50,6 @@ petgraph = { version = "0.6.3", default-features = false } context-iterators = "0.2.0" serde_json = "1.0.97" delegate = "0.10.0" -either = "1.9.0" [features] pyo3 = ["dep:pyo3"] diff --git a/src/core.rs b/src/core.rs index fa9ece839..4df6b52a0 100644 --- a/src/core.rs +++ b/src/core.rs @@ -2,9 +2,11 @@ //! //! These types are re-exported in the root of the crate. +pub use itertools::Either; + use derive_more::From; +use itertools::Either::{Left, Right}; -use either::Either::{self, Left, Right}; #[cfg(feature = "pyo3")] use pyo3::pyclass; diff --git a/src/lib.rs b/src/lib.rs index d1675d6e7..80a7b8edd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,7 +14,7 @@ pub mod algorithm; pub mod builder; -mod core; +pub mod core; pub mod extension; pub mod hugr; pub mod macros;