Skip to content

Commit

Permalink
Bring back TransactionalRewrite (needs test, copies twice)
Browse files Browse the repository at this point in the history
  • Loading branch information
acl-cqc committed Jul 18, 2023
1 parent 1b382a9 commit e575bf9
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/hugr/rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub trait Rewrite {
/// being preferred.
fn apply(self, h: &mut impl HugrMut) -> Result<(), Self::Error>;
}
/*

/// Wraps any rewrite into a transaction (i.e. that has no effect upon failure)
pub struct Transactional<R> {
underlying: R,
Expand All @@ -43,21 +43,30 @@ impl<R: Rewrite> Rewrite for Transactional<R> {
type Error = R::Error;
const UNCHANGED_ON_FAILURE: bool = true;

fn verify(&self, h: &Hugr) -> Result<(), Self::Error> {
fn verify(&self, h: &impl HugrView) -> Result<(), Self::Error> {
self.underlying.verify(h)
}

fn apply(self, h: &mut Hugr) -> Result<(), Self::Error> {
fn apply(self, h: &mut impl HugrMut) -> Result<(), Self::Error> {
if R::UNCHANGED_ON_FAILURE {
return self.underlying.apply(h);
}
let backup = h.clone();
let mut backup = Hugr::new(h.root_type().clone());
backup.insert_from_view(backup.root(), h).unwrap();
let r = self.underlying.apply(h);
if r.is_err() {
// drop the old h, it was undefined
let _ = mem::replace(h, backup);
h.replace_op(h.root(), backup.root_type().clone());
loop {
let ch = h.children(h.root()).next();
match ch {
Some(child) => h.remove_node(child).unwrap(),
None => {
break;
}
}
}
h.insert_from_view(h.root(), &backup).unwrap();
}
r
Ok(())
}
}
*/

0 comments on commit e575bf9

Please sign in to comment.