From 1aaecb09f6cb235a0ef1a048dc100b12699b7e9f Mon Sep 17 00:00:00 2001 From: Seyon Sivarajah Date: Thu, 31 Aug 2023 10:38:08 +0100 Subject: [PATCH] feat: add `ApplyResult` associated type to `Rewrite` (#472) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #471 BREAKING CHANGE: implementers of `Rewrite` must define `ApplyResult`. --------- Co-authored-by: Agustín Borgna <121866228+aborgna-q@users.noreply.github.com> --- src/hugr.rs | 5 ++++- src/hugr/rewrite.rs | 9 ++++++--- src/hugr/rewrite/outline_cfg.rs | 2 ++ src/hugr/rewrite/simple_replace.rs | 2 ++ 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/hugr.rs b/src/hugr.rs index 51535e584..e64f1533c 100644 --- a/src/hugr.rs +++ b/src/hugr.rs @@ -189,7 +189,10 @@ pub type Direction = portgraph::Direction; /// Public API for HUGRs. impl Hugr { /// Applies a rewrite to the graph. - pub fn apply_rewrite(&mut self, rw: impl Rewrite) -> Result<(), E> { + pub fn apply_rewrite( + &mut self, + rw: impl Rewrite, + ) -> Result { rw.apply(self) } diff --git a/src/hugr/rewrite.rs b/src/hugr/rewrite.rs index f5bbb7088..415c39a18 100644 --- a/src/hugr/rewrite.rs +++ b/src/hugr/rewrite.rs @@ -11,7 +11,8 @@ pub use simple_replace::{SimpleReplacement, SimpleReplacementError}; pub trait Rewrite { /// The type of Error with which this Rewrite may fail type Error: std::error::Error; - + /// The type returned on successful application of the rewrite. + type ApplyResult; /// If `true`, [self.apply]'s of this rewrite guarantee that they do not mutate the Hugr when they return an Err. /// If `false`, there is no guarantee; the Hugr should be assumed invalid when Err is returned. const UNCHANGED_ON_FAILURE: bool; @@ -22,13 +23,14 @@ pub trait Rewrite { fn verify(&self, h: &Hugr) -> Result<(), Self::Error>; /// Mutate the specified Hugr, or fail with an error. + /// Returns [`Self::ApplyResult`] if successful. /// If [self.unchanged_on_failure] is true, then `h` must be unchanged if Err is returned. /// See also [self.verify] /// # Panics /// May panic if-and-only-if `h` would have failed [Hugr::validate]; that is, /// implementations may begin with `assert!(h.validate())`, with `debug_assert!(h.validate())` /// being preferred. - fn apply(self, h: &mut Hugr) -> Result<(), Self::Error>; + fn apply(self, h: &mut Hugr) -> Result; } /// Wraps any rewrite into a transaction (i.e. that has no effect upon failure) @@ -40,13 +42,14 @@ pub struct Transactional { // is not yet supported, https://github.com/rust-lang/rust/issues/92827 impl Rewrite for Transactional { type Error = R::Error; + type ApplyResult = R::ApplyResult; const UNCHANGED_ON_FAILURE: bool = true; fn verify(&self, h: &Hugr) -> Result<(), Self::Error> { self.underlying.verify(h) } - fn apply(self, h: &mut Hugr) -> Result<(), Self::Error> { + fn apply(self, h: &mut Hugr) -> Result { if R::UNCHANGED_ON_FAILURE { return self.underlying.apply(h); } diff --git a/src/hugr/rewrite/outline_cfg.rs b/src/hugr/rewrite/outline_cfg.rs index e6f45b77e..894be261a 100644 --- a/src/hugr/rewrite/outline_cfg.rs +++ b/src/hugr/rewrite/outline_cfg.rs @@ -82,6 +82,8 @@ impl OutlineCfg { impl Rewrite for OutlineCfg { type Error = OutlineCfgError; + type ApplyResult = (); + const UNCHANGED_ON_FAILURE: bool = true; fn verify(&self, h: &Hugr) -> Result<(), OutlineCfgError> { self.compute_entry_exit_outside(h)?; diff --git a/src/hugr/rewrite/simple_replace.rs b/src/hugr/rewrite/simple_replace.rs index 3f323c430..966aa765a 100644 --- a/src/hugr/rewrite/simple_replace.rs +++ b/src/hugr/rewrite/simple_replace.rs @@ -51,6 +51,8 @@ impl SimpleReplacement { impl Rewrite for SimpleReplacement { type Error = SimpleReplacementError; + type ApplyResult = (); + const UNCHANGED_ON_FAILURE: bool = true; fn verify(&self, _h: &Hugr) -> Result<(), SimpleReplacementError> {