Skip to content

Commit 047d3ad

Browse files
committed
Drop required Boxing of lightning trait Futures
Now that our MSRV is 1.75, we can return `impl Trait` from trait methods. Here we use this to clean up `lightning` crate trait methods, dropping the `Pin<Box<dyn ...>>`/`AsyncResult` we had to use to have trait methods return a concrete type.
1 parent 528b0ca commit 047d3ad

File tree

4 files changed

+54
-37
lines changed

4 files changed

+54
-37
lines changed

lightning/src/events/bump_transaction/mod.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
pub mod sync;
1515

1616
use alloc::collections::BTreeMap;
17+
use core::future::Future;
1718
use core::ops::Deref;
1819

1920
use crate::chain::chaininterface::{
@@ -36,7 +37,7 @@ use crate::sign::{
3637
ChannelDerivationParameters, HTLCDescriptor, SignerProvider, P2WPKH_WITNESS_WEIGHT,
3738
};
3839
use crate::sync::Mutex;
39-
use crate::util::async_poll::{AsyncResult, MaybeSend, MaybeSync};
40+
use crate::util::async_poll::{MaybeSend, MaybeSync};
4041
use crate::util::logger::Logger;
4142

4243
use bitcoin::amount::Amount;
@@ -391,13 +392,15 @@ pub trait CoinSelectionSource {
391392
fn select_confirmed_utxos<'a>(
392393
&'a self, claim_id: ClaimId, must_spend: Vec<Input>, must_pay_to: &'a [TxOut],
393394
target_feerate_sat_per_1000_weight: u32, max_tx_weight: u64,
394-
) -> AsyncResult<'a, CoinSelection, ()>;
395+
) -> impl Future<Output = Result<CoinSelection, ()>> + MaybeSend + 'a;
395396
/// Signs and provides the full witness for all inputs within the transaction known to the
396397
/// trait (i.e., any provided via [`CoinSelectionSource::select_confirmed_utxos`]).
397398
///
398399
/// If your wallet does not support signing PSBTs you can call `psbt.extract_tx()` to get the
399400
/// unsigned transaction and then sign it with your wallet.
400-
fn sign_psbt<'a>(&'a self, psbt: Psbt) -> AsyncResult<'a, Transaction, ()>;
401+
fn sign_psbt<'a>(
402+
&'a self, psbt: Psbt,
403+
) -> impl Future<Output = Result<Transaction, ()>> + MaybeSend + 'a;
401404
}
402405

403406
/// An alternative to [`CoinSelectionSource`] that can be implemented and used along [`Wallet`] to
@@ -406,17 +409,23 @@ pub trait CoinSelectionSource {
406409
/// For a synchronous version of this trait, see [`sync::WalletSourceSync`].
407410
pub trait WalletSource {
408411
/// Returns all UTXOs, with at least 1 confirmation each, that are available to spend.
409-
fn list_confirmed_utxos<'a>(&'a self) -> AsyncResult<'a, Vec<Utxo>, ()>;
412+
fn list_confirmed_utxos<'a>(
413+
&'a self,
414+
) -> impl Future<Output = Result<Vec<Utxo>, ()>> + MaybeSend + 'a;
410415
/// Returns a script to use for change above dust resulting from a successful coin selection
411416
/// attempt.
412-
fn get_change_script<'a>(&'a self) -> AsyncResult<'a, ScriptBuf, ()>;
417+
fn get_change_script<'a>(
418+
&'a self,
419+
) -> impl Future<Output = Result<ScriptBuf, ()>> + MaybeSend + 'a;
413420
/// Signs and provides the full [`TxIn::script_sig`] and [`TxIn::witness`] for all inputs within
414421
/// the transaction known to the wallet (i.e., any provided via
415422
/// [`WalletSource::list_confirmed_utxos`]).
416423
///
417424
/// If your wallet does not support signing PSBTs you can call `psbt.extract_tx()` to get the
418425
/// unsigned transaction and then sign it with your wallet.
419-
fn sign_psbt<'a>(&'a self, psbt: Psbt) -> AsyncResult<'a, Transaction, ()>;
426+
fn sign_psbt<'a>(
427+
&'a self, psbt: Psbt,
428+
) -> impl Future<Output = Result<Transaction, ()>> + MaybeSend + 'a;
420429
}
421430

