Skip to content

Commit 0085612

Browse files
authored
core/: Remove TInEvent and TOutEvent (#2183)
TInEvent and TOutEvent are implied through THandler and thus superflucious. Both are removed in favor of a derivation through THandler.
1 parent 7391b6e commit 0085612

File tree

24 files changed

+356
-244
lines changed

24 files changed

+356
-244
lines changed

core/CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,18 @@
1212

1313
- Add `From<&PublicKey> for PeerId` (see [PR 2145]).
1414

15+
- Remove `TInEvent` and `TOutEvent` trait paramters on most public types.
16+
`TInEvent` and `TOutEvent` are implied through `THandler` and thus
17+
superflucious. Both are removed in favor of a derivation through `THandler`
18+
(see [PR 2183]).
19+
20+
- Require `ConnectionHandler::{InEvent,OutEvent,Error}` to implement `Debug`
21+
(see [PR 2183]).
22+
1523
[PR 2145]: https://github.com/libp2p/rust-libp2p/pull/2145
1624
[PR 2142]: https://github.com/libp2p/rust-libp2p/pull/2142
17-
[PR 2137]: https://github.com/libp2p/rust-libp2p/pull/2137/
25+
[PR 2137]: https://github.com/libp2p/rust-libp2p/pull/2137
26+
[PR 2183]: https://github.com/libp2p/rust-libp2p/pull/2183
1827

1928
# 0.29.0 [2021-07-12]
2029

core/src/connection.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// DEALINGS IN THE SOFTWARE.
2020

2121
mod error;
22-
mod handler;
22+
pub(crate) mod handler;
2323
mod listeners;
2424
mod substream;
2525

core/src/connection/handler.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// DEALINGS IN THE SOFTWARE.
2020

2121
use crate::Multiaddr;
22-
use std::{task::Context, task::Poll};
22+
use std::{fmt::Debug, task::Context, task::Poll};
2323
use super::{Connected, SubstreamEndpoint};
2424

2525
/// The interface of a connection handler.
@@ -30,14 +30,14 @@ pub trait ConnectionHandler {
3030
///
3131
/// See also [`EstablishedConnection::notify_handler`](super::EstablishedConnection::notify_handler)
3232
/// and [`ConnectionHandler::inject_event`].
33-
type InEvent;
33+
type InEvent: Debug + Send + 'static;
3434
/// The outbound type of events that the handler emits to the `Network`
3535
/// through [`ConnectionHandler::poll`].
3636
///
3737
/// See also [`NetworkEvent::ConnectionEvent`](crate::network::NetworkEvent::ConnectionEvent).
38-
type OutEvent;
38+
type OutEvent: Debug + Send + 'static;
3939
/// The type of errors that the handler can produce when polled by the `Network`.
40-
type Error;
40+
type Error: Debug + Send + 'static;
4141
/// The type of the substream containing the data.
4242
type Substream;
4343
/// Information about a substream. Can be sent to the handler through a `SubstreamEndpoint`,
@@ -91,6 +91,10 @@ where
9191
}
9292
}
9393

94+
pub(crate) type THandlerInEvent<THandler> = <<THandler as IntoConnectionHandler>::Handler as ConnectionHandler>::InEvent;
95+
pub(crate) type THandlerOutEvent<THandler> = <<THandler as IntoConnectionHandler>::Handler as ConnectionHandler>::OutEvent;
96+
pub(crate) type THandlerError<THandler> = <<THandler as IntoConnectionHandler>::Handler as ConnectionHandler>::Error;
97+
9498
/// Event produced by a handler.
9599
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
96100
pub enum ConnectionHandlerEvent<TOutboundOpenInfo, TCustom> {
@@ -127,4 +131,3 @@ impl<TOutboundOpenInfo, TCustom> ConnectionHandlerEvent<TOutboundOpenInfo, TCust
127131
}
128132
}
129133
}
130-

core/src/connection/manager.rs

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ use super::{
4444
ConnectionHandler,
4545
IntoConnectionHandler,
4646
PendingConnectionError,
47-
Substream
47+
Substream,
48+
handler::{
49+
THandlerInEvent,
50+
THandlerOutEvent,
51+
THandlerError,
52+
},
4853
};
4954
use task::{Task, TaskId};
5055

@@ -88,15 +93,15 @@ impl ConnectionId {
8893
}
8994

