Skip to content

Commit

Permalink
refactor!: No Ports in TypeRow (#1087)
Browse files Browse the repository at this point in the history
TypeRow is not just used for node input/output signatures, but also for
elements of tuples/sums, and FunctionTypes that are not part of Nodes.
So, using Port to index into it seems odd...

BREAKING CHANGE: get() and get_mut() now take only usize, so must call
PortIndex::index() on the argument first
  • Loading branch information
acl-cqc authored May 20, 2024
1 parent bcffabd commit 02f3864
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
9 changes: 5 additions & 4 deletions hugr/src/types/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::fmt::{self, Display, Write};
use super::type_param::TypeParam;
use super::{subst_row, Substitution, Type, TypeRow};

use crate::core::PortIndex;
use crate::extension::{ExtensionRegistry, ExtensionSet, SignatureError};
use crate::{Direction, IncomingPort, OutgoingPort, Port};

Expand Down Expand Up @@ -98,28 +99,28 @@ impl FunctionType {
/// of bounds.
#[inline]
pub fn in_port_type(&self, port: impl Into<IncomingPort>) -> Option<&Type> {
self.input.get(port.into())
self.input.get(port.into().index())
}

/// Returns the type of a value output [`Port`]. Returns `None` if the port is out
/// of bounds.
#[inline]
pub fn out_port_type(&self, port: impl Into<OutgoingPort>) -> Option<&Type> {
self.output.get(port.into())
self.output.get(port.into().index())
}

/// Returns a mutable reference to the type of a value input [`Port`]. Returns `None` if the port is out
/// of bounds.
#[inline]
pub fn in_port_type_mut(&mut self, port: impl Into<IncomingPort>) -> Option<&mut Type> {
self.input.get_mut(port.into())
self.input.get_mut(port.into().index())
}

/// Returns the type of a value output [`Port`]. Returns `None` if the port is out
/// of bounds.
#[inline]
pub fn out_port_type_mut(&mut self, port: impl Into<OutgoingPort>) -> Option<&mut Type> {
self.output.get_mut(port.into())
self.output.get_mut(port.into().index())
}

/// Returns a mutable reference to the type of a value [`Port`].
Expand Down
23 changes: 10 additions & 13 deletions hugr/src/types/type_row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use std::{

use super::Type;
use crate::utils::display_list;
use crate::PortIndex;
use delegate::delegate;
use itertools::Itertools;

Expand Down Expand Up @@ -38,18 +37,6 @@ impl TypeRow {
}
}

#[inline(always)]
/// Returns the port type given an offset. Returns `None` if the offset is out of bounds.
pub fn get(&self, offset: impl PortIndex) -> Option<&Type> {
self.types.get(offset.index())
}

#[inline(always)]
/// Returns the port type given an offset. Returns `None` if the offset is out of bounds.
pub fn get_mut(&mut self, offset: impl PortIndex) -> Option<&mut Type> {
self.types.to_mut().get_mut(offset.index())
}

/// Returns a new `TypeRow` with `xs` concatenated onto `self`.
pub fn extend<'a>(&'a self, rest: impl IntoIterator<Item = &'a Type>) -> Self {
self.iter().chain(rest).cloned().collect_vec().into()
Expand All @@ -76,6 +63,16 @@ impl TypeRow {

/// Returns `true` if the row contains no types.
pub fn is_empty(&self) -> bool ;

#[inline(always)]
/// Returns the type at the specified index. Returns `None` if out of bounds.
pub fn get(&self, offset: usize) -> Option<&Type>;
}

to self.types.to_mut() {
#[inline(always)]
/// Returns the type at the specified index. Returns `None` if out of bounds.
pub fn get_mut(&mut self, offset: usize) -> Option<&mut Type>;
}
}
}
Expand Down

0 comments on commit 02f3864

Please sign in to comment.