Skip to content

Commit b9bd969

Browse files
committed
chore: add more docs, rm unused async_trait, fix workspace lint rules, clean up unnecessary trait bounds
1 parent 23599e3 commit b9bd969

File tree

9 files changed

+61
-50
lines changed

9 files changed

+61
-50
lines changed

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,10 @@ all = "warn"
108108
[workspace.lints.rust]
109109
missing_debug_implementations = "warn"
110110
missing_docs = "warn"
111-
rust-2018-idioms = { level = "deny", priority = -1 }
112-
unreachable-pub = "warn"
113-
unused-must-use = "deny"
111+
rust_2018_idioms = { level = "deny", priority = -1 }
112+
unreachable_pub = "warn"
113+
unused_must_use = "deny"
114+
rust_2024_incompatible_pat = "warn"
114115

115116
[workspace.lints.clippy]
116117
# These are some of clippy's nursery (i.e., experimental) lints that we like.

msg-socket/src/connection/backoff.rs

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
use futures::{FutureExt, Stream};
2-
use std::{pin::Pin, task::Poll, time::Duration};
2+
use std::{
3+
pin::Pin,
4+
task::{Context, Poll},
5+
time::Duration,
6+
};
37
use tokio::time::sleep;
48

59
/// Helper trait alias for backoff streams.
610
/// We define any stream that yields `Duration`s as a backoff
711
pub trait Backoff: Stream<Item = Duration> + Unpin {}
812

9-
/// Blanket implementation of `Backoff` for any stream that yields `Duration`s.
13+
// Blanket implementation of `Backoff` for any stream that yields `Duration`s.
1014
impl<T> Backoff for T where T: Stream<Item = Duration> + Unpin {}
1115

1216
/// A stream that yields exponentially increasing backoff durations.
@@ -23,6 +27,7 @@ pub struct ExponentialBackoff {
2327
}
2428

2529
impl ExponentialBackoff {
30+
/// Creates a new exponential backoff stream with the given initial duration and max retries.
2631
pub fn new(initial: Duration, max_retries: usize) -> Self {
2732
Self { retry_count: 0, max_retries, backoff: initial, timeout: None }
2833
}
@@ -38,38 +43,36 @@ impl Stream for ExponentialBackoff {
3843

3944
/// Polls the exponential backoff stream. Returns `Poll::Ready` with the current backoff
4045
/// duration if the backoff timeout has elapsed, otherwise returns `Poll::Pending`.
41-
fn poll_next(
42-
self: Pin<&mut Self>,
43-
cx: &mut std::task::Context<'_>,
44-
) -> Poll<Option<Self::Item>> {
46+
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
4547
let this = self.get_mut();
4648

4749
loop {
48-
if let Some(ref mut timeout) = this.timeout {
49-
if timeout.poll_unpin(cx).is_ready() {
50-
// Timeout has elapsed, so reset the timeout and double the backoff
51-
this.backoff *= 2;
52-
this.retry_count += 1;
50+
let Some(ref mut timeout) = this.timeout else {
51+
// Set the initial timeout
52+
this.reset_timeout();
53+
continue;
54+
};
55+
56+
if timeout.poll_unpin(cx).is_ready() {
57+
// Timeout has elapsed, so reset the timeout and double the backoff
58+
this.backoff *= 2;
59+
this.retry_count += 1;
5360

54-
// Close the stream
55-
if this.retry_count >= this.max_retries {
56-
return Poll::Ready(None);
57-
}
61+
// Close the stream
62+
if this.retry_count >= this.max_retries {
63+
return Poll::Ready(None);
64+
}
5865

59-
this.reset_timeout();
66+
this.reset_timeout();
6067

61-
// Wake up the task to poll the timeout again
62-
cx.waker().wake_by_ref();
68+
// Wake up the task to poll the timeout again
69+
cx.waker().wake_by_ref();
6370

64-
// Return the current backoff duration
65-
return Poll::Ready(Some(this.backoff));
66-
} else {
67-
// Timeout has not elapsed, so return pending
68-
return Poll::Pending;
69-
}
71+
// Return the current backoff duration
72+
return Poll::Ready(Some(this.backoff));
7073
} else {
71-
// Set initial timeout
72-
this.reset_timeout();
74+
// Timeout has not elapsed, so return pending
75+
return Poll::Pending;
7376
}
7477
}
7578
}

msg-socket/src/connection/state.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
use msg_transport::Address;
2-
3-
use super::Backoff;
4-
51
/// Abstraction to represent the state of a connection.
62
///
73
/// * `C` is the channel type, which is used to send and receive generic messages.
84
/// * `B` is the backoff type, used to control the backoff state for inactive connections.
9-
pub enum ConnectionState<C, B, A: Address> {
5+
/// * `A` is the address type, used to represent the address of the connection.
6+
pub enum ConnectionState<C, B, A> {
107
Active {
118
/// Channel to control the underlying connection. This is used to send
129
/// and receive any kind of message in any direction.
@@ -19,15 +16,13 @@ pub enum ConnectionState<C, B, A: Address> {
1916
},
2017
}
2118

22-
impl<C, B: Backoff, A: Address> ConnectionState<C, B, A> {
19+
impl<C, B, A> ConnectionState<C, B, A> {
2320
/// Returns `true` if the connection is active.
24-
#[allow(unused)]
2521
pub fn is_active(&self) -> bool {
2622
matches!(self, Self::Active { .. })
2723
}
2824

2925
/// Returns `true` if the connection is inactive.
30-
#[allow(unused)]
3126
pub fn is_inactive(&self) -> bool {
3227
matches!(self, Self::Inactive { .. })
3328
}

msg-socket/src/req/socket.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ where
113113
/// Tries to connect to the target endpoint with the default options.
114114
/// A ReqSocket can only be connected to a single address.
115115
pub async fn try_connect(&mut self, endpoint: A) -> Result<(), ReqError> {
116-
// Initialize communication channels
117116
let (to_driver, from_socket) = mpsc::channel(DEFAULT_BUFFER_SIZE);
118117

119118
// TODO: Don't panic, return error

msg-transport/src/ipc/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,20 @@ pub struct Ipc {
4141
}
4242

4343
impl Ipc {
44+
/// Creates a new `Ipc` transport.
4445
pub fn new(config: Config) -> Self {
4546
Self { config, listener: None, path: None }
4647
}
4748
}
4849

50+
/// A stream representing an IPC connection.
4951
pub struct IpcStream {
5052
peer: PathBuf,
5153
stream: UnixStream,
5254
}
5355

5456
impl IpcStream {
57+
/// Connects to the given peer path.
5558
pub async fn connect(peer: PathBuf) -> io::Result<Self> {
5659
let stream = UnixStream::connect(&peer).await?;
5760
Ok(Self { peer, stream })

msg-transport/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ pub trait Transport<A: Address>: Send + Sync + Unpin + 'static {
196196
fn poll_accept(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Accept>;
197197
}
198198

199+
/// Extension trait for transports that provides additional methods.
199200
pub trait TransportExt<A: Address>: Transport<A> {
200201
/// Async-friendly interface for accepting inbound connections.
201202
fn accept(&mut self) -> Acceptor<'_, Self, A>
@@ -226,6 +227,7 @@ where
226227
T: Transport<A>,
227228
A: Address,
228229
{
230+
/// Creates a new `Acceptor` for the given transport.
229231
fn new(inner: &'a mut T) -> Self {
230232
Self { inner, pending: None, _marker: PhantomData }
231233
}

msg-transport/src/quic/config.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,20 @@ pub struct ConfigBuilder<C> {
2525
keep_alive_interval: Duration,
2626
}
2727

28+
impl<C> Default for ConfigBuilder<C>
29+
where
30+
C: ControllerFactory + Default + Send + Sync + 'static,
31+
{
32+
fn default() -> Self {
33+
Self::new()
34+
}
35+
}
36+
2837
impl<C> ConfigBuilder<C>
2938
where
3039
C: ControllerFactory + Default + Send + Sync + 'static,
3140
{
3241
/// Creates a new [`ConfigBuilder`] with sensible defaults.
33-
#[allow(clippy::new_without_default)]
3442
pub fn new() -> Self {
3543
Self {
3644
cc: C::default(),
@@ -43,37 +51,39 @@ where
4351
}
4452

4553
/// Sets the initial MTU.
46-
pub fn initial_mtu(mut self, mtu: u16) -> Self {
54+
pub fn with_initial_mtu(mut self, mtu: u16) -> Self {
4755
self.initial_mtu = mtu;
4856
self
4957
}
5058

5159
/// Sets the maximum stream bandwidth in bytes per second.
52-
pub fn max_stream_bandwidth(mut self, bandwidth: u32) -> Self {
60+
pub fn with_max_stream_bandwidth(mut self, bandwidth: u32) -> Self {
5361
self.max_stream_bandwidth = bandwidth;
5462
self
5563
}
5664

5765
/// Sets the expected round-trip time in milliseconds.
58-
pub fn expected_rtt(mut self, rtt: u32) -> Self {
66+
pub fn with_expected_rtt(mut self, rtt: u32) -> Self {
5967
self.expected_rtt = rtt;
6068
self
6169
}
6270

6371
/// Sets the maximum idle timeout.
64-
pub fn max_idle_timeout(mut self, timeout: Duration) -> Self {
72+
///
73+
/// NOTE: this value must be less than 2^62 milliseconds.
74+
pub fn with_max_idle_timeout(mut self, timeout: Duration) -> Self {
6575
self.max_idle_timeout = timeout;
6676
self
6777
}
6878

6979
/// Sets the keep-alive interval.
70-
pub fn keep_alive_interval(mut self, interval: Duration) -> Self {
80+
pub fn with_keep_alive_interval(mut self, interval: Duration) -> Self {
7181
self.keep_alive_interval = interval;
7282
self
7383
}
7484

7585
/// Sets the congestion controller.
76-
pub fn congestion_controller(mut self, cc: C) -> Self {
86+
pub fn with_congestion_controller(mut self, cc: C) -> Self {
7787
self.cc = cc;
7888
self
7989
}
@@ -85,11 +95,11 @@ where
8595
// Stream receive window
8696
let stream_rwnd = self.max_stream_bandwidth / 1000 * self.expected_rtt;
8797

98+
let timeout = IdleTimeout::try_from(self.max_idle_timeout).expect("Valid idle timeout");
99+
88100
transport
89101
.keep_alive_interval(Some(self.keep_alive_interval))
90-
.max_idle_timeout(Some(
91-
IdleTimeout::try_from(self.max_idle_timeout).expect("Valid idle timeout"),
92-
))
102+
.max_idle_timeout(Some(timeout))
93103
// Disable datagram support
94104
.datagram_receive_buffer_size(None)
95105
.datagram_send_buffer_size(0)

msg-transport/src/tcp/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ impl Transport<SocketAddr> for Tcp {
8989
}
9090
}
9191

92-
#[async_trait::async_trait]
9392
impl TransportExt<SocketAddr> for Tcp {
9493
fn accept(&mut self) -> Acceptor<'_, Self, SocketAddr>
9594
where

msg-transport/src/tcp_tls/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,6 @@ impl Transport<SocketAddr> for TcpTls {
273273
}
274274
}
275275

276-
#[async_trait::async_trait]
277276
impl TransportExt<SocketAddr> for TcpTls {
278277
fn accept(&mut self) -> Acceptor<'_, Self, SocketAddr>
279278
where

0 commit comments

Comments
 (0)