-
Notifications
You must be signed in to change notification settings - Fork 13
feat: Create Module/FunctionBuilders from existing Hugrs #2359
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -259,6 +259,31 @@ 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(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could remove the
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, we could drop it but we use the |
||
| 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 { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this useful without returning the Node of the inserted FuncDefn?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The builder itself carries the node, all the
DFGBuildermethods run from there. The node itself can be retrieved withcontainer_nodeif needed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see 👍