Skip to content

Commit

Permalink
Merge 'origin/main' into new/replace, update add_{op=>node}_...
Browse files Browse the repository at this point in the history
  • Loading branch information
acl-cqc committed Nov 8, 2023
2 parents 1b39df7 + 061ec3e commit ce9311a
Show file tree
Hide file tree
Showing 18 changed files with 335 additions and 226 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ jobs:
with:
prefix-key: v0-rust-${{ matrix.rust }}
- name: Build with no features
run: cargo build --verbose --no-default-features
- name: Build with all features
run: cargo build --verbose --all-features
run: cargo test --verbose --no-default-features --no-run
- name: Tests with no features
run: cargo test --verbose --no-default-features
- name: Build with all features
run: cargo test --verbose --all-features --no-run
- name: Tests with all features
run: cargo test --verbose --all-features

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
name: Coverage
name: Notify coverage changes

# Daily notification for coverage changes in `main`.
#
# PR coverage diffs are computed directly in the `Continuous Integration` workflow.

on:
schedule:
Expand Down
4 changes: 2 additions & 2 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,14 @@ pub(crate) mod test {
let mut hugr = Hugr::new(NodeType::new_pure(ops::DFG {
signature: signature.clone(),
}));
hugr.add_op_with_parent(
hugr.add_node_with_parent(
hugr.root(),
ops::Input {
types: signature.input,
},
)
.unwrap();
hugr.add_op_with_parent(
hugr.add_node_with_parent(
hugr.root(),
ops::Output {
types: signature.output,
Expand Down
17 changes: 5 additions & 12 deletions src/builder/build_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub trait Container {
/// Add an [`OpType`] as the final child of the container.
fn add_child_op(&mut self, op: impl Into<OpType>) -> Result<Node, BuildError> {
let parent = self.container_node();
Ok(self.hugr_mut().add_op_with_parent(parent, op)?)
Ok(self.hugr_mut().add_node_with_parent(parent, op)?)
}
/// Add a [`NodeType`] as the final child of the container.
fn add_child_node(&mut self, node: NodeType) -> Result<Node, BuildError> {
Expand Down Expand Up @@ -416,7 +416,7 @@ pub trait Dataflow: Container {
rest: rest_types.into(),
};
// TODO: Make input extensions a parameter
let (loop_node, _) = add_op_with_wires(self, tail_loop.clone(), input_wires)?;
let (loop_node, _) = add_node_with_wires(self, tail_loop.clone(), input_wires)?;

TailLoopBuilder::create_with_io(self.hugr_mut(), loop_node, &tail_loop)
}
Expand Down Expand Up @@ -623,21 +623,14 @@ pub trait Dataflow: Container {
}
}

fn add_op_with_wires<T: Dataflow + ?Sized>(
data_builder: &mut T,
optype: impl Into<OpType>,
inputs: Vec<Wire>,
) -> Result<(Node, usize), BuildError> {
add_node_with_wires(data_builder, NodeType::new_auto(optype), inputs)
}

fn add_node_with_wires<T: Dataflow + ?Sized>(
data_builder: &mut T,
nodetype: NodeType,
nodetype: impl Into<NodeType>,
inputs: Vec<Wire>,
) -> Result<(Node, usize), BuildError> {
let op_node = data_builder.add_child_node(nodetype.clone())?;
let nodetype = nodetype.into();
let sig = nodetype.op_signature();
let op_node = data_builder.add_child_node(nodetype)?;

wire_up_inputs(inputs, op_node, data_builder)?;

Expand Down
6 changes: 3 additions & 3 deletions src/builder/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> CFGBuilder<B> {
let exit_node = base
.as_mut()
// Make the extensions a parameter
.add_op_with_parent(cfg_node, exit_block_type)?;
.add_node_with_parent(cfg_node, exit_block_type)?;
Ok(Self {
base,
cfg_node,
Expand Down Expand Up @@ -144,10 +144,10 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> CFGBuilder<B> {
let block_n = if entry {
let exit = self.exit_node;
// TODO: Make extensions a parameter
self.hugr_mut().add_op_before(exit, op)
self.hugr_mut().add_node_before(exit, op)
} else {
// TODO: Make extensions a parameter
self.hugr_mut().add_op_with_parent(parent, op)
self.hugr_mut().add_node_with_parent(parent, op)
}?;

BlockBuilder::create(
Expand Down
2 changes: 1 addition & 1 deletion src/builder/conditional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl<B: AsMut<Hugr> + AsRef<Hugr>> ConditionalBuilder<B> {
let case_node =
// add case before any existing subsequent cases
if let Some(&sibling_node) = self.case_nodes[case + 1..].iter().flatten().next() {
self.hugr_mut().add_op_before(sibling_node, case_op)?
self.hugr_mut().add_node_before(sibling_node, case_op)?
} else {
self.add_child_op(case_op)?
};
Expand Down
36 changes: 18 additions & 18 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
//!
//! These types are re-exported in the root of the crate.
pub use itertools::Either;

use derive_more::From;
use itertools::Either::{Left, Right};

#[cfg(feature = "pyo3")]
use pyo3::pyclass;
Expand Down Expand Up @@ -92,35 +95,32 @@ impl Port {
/// [HugrError::InvalidPortDirection]
#[inline]
pub fn as_incoming(&self) -> Result<IncomingPort, HugrError> {
match self.direction() {
Direction::Incoming => Ok(IncomingPort {
index: self.index() as u16,
}),
dir @ Direction::Outgoing => Err(HugrError::InvalidPortDirection(dir)),
}
self.as_directed()
.left()
.ok_or(HugrError::InvalidPortDirection(self.direction()))
}

/// Converts to an [OutgoingPort] if this port is one; else fails with
/// [HugrError::InvalidPortDirection]
#[inline]
pub fn as_outgoing(&self) -> Result<OutgoingPort, HugrError> {
self.as_directed()
.right()
.ok_or(HugrError::InvalidPortDirection(self.direction()))
}

/// Converts to either an [IncomingPort] or an [OutgoingPort], as appropriate.
#[inline]
pub fn as_directed(&self) -> Either<IncomingPort, OutgoingPort> {
match self.direction() {
Direction::Outgoing => Ok(OutgoingPort {
Direction::Incoming => Left(IncomingPort {
index: self.index() as u16,
}),
Direction::Outgoing => Right(OutgoingPort {
index: self.index() as u16,
}),
dir @ Direction::Incoming => Err(HugrError::InvalidPortDirection(dir)),
}
}
/// Creates a new outgoing port.
#[inline]
pub fn try_new_outgoing(port: impl TryInto<OutgoingPort>) -> Result<Self, HugrError> {
let Ok(port) = port.try_into() else {
return Err(HugrError::InvalidPortDirection(Direction::Incoming));
};
Ok(Self {
offset: portgraph::PortOffset::new_outgoing(port.index()),
})
}

/// Returns the direction of the port.
#[inline]
Expand Down
Loading

0 comments on commit ce9311a

Please sign in to comment.