From 19037c785f70b41e82b6713fa457e7530431b764 Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Wed, 19 Jul 2023 09:15:44 +0100 Subject: [PATCH] clippy (inc 1 where VSCode was wrong) --- src/builder/build_traits.rs | 4 +-- src/builder/cfg.rs | 3 +- src/builder/tail_loop.rs | 4 +-- src/hugr/hugrmut.rs | 69 +++++++++++++++---------------------- src/hugr/view.rs | 35 +++++++++---------- 5 files changed, 50 insertions(+), 65 deletions(-) diff --git a/src/builder/build_traits.rs b/src/builder/build_traits.rs index c2c47abcb..ab83672eb 100644 --- a/src/builder/build_traits.rs +++ b/src/builder/build_traits.rs @@ -53,7 +53,7 @@ impl Buildable for Hugr { } fn hugr(&self) -> Self::BaseView<'_> { - &self + self } } @@ -67,7 +67,7 @@ impl Buildable for &mut H { } fn hugr(&self) -> Self::BaseView<'_> { - &self + self } } diff --git a/src/builder/cfg.rs b/src/builder/cfg.rs index 3d690d65e..189a9e2e0 100644 --- a/src/builder/cfg.rs +++ b/src/builder/cfg.rs @@ -190,7 +190,8 @@ impl CFGBuilder { other_outputs: TypeRow, ) -> Result::Base as Buildable>::BaseMut<'_>>, BuildError> { - let inputs = (&mut self.inputs) + let inputs = self + .inputs .take() .ok_or(BuildError::EntryBuiltError(self.cfg_node))?; self.any_block_builder(inputs, predicate_variants, other_outputs, true) diff --git a/src/builder/tail_loop.rs b/src/builder/tail_loop.rs index 04ca1ba06..38ae31e57 100644 --- a/src/builder/tail_loop.rs +++ b/src/builder/tail_loop.rs @@ -111,7 +111,7 @@ mod test { let [i1] = loop_b.input_wires_arr(); let const_wire = loop_b.add_load_const(ConstValue::i64(1))?; - let break_wire = loop_b.make_break(loop_b.loop_signature()?.clone(), [const_wire])?; + let break_wire = loop_b.make_break(loop_b.loop_signature()?, [const_wire])?; loop_b.set_outputs(break_wire, [i1])?; loop_b.finish_hugr() }; @@ -131,7 +131,7 @@ mod test { let loop_id = { let mut loop_b = fbuild.tail_loop_builder(vec![(BIT, b1)], vec![], type_row![NAT])?; - let signature = loop_b.loop_signature()?.clone(); + let signature = loop_b.loop_signature()?; let const_wire = loop_b.add_load_const(ConstValue::true_val())?; let [b1] = loop_b.input_wires_arr(); let conditional_id = { diff --git a/src/hugr/hugrmut.rs b/src/hugr/hugrmut.rs index 14bc32353..42479a0c0 100644 --- a/src/hugr/hugrmut.rs +++ b/src/hugr/hugrmut.rs @@ -153,7 +153,7 @@ impl HugrMut for Hugr { } fn remove_node(&mut self, node: Node) -> Result<(), HugrError> { - if node.index == self.as_ref().root { + if node.index == self.root { // TODO: Add a HugrMutError ? panic!("cannot remove root node"); } @@ -187,7 +187,7 @@ impl HugrMut for Hugr { offset, }, )?; - self.as_mut().graph.unlink_port(port); + self.graph.unlink_port(port); Ok(()) } @@ -206,15 +206,14 @@ impl HugrMut for Hugr { #[inline] fn set_num_ports(&mut self, node: Node, incoming: usize, outgoing: usize) { - self.as_mut() - .graph + self.graph .set_num_ports(node.index, incoming, outgoing, |_, _| {}) } #[inline] fn add_ports(&mut self, node: Node, direction: Direction, amount: isize) -> Range { - let mut incoming = self.as_mut().graph.num_inputs(node.index); - let mut outgoing = self.as_mut().graph.num_outputs(node.index); + let mut incoming = self.graph.num_inputs(node.index); + let mut outgoing = self.graph.num_outputs(node.index); let increment = |num: &mut usize| { let new = num.saturating_add_signed(amount); let range = *num..new; @@ -225,33 +224,26 @@ impl HugrMut for Hugr { Direction::Incoming => increment(&mut incoming), Direction::Outgoing => increment(&mut outgoing), }; - self.as_mut() - .graph + self.graph .set_num_ports(node.index, incoming, outgoing, |_, _| {}); range } fn set_parent(&mut self, node: Node, parent: Node) -> Result<(), HugrError> { - self.as_mut().hierarchy.detach(node.index); - self.as_mut() - .hierarchy - .push_child(node.index, parent.index)?; + self.hierarchy.detach(node.index); + self.hierarchy.push_child(node.index, parent.index)?; Ok(()) } fn move_after_sibling(&mut self, node: Node, after: Node) -> Result<(), HugrError> { - self.as_mut().hierarchy.detach(node.index); - self.as_mut() - .hierarchy - .insert_after(node.index, after.index)?; + self.hierarchy.detach(node.index); + self.hierarchy.insert_after(node.index, after.index)?; Ok(()) } fn move_before_sibling(&mut self, node: Node, before: Node) -> Result<(), HugrError> { - self.as_mut().hierarchy.detach(node.index); - self.as_mut() - .hierarchy - .insert_before(node.index, before.index)?; + self.hierarchy.detach(node.index); + self.hierarchy.insert_before(node.index, before.index)?; Ok(()) } @@ -261,53 +253,47 @@ impl HugrMut for Hugr { op: impl Into, ) -> Result { let node = self.add_op(op.into()); - self.as_mut() - .hierarchy - .push_child(node.index, parent.index)?; + self.hierarchy.push_child(node.index, parent.index)?; Ok(node) } fn add_op_before(&mut self, sibling: Node, op: impl Into) -> Result { let node = self.add_op(op.into()); - self.as_mut() - .hierarchy - .insert_before(node.index, sibling.index)?; + self.hierarchy.insert_before(node.index, sibling.index)?; Ok(node) } fn add_op_after(&mut self, sibling: Node, op: impl Into) -> Result { let node = self.add_op(op.into()); - self.as_mut() - .hierarchy - .insert_after(node.index, sibling.index)?; + self.hierarchy.insert_after(node.index, sibling.index)?; Ok(node) } fn replace_op(&mut self, node: Node, op: impl Into) -> OpType { - let cur = self.as_mut().op_types.get_mut(node.index); + let cur = self.op_types.get_mut(node.index); std::mem::replace(cur, op.into()) } fn insert_hugr(&mut self, root: Node, mut other: Hugr) -> Result { - let (other_root, node_map) = insert_hugr_internal(self.as_mut(), root, &other)?; + let (other_root, node_map) = insert_hugr_internal(self, root, &other)?; // Update the optypes and metadata, taking them from the other graph. for (&node, &new_node) in node_map.iter() { let optype = other.op_types.take(node); - self.as_mut().op_types.set(new_node, optype); + self.op_types.set(new_node, optype); let meta = other.metadata.take(node); - self.as_mut().set_metadata(node.into(), meta); + self.set_metadata(node.into(), meta); } Ok(other_root) } fn insert_from_view(&mut self, root: Node, other: &impl HugrView) -> Result { - let (other_root, node_map) = insert_hugr_internal(self.as_mut(), root, other)?; + let (other_root, node_map) = insert_hugr_internal(self, root, other)?; // Update the optypes and metadata, copying them from the other graph. for (&node, &new_node) in node_map.iter() { let optype = other.get_optype(node.into()); - self.as_mut().op_types.set(new_node, optype.clone()); + self.op_types.set(new_node, optype.clone()); let meta = other.get_metadata(node.into()); - self.as_mut().set_metadata(node.into(), meta.clone()); + self.set_metadata(node.into(), meta.clone()); } Ok(other_root) } @@ -330,19 +316,18 @@ impl HugrMut for Hugr { let target: Node = NodeIndex::new(position).into(); if target != source { - let hugr = self.as_mut(); - hugr.graph.swap_nodes(target.index, source.index); - hugr.op_types.swap(target.index, source.index); - hugr.hierarchy.swap_nodes(target.index, source.index); + self.graph.swap_nodes(target.index, source.index); + self.op_types.swap(target.index, source.index); + self.hierarchy.swap_nodes(target.index, source.index); rekey(source, target); } } - self.as_mut().root = NodeIndex::new(0); + self.root = NodeIndex::new(0); // Finish by compacting the copy nodes. // The operation nodes will be left in place. // This step is not strictly necessary. - self.as_mut().graph.compact_nodes(|_, _| {}); + self.graph.compact_nodes(|_, _| {}); } } diff --git a/src/hugr/view.rs b/src/hugr/view.rs index 1ce91aa86..a61326450 100644 --- a/src/hugr/view.rs +++ b/src/hugr/view.rs @@ -208,51 +208,50 @@ impl HugrView for Hugr { #[inline] fn root(&self) -> Node { - self.as_ref().root.into() + self.root.into() } #[inline] fn get_parent(&self, node: Node) -> Option { - self.as_ref().hierarchy.parent(node.index).map(Into::into) + self.hierarchy.parent(node.index).map(Into::into) } #[inline] fn get_optype(&self, node: Node) -> &OpType { - self.as_ref().op_types.get(node.index) + self.op_types.get(node.index) } #[inline] fn node_count(&self) -> usize { - self.as_ref().graph.node_count() + self.graph.node_count() } #[inline] fn edge_count(&self) -> usize { - self.as_ref().graph.link_count() + self.graph.link_count() } #[inline] fn nodes(&self) -> Self::Nodes<'_> { - self.as_ref().graph.nodes_iter().map_into() + self.graph.nodes_iter().map_into() } #[inline] fn node_ports(&self, node: Node, dir: Direction) -> Self::NodePorts<'_> { - self.as_ref().graph.port_offsets(node.index, dir).map_into() + self.graph.port_offsets(node.index, dir).map_into() } #[inline] fn all_node_ports(&self, node: Node) -> Self::NodePorts<'_> { - self.as_ref().graph.all_port_offsets(node.index).map_into() + self.graph.all_port_offsets(node.index).map_into() } #[inline] fn linked_ports(&self, node: Node, port: Port) -> Self::PortLinks<'_> { - let hugr = self.as_ref(); - let port = hugr.graph.port_index(node.index, port.offset).unwrap(); - hugr.graph + let port = self.graph.port_index(node.index, port.offset).unwrap(); + self.graph .port_links(port) - .with_context(hugr) + .with_context(self) .map_with_context(|(_, link), hugr| { let port = link.port(); let node = hugr.graph.port_node(port).unwrap(); @@ -263,27 +262,27 @@ impl HugrView for Hugr { #[inline] fn num_ports(&self, node: Node, dir: Direction) -> usize { - self.as_ref().graph.num_ports(node.index, dir) + self.graph.num_ports(node.index, dir) } #[inline] fn children(&self, node: Node) -> Self::Children<'_> { - self.as_ref().hierarchy.children(node.index).map_into() + self.hierarchy.children(node.index).map_into() } #[inline] fn neighbours(&self, node: Node, dir: Direction) -> Self::Neighbours<'_> { - self.as_ref().graph.neighbours(node.index, dir).map_into() + self.graph.neighbours(node.index, dir).map_into() } #[inline] fn all_neighbours(&self, node: Node) -> Self::Neighbours<'_> { - self.as_ref().graph.all_neighbours(node.index).map_into() + self.graph.all_neighbours(node.index).map_into() } #[inline] fn get_metadata(&self, node: Node) -> &NodeMetadata { - self.as_ref().metadata.get(node.index) + self.metadata.get(node.index) } } @@ -470,7 +469,7 @@ pub(crate) mod sealed { } fn base_hugr(&self) -> &Hugr { - &self + self } }