422431
/// A wrapper over [`WalletSource`] that implements [`CoinSelection`] by preferring UTXOs that would
@@ -608,8 +617,8 @@ where
608617
fn select_confirmed_utxos<'a>(
609618
&'a self, claim_id: ClaimId, must_spend: Vec<Input>, must_pay_to: &'a [TxOut],
610619
target_feerate_sat_per_1000_weight: u32, max_tx_weight: u64,
611-
) -> AsyncResult<'a, CoinSelection, ()> {
612-
Box::pin(async move {
620+
) -> impl Future<Output = Result<CoinSelection, ()>> + MaybeSend + 'a {
621+
async move {
613622
let utxos = self.source.list_confirmed_utxos().await?;
614623
// TODO: Use fee estimation utils when we upgrade to bitcoin v0.30.0.
615624
let total_output_size: u64 = must_pay_to
@@ -656,10 +665,12 @@ where
656665
}
657666
}
658667
Err(())
659-
})
668+
}
660669
}
661670

662-
fn sign_psbt<'a>(&'a self, psbt: Psbt) -> AsyncResult<'a, Transaction, ()> {
671+
fn sign_psbt<'a>(
672+
&'a self, psbt: Psbt,
673+
) -> impl Future<Output = Result<Transaction, ()>> + MaybeSend + 'a {
663674
self.source.sign_psbt(psbt)
664675
}
665676
}

lightning/src/events/bump_transaction/sync.rs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::chain::chaininterface::BroadcasterInterface;
1818
use crate::chain::ClaimId;
1919
use crate::prelude::*;
2020
use crate::sign::SignerProvider;
21-
use crate::util::async_poll::{dummy_waker, AsyncResult, MaybeSend, MaybeSync};
21+
use crate::util::async_poll::{dummy_waker, MaybeSend, MaybeSync};
2222
use crate::util::logger::Logger;
2323

2424
use bitcoin::{Psbt, ScriptBuf, Transaction, TxOut};
@@ -59,19 +59,25 @@ impl<T: Deref> WalletSource for WalletSourceSyncWrapper<T>
5959
where
6060
T::Target: WalletSourceSync,
6161
{
62-
fn list_confirmed_utxos<'a>(&'a self) -> AsyncResult<'a, Vec<Utxo>, ()> {
62+
fn list_confirmed_utxos<'a>(
63+
&'a self,
64+
) -> impl Future<Output = Result<Vec<Utxo>, ()>> + MaybeSend + 'a {
6365
let utxos = self.0.list_confirmed_utxos();
64-
Box::pin(async move { utxos })
66+
async move { utxos }
6567
}
6668

67-
fn get_change_script<'a>(&'a self) -> AsyncResult<'a, ScriptBuf, ()> {
69+
fn get_change_script<'a>(
70+
&'a self,
71+
) -> impl Future<Output = Result<ScriptBuf, ()>> + MaybeSend + 'a {
6872
let script = self.0.get_change_script();
69-
Box::pin(async move { script })
73+
async move { script }
7074
}
7175

72-
fn sign_psbt<'a>(&'a self, psbt: Psbt) -> AsyncResult<'a, Transaction, ()> {
76+
fn sign_psbt<'a>(
77+
&'a self, psbt: Psbt,
78+
) -> impl Future<Output = Result<Transaction, ()>> + MaybeSend + 'a {
7379
let signed_psbt = self.0.sign_psbt(psbt);
74-
Box::pin(async move { signed_psbt })
80+
async move { signed_psbt }
7581
}
7682
}
7783

@@ -105,7 +111,7 @@ where
105111
&self, claim_id: ClaimId, must_spend: Vec<Input>, must_pay_to: &[TxOut],
106112
target_feerate_sat_per_1000_weight: u32, max_tx_weight: u64,
107113
) -> Result<CoinSelection, ()> {
108-
let mut fut = self.wallet.select_confirmed_utxos(
114+
let fut = self.wallet.select_confirmed_utxos(
109115
claim_id,
110116
must_spend,
111117
must_pay_to,
@@ -114,7 +120,7 @@ where
114120
);
115121
let mut waker = dummy_waker();
116122
let mut ctx = task::Context::from_waker(&mut waker);
117-
match fut.as_mut().poll(&mut ctx) {
123+
match pin!(fut).poll(&mut ctx) {
118124
task::Poll::Ready(result) => result,
119125
task::Poll::Pending => {
120126
unreachable!(
@@ -125,10 +131,10 @@ where
125131
}
126132

127133
fn sign_psbt(&self, psbt: Psbt) -> Result<Transaction, ()> {
128-
let mut fut = self.wallet.sign_psbt(psbt);
134+
let fut = self.wallet.sign_psbt(psbt);
129135
let mut waker = dummy_waker();
130136
let mut ctx = task::Context::from_waker(&mut waker);
131-
match fut.as_mut().poll(&mut ctx) {
137+
match pin!(fut).poll(&mut ctx) {
132138
task::Poll::Ready(result) => result,
133139
task::Poll::Pending => {
134140
unreachable!("Wallet::sign_psbt should not be pending in a sync context");
@@ -172,20 +178,22 @@ where
172178
fn select_confirmed_utxos<'a>(
173179
&'a self, claim_id: ClaimId, must_spend: Vec<Input>, must_pay_to: &'a [TxOut],
174180
target_feerate_sat_per_1000_weight: u32, max_tx_weight: u64,
175-
) -> AsyncResult<'a, CoinSelection, ()> {
181+
) -> impl Future<Output = Result<CoinSelection, ()>> + MaybeSend + 'a {
176182
let coins = self.0.select_confirmed_utxos(
177183
claim_id,
178184
must_spend,
179185
must_pay_to,
180186
target_feerate_sat_per_1000_weight,
181187
max_tx_weight,
182188
);
183-
Box::pin(async move { coins })
189+
async move { coins }
184190
}
185191

186-
fn sign_psbt<'a>(&'a self, psbt: Psbt) -> AsyncResult<'a, Transaction, ()> {
192+
fn sign_psbt<'a>(
193+
&'a self, psbt: Psbt,
194+
) -> impl Future<Output = Result<Transaction, ()>> + MaybeSend + 'a {
187195
let psbt = self.0.sign_psbt(psbt);
188-
Box::pin(async move { psbt })
196+
async move { psbt }
189197
}
190198
}
191199

lightning/src/sign/mod.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ use crate::ln::script::ShutdownScript;
5858
use crate::offers::invoice::UnsignedBolt12Invoice;
5959
use crate::types::features::ChannelTypeFeatures;
6060
use crate::types::payment::PaymentPreimage;
61-
use crate::util::async_poll::AsyncResult;
61+
use crate::util::async_poll::MaybeSend;
6262
use crate::util::ser::{ReadableArgs, Writeable};
6363
use crate::util::transaction_utils;
6464

@@ -68,7 +68,9 @@ use crate::sign::ecdsa::EcdsaChannelSigner;
6868
#[cfg(taproot)]
6969
use crate::sign::taproot::TaprootChannelSigner;
7070
use crate::util::atomic_counter::AtomicCounter;
71+
7172
use core::convert::TryInto;
73+
use core::future::Future;
7274
use core::ops::Deref;
7375
use core::sync::atomic::{AtomicUsize, Ordering};
7476
#[cfg(taproot)]
@@ -1064,7 +1066,9 @@ pub trait ChangeDestinationSource {
10641066
///
10651067
/// This method should return a different value each time it is called, to avoid linking
10661068
/// on-chain funds controlled to the same user.
1067-
fn get_change_destination_script<'a>(&'a self) -> AsyncResult<'a, ScriptBuf, ()>;
1069+
fn get_change_destination_script<'a>(
1070+
&'a self,
1071+
) -> impl Future<Output = Result<ScriptBuf, ()>> + MaybeSend + 'a;
10681072
}
10691073

10701074
/// A synchronous helper trait that describes an on-chain wallet capable of returning a (change) destination script.
@@ -1096,9 +1100,11 @@ impl<T: Deref> ChangeDestinationSource for ChangeDestinationSourceSyncWrapper<T>
10961100
where
10971101
T::Target: ChangeDestinationSourceSync,
10981102
{
1099-
fn get_change_destination_script<'a>(&'a self) -> AsyncResult<'a, ScriptBuf, ()> {
1103+
fn get_change_destination_script<'a>(
1104+
&'a self,
1105+
) -> impl Future<Output = Result<ScriptBuf, ()>> + MaybeSend + 'a {
11001106
let script = self.0.get_change_destination_script();
1101-
Box::pin(async move { script })
1107+
async move { script }
11021108
}
11031109
}
11041110

lightning/src/util/async_poll.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
//! Some utilities to make working with the standard library's [`Future`]s easier
1111
12-
use alloc::boxed::Box;
1312
use alloc::vec::Vec;
1413
use core::future::Future;
1514
use core::marker::Unpin;
@@ -92,13 +91,6 @@ pub(crate) fn dummy_waker() -> Waker {
9291
unsafe { Waker::from_raw(RawWaker::new(core::ptr::null(), &DUMMY_WAKER_VTABLE)) }
9392
}
9493

95-
#[cfg(feature = "std")]
96-
/// A type alias for a future that returns a result of type `T` or error `E`.
97-
pub type AsyncResult<'a, T, E> = Pin<Box<dyn Future<Output = Result<T, E>> + 'a + Send>>;
98-
#[cfg(not(feature = "std"))]
99-
/// A type alias for a future that returns a result of type `T` or error `E`.
100-
pub type AsyncResult<'a, T, E> = Pin<Box<dyn Future<Output = Result<T, E>> + 'a>>;
101-
10294
/// Marker trait to optionally implement `Sync` under std.
10395
#[cfg(feature = "std")]
10496
pub use core::marker::Sync as MaybeSync;

0 commit comments

Comments
 (0)