]> git.bitcoin.ninja Git - rust-lightning/commitdiff
f - Add a ReadOnlyScorer wrapper struct
authorJeffrey Czyz <jkczyz@gmail.com>
Wed, 27 Oct 2021 19:02:49 +0000 (14:02 -0500)
committerJeffrey Czyz <jkczyz@gmail.com>
Wed, 27 Oct 2021 19:03:51 +0000 (14:03 -0500)
lightning-invoice/src/payment.rs

index 365d9a2bf7f83445881548b1f976e825d732d835..c273a5ac2c3d90c707234b0691365fb0aa7f7e9d 100644 (file)
@@ -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<P: Deref, R, S, L: Deref, E> InvoicePayer<P, R, S, L, E>
 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.