From: Jeffrey Czyz Date: Wed, 27 Oct 2021 19:02:49 +0000 (-0500) Subject: f - Add a ReadOnlyScorer wrapper struct X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=5b3a33a8f21168d677f04a1b57805344a3afecbf;p=rust-lightning f - Add a ReadOnlyScorer wrapper struct --- 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.