-
Notifications
You must be signed in to change notification settings - Fork 475
Add ScopeGuard and use it in Binder.
#317
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -91,3 +91,66 @@ impl<T: PointerWrapper + Deref> PointerWrapper for Pin<T> { | |
| Pin::new_unchecked(T::from_pointer(p)) | ||
| } | ||
| } | ||
|
|
||
| /// Runs a cleanup function/closure when dropped. | ||
| /// | ||
| /// The [`ScopeGuard::dismiss`] function prevents the cleanup function from running. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// In the example below, we have multiple exit paths and we want to log regardless of which one is | ||
| /// taken: | ||
| /// ``` | ||
| /// fn example1(arg: bool) { | ||
| /// let _log = ScopeGuard::new(|| pr_info!("example1 completed\n")); | ||
| /// | ||
| /// if arg { | ||
| /// return; | ||
| /// } | ||
| /// | ||
| /// // Do something... | ||
| /// } | ||
| /// ``` | ||
| /// | ||
| /// In the example below, we want to log the same message on all early exits but a different one on | ||
| /// the main exit path: | ||
| /// ``` | ||
| /// fn example2(arg: bool) { | ||
| /// let log = ScopeGuard::new(|| pr_info!("example2 returned early\n")); | ||
| /// | ||
| /// if arg { | ||
| /// return; | ||
| /// } | ||
| /// | ||
| /// // (Other early returns...) | ||
| /// | ||
| /// log.dismiss(); | ||
| /// pr_info!("example2 no early return\n"); | ||
| /// } | ||
| /// ``` | ||
| pub struct ScopeGuard<T: FnOnce()> { | ||
| cleanup_func: Option<T>, | ||
|
||
| } | ||
|
|
||
| impl<T: FnOnce()> ScopeGuard<T> { | ||
| /// Creates a new cleanup object with the given cleanup function. | ||
| pub fn new(cleanup_func: T) -> Self { | ||
| Self { | ||
| cleanup_func: Some(cleanup_func), | ||
| } | ||
| } | ||
|
|
||
| /// Prevents the cleanup function from running. | ||
| pub fn dismiss(mut self) { | ||
| self.cleanup_func.take(); | ||
| } | ||
| } | ||
|
|
||
| impl<T: FnOnce()> Drop for ScopeGuard<T> { | ||
| fn drop(&mut self) { | ||
| // Run the cleanup function if one is still present. | ||
| if let Some(cleanup) = self.cleanup_func.take() { | ||
ojeda marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| cleanup(); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
Please add two or three examples, e.g. a trivial one with a explicit scope for extra clarity, one with
dismiss()to show the difference and one with e.g. the?operator etc. in the middle (like in your use case in Binder).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.
Added two examples.