Skip to content

Commit 46949f4

Browse files
committed
Trivially replace Box::pin with pin! in a few places
Now that our MSRV is above 1.68 we can use the `pin!` macro to avoid having to `Box` various futures, avoiding some allocations, especially in `lightning-net-tokio`, which happens in a tight loop.
1 parent 2151101 commit 46949f4

File tree

6 files changed

+25
-26
lines changed

6 files changed

+25
-26
lines changed

lightning-liquidity/src/lsps2/service.rs

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

1010
//! Contains the main bLIP-52 / LSPS2 server-side object, [`LSPS2ServiceHandler`].
1111
12-
use alloc::boxed::Box;
1312
use alloc::string::{String, ToString};
1413
use alloc::vec::Vec;
1514
use lightning::util::persist::KVStore;
1615

1716
use core::cmp::Ordering as CmpOrdering;
1817
use core::future::Future as StdFuture;
1918
use core::ops::Deref;
19+
use core::pin::pin;
2020
use core::sync::atomic::{AtomicUsize, Ordering};
2121
use core::task;
2222

@@ -2173,7 +2173,7 @@ where
21732173
&self, counterparty_node_id: &PublicKey, request_id: LSPSRequestId, intercept_scid: u64,
21742174
cltv_expiry_delta: u32, client_trusts_lsp: bool, user_channel_id: u128,
21752175
) -> Result<(), APIError> {
2176-
let mut fut = Box::pin(self.inner.invoice_parameters_generated(
2176+
let mut fut = pin!(self.inner.invoice_parameters_generated(
21772177
counterparty_node_id,
21782178
request_id,
21792179
intercept_scid,
@@ -2202,7 +2202,7 @@ where
22022202
&self, intercept_scid: u64, intercept_id: InterceptId, expected_outbound_amount_msat: u64,
22032203
payment_hash: PaymentHash,
22042204
) -> Result<(), APIError> {
2205-
let mut fut = Box::pin(self.inner.htlc_intercepted(
2205+
let mut fut = pin!(self.inner.htlc_intercepted(
22062206
intercept_scid,
22072207
intercept_id,
22082208
expected_outbound_amount_msat,
@@ -2228,7 +2228,7 @@ where
22282228
pub fn htlc_handling_failed(
22292229
&self, failure_type: HTLCHandlingFailureType,
22302230
) -> Result<(), APIError> {
2231-
let mut fut = Box::pin(self.inner.htlc_handling_failed(failure_type));
2231+
let mut fut = pin!(self.inner.htlc_handling_failed(failure_type));
22322232

22332233
let mut waker = dummy_waker();
22342234
let mut ctx = task::Context::from_waker(&mut waker);
@@ -2249,7 +2249,7 @@ where
22492249
pub fn payment_forwarded(
22502250
&self, next_channel_id: ChannelId, skimmed_fee_msat: u64,
22512251
) -> Result<(), APIError> {
2252-
let mut fut = Box::pin(self.inner.payment_forwarded(next_channel_id, skimmed_fee_msat));
2252+
let mut fut = pin!(self.inner.payment_forwarded(next_channel_id, skimmed_fee_msat));
22532253

22542254
let mut waker = dummy_waker();
22552255
let mut ctx = task::Context::from_waker(&mut waker);
@@ -2290,7 +2290,7 @@ where
22902290
&self, counterparty_node_id: &PublicKey, user_channel_id: u128,
22912291
) -> Result<(), APIError> {
22922292
let mut fut =
2293-
Box::pin(self.inner.channel_open_abandoned(counterparty_node_id, user_channel_id));
2293+
pin!(self.inner.channel_open_abandoned(counterparty_node_id, user_channel_id));
22942294

22952295
let mut waker = dummy_waker();
22962296
let mut ctx = task::Context::from_waker(&mut waker);
@@ -2309,8 +2309,7 @@ where
23092309
pub fn channel_open_failed(
23102310
&self, counterparty_node_id: &PublicKey, user_channel_id: u128,
23112311
) -> Result<(), APIError> {
2312-
let mut fut =
2313-
Box::pin(self.inner.channel_open_failed(counterparty_node_id, user_channel_id));
2312+
let mut fut = pin!(self.inner.channel_open_failed(counterparty_node_id, user_channel_id));
23142313

23152314
let mut waker = dummy_waker();
23162315
let mut ctx = task::Context::from_waker(&mut waker);
@@ -2332,7 +2331,7 @@ where
23322331
&self, user_channel_id: u128, channel_id: &ChannelId, counterparty_node_id: &PublicKey,
23332332
) -> Result<(), APIError> {
23342333
let mut fut =
2335-
Box::pin(self.inner.channel_ready(user_channel_id, channel_id, counterparty_node_id));
2334+
pin!(self.inner.channel_ready(user_channel_id, channel_id, counterparty_node_id));
23362335

23372336
let mut waker = dummy_waker();
23382337
let mut ctx = task::Context::from_waker(&mut waker);

lightning-liquidity/src/manager.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
// You may not use this file except in accordance with one or both of these
88
// licenses.
99

10-
use alloc::boxed::Box;
1110
use alloc::string::ToString;
1211
use alloc::vec::Vec;
1312

@@ -61,6 +60,7 @@ use bitcoin::secp256k1::PublicKey;
6160

6261
use core::future::Future as StdFuture;
6362
use core::ops::Deref;
63+
use core::pin::pin;
6464
use core::task;
6565

6666
const LSPS_FEATURE_BIT: usize = 729;
@@ -1106,7 +1106,7 @@ where
11061106
) -> Result<Self, lightning::io::Error> {
11071107
let kv_store = KVStoreSyncWrapper(kv_store_sync);
11081108

1109-
let mut fut = Box::pin(LiquidityManager::new(
1109+
let mut fut = pin!(LiquidityManager::new(
11101110
entropy_source,
11111111
node_signer,
11121112
channel_manager,
@@ -1159,7 +1159,7 @@ where
11591159
client_config: Option<LiquidityClientConfig>, time_provider: TP,
11601160
) -> Result<Self, lightning::io::Error> {
11611161
let kv_store = KVStoreSyncWrapper(kv_store_sync);
1162-
let mut fut = Box::pin(LiquidityManager::new_with_custom_time_provider(
1162+
let mut fut = pin!(LiquidityManager::new_with_custom_time_provider(
11631163
entropy_source,
11641164
node_signer,
11651165
channel_manager,
@@ -1289,7 +1289,7 @@ where
12891289
pub fn persist(&self) -> Result<(), lightning::io::Error> {
12901290
let mut waker = dummy_waker();
12911291
let mut ctx = task::Context::from_waker(&mut waker);
1292-
match Box::pin(self.inner.persist()).as_mut().poll(&mut ctx) {
1292+
match pin!(self.inner.persist()).as_mut().poll(&mut ctx) {
12931293
task::Poll::Ready(result) => result,
12941294
task::Poll::Pending => {
12951295
// In a sync context, we can't wait for the future to complete.

lightning-net-tokio/src/lib.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use std::hash::Hash;
4343
use std::net::SocketAddr;
4444
use std::net::TcpStream as StdTcpStream;
4545
use std::ops::Deref;
46-
use std::pin::Pin;
46+
use std::pin::{pin, Pin};
4747
use std::sync::atomic::{AtomicU64, Ordering};
4848
use std::sync::{Arc, Mutex};
4949
use std::task::{self, Poll};
@@ -205,18 +205,17 @@ impl Connection {
205205
}
206206
us_lock.read_paused
207207
};
208-
// TODO: Drop the Box'ing of the futures once Rust has pin-on-stack support.
209208
let select_result = if read_paused {
210209
TwoSelector {
211-
a: Box::pin(write_avail_receiver.recv()),
212-
b: Box::pin(read_wake_receiver.recv()),
210+
a: pin!(write_avail_receiver.recv()),
211+
b: pin!(read_wake_receiver.recv()),
213212
}
214213
.await
215214
} else {
216215
ThreeSelector {
217-
a: Box::pin(write_avail_receiver.recv()),
218-
b: Box::pin(read_wake_receiver.recv()),
219-
c: Box::pin(reader.readable()),
216+
a: pin!(write_avail_receiver.recv()),
217+
b: pin!(read_wake_receiver.recv()),
218+
c: pin!(reader.readable()),
220219
}
221220
.await
222221
};

lightning/src/events/bump_transaction/sync.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
1212
use core::future::Future;
1313
use core::ops::Deref;
14+
use core::pin::pin;
1415
use core::task;
1516

1617
use crate::chain::chaininterface::BroadcasterInterface;
@@ -289,7 +290,7 @@ where
289290

290291
/// Handles all variants of [`BumpTransactionEvent`].
291292
pub fn handle_event(&self, event: &BumpTransactionEvent) {
292-
let mut fut = Box::pin(self.bump_transaction_event_handler.handle_event(event));
293+
let mut fut = pin!(self.bump_transaction_event_handler.handle_event(event));
293294
let mut waker = dummy_waker();
294295
let mut ctx = task::Context::from_waker(&mut waker);
295296
match fut.as_mut().poll(&mut ctx) {

lightning/src/util/persist.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use bitcoin::{BlockHash, Txid};
1919
use core::future::Future;
2020
use core::mem;
2121
use core::ops::Deref;
22-
use core::pin::Pin;
22+
use core::pin::{pin, Pin};
2323
use core::str::FromStr;
2424
use core::task;
2525

@@ -490,8 +490,7 @@ impl FutureSpawner for PanicingSpawner {
490490
fn poll_sync_future<F: Future>(future: F) -> F::Output {
491491
let mut waker = dummy_waker();
492492
let mut ctx = task::Context::from_waker(&mut waker);
493-
// TODO A future MSRV bump to 1.68 should allow for the pin macro
494-
match Pin::new(&mut Box::pin(future)).poll(&mut ctx) {
493+
match pin!(future).poll(&mut ctx) {
495494
task::Poll::Ready(result) => result,
496495
task::Poll::Pending => {
497496
// In a sync context, we can't wait for the future to complete.

lightning/src/util/sweep.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use bitcoin::{BlockHash, ScriptBuf, Transaction, Txid};
3535

3636
use core::future::Future;
3737
use core::ops::Deref;
38+
use core::pin::pin;
3839
use core::sync::atomic::{AtomicBool, Ordering};
3940
use core::task;
4041

@@ -970,7 +971,7 @@ where
970971
&self, output_descriptors: Vec<SpendableOutputDescriptor>, channel_id: Option<ChannelId>,
971972
exclude_static_outputs: bool, delay_until_height: Option<u32>,
972973
) -> Result<(), ()> {
973-
let mut fut = Box::pin(self.sweeper.track_spendable_outputs(
974+
let mut fut = pin!(self.sweeper.track_spendable_outputs(
974975
output_descriptors,
975976
channel_id,
976977
exclude_static_outputs,
@@ -1005,7 +1006,7 @@ where
10051006
///
10061007
/// Wraps [`OutputSweeper::regenerate_and_broadcast_spend_if_necessary`].
10071008
pub fn regenerate_and_broadcast_spend_if_necessary(&self) -> Result<(), ()> {
1008-
let mut fut = Box::pin(self.sweeper.regenerate_and_broadcast_spend_if_necessary());
1009+
let mut fut = pin!(self.sweeper.regenerate_and_broadcast_spend_if_necessary());
10091010
let mut waker = dummy_waker();
10101011
let mut ctx = task::Context::from_waker(&mut waker);
10111012
match fut.as_mut().poll(&mut ctx) {

0 commit comments

Comments
 (0)