-
Notifications
You must be signed in to change notification settings - Fork 7
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
feat!: MakeOpDef
has new extension
method.
#1266
Conversation
Used to ensure `try_from_name` only succeeds when the extension is correct BREAKING CHANGE: - `MakeOpDef` trait has new required `extension_id` static method. - `try_from_name` takes an `&OpDef` rather than just names and checks the extension is expected
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1266 +/- ##
==========================================
- Coverage 87.15% 87.09% -0.07%
==========================================
Files 103 103
Lines 19284 19311 +27
Branches 17136 17163 +27
==========================================
+ Hits 16807 16818 +11
- Misses 1699 1715 +16
Partials 778 778
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
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.
I find this code(that was already here!) quite confusing, I may be off track here, but nevertheless:
All callsites of try_from_name
get it out of an opdef. All implementations of MakeOpDef::from_def
delegate to try_from_name
.
How about:
- try_from_name takes an
&ExtensionId
and aimpl FromStr
. extension_id
gets a&self
parameter
from_def
implementations are changed to pass the ExtensionId
they each have access to.
This breaks from_def
implementations that were previously not checking the extension(all of them).
It means types that implement MakeOpDef
are free to be used with multiple extensions. As they were before.
I do think I weakly prefer putting extension_id
on NamedOp
, but this is also fine.
hugr-core/src/extension/simple_op.rs
Outdated
@@ -138,10 +143,19 @@ impl<T: MakeOpDef> MakeExtensionOp for T { | |||
|
|||
/// Load an [MakeOpDef] from its name. | |||
/// See [strum_macros::EnumString]. | |||
pub fn try_from_name<T>(name: &OpNameRef) -> Result<T, OpLoadError> | |||
pub fn try_from_name<T>(def: &OpDef) -> Result<T, OpLoadError> |
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.
I'm not sure this change makes sense. we already have MakeOpDef::from_def
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.
from_def
is abstract, MakeOpDef
implementers need to have some way of loading themselves from a definition. It so happens that many can load themselves from (name, extension) because they implement FromStr
, so we use a specific function for that case (try_from_name
).
i dont mind passing name and extension explicitly like you suggest, so I've done that anyway. It's a bit clearer what the job of the function is.
hugr-core/src/extension/simple_op.rs
Outdated
@@ -51,6 +53,9 @@ pub trait MakeOpDef: NamedOp { | |||
/// Return the signature (polymorphic function type) of the operation. | |||
fn signature(&self) -> SignatureFunc; | |||
|
|||
/// The ID of the extension this operation is defined in. | |||
fn extension_id() -> ExtensionId; |
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.
This should have where Self: Sized
to maintain object-safety.
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.
taking &self is a good idea, going with that
Agreed, so do I, which is a lot worse given I wrote it. A clean-up is in order but don't want to do that in this PR. |
MakeOpDef
has new extension_id
static method.MakeOpDef
has new extension_id
method.
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.
Let's rename expected_extension
-> extension
. I have some op: &impl MakeOpDef
, i can ask it it's extension: op.extension()
.
hugr-core/src/extension/simple_op.rs
Outdated
@@ -51,6 +53,9 @@ pub trait MakeOpDef: NamedOp { | |||
/// Return the signature (polymorphic function type) of the operation. | |||
fn signature(&self) -> SignatureFunc; | |||
|
|||
/// The ID of the extension this operation is defined in. | |||
fn expected_extension(&self) -> ExtensionId; |
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.
fn expected_extension(&self) -> ExtensionId; | |
fn extension(&self) -> ExtensionId; |
MakeOpDef
has new extension_id
method.MakeOpDef
has new extension
method.
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.
Thanks. I've taken the liberty of updating the PR title and commit message.
## 🤖 New release * `hugr`: 0.6.1 -> 0.7.0 * `hugr-core`: 0.3.1 -> 0.4.0 * `hugr-passes`: 0.3.0 -> 0.4.0 * `hugr-cli`: 0.1.2 -> 0.1.3 <details><summary><i><b>Changelog</b></i></summary><p> ## `hugr` <blockquote> ## 0.7.0 (2024-07-10) ### Bug Fixes - Bring back input_extensions serialized field in rust NodeSer ([#1275](#1275)) - [**breaking**] `ops::Module` now empty struct rather than unit struct ([#1271](#1271)) ### Features - Add `force_order` pass. ([#1285](#1285)) - [**breaking**] `MakeOpDef` has new `extension` method. ([#1266](#1266)) ### Refactor - [**breaking**] Remove `Value::Tuple` ([#1255](#1255)) - [**breaking**] Rename `HugrView` function type methods + simplify behaviour ([#1265](#1265)) ### Styling - Change "serialise" etc to "serialize" etc. ([#1251](#1251)) ### Testing - Add a test for [#1257](#1257) ([#1260](#1260)) </blockquote> ## `hugr-core` <blockquote> ## 0.4.0 (2024-07-10) ### Bug Fixes - Bring back input_extensions serialized field in rust NodeSer ([#1275](#1275)) - [**breaking**] `ops::Module` now empty struct rather than unit struct ([#1271](#1271)) ### Features - [**breaking**] `MakeOpDef` has new `extension` method. ([#1266](#1266)) ### Refactor - [**breaking**] Remove `Value::Tuple` ([#1255](#1255)) - [**breaking**] Rename `HugrView` function type methods + simplify behaviour ([#1265](#1265)) ### Styling - Change "serialise" etc to "serialize" etc. ([#1251](#1251)) ### Testing - Add a test for [#1257](#1257) ([#1260](#1260)) </blockquote> ## `hugr-passes` <blockquote> ## 0.4.0 (2024-07-10) ### Features - Add `force_order` pass. ([#1285](#1285)) ### Refactor - [**breaking**] Remove `Value::Tuple` ([#1255](#1255)) </blockquote> ## `hugr-cli` <blockquote> ## 0.1.3 (2024-07-10) ### Styling - Change "serialise" etc to "serialize" etc. ([#1251](#1251)) </blockquote> </p></details> --- This PR was generated with [release-plz](https://github.com/MarcoIeni/release-plz/).
Closes #1241
Used to ensure
try_from_name
only succeeds when the extension is correctBREAKING CHANGE:
MakeOpDef
trait has new requiredextension
method.try_from_name
takes the OpDef extension and checks it