From 02f3864b70660d8121debdc34202a66d565d597b Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Mon, 20 May 2024 17:04:45 +0100 Subject: [PATCH] refactor!: No Ports in TypeRow (#1087) 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 --- hugr/src/types/signature.rs | 9 +++++---- hugr/src/types/type_row.rs | 23 ++++++++++------------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/hugr/src/types/signature.rs b/hugr/src/types/signature.rs index 41bc7b58c..89c832fa1 100644 --- a/hugr/src/types/signature.rs +++ b/hugr/src/types/signature.rs @@ -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}; @@ -98,28 +99,28 @@ impl FunctionType { /// of bounds. #[inline] pub fn in_port_type(&self, port: impl Into) -> 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) -> 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) -> 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) -> 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`]. diff --git a/hugr/src/types/type_row.rs b/hugr/src/types/type_row.rs index 9bba60553..3d97a8068 100644 --- a/hugr/src/types/type_row.rs +++ b/hugr/src/types/type_row.rs @@ -9,7 +9,6 @@ use std::{ use super::Type; use crate::utils::display_list; -use crate::PortIndex; use delegate::delegate; use itertools::Itertools; @@ -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) -> Self { self.iter().chain(rest).cloned().collect_vec().into() @@ -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>; } } }