Skip to content

Commit

Permalink
Replace futures select with our own select enum to fix MSRV
Browse files Browse the repository at this point in the history
`futures` recently broke our MSRV by bumping the `syn` major
version in a patch release. This makes it impractical for us to
use, instead here we replace the usage of its `select_biased` macro
with a trivial enum.

Given its simplicity we likely should have done this without ever
taking the dependency.
  • Loading branch information
TheBlueMatt committed Mar 30, 2023
1 parent 387ae75 commit a041fc9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lightning-background-processor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ default = ["std"]
bitcoin = { version = "0.29.0", default-features = false }
lightning = { version = "0.0.114", path = "../lightning", default-features = false }
lightning-rapid-gossip-sync = { version = "0.0.114", path = "../lightning-rapid-gossip-sync", default-features = false }
futures-util = { version = "0.3", default-features = false, features = ["async-await-macro"], optional = true }
futures-util = { version = "0.3", default-features = false, optional = true }

[dev-dependencies]
lightning = { version = "0.0.114", path = "../lightning", features = ["_test_utils"] }
Expand Down
44 changes: 41 additions & 3 deletions lightning-background-processor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,40 @@ macro_rules! define_run_body {
} }
}

#[cfg(feature = "futures")]
use core::future::Future;
#[cfg(feature = "futures")]
use core::task::Poll;
#[cfg(feature = "futures")]
use core::pin::Pin;
#[cfg(feature = "futures")]
use core::marker::Unpin;
#[cfg(feature = "futures")]
struct Selecter<A: Future<Output=()> + Unpin, B: Future<Output=bool> + Unpin> {
a: A,
b: B,
}
#[cfg(feature = "futures")]
enum SelecterOutput {
A, B(bool),
}

#[cfg(feature = "futures")]
impl<A: Future<Output=()> + Unpin, B: Future<Output=bool> + Unpin> Future for Selecter<A, B> {
type Output = SelecterOutput;
fn poll(mut self: Pin<&mut Self>, ctx: &mut core::task::Context<'_>) -> Poll<SelecterOutput> {
match Pin::new(&mut self.a).poll(ctx) {
Poll::Ready(()) => { return Poll::Ready(SelecterOutput::A); },
Poll::Pending => {},
}
match Pin::new(&mut self.b).poll(ctx) {
Poll::Ready(res) => { return Poll::Ready(SelecterOutput::B(res)); },
Poll::Pending => {},
}
Poll::Pending
}
}

/// Processes background events in a future.
///
/// `sleeper` should return a future which completes in the given amount of time and returns a
Expand Down Expand Up @@ -470,9 +504,13 @@ where
chain_monitor, chain_monitor.process_pending_events_async(async_event_handler).await,
channel_manager, channel_manager.process_pending_events_async(async_event_handler).await,
gossip_sync, peer_manager, logger, scorer, should_break, {
select_biased! {
_ = channel_manager.get_persistable_update_future().fuse() => true,
exit = sleeper(Duration::from_millis(100)).fuse() => {
let fut = Selecter {
a: channel_manager.get_persistable_update_future(),
b: sleeper(Duration::from_millis(100)),
};
match fut.await {
SelecterOutput::A => true,
SelecterOutput::B(exit) => {
should_break = exit;
false
}
Expand Down

0 comments on commit a041fc9

Please sign in to comment.