Skip to content
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

Add OnResultOwned #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion aspect/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "aspect"
version = "0.3.0"
version = "0.4.0"
authors = ["Simon Chemouil <[email protected]>"]
license = "Apache-2.0 OR MIT"
readme = "../README.md"
Expand Down
31 changes: 31 additions & 0 deletions aspect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,34 @@ impl<R, A: OnResult<R>> OnResultMut<R> for A {
<Self as OnResult<R>>::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<R>: 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: <Self as 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: <Self as Enter>::E) -> Advice {
Advice::Return
}
}

impl<R, A: OnResultMut<R>> OnResultOwned<R> for A {
fn on_result(&self, enter: <Self as Enter>::E, mut result: R) -> (Advice, R) {
let advice = <Self as OnResultMut<R>>::on_result(self, enter, &mut result);
(advice, result)
}

fn leave_scope(&self, enter: <Self as Enter>::E) -> Advice {
<Self as OnResultMut<R>>::leave_scope(self, enter)
}
}