mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-28 09:11: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,
|
UpgradeError,
|
||||||
};
|
};
|
||||||
use futures::prelude::*;
|
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};
|
use tokio_io::{AsyncRead, AsyncWrite};
|
||||||
|
|
||||||
pub use self::dummy::DummyProtocolsHandler;
|
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
|
/// On the other hand, the return value is only an indication and doesn't mean that the user
|
||||||
/// will not call `shutdown()`.
|
/// will not call `shutdown()`.
|
||||||
///
|
///
|
||||||
/// When multiple `ProtocolsHandler` are combined together, they should use return the largest
|
/// When multiple `ProtocolsHandler` are combined together, the largest `KeepAlive` should be
|
||||||
/// value of the two, or `Forever` if either returns `Forever`.
|
/// used.
|
||||||
///
|
///
|
||||||
/// The result of this method should be checked every time `poll()` is invoked.
|
/// 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]
|
#[inline]
|
||||||
fn connection_keep_alive(&self) -> KeepAlive {
|
fn connection_keep_alive(&self) -> KeepAlive {
|
||||||
match (self.proto1.connection_keep_alive(), self.proto2.connection_keep_alive()) {
|
cmp::max(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)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
Reference in New Issue
Block a user