mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-28 01:01:34 +00:00
Add Ord implementation for KeepAlive (#918)
* Add Ord implementation for KeepAlive * Fix import path * Implement PartialOrd for KeepAlive * remove Ord implementation * Reimplement Ord for KeepAlive * Add equality cases to KeepAlive Ord implementation
This commit is contained in:
@ -40,7 +40,7 @@ use crate::upgrade::{
|
||||
UpgradeError,
|
||||
};
|
||||
use futures::prelude::*;
|
||||
use std::{error, fmt, time::Duration, time::Instant};
|
||||
use std::{cmp::Ordering, error, fmt, time::Duration, time::Instant};
|
||||
use tokio_io::{AsyncRead, AsyncWrite};
|
||||
|
||||
pub use self::dummy::DummyProtocolsHandler;
|
||||
@ -152,8 +152,8 @@ pub trait ProtocolsHandler {
|
||||
/// On the other hand, the return value is only an indication and doesn't mean that the user
|
||||
/// will not call `shutdown()`.
|
||||
///
|
||||
/// When multiple `ProtocolsHandler` are combined together, they should use return the largest
|
||||
/// value of the two, or `Forever` if either returns `Forever`.
|
||||
/// When multiple `ProtocolsHandler` are combined together, the largest `KeepAlive` should be
|
||||
/// used.
|
||||
///
|
||||
/// The result of this method should be checked every time `poll()` is invoked.
|
||||
///
|
||||
@ -434,3 +434,22 @@ impl KeepAlive {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for KeepAlive {
|
||||
fn partial_cmp(&self, other: &KeepAlive) -> Option<Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for KeepAlive {
|
||||
fn cmp(&self, other: &KeepAlive) -> Ordering {
|
||||
use self::KeepAlive::*;
|
||||
|
||||
match (self, other) {
|
||||
(Now, Now) | (Forever, Forever) => Ordering::Equal,
|
||||
(Now, _) | (_, Forever) => Ordering::Less,
|
||||
(_, Now) | (Forever, _) => Ordering::Greater,
|
||||
(Until(expiration), Until(other_expiration)) => expiration.cmp(other_expiration),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -210,11 +210,7 @@ where
|
||||
|
||||
#[inline]
|
||||
fn connection_keep_alive(&self) -> KeepAlive {
|
||||
match (self.proto1.connection_keep_alive(), self.proto2.connection_keep_alive()) {
|
||||
(KeepAlive::Forever, _) | (_, KeepAlive::Forever) => KeepAlive::Forever,
|
||||
(a, KeepAlive::Now) | (KeepAlive::Now, a) => a,
|
||||
(KeepAlive::Until(a), KeepAlive::Until(b)) => KeepAlive::Until(cmp::max(a, b)),
|
||||
}
|
||||
cmp::max(self.proto1.connection_keep_alive(), self.proto2.connection_keep_alive())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
Reference in New Issue
Block a user