9095
/// A connection `Manager` orchestrates the I/O of a set of connections.
91-
pub struct Manager<I, O, H, E, HE> {
96+
pub struct Manager<H: IntoConnectionHandler, E> {
9297
/// The tasks of the managed connections.
9398
///
9499
/// Each managed connection is associated with a (background) task
95100
/// spawned onto an executor. Each `TaskInfo` in `tasks` is linked to such a
96101
/// background task via a channel. Closing that channel (i.e. dropping
97102
/// the sender in the associated `TaskInfo`) stops the background task,
98103
/// which will attempt to gracefully close the connection.
99-
tasks: FnvHashMap<TaskId, TaskInfo<I>>,
104+
tasks: FnvHashMap<TaskId, TaskInfo<THandlerInEvent<H>>>,
100105

101106
/// Next available identifier for a new connection / task.
102107
next_task_id: TaskId,
@@ -115,13 +120,13 @@ pub struct Manager<I, O, H, E, HE> {
115120

116121
/// Sender distributed to managed tasks for reporting events back
117122
/// to the manager.
118-
events_tx: mpsc::Sender<task::Event<O, H, E, HE>>,
123+
events_tx: mpsc::Sender<task::Event<H, E>>,
119124

120125
/// Receiver for events reported from managed tasks.
121-
events_rx: mpsc::Receiver<task::Event<O, H, E, HE>>
126+
events_rx: mpsc::Receiver<task::Event<H, E>>
122127
}
123128

124-
impl<I, O, H, E, HE> fmt::Debug for Manager<I, O, H, E, HE>
129+
impl<H: IntoConnectionHandler, E> fmt::Debug for Manager<H, E>
125130
{
126131
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
127132
f.debug_map()
@@ -179,7 +184,7 @@ enum TaskState {
179184

180185
/// Events produced by the [`Manager`].
181186
#[derive(Debug)]
182-
pub enum Event<'a, I, O, H, TE, HE> {
187+
pub enum Event<'a, H: IntoConnectionHandler, TE> {
183188
/// A connection attempt has failed.
184189
PendingConnectionError {
185190
/// The connection ID.
@@ -206,35 +211,35 @@ pub enum Event<'a, I, O, H, TE, HE> {
206211
connected: Connected,
207212
/// The error that occurred, if any. If `None`, the connection
208213
/// has been actively closed.
209-
error: Option<ConnectionError<HE>>,
214+
error: Option<ConnectionError<THandlerError<H>>>,
210215
},
211216

212217
/// A connection has been established.
213218
ConnectionEstablished {
214219
/// The entry associated with the new connection.
215-
entry: EstablishedEntry<'a, I>,
220+
entry: EstablishedEntry<'a, THandlerInEvent<H>>,
216221
},
217222

218223
/// A connection handler has produced an event.
219224
ConnectionEvent {
220225
/// The entry associated with the connection that produced the event.
221-
entry: EstablishedEntry<'a, I>,
226+
entry: EstablishedEntry<'a, THandlerInEvent<H>>,
222227
/// The produced event.
223-
event: O
228+
event: THandlerOutEvent<H>
224229
},
225230

226231
/// A connection to a node has changed its address.
227232
AddressChange {
228233
/// The entry associated with the connection that changed address.
229-
entry: EstablishedEntry<'a, I>,
234+
entry: EstablishedEntry<'a, THandlerInEvent<H>>,
230235
/// The former [`ConnectedPoint`].
231236
old_endpoint: ConnectedPoint,
232237
/// The new [`ConnectedPoint`].
233238
new_endpoint: ConnectedPoint,
234239
},
235240
}
236241

237-
impl<I, O, H, TE, HE> Manager<I, O, H, TE, HE> {
242+
impl<H: IntoConnectionHandler, TE> Manager<H, TE> {
238243
/// Creates a new connection manager.
239244
pub fn new(config: ManagerConfig) -> Self {
240245
let (tx, rx) = mpsc::channel(config.task_event_buffer_size);
@@ -255,19 +260,13 @@ impl<I, O, H, TE, HE> Manager<I, O, H, TE, HE> {
255260
/// processing the node's events.
256261
pub fn add_pending<F, M>(&mut self, future: F, handler: H) -> ConnectionId
257262
where
258-
I: Send + 'static,
259-
O: Send + 'static,
260263
TE: error::Error + Send + 'static,
261-
HE: error::Error + Send + 'static,
262264
M: StreamMuxer + Send + Sync + 'static,
263265
M::OutboundSubstream: Send + 'static,
264266
F: Future<Output = ConnectResult<M, TE>> + Send + 'static,
265267
H: IntoConnectionHandler + Send + 'static,
266268
H::Handler: ConnectionHandler<
267269
Substream = Substream<M>,
268-
InEvent = I,
269-
OutEvent = O,
270-
Error = HE
271270
> + Send + 'static,
272271
<H::Handler as ConnectionHandler>::OutboundOpenInfo: Send + 'static,
273272
{
@@ -293,15 +292,9 @@ impl<I, O, H, TE, HE> Manager<I, O, H, TE, HE> {
293292
H: IntoConnectionHandler + Send + 'static,
294293
H::Handler: ConnectionHandler<
295294
Substream = Substream<M>,
296-
InEvent = I,
297-
OutEvent = O,
298-
Error = HE
299295
> + Send + 'static,
300296
<H::Handler as ConnectionHandler>::OutboundOpenInfo: Send + 'static,
301297
TE: error::Error + Send + 'static,
302-
HE: error::Error + Send + 'static,
303-
I: Send + 'static,
304-
O: Send + 'static,
305298
M: StreamMuxer + Send + Sync + 'static,
306299
M::OutboundSubstream: Send + 'static,
307300
{
@@ -313,7 +306,7 @@ impl<I, O, H, TE, HE> Manager<I, O, H, TE, HE> {
313306
sender: tx, state: TaskState::Established(info)
314307
});
315308

316-
let task: Pin<Box<Task<Pin<Box<future::Pending<_>>>, _, _, _, _, _>>> =
309+
let task: Pin<Box<Task<Pin<Box<future::Pending<_>>>, _, _, _>>> =
317310
Box::pin(Task::established(task_id, self.events_tx.clone(), rx, conn));
318311

319312
if let Some(executor) = &mut self.executor {
@@ -326,7 +319,7 @@ impl<I, O, H, TE, HE> Manager<I, O, H, TE, HE> {
326319
}
327320

328321
/// Gets an entry for a managed connection, if it exists.
329-
pub fn entry(&mut self, id: ConnectionId) -> Option<Entry<'_, I>> {
322+
pub fn entry(&mut self, id: ConnectionId) -> Option<Entry<'_, THandlerInEvent<H>>> {
330323
if let hash_map::Entry::Occupied(task) = self.tasks.entry(id.0) {
331324
Some(Entry::new(task))
332325
} else {
@@ -340,7 +333,7 @@ impl<I, O, H, TE, HE> Manager<I, O, H, TE, HE> {
340333
}
341334

342335
/// Polls the manager for events relating to the managed connections.
343-
pub fn poll<'a>(&'a mut self, cx: &mut Context<'_>) -> Poll<Event<'a, I, O, H, TE, HE>> {
336+
pub fn poll<'a>(&'a mut self, cx: &mut Context<'_>) -> Poll<Event<'a, H, TE>> {
344337
// Advance the content of `local_spawns`.
345338
while let Poll::Ready(Some(_)) = self.local_spawns.poll_next_unpin(cx) {}
346339

core/src/connection/manager/task.rs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ use crate::{
3131
IntoConnectionHandler,
3232
PendingConnectionError,
3333
Substream,
34+
handler::{
35+
THandlerInEvent,
36+
THandlerOutEvent,
37+
THandlerError,
38+
},
3439
},
3540
};
3641
use futures::{prelude::*, channel::mpsc, stream};
@@ -53,23 +58,23 @@ pub enum Command<T> {
5358

5459
/// Events that a task can emit to its manager.
5560
#[derive(Debug)]
56-
pub enum Event<T, H, TE, HE> {
61+
pub enum Event<H: IntoConnectionHandler, TE> {
5762
/// A connection to a node has succeeded.
5863
Established { id: TaskId, info: Connected },
5964
/// A pending connection failed.
6065
Failed { id: TaskId, error: PendingConnectionError<TE>, handler: H },
6166
/// A node we are connected to has changed its address.
6267
AddressChange { id: TaskId, new_address: Multiaddr },
6368
/// Notify the manager of an event from the connection.
64-
Notify { id: TaskId, event: T },
69+
Notify { id: TaskId, event: THandlerOutEvent<H> },
6570
/// A connection closed, possibly due to an error.
6671
///
6772
/// If `error` is `None`, the connection has completed
6873
/// an active orderly close.
69-
Closed { id: TaskId, error: Option<ConnectionError<HE>> }
74+
Closed { id: TaskId, error: Option<ConnectionError<THandlerError<H>>> }
7075
}
7176

72-
impl<T, H, TE, HE> Event<T, H, TE, HE> {
77+
impl<H: IntoConnectionHandler, TE> Event<H, TE> {
7378
pub fn id(&self) -> &TaskId {
7479
match self {
7580
Event::Established { id, .. } => id,
@@ -82,7 +87,7 @@ impl<T, H, TE, HE> Event<T, H, TE, HE> {
8287
}
8388

8489
/// A `Task` is a [`Future`] that handles a single connection.
85-
pub struct Task<F, M, H, I, O, E>
90+
pub struct Task<F, M, H, E>
8691
where
8792
M: StreamMuxer,
8893
H: IntoConnectionHandler,
@@ -92,16 +97,16 @@ where
9297
id: TaskId,
9398

9499
/// Sender to emit events to the manager of this task.
95-
events: mpsc::Sender<Event<O, H, E, <H::Handler as ConnectionHandler>::Error>>,
100+
events: mpsc::Sender<Event<H, E>>,
96101

97102
/// Receiver for commands sent by the manager of this task.
98-
commands: stream::Fuse<mpsc::Receiver<Command<I>>>,
103+
commands: stream::Fuse<mpsc::Receiver<Command<THandlerInEvent<H>>>>,
99104

100105
/// Inner state of this `Task`.
101-
state: State<F, M, H, O, E>,
106+
state: State<F, M, H, E>,
102107
}
103108

104-
impl<F, M, H, I, O, E> Task<F, M, H, I, O, E>
109+
impl<F, M, H, E> Task<F, M, H, E>
105110
where
106111
M: StreamMuxer,
107112
H: IntoConnectionHandler,
@@ -110,8 +115,8 @@ where
110115
/// Create a new task to connect and handle some node.
111116
pub fn pending(
112117
id: TaskId,
113-
events: mpsc::Sender<Event<O, H, E, <H::Handler as ConnectionHandler>::Error>>,
114-
commands: mpsc::Receiver<Command<I>>,
118+
events: mpsc::Sender<Event<H, E>>,
119+
commands: mpsc::Receiver<Command<THandlerInEvent<H>>>,
115120
future: F,
116121
handler: H
117122
) -> Self {
@@ -129,8 +134,8 @@ where
129134
/// Create a task for an existing node we are already connected to.
130135
pub fn established(
131136
id: TaskId,
132-
events: mpsc::Sender<Event<O, H, E, <H::Handler as ConnectionHandler>::Error>>,
133-
commands: mpsc::Receiver<Command<I>>,
137+
events: mpsc::Sender<Event<H, E>>,
138+
commands: mpsc::Receiver<Command<THandlerInEvent<H>>>,
134139
connection: Connection<M, H::Handler>
135140
) -> Self {
136141
Task {
@@ -143,7 +148,7 @@ where
143148
}
144149

145150
/// The state associated with the `Task` of a connection.
146-
enum State<F, M, H, O, E>
151+
enum State<F, M, H, E>
147152
where
148153
M: StreamMuxer,
149154
H: IntoConnectionHandler,
@@ -165,33 +170,35 @@ where
165170
/// is polled for new events in this state, otherwise the event
166171
/// must be sent to the `Manager` before the connection can be
167172
/// polled again.
168-
event: Option<Event<O, H, E, <H::Handler as ConnectionHandler>::Error>>
173+
event: Option<Event<H, E>>,
169174
},
170175

171176
/// The connection is closing (active close).
172177
Closing(Close<M>),
173178

174179
/// The task is terminating with a final event for the `Manager`.
175-
Terminating(Event<O, H, E, <H::Handler as ConnectionHandler>::Error>),
180+
Terminating(Event<H, E>),
176181

177182
/// The task has finished.
178183
Done
179184
}
180185

181-
impl<F, M, H, I, O, E> Unpin for Task<F, M, H, I, O, E>
186+
impl<F, M, H, E> Unpin for Task<F, M, H, E>
182187
where
183188
M: StreamMuxer,
184189
H: IntoConnectionHandler,
185190
H::Handler: ConnectionHandler<Substream = Substream<M>>
186191
{
187192
}
188193

189-
impl<F, M, H, I, O, E> Future for Task<F, M, H, I, O, E>
194+
impl<F, M, H, E> Future for Task<F, M, H, E>
190195
where
191196
M: StreamMuxer,
192197
F: Future<Output = ConnectResult<M, E>>,
193198
H: IntoConnectionHandler,
194-
H::Handler: ConnectionHandler<Substream = Substream<M>, InEvent = I, OutEvent = O>
199+
H::Handler: ConnectionHandler<
200+
Substream = Substream<M>,
201+
> + Send + 'static,
195202
{
196203
type Output = ();
197204

0 commit comments

Comments
 (0)