Skip to content

feat: Call FunctionBuilder::add_{in,out}put for any AsMut<Hugr> #2376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions hugr-core/src/builder/dataflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,31 @@ impl FunctionBuilder<Hugr> {
let db = DFGBuilder::create_with_io(base, root, body)?;
Ok(Self::from_dfg_builder(db))
}
}

impl<B: AsMut<Hugr> + AsRef<Hugr>> FunctionBuilder<B> {
/// Initialize a new function definition on the root module of an existing HUGR.
///
/// The HUGR's entrypoint will **not** be modified.
///
/// # Errors
///
/// Error in adding DFG child nodes.
pub fn with_hugr(
mut hugr: B,
name: impl Into<String>,
signature: impl Into<PolyFuncType>,
) -> Result<Self, BuildError> {
let signature: PolyFuncType = signature.into();
let body = signature.body().clone();
let op = ops::FuncDefn::new(name, signature);

let module = hugr.as_ref().module_root();
let func = hugr.as_mut().add_node_with_parent(module, op);

let db = DFGBuilder::create_with_io(hugr, func, body)?;
Ok(Self::from_dfg_builder(db))
}

/// Add a new input to the function being constructed.
///
Expand Down Expand Up @@ -259,31 +284,6 @@ impl FunctionBuilder<Hugr> {
}
}

impl<B: AsMut<Hugr> + AsRef<Hugr>> FunctionBuilder<B> {
/// Initialize a new function definition on the root module of an existing HUGR.
///
/// The HUGR's entrypoint will **not** be modified.
///
/// # Errors
///
/// Error in adding DFG child nodes.
pub fn with_hugr(
mut hugr: B,
name: impl Into<String>,
signature: impl Into<PolyFuncType>,
) -> Result<Self, BuildError> {
let signature: PolyFuncType = signature.into();
let body = signature.body().clone();
let op = ops::FuncDefn::new(name, signature);

let module = hugr.as_ref().module_root();
let func = hugr.as_mut().add_node_with_parent(module, op);

let db = DFGBuilder::create_with_io(hugr, func, body)?;
Ok(Self::from_dfg_builder(db))
}
}

impl<B: AsMut<Hugr> + AsRef<Hugr>, T> Container for DFGWrapper<B, T> {
#[inline]
fn container_node(&self) -> Node {
Expand Down
Loading