-
Notifications
You must be signed in to change notification settings - Fork 8
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
[2 of 4] Builder operates on any HugrMut #281
Conversation
ec62652
to
19037c7
Compare
19037c7
to
5e64b0a
Compare
So I think it should be possible to...
...and hopefully that'll make this palatable enough. That's the plan, anyway...we'll see... |
…ugr/&mut H -> H); return &mut BaseMut
Done. That seemed straightforward, didn't encounter any problem with retrieving the Hugr
No. I did eventually drop the
That worked ok; BaseMut + BaseView => Base. Not sure what I meant by "This might allow removing some of the other
This saved a massive amount of So, please fire away, if you see a better way to do this then I'm game :-) |
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.
Some code-specific comments. Let's discuss trait visibility irl
src/hugr/view.rs
Outdated
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 impls for ref could use the delegate!
macro (Same thing in hugrmut.rs
). See
@@ -1,3 +1,4 @@ | |||
#![allow(missing_docs)] |
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.
🤨
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.
Yes, this is not for submission, but good to flag up, sorry. If we decide to go this way then I'll take this out and put in docs, but at least it shows that the rest of CI passes
src/builder/conditional.rs
Outdated
pub fn case_builder( | ||
&mut self, | ||
case: usize, | ||
) -> Result<CaseBuilder<&mut <Self as Buildable>::Base>, BuildError> { |
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.
Isn't this
) -> Result<CaseBuilder<&mut <Self as Buildable>::Base>, BuildError> { | |
) -> Result<CaseBuilder<&mut B>, BuildError> { |
?
(Same thing in other similar structs).
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.
Oh, yeah, thank you! That definitely helps :-)
Seyon wondered if we could make |
The problem with where we are now is that every time you create a new sub-builder (which internally calls HugrMut), we increase the level of indirection - originally you had a So I think actually 42d5eef (returning Self::BaseMut) may have been the right thing, as I think that allows an |
…direction No blanket impl<F: Foo> Foo for &[mut] F (for Foo=HugrMut/HugrView/Buildable) Implement Buildable for <T: AsRef<Hugr> + AsMut<Hugr>> Buildable does not require HugrMut (too awkward as needs many impls) Revert hugrmut.rs and view.rs to branch main
Ok, major changes, somewhat of a reversion. We are back to separate In particular there are no |
@@ -29,17 +30,40 @@ use crate::Hugr; | |||
|
|||
use crate::hugr::HugrMut; | |||
|
|||
pub trait Buildable { | |||
type BaseMut<'a>: Buildable + HugrMut |
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.
We could avoid this Buildable + HugrMut
everywhere if we wrote a blanket impl<B:Buildable> HugrMut for B
(just by delegating to self.hugr_mut()
). That would let us just constrain everything by Buildable
instead, which would be nicer. OTOH it means one can execute HugrMut methods on every Buildable, of which there are many (every Container, etc.)....maybe that'd be a good thing (and could be done as a follow-up PR if we want to see what it looks like?)
fn hugr(&self) -> Self::BaseView<'_>; | ||
} | ||
|
||
impl<T: AsMut<Hugr> + AsRef<Hugr>> Buildable for T { |
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.
An interesting alternative (in that it illustrates what's going on) would be one impl Buildable for Hugr
and a second impl<T: HugrMut + Copy> Buildable for T
- as every &mut
is Copy (right?!). That's two impl
blocks rather than one though so I didn't...
@@ -25,24 +25,28 @@ pub struct CFGBuilder<T> { | |||
pub(super) n_out_wires: usize, | |||
} | |||
|
|||
impl<B: AsMut<Hugr> + AsRef<Hugr>> Container for CFGBuilder<B> { | |||
impl<B: Buildable> Buildable for CFGBuilder<B> { |
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.
There are, indeed, a lot of impl
blocks like this. I guess these could be done via a macro...
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.
Indeed, if/presuming that works, one could have a macro implementing HugrMut
for all of these, which would allow combining Buildable
into HugrMut
. (If we want HugrMut
-ness spread so widely).
Closing in favour of #298 |
[note: this refers to the situation in commit 42d5eef. It has changed, hopefully for the better, since...]
Via a new
Buildable
trait, implemented forHugr
,&mut Hugr
and&mut T
for any T implementing HugrMut.The end goal (not in this PR) is to allow Rewrites to work on any HugrMut, which in itself I am proposing as a way of allowing Rewrites to be invoked on a region of the Hugr (some future PR can
impl HugrMut for FlatRegionView
or similar.)The changes here were mostly straightforward, but
<<<Self as Container>::Base as Buildable>::BaseMut<'_>>
kind of thing. I'm wondering if this can be reduced by declaringtrait Container: Buildable
, say. (Of course, all this would go away if we'd accept a method returningdyn HugrMut
....)impl<T: HugrMut> HugrMut for &mut T
blocks which are very obvious/tedious (3 of them - 1 for HugrMut and 2 for HugrView, so the latter 2 could be combined via a macro I guess)