Skip to content

Commit 6e3ba86

Browse files
committed
Add a generic public FutureSpawner in LDK directly
In the next commit we'll use this to spawn async persistence operations in the background, but for now we just move the `lightning-block-sync` `FutureSpawner` into `lightning`.
1 parent 4d2d9d2 commit 6e3ba86

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

lightning-block-sync/src/gossip.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ use bitcoin::hash_types::BlockHash;
1010
use bitcoin::transaction::{OutPoint, TxOut};
1111

1212
use lightning::ln::peer_handler::APeerManager;
13-
1413
use lightning::routing::gossip::{NetworkGraph, P2PGossipSync};
1514
use lightning::routing::utxo::{UtxoFuture, UtxoLookup, UtxoLookupError, UtxoResult};
16-
1715
use lightning::util::logger::Logger;
16+
use lightning::util::native_async::FutureSpawner;
1817

1918
use std::collections::VecDeque;
2019
use std::future::Future;
@@ -43,17 +42,6 @@ pub trait UtxoSource: BlockSource + 'static {
4342
fn is_output_unspent<'a>(&'a self, outpoint: OutPoint) -> AsyncBlockSourceResult<'a, bool>;
4443
}
4544

46-
/// A generic trait which is able to spawn futures in the background.
47-
///
48-
/// If the `tokio` feature is enabled, this is implemented on `TokioSpawner` struct which
49-
/// delegates to `tokio::spawn()`.
50-
pub trait FutureSpawner: Send + Sync + 'static {
51-
/// Spawns the given future as a background task.
52-
///
53-
/// This method MUST NOT block on the given future immediately.
54-
fn spawn<T: Future<Output = ()> + Send + 'static>(&self, future: T);
55-
}
56-
5745
#[cfg(feature = "tokio")]
5846
/// A trivial [`FutureSpawner`] which delegates to `tokio::spawn`.
5947
pub struct TokioSpawner;

lightning/src/util/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub mod base32;
2626
pub(crate) mod base32;
2727
pub mod errors;
2828
pub mod message_signing;
29+
pub mod native_async;
2930
pub mod persist;
3031
pub mod scid_utils;
3132
pub mod ser;

lightning/src/util/native_async.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
2+
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
4+
// You may not use this file except in accordance with one or both of these
5+
// licenses.
6+
7+
//! This module contains a few public utility which are used to run LDK in a native Rust async
8+
//! environment.
9+
10+
use crate::util::async_poll::MaybeSend;
11+
use core::future::Future;
12+
13+
/// A generic trait which is able to spawn futures in the background.
14+
pub trait FutureSpawner: Send + Sync + 'static {
15+
/// Spawns the given future as a background task.
16+
///
17+
/// This method MUST NOT block on the given future immediately.
18+
fn spawn<T: Future<Output = ()> + MaybeSend + 'static>(&self, future: T);
19+
}

0 commit comments

Comments
 (0)