Skip to content

Commit f442ada

Browse files
committed
Manually implement PartialOrd for ChannelState
Relying on the order in which `ChannelState` variants are defined is error-prone.
1 parent 2d26679 commit f442ada

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

lightning/src/ln/channel.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ define_state_flags!(
414414
]
415415
);
416416

417-
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq)]
417+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
418418
enum ChannelState {
419419
/// We are negotiating the parameters required for the channel prior to funding it.
420420
NegotiatingFunding(NegotiatingFundingFlags),
@@ -433,6 +433,48 @@ enum ChannelState {
433433
ShutdownComplete,
434434
}
435435

436+
impl PartialOrd for ChannelState {
437+
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
438+
match self {
439+
ChannelState::NegotiatingFunding(flags) => match other {
440+
ChannelState::NegotiatingFunding(other_flags) => flags.partial_cmp(other_flags),
441+
ChannelState::FundingNegotiated => Some(cmp::Ordering::Less),
442+
ChannelState::AwaitingChannelReady(_) => Some(cmp::Ordering::Less),
443+
ChannelState::ChannelReady(_) => Some(cmp::Ordering::Less),
444+
ChannelState::ShutdownComplete => Some(cmp::Ordering::Less),
445+
},
446+
ChannelState::FundingNegotiated => match other {
447+
ChannelState::NegotiatingFunding(_) => Some(cmp::Ordering::Greater),
448+
ChannelState::FundingNegotiated => Some(cmp::Ordering::Equal),
449+
ChannelState::AwaitingChannelReady(_) => Some(cmp::Ordering::Less),
450+
ChannelState::ChannelReady(_) => Some(cmp::Ordering::Less),
451+
ChannelState::ShutdownComplete => Some(cmp::Ordering::Less),
452+
},
453+
ChannelState::AwaitingChannelReady(flags) => match other {
454+
ChannelState::NegotiatingFunding(_) => Some(cmp::Ordering::Greater),
455+
ChannelState::FundingNegotiated => Some(cmp::Ordering::Greater),
456+
ChannelState::AwaitingChannelReady(other_flags) => flags.partial_cmp(other_flags),
457+
ChannelState::ChannelReady(_) => Some(cmp::Ordering::Less),
458+
ChannelState::ShutdownComplete => Some(cmp::Ordering::Less),
459+
},
460+
ChannelState::ChannelReady(flags) => match other {
461+
ChannelState::NegotiatingFunding(_) => Some(cmp::Ordering::Greater),
462+
ChannelState::FundingNegotiated => Some(cmp::Ordering::Greater),
463+
ChannelState::AwaitingChannelReady(_) => Some(cmp::Ordering::Greater),
464+
ChannelState::ChannelReady(other_flags) => flags.partial_cmp(other_flags),
465+
ChannelState::ShutdownComplete => Some(cmp::Ordering::Less),
466+
},
467+
ChannelState::ShutdownComplete => match other {
468+
ChannelState::NegotiatingFunding(_) => Some(cmp::Ordering::Greater),
469+
ChannelState::FundingNegotiated => Some(cmp::Ordering::Greater),
470+
ChannelState::AwaitingChannelReady(_) => Some(cmp::Ordering::Greater),
471+
ChannelState::ChannelReady(_) => Some(cmp::Ordering::Greater),
472+
ChannelState::ShutdownComplete => Some(cmp::Ordering::Equal),
473+
},
474+
}
475+
}
476+
}
477+
436478
macro_rules! impl_state_flag {
437479
($get: ident, $set: ident, $clear: ident, $state_flag: expr, [$($state: ident),+]) => {
438480
#[allow(unused)]

0 commit comments

Comments
 (0)