Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: port.as_directed to match on either direction #647

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an Either in itertools already, can you use that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, and re-exported it so we don't require itertools on the dependants.


[features]
pyo3 = ["dep:pyo3"]
Expand Down
34 changes: 16 additions & 18 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use derive_more::From;

use either::Either::{self, Left, Right};
#[cfg(feature = "pyo3")]
use pyo3::pyclass;

Expand Down Expand Up @@ -92,35 +93,32 @@ impl Port {
/// [HugrError::InvalidPortDirection]
#[inline]
pub fn as_incoming(&self) -> Result<IncomingPort, HugrError> {
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<OutgoingPort, HugrError> {
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<IncomingPort, OutgoingPort> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly in_or_out? But happy with as_directed too

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say in_or_out would be more similar to direction, where as_* is a cast.

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<OutgoingPort>) -> Result<Self, HugrError> {
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]
Expand Down