Skip to content

Commit 7f3885b

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 8e02e4c commit 7f3885b

File tree

4 files changed

+54
-41
lines changed

4 files changed

+54
-41
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;
@@ -394,13 +395,15 @@ pub trait CoinSelectionSource {
394395
fn select_confirmed_utxos<'a>(
395396
&'a self, claim_id: ClaimId, must_spend: Vec<Input>, must_pay_to: &'a [TxOut],
396397
target_feerate_sat_per_1000_weight: u32, max_tx_weight: u64,
397-
) -> AsyncResult<'a, CoinSelection, ()>;
398+
) -> impl Future<Output = Result<CoinSelection, ()>> + MaybeSend + 'a;
398399
/// Signs and provides the full witness for all inputs within the transaction known to the
399400
/// trait (i.e., any provided via [`CoinSelectionSource::select_confirmed_utxos`]).
400401
///
401402
/// If your wallet does not support signing PSBTs you can call `psbt.extract_tx()` to get the
402403
/// unsigned transaction and then sign it with your wallet.
403-
fn sign_psbt<'a>(&'a self, psbt: Psbt) -> AsyncResult<'a, Transaction, ()>;
404+
fn sign_psbt<'a>(
405+
&'a self, psbt: Psbt,
406+
) -> impl Future<Output = Result<Transaction, ()>> + MaybeSend + 'a;
404407
}
405408

406409
/// An alternative to [`CoinSelectionSource`] that can be implemented and used along [`Wallet`] to
@@ -412,17 +415,23 @@ pub trait CoinSelectionSource {
412415
// Note that updates to documentation on this trait should be copied to the synchronous version.
413416
pub trait WalletSource {
414417
/// Returns all UTXOs, with at least 1 confirmation each, that are available to spend.
415-
fn list_confirmed_utxos<'a>(&'a self) -> AsyncResult<'a, Vec<Utxo>, ()>;
418+
fn list_confirmed_utxos<'a>(
419+
&'a self,
420+
) -> impl Future<Output = Result<Vec<Utxo>, ()>> + MaybeSend + 'a;
416421
/// Returns a script to use for change above dust resulting from a successful coin selection
417422
/// attempt.
418-
fn get_change_script<'a>(&'a self) -> AsyncResult<'a, ScriptBuf, ()>;
423+
fn get_change_script<'a>(
424+
&'a self,
425+
) -> impl Future<Output = Result<ScriptBuf, ()>> + MaybeSend + 'a;
419426
/// Signs and provides the full [`TxIn::script_sig`] and [`TxIn::witness`] for all inputs within
420427
/// the transaction known to the wallet (i.e., any provided via
421428
/// [`WalletSource::list_confirmed_utxos`]).
422429
///
423430
/// If your wallet does not support signing PSBTs you can call `psbt.extract_tx()` to get the
424431
/// unsigned transaction and then sign it with your wallet.
425-
fn sign_psbt<'a>(&'a self, psbt: Psbt) -> AsyncResult<'a, Transaction, ()>;
432+
fn sign_psbt<'a>(
433+
&'a self, psbt: Psbt,
434+
) -> impl Future<Output = Result<Transaction, ()>> + MaybeSend + 'a;
426435
}
427436

428437
/// A wrapper over [`WalletSource`] that implements [`CoinSelectionSource`] by preferring UTXOs
@@ -617,8 +626,8 @@ where
617626
fn select_confirmed_utxos<'a>(
618627
&'a self, claim_id: ClaimId, must_spend: Vec<Input>, must_pay_to: &'a [TxOut],
619628
target_feerate_sat_per_1000_weight: u32, max_tx_weight: u64,
620-
) -> AsyncResult<'a, CoinSelection, ()> {
621-
Box::pin(async move {
629+
) -> impl Future<Output = Result<CoinSelection, ()>> + MaybeSend + 'a {
630+
async move {
622631
let utxos = self.source.list_confirmed_utxos().await?;
623632
// TODO: Use fee estimation utils when we upgrade to bitcoin v0.30.0.
624633
let total_output_size: u64 = must_pay_to
@@ -665,10 +674,12 @@ where
665674
}
666675
}
667676
Err(())
668-
})
677+
}
669678
}
670679

671-
fn sign_psbt<'a>(&'a self, psbt: Psbt) -> AsyncResult<'a, Transaction, ()> {
680+
fn sign_psbt<'a>(
681+
&'a self, psbt: Psbt,
682+
) -> impl Future<Output = Result<Transaction, ()>> + MaybeSend + 'a {
672683
self.source.sign_psbt(psbt)
673684
}
674685
}

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};
@@ -72,19 +72,25 @@ impl<T: Deref> WalletSource for WalletSourceSyncWrapper<T>
7272
where
7373
T::Target: WalletSourceSync,
7474
{
75-
fn list_confirmed_utxos<'a>(&'a self) -> AsyncResult<'a, Vec<Utxo>, ()> {
75+
fn list_confirmed_utxos<'a>(
76+
&'a self,
77+
) -> impl Future<Output = Result<Vec<Utxo>, ()>> + MaybeSend + 'a {
7678
let utxos = self.0.list_confirmed_utxos();
77-
Box::pin(async move { utxos })
79+
async move { utxos }
7880
}
7981

80-
fn get_change_script<'a>(&'a self) -> AsyncResult<'a, ScriptBuf, ()> {
82+
fn get_change_script<'a>(
83+
&'a self,
84+
) -> impl Future<Output = Result<ScriptBuf, ()>> + MaybeSend + 'a {
8185
let script = self.0.get_change_script();
82-
Box::pin(async move { script })
86+
async move { script }
8387
}
8488

85-
fn sign_psbt<'a>(&'a self, psbt: Psbt) -> AsyncResult<'a, Transaction, ()> {
89+
fn sign_psbt<'a>(
90+
&'a self, psbt: Psbt,
91+
) -> impl Future<Output = Result<Transaction, ()>> + MaybeSend + 'a {
8692
let signed_psbt = self.0.sign_psbt(psbt);
87-
Box::pin(async move { signed_psbt })
93+
async move { signed_psbt }
8894
}
8995
}
9096

@@ -123,7 +129,7 @@ where
123129
&self, claim_id: ClaimId, must_spend: Vec<Input>, must_pay_to: &[TxOut],
124130
target_feerate_sat_per_1000_weight: u32, max_tx_weight: u64,
125131
) -> Result<CoinSelection, ()> {
126-
let mut fut = self.wallet.select_confirmed_utxos(
132+
let fut = self.wallet.select_confirmed_utxos(
127133
claim_id,
128134
must_spend,
129135
must_pay_to,
@@ -132,7 +138,7 @@ where
132138
);
133139
let mut waker = dummy_waker();
134140
let mut ctx = task::Context::from_waker(&mut waker);
135-
match fut.as_mut().poll(&mut ctx) {
141+
match pin!(fut).poll(&mut ctx) {
136142
task::Poll::Ready(result) => result,
137143
task::Poll::Pending => {
138144
unreachable!(
@@ -143,10 +149,10 @@ where
143149
}
144150

145151
fn sign_psbt(&self, psbt: Psbt) -> Result<Transaction, ()> {
146-
let mut fut = self.wallet.sign_psbt(psbt);
152+
let fut = self.wallet.sign_psbt(psbt);
147153
let mut waker = dummy_waker();
148154
let mut ctx = task::Context::from_waker(&mut waker);
149-
match fut.as_mut().poll(&mut ctx) {
155+
match pin!(fut).poll(&mut ctx) {
150156
task::Poll::Ready(result) => result,
151157
task::Poll::Pending => {
152158
unreachable!("Wallet::sign_psbt should not be pending in a sync context");
@@ -234,20 +240,22 @@ where
234240
fn select_confirmed_utxos<'a>(
235241
&'a self, claim_id: ClaimId, must_spend: Vec<Input>, must_pay_to: &'a [TxOut],
236242
target_feerate_sat_per_1000_weight: u32, max_tx_weight: u64,
237-
) -> AsyncResult<'a, CoinSelection, ()> {
243+
) -> impl Future<Output = Result<CoinSelection, ()>> + MaybeSend + 'a {
238244
let coins = self.0.select_confirmed_utxos(
239245
claim_id,
240246
must_spend,
241247
must_pay_to,
242248
target_feerate_sat_per_1000_weight,
243249
max_tx_weight,
244250
);
245-
Box::pin(async move { coins })
251+
async move { coins }
246252
}
247253

248-
fn sign_psbt<'a>(&'a self, psbt: Psbt) -> AsyncResult<'a, Transaction, ()> {
254+
fn sign_psbt<'a>(
255+
&'a self, psbt: Psbt,
256+
) -> impl Future<Output = Result<Transaction, ()>> + MaybeSend + 'a {
249257
let psbt = self.0.sign_psbt(psbt);
250-
Box::pin(async move { psbt })
258+
async move { psbt }
251259
}
252260
}
253261

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)]
@@ -1066,7 +1068,9 @@ pub trait ChangeDestinationSource {
10661068
///
10671069
/// This method should return a different value each time it is called, to avoid linking
10681070
/// on-chain funds controlled to the same user.
1069-
fn get_change_destination_script<'a>(&'a self) -> AsyncResult<'a, ScriptBuf, ()>;
1071+
fn get_change_destination_script<'a>(
1072+
&'a self,
1073+
) -> impl Future<Output = Result<ScriptBuf, ()>> + MaybeSend + 'a;
10701074
}
10711075

10721076
/// A synchronous helper trait that describes an on-chain wallet capable of returning a (change) destination script.
@@ -1101,9 +1105,11 @@ impl<T: Deref> ChangeDestinationSource for ChangeDestinationSourceSyncWrapper<T>
11011105
where
11021106
T::Target: ChangeDestinationSourceSync,
11031107
{
1104-
fn get_change_destination_script<'a>(&'a self) -> AsyncResult<'a, ScriptBuf, ()> {
1108+
fn get_change_destination_script<'a>(
1109+
&'a self,
1110+
) -> impl Future<Output = Result<ScriptBuf, ()>> + MaybeSend + 'a {
11051111
let script = self.0.get_change_destination_script();
1106-
Box::pin(async move { script })
1112+
async move { script }
11071113
}
11081114
}
11091115

lightning/src/util/async_poll.rs

Lines changed: 0 additions & 12 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,17 +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-
///
98-
/// This is not exported to bindings users as async is only supported in Rust.
99-
pub type AsyncResult<'a, T, E> = Pin<Box<dyn Future<Output = Result<T, E>> + 'a + Send>>;
100-
#[cfg(not(feature = "std"))]
101-
/// A type alias for a future that returns a result of type `T` or error `E`.
102-
///
103-
/// This is not exported to bindings users as async is only supported in Rust.
104-
pub type AsyncResult<'a, T, E> = Pin<Box<dyn Future<Output = Result<T, E>> + 'a>>;
105-
10694
/// Marker trait to optionally implement `Sync` under std.
10795
///
10896
/// This is not exported to bindings users as async is only supported in Rust.

0 commit comments

Comments
 (0)