diff --git a/aspect/Cargo.toml b/aspect/Cargo.toml index 8510788..bd59d04 100644 --- a/aspect/Cargo.toml +++ b/aspect/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "aspect" -version = "0.3.0" +version = "0.4.0" authors = ["Simon Chemouil "] license = "Apache-2.0 OR MIT" readme = "../README.md" diff --git a/aspect/src/lib.rs b/aspect/src/lib.rs index a93d51a..2b968b1 100644 --- a/aspect/src/lib.rs +++ b/aspect/src/lib.rs @@ -81,3 +81,34 @@ impl> OnResultMut for A { >::leave_scope(self, enter) } } + +/// The `OnResultOwned` trait is implemented on Aspects to get notified +/// when an expression has returned, and provide the possibility to +/// replace the result. +pub trait OnResultOwned: Enter { + /// Called when an expression has returned. + /// + /// This function is passed both the enter return value, and the expression return value. + fn on_result(&self, enter: ::E, result: R) -> (Advice, R) { + let advice = self.leave_scope(enter); + (advice, result) + } + + /// Called when an expression has exited, but the return value isn't known. + /// This can happen because of a panic, or if control flow bypasses a macro. + /// This is also called by the default implementation of `on_result`. + fn leave_scope(&self, _enter: ::E) -> Advice { + Advice::Return + } +} + +impl> OnResultOwned for A { + fn on_result(&self, enter: ::E, mut result: R) -> (Advice, R) { + let advice = >::on_result(self, enter, &mut result); + (advice, result) + } + + fn leave_scope(&self, enter: ::E) -> Advice { + >::leave_scope(self, enter) + } +}