Skip to content

Commit

Permalink
clippy (inc 1 where VSCode was wrong)
Browse files Browse the repository at this point in the history
  • Loading branch information
acl-cqc committed Jul 24, 2023
1 parent e8c0d81 commit 5e64b0a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 65 deletions.
4 changes: 2 additions & 2 deletions src/builder/build_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl Buildable for Hugr {
}

fn hugr(&self) -> Self::BaseView<'_> {
&self
self
}
}

Expand All @@ -67,7 +67,7 @@ impl<H: HugrMut + HugrView> Buildable for &mut H {
}

fn hugr(&self) -> Self::BaseView<'_> {
&self
self
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/builder/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ impl<B: Buildable> CFGBuilder<B> {
other_outputs: TypeRow,
) -> Result<BlockBuilder<<<Self as Container>::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)
Expand Down
4 changes: 2 additions & 2 deletions src/builder/tail_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,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()
};
Expand All @@ -129,7 +129,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 = {
Expand Down
69 changes: 27 additions & 42 deletions src/hugr/hugrmut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down Expand Up @@ -187,7 +187,7 @@ impl HugrMut for Hugr {
offset,
},
)?;
self.as_mut().graph.unlink_port(port);
self.graph.unlink_port(port);
Ok(())
}

Expand All @@ -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<usize> {
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;
Expand All @@ -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(())
}

Expand All @@ -261,53 +253,47 @@ impl HugrMut for Hugr {
op: impl Into<OpType>,
) -> Result<Node, HugrError> {
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<OpType>) -> Result<Node, HugrError> {
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<OpType>) -> Result<Node, HugrError> {
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>) -> 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<Node, HugrError> {
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<Node, HugrError> {
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)
}
Expand All @@ -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(|_, _| {});
}
}

Expand Down
35 changes: 17 additions & 18 deletions src/hugr/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Node> {
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();
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -470,7 +469,7 @@ pub(crate) mod sealed {
}

fn base_hugr(&self) -> &Hugr {
&self
self
}
}

Expand Down

0 comments on commit 5e64b0a

Please sign in to comment.