From 5b3a33a8f21168d677f04a1b57805344a3afecbf Mon Sep 17 00:00:00 2001 From: Jeffrey Czyz Date: Wed, 27 Oct 2021 14:02:49 -0500 Subject: [PATCH] f - Add a ReadOnlyScorer wrapper struct --- lightning-invoice/src/payment.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lightning-invoice/src/payment.rs b/lightning-invoice/src/payment.rs index 365d9a2bf..c273a5ac2 100644 --- a/lightning-invoice/src/payment.rs +++ b/lightning-invoice/src/payment.rs @@ -187,6 +187,17 @@ pub enum PaymentError { Sending(PaymentSendFailure), } +/// A read-only version of the scorer. +pub struct ReadOnlyScorer<'a, S: routing::Score>(std::sync::RwLockReadGuard<'a, S>); + +impl<'a, S: routing::Score> Deref for ReadOnlyScorer<'a, S> { + type Target = S; + + fn deref(&self) -> &Self::Target { + &*self.0 + } +} + impl InvoicePayer where P::Target: Payer, @@ -217,8 +228,8 @@ where /// /// Useful if the scorer needs to be persisted. Be sure to drop the returned guard immediately /// after use since retrying failed payment paths require write access. - pub fn scorer(&self) -> std::sync::RwLockReadGuard<'_, S> { - self.scorer.read().unwrap() + pub fn scorer(&'_ self) -> ReadOnlyScorer<'_, S> { + ReadOnlyScorer(self.scorer.read().unwrap()) } /// Pays the given [`Invoice`], caching it for later use in case a retry is needed. -- 2.39.5