Skip to content

Commit 9621044

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 9621044

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2815,10 +2815,12 @@ 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+
// Always `Some` once initialized, but tracked as an `Option` to obtain the closure by value in
2822+
// [`PersistenceNotifierGuard::drop`].
2823+
should_persist: Option<F>,
28222824
// We hold onto this result so the lock doesn't get released immediately.
28232825
_read_guard: RwLockReadGuard<'a, ()>,
28242826
}
@@ -2833,20 +2835,20 @@ impl<'a> PersistenceNotifierGuard<'a, fn() -> NotifyOption> {
28332835
/// isn't ideal.
28342836
fn notify_on_drop<C: AChannelManager>(
28352837
cm: &'a C,
2836-
) -> PersistenceNotifierGuard<'a, impl FnMut() -> NotifyOption> {
2838+
) -> PersistenceNotifierGuard<'a, impl FnOnce() -> NotifyOption> {
28372839
Self::optionally_notify(cm, || -> NotifyOption { NotifyOption::DoPersist })
28382840
}
28392841

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

28462848
PersistenceNotifierGuard {
28472849
event_persist_notifier: &cm.get_cm().event_persist_notifier,
28482850
needs_persist_flag: &cm.get_cm().needs_persist_flag,
2849-
should_persist: move || {
2851+
should_persist: Some(move || {
28502852
// Pick the "most" action between `persist_check` and the background events
28512853
// processing and return that.
28522854
let notify = persist_check();
@@ -2857,7 +2859,7 @@ impl<'a> PersistenceNotifierGuard<'a, fn() -> NotifyOption> {
28572859
(_, NotifyOption::SkipPersistHandleEvents) => NotifyOption::SkipPersistHandleEvents,
28582860
_ => NotifyOption::SkipPersistNoEvents,
28592861
}
2860-
},
2862+
}),
28612863
_read_guard: read_guard,
28622864
}
28632865
}
@@ -2873,16 +2875,16 @@ impl<'a> PersistenceNotifierGuard<'a, fn() -> NotifyOption> {
28732875
PersistenceNotifierGuard {
28742876
event_persist_notifier: &cm.get_cm().event_persist_notifier,
28752877
needs_persist_flag: &cm.get_cm().needs_persist_flag,
2876-
should_persist: persist_check,
2878+
should_persist: Some(persist_check),
28772879
_read_guard: read_guard,
28782880
}
28792881
}
28802882
}
28812883

2882-
impl<'a, F: FnMut() -> NotifyOption> Drop for PersistenceNotifierGuard<'a, F> {
2884+
impl<'a, F: FnOnce() -> NotifyOption> Drop for PersistenceNotifierGuard<'a, F> {
28832885
#[rustfmt::skip]
28842886
fn drop(&mut self) {
2885-
match (self.should_persist)() {
2887+
match (self.should_persist.take().unwrap())() {
28862888
NotifyOption::DoPersist => {
28872889
self.needs_persist_flag.store(true, Ordering::Release);
28882890
self.event_persist_notifier.notify()

0 commit comments

Comments
 (0)