Skip to content

Commit 5f1d792

Browse files
committed
Switch PersistenceNotifierGuard persist closure to FnOnce
This allows us to move owned and mutable values into the closure, with the assumption that the closure can only be invoked once. As a result, we now have to track `should_persist` as an `Option`, such that we can `take` the owned closure and invoke it within the `PersistenceNotifierGuard::drop` implementation.
1 parent 0430b1e commit 5f1d792

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2815,10 +2815,10 @@ enum NotifyOption {
28152815
/// We allow callers to either always notify by constructing with `notify_on_drop` or choose to
28162816
/// notify or not based on whether relevant changes have been made, providing a closure to
28172817
/// `optionally_notify` which returns a `NotifyOption`.
2818-
struct PersistenceNotifierGuard<'a, F: FnMut() -> NotifyOption> {
2818+
struct PersistenceNotifierGuard<'a, F: FnOnce() -> NotifyOption> {
28192819
event_persist_notifier: &'a Notifier,
28202820
needs_persist_flag: &'a AtomicBool,
2821-
should_persist: F,
2821+
should_persist: Option<F>,
28222822
// We hold onto this result so the lock doesn't get released immediately.
28232823
_read_guard: RwLockReadGuard<'a, ()>,
28242824
}
@@ -2833,20 +2833,20 @@ impl<'a> PersistenceNotifierGuard<'a, fn() -> NotifyOption> {
28332833
/// isn't ideal.
28342834
fn notify_on_drop<C: AChannelManager>(
28352835
cm: &'a C,
2836-
) -> PersistenceNotifierGuard<'a, impl FnMut() -> NotifyOption> {
2836+
) -> PersistenceNotifierGuard<'a, impl FnOnce() -> NotifyOption> {
28372837
Self::optionally_notify(cm, || -> NotifyOption { NotifyOption::DoPersist })
28382838
}
28392839

28402840
#[rustfmt::skip]
2841-
fn optionally_notify<F: FnMut() -> NotifyOption, C: AChannelManager>(cm: &'a C, mut persist_check: F)
2842-
-> PersistenceNotifierGuard<'a, impl FnMut() -> NotifyOption> {
2841+
fn optionally_notify<F: FnOnce() -> NotifyOption, C: AChannelManager>(cm: &'a C, persist_check: F)
2842+
-> PersistenceNotifierGuard<'a, impl FnOnce() -> NotifyOption> {
28432843
let read_guard = cm.get_cm().total_consistency_lock.read().unwrap();
28442844
let force_notify = cm.get_cm().process_background_events();
28452845

28462846
PersistenceNotifierGuard {
28472847
event_persist_notifier: &cm.get_cm().event_persist_notifier,
28482848
needs_persist_flag: &cm.get_cm().needs_persist_flag,
2849-
should_persist: move || {
2849+
should_persist: Some(move || {
28502850
// Pick the "most" action between `persist_check` and the background events
28512851
// processing and return that.
28522852
let notify = persist_check();
@@ -2857,7 +2857,7 @@ impl<'a> PersistenceNotifierGuard<'a, fn() -> NotifyOption> {
28572857
(_, NotifyOption::SkipPersistHandleEvents) => NotifyOption::SkipPersistHandleEvents,
28582858
_ => NotifyOption::SkipPersistNoEvents,
28592859
}
2860-
},
2860+
}),
28612861
_read_guard: read_guard,
28622862
}
28632863
}
@@ -2873,16 +2873,16 @@ impl<'a> PersistenceNotifierGuard<'a, fn() -> NotifyOption> {
28732873
PersistenceNotifierGuard {
28742874
event_persist_notifier: &cm.get_cm().event_persist_notifier,
28752875
needs_persist_flag: &cm.get_cm().needs_persist_flag,
2876-
should_persist: persist_check,
2876+
should_persist: Some(persist_check),
28772877
_read_guard: read_guard,
28782878
}
28792879
}
28802880
}
28812881

2882-
impl<'a, F: FnMut() -> NotifyOption> Drop for PersistenceNotifierGuard<'a, F> {
2882+
impl<'a, F: FnOnce() -> NotifyOption> Drop for PersistenceNotifierGuard<'a, F> {
28832883
#[rustfmt::skip]
28842884
fn drop(&mut self) {
2885-
match (self.should_persist)() {
2885+
match (self.should_persist.take().unwrap())() {
28862886
NotifyOption::DoPersist => {
28872887
self.needs_persist_flag.store(true, Ordering::Release);
28882888
self.event_persist_notifier.notify()

0 commit comments

Comments
 (0)