From c02ab92f75b99ecb3df1e4066d1540f4a77ce66f Mon Sep 17 00:00:00 2001 From: Craig Roy Date: Tue, 5 Mar 2024 15:02:55 +0000 Subject: [PATCH] refactor: Remove clone in `ExtensionSet::union` (#859) --- src/builder/dataflow.rs | 4 ++-- src/extension.rs | 4 ++-- src/extension/infer.rs | 4 ++-- src/extension/infer/test.rs | 11 +++++++---- src/extension/validate.rs | 2 +- src/hugr.rs | 6 +++--- src/hugr/rewrite/outline_cfg.rs | 2 +- src/std_extensions/collections.rs | 2 +- src/types/signature.rs | 2 +- 9 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/builder/dataflow.rs b/src/builder/dataflow.rs index 6f0cf003f..2aa471882 100644 --- a/src/builder/dataflow.rs +++ b/src/builder/dataflow.rs @@ -55,7 +55,7 @@ impl + AsRef> DFGBuilder { parent, NodeType::new( output, - input_extensions.map(|inp| inp.union(&signature.extension_reqs)), + input_extensions.map(|inp| inp.union(signature.extension_reqs)), ), )?; @@ -430,7 +430,7 @@ pub(crate) mod test { let xb: ExtensionId = "B".try_into().unwrap(); let xc: ExtensionId = "C".try_into().unwrap(); let ab_extensions = ExtensionSet::from_iter([xa.clone(), xb.clone()]); - let abc_extensions = ab_extensions.clone().union(&xc.clone().into()); + let abc_extensions = ab_extensions.clone().union(xc.clone().into()); let parent_sig = FunctionType::new(type_row![BIT], type_row![BIT]).with_extension_delta(abc_extensions); diff --git a/src/extension.rs b/src/extension.rs index 07b8d780e..a15dd66da 100644 --- a/src/extension.rs +++ b/src/extension.rs @@ -455,8 +455,8 @@ impl ExtensionSet { } /// Returns the union of two extension sets. - pub fn union(mut self, other: &Self) -> Self { - self.0.extend(other.0.iter().cloned()); + pub fn union(mut self, other: Self) -> Self { + self.0.extend(other.0); self } diff --git a/src/extension/infer.rs b/src/extension/infer.rs index 3def7e21f..941ebb010 100644 --- a/src/extension/infer.rs +++ b/src/extension/infer.rs @@ -512,7 +512,7 @@ impl UnificationContext { // to a set which already contained it. Constraint::Plus(r, other_meta) => { if let Some(rs) = self.get_solution(other_meta) { - let rrs = rs.clone().union(r); + let rrs = rs.clone().union(r.clone()); match self.get_solution(&meta) { // Let's check that this is right? Some(rs) => { @@ -725,7 +725,7 @@ impl UnificationContext { .iter() .flat_map(|m| self.get_constraints(m).unwrap()) .filter_map(|c| match c { - Constraint::Plus(_, other_m) => solutions.get(&self.resolve(*other_m)), + Constraint::Plus(_, other_m) => solutions.get(&self.resolve(*other_m)).cloned(), Constraint::Equal(_) => None, }) .fold(ExtensionSet::new(), ExtensionSet::union); diff --git a/src/extension/infer/test.rs b/src/extension/infer/test.rs index c9b36ce40..f070abcf1 100644 --- a/src/extension/infer/test.rs +++ b/src/extension/infer/test.rs @@ -255,7 +255,7 @@ fn dangling_src() -> Result<(), Box> { assert_eq!(hugr.get_nodetype(src.node()).io_extensions().unwrap().1, rs); assert_eq!( hugr.get_nodetype(mult.node()).io_extensions().unwrap(), - (&rs.clone(), rs) + (rs.clone(), rs) ); Ok(()) } @@ -366,11 +366,11 @@ fn test_conditional_inference() -> Result<(), Box> { for node in [case0_node, case1_node, conditional_node] { assert_eq!( hugr.get_nodetype(node).io_extensions().unwrap().0, - &ExtensionSet::new() + ExtensionSet::new() ); assert_eq!( hugr.get_nodetype(node).io_extensions().unwrap().0, - &ExtensionSet::new() + ExtensionSet::new() ); } Ok(()) @@ -724,7 +724,10 @@ fn make_looping_cfg( bb1_ext: ExtensionSet, bb2_ext: ExtensionSet, ) -> Result> { - let hugr_delta = entry_ext.clone().union(&bb1_ext).union(&bb2_ext); + let hugr_delta = entry_ext + .clone() + .union(bb1_ext.clone()) + .union(bb2_ext.clone()); let mut hugr = Hugr::new(NodeType::new_open(ops::CFG { signature: FunctionType::new(type_row![NAT], type_row![NAT]) diff --git a/src/extension/validate.rs b/src/extension/validate.rs index 7d81a88f1..16db73737 100644 --- a/src/extension/validate.rs +++ b/src/extension/validate.rs @@ -31,7 +31,7 @@ impl ExtensionValidator { .map(|s| s.extension_reqs) .unwrap_or_default(); - let outgoing_sol = extension_reqs.union(&incoming_sol); + let outgoing_sol = extension_reqs.union(incoming_sol.clone()); extensions.insert((node, Direction::Incoming), incoming_sol); extensions.insert((node, Direction::Outgoing), outgoing_sol); diff --git a/src/hugr.rs b/src/hugr.rs index 4a3ca1f40..514c8dcbc 100644 --- a/src/hugr.rs +++ b/src/hugr.rs @@ -126,10 +126,10 @@ impl NodeType { /// /// `None`` if the [Self::input_extensions] is `None`. /// Otherwise, will return Some, with the output extensions computed from the node's delta - pub fn io_extensions(&self) -> Option<(&ExtensionSet, ExtensionSet)> { + pub fn io_extensions(&self) -> Option<(ExtensionSet, ExtensionSet)> { self.input_extensions - .as_ref() - .map(|e| (e, self.op.extension_delta().union(e))) + .clone() + .map(|e| (e.clone(), self.op.extension_delta().union(e))) } /// Gets the underlying [OpType] i.e. without any [input_extensions] diff --git a/src/hugr/rewrite/outline_cfg.rs b/src/hugr/rewrite/outline_cfg.rs index 7ab612548..42156433a 100644 --- a/src/hugr/rewrite/outline_cfg.rs +++ b/src/hugr/rewrite/outline_cfg.rs @@ -72,7 +72,7 @@ impl OutlineCfg { } } } - extension_delta = extension_delta.union(&o.signature().extension_reqs); + extension_delta = extension_delta.union(o.signature().extension_reqs); let external_succs = h.output_neighbours(n).filter(|s| !self.blocks.contains(s)); match external_succs.at_most_one() { Ok(None) => (), // No external successors diff --git a/src/std_extensions/collections.rs b/src/std_extensions/collections.rs index be7c3f430..3cd89c2bb 100644 --- a/src/std_extensions/collections.rs +++ b/src/std_extensions/collections.rs @@ -91,7 +91,7 @@ impl CustomConst for ListValue { fn extension_reqs(&self) -> ExtensionSet { ExtensionSet::union_over(self.0.iter().map(Value::extension_reqs)) - .union(&(EXTENSION_NAME.into())) + .union(EXTENSION_NAME.into()) } } diff --git a/src/types/signature.rs b/src/types/signature.rs index e5ca5a56e..38641f13c 100644 --- a/src/types/signature.rs +++ b/src/types/signature.rs @@ -25,7 +25,7 @@ pub struct FunctionType { impl FunctionType { /// Builder method, add extension_reqs to an FunctionType pub fn with_extension_delta(mut self, rs: impl Into) -> Self { - self.extension_reqs = self.extension_reqs.union(&rs.into()); + self.extension_reqs = self.extension_reqs.union(rs.into()); self }