Skip to content

Commit e8fa417

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 319bda9 commit e8fa417

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::{
@@ -35,7 +36,7 @@ use crate::sign::{
3536
ChannelDerivationParameters, HTLCDescriptor, SignerProvider, P2WPKH_WITNESS_WEIGHT,
3637
};
3738
use crate::sync::Mutex;
38-
use crate::util::async_poll::{AsyncResult, MaybeSend, MaybeSync};
39+
use crate::util::async_poll::{MaybeSend, MaybeSync};
3940
use crate::util::logger::Logger;
4041

4142
use bitcoin::amount::Amount;
@@ -367,13 +368,15 @@ pub trait CoinSelectionSource {
367368
fn select_confirmed_utxos<'a>(
368369
&'a self, claim_id: ClaimId, must_spend: Vec<Input>, must_pay_to: &'a [TxOut],
369370
target_feerate_sat_per_1000_weight: u32,
370-
) -> AsyncResult<'a, CoinSelection>;
371+
) -> impl Future<Output = Result<CoinSelection, ()>> + MaybeSend + 'a;
371372
/// Signs and provides the full witness for all inputs within the transaction known to the
372373
/// trait (i.e., any provided via [`CoinSelectionSource::select_confirmed_utxos`]).
373374
///
374375
/// If your wallet does not support signing PSBTs you can call `psbt.extract_tx()` to get the
375376
/// unsigned transaction and then sign it with your wallet.
376-
fn sign_psbt<'a>(&'a self, psbt: Psbt) -> AsyncResult<'a, Transaction>;
377+
fn sign_psbt<'a>(
378+
&'a self, psbt: Psbt,
379+
) -> impl Future<Output = Result<Transaction, ()>> + MaybeSend + 'a;
377380
}
378381

379382
/// An alternative to [`CoinSelectionSource`] that can be implemented and used along [`Wallet`] to
@@ -382,17 +385,23 @@ pub trait CoinSelectionSource {
382385
/// For a synchronous version of this trait, see [`sync::WalletSourceSync`].
383386
pub trait WalletSource {
384387
/// Returns all UTXOs, with at least 1 confirmation each, that are available to spend.
385-
fn list_confirmed_utxos<'a>(&'a self) -> AsyncResult<'a, Vec<Utxo>>;
388+
fn list_confirmed_utxos<'a>(
389+
&'a self,
390+
) -> impl Future<Output = Result<Vec<Utxo>, ()>> + MaybeSend + 'a;
386391
/// Returns a script to use for change above dust resulting from a successful coin selection
387392
/// attempt.
388-
fn get_change_script<'a>(&'a self) -> AsyncResult<'a, ScriptBuf>;
393+
fn get_change_script<'a>(
394+
&'a self,
395+
) -> impl Future<Output = Result<ScriptBuf, ()>> + MaybeSend + 'a;
389396
/// Signs and provides the full [`TxIn::script_sig`] and [`TxIn::witness`] for all inputs within
390397
/// the transaction known to the wallet (i.e., any provided via
391398
/// [`WalletSource::list_confirmed_utxos`]).
392399
///
393400
/// If your wallet does not support signing PSBTs you can call `psbt.extract_tx()` to get the
394401
/// unsigned transaction and then sign it with your wallet.
395-
fn sign_psbt<'a>(&'a self, psbt: Psbt) -> AsyncResult<'a, Transaction>;
402+
fn sign_psbt<'a>(
403+
&'a self, psbt: Psbt,
404+
) -> impl Future<Output = Result<Transaction, ()>> + MaybeSend + 'a;
396405
}
397406

398407
/// A wrapper over [`WalletSource`] that implements [`CoinSelection`] by preferring UTXOs that would
@@ -534,8 +543,8 @@ where
534543
fn select_confirmed_utxos<'a>(
535544
&'a self, claim_id: ClaimId, must_spend: Vec<Input>, must_pay_to: &'a [TxOut],
536545
target_feerate_sat_per_1000_weight: u32,
537-
) -> AsyncResult<'a, CoinSelection> {
538-
Box::pin(async move {
546+
) -> impl Future<Output = Result<CoinSelection, ()>> + MaybeSend + 'a {
547+
async move {
539548
let utxos = self.source.list_confirmed_utxos().await?;
540549
// TODO: Use fee estimation utils when we upgrade to bitcoin v0.30.0.
541550
const BASE_TX_SIZE: u64 = 4 /* version */ + 1 /* input count */ + 1 /* output count */ + 4 /* locktime */;
@@ -581,10 +590,12 @@ where
581590
}
582591
}
583592
Err(())
584-
})
593+
}
585594
}
586595

587-
fn sign_psbt<'a>(&'a self, psbt: Psbt) -> AsyncResult<'a, Transaction> {
596+
fn sign_psbt<'a>(
597+
&'a self, psbt: Psbt,
598+
) -> impl Future<Output = Result<Transaction, ()>> + MaybeSend + 'a {
588599
self.source.sign_psbt(psbt)
589600
}
590601
}

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,15 +111,15 @@ where
105111
&self, claim_id: ClaimId, must_spend: Vec<Input>, must_pay_to: &[TxOut],
106112
target_feerate_sat_per_1000_weight: u32,
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,
112118
target_feerate_sat_per_1000_weight,
113119
);
114120
let mut waker = dummy_waker();
115121
let mut ctx = task::Context::from_waker(&mut waker);
116-
match fut.as_mut().poll(&mut ctx) {
122+
match pin!(fut).poll(&mut ctx) {
117123
task::Poll::Ready(result) => result,
118124
task::Poll::Pending => {
119125
unreachable!(
@@ -124,10 +130,10 @@ where
124130
}
125131

126132
fn sign_psbt(&self, psbt: Psbt) -> Result<Transaction, ()> {
127-
let mut fut = self.wallet.sign_psbt(psbt);
133+
let fut = self.wallet.sign_psbt(psbt);
128134
let mut waker = dummy_waker();
129135
let mut ctx = task::Context::from_waker(&mut waker);
130-
match fut.as_mut().poll(&mut ctx) {
136+
match pin!(fut).poll(&mut ctx) {
131137
task::Poll::Ready(result) => result,
132138
task::Poll::Pending => {
133139
unreachable!("Wallet::sign_psbt should not be pending in a sync context");
@@ -171,19 +177,21 @@ where
171177
fn select_confirmed_utxos<'a>(
172178
&'a self, claim_id: ClaimId, must_spend: Vec<Input>, must_pay_to: &'a [TxOut],
173179
target_feerate_sat_per_1000_weight: u32,
174-
) -> AsyncResult<'a, CoinSelection> {
180+
) -> impl Future<Output = Result<CoinSelection, ()>> + MaybeSend + 'a {
175181
let coins = self.0.select_confirmed_utxos(
176182
claim_id,
177183
must_spend,
178184
must_pay_to,
179185
target_feerate_sat_per_1000_weight,
180186
);
181-
Box::pin(async move { coins })
187+
async move { coins }
182188
}
183189

184-
fn sign_psbt<'a>(&'a self, psbt: Psbt) -> AsyncResult<'a, Transaction> {
190+
fn sign_psbt<'a>(
191+
&'a self, psbt: Psbt,
192+
) -> impl Future<Output = Result<Transaction, ()>> + MaybeSend + 'a {
185193
let psbt = self.0.sign_psbt(psbt);
186-
Box::pin(async move { psbt })
194+
async move { psbt }
187195
}
188196
}
189197

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)]
@@ -1058,7 +1060,9 @@ pub trait ChangeDestinationSource {
10581060
///
10591061
/// This method should return a different value each time it is called, to avoid linking
10601062
/// on-chain funds controlled to the same user.
1061-
fn get_change_destination_script<'a>(&'a self) -> AsyncResult<'a, ScriptBuf>;
1063+
fn get_change_destination_script<'a>(
1064+
&'a self,
1065+
) -> impl Future<Output = Result<ScriptBuf, ()>> + MaybeSend + 'a;
10621066
}
10631067

10641068
/// A synchronous helper trait that describes an on-chain wallet capable of returning a (change) destination script.
@@ -1090,9 +1094,11 @@ impl<T: Deref> ChangeDestinationSource for ChangeDestinationSourceSyncWrapper<T>
10901094
where
10911095
T::Target: ChangeDestinationSourceSync,
10921096
{
1093-
fn get_change_destination_script<'a>(&'a self) -> AsyncResult<'a, ScriptBuf> {
1097+
fn get_change_destination_script<'a>(
1098+
&'a self,
1099+
) -> impl Future<Output = Result<ScriptBuf, ()>> + MaybeSend + 'a {
10941100
let script = self.0.get_change_destination_script();
1095-
Box::pin(async move { script })
1101+
async move { script }
10961102
}
10971103
}
10981104

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.
97-
pub type AsyncResult<'a, T> = Pin<Box<dyn Future<Output = Result<T, ()>> + 'a + Send>>;
98-
#[cfg(not(feature = "std"))]
99-
/// A type alias for a future that returns a result of type T.
100-
pub type AsyncResult<'a, T> = Pin<Box<dyn Future<Output = Result<T, ()>> + '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)