From 5f847d2f6958a419fa560badc77599f4898edaa9 Mon Sep 17 00:00:00 2001 From: tcharding Date: Wed, 25 Mar 2020 21:06:04 +1100 Subject: [PATCH 1/3] Pass the error to inject_listener_closed method If there is an error when the listener closes, found in the `NetworkEvent::ListenerClosed` `reason` field, we would like to pass it on to the `inject_listener_closed()` method so that implementors of this method have access to it. Add an error parameter to `inject_listener_closed`. Convert the `reason` field from a `Result` to an `Option` and if there is an error pass `Some(error)` at the method call site. --- swarm/src/behaviour.rs | 3 +-- swarm/src/lib.rs | 6 +++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/swarm/src/behaviour.rs b/swarm/src/behaviour.rs index 97497b8aebf..b8a5439128e 100644 --- a/swarm/src/behaviour.rs +++ b/swarm/src/behaviour.rs @@ -130,7 +130,7 @@ pub trait NetworkBehaviour: Send + 'static { } /// A listener closed. - fn inject_listener_closed(&mut self, _id: ListenerId) { + fn inject_listener_closed(&mut self, _id: ListenerId, _err: Option) { } /// Polls for things that swarm should do. @@ -252,4 +252,3 @@ pub enum NotifyHandler { /// Notify all connection handlers. All } - diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs index 2af0ca5d215..4d561daeb38 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -450,7 +450,11 @@ where TBehaviour: NetworkBehaviour, for addr in addresses.iter() { this.behaviour.inject_expired_listen_addr(addr); } - this.behaviour.inject_listener_closed(listener_id); + let err = match reason { + Ok(()) => None, + Err(e) => Some(e), + }; + this.behaviour.inject_listener_closed(listener_id, err); } Poll::Ready(NetworkEvent::ListenerError { listener_id, error }) => this.behaviour.inject_listener_error(listener_id, &error), From fc5b4cd5b1fc7827fc608def6ac3a6152d96b5fc Mon Sep 17 00:00:00 2001 From: tcharding Date: Tue, 31 Mar 2020 08:11:05 +1100 Subject: [PATCH 2/3] Pass 'reason' as a Result --- swarm/src/behaviour.rs | 2 +- swarm/src/lib.rs | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/swarm/src/behaviour.rs b/swarm/src/behaviour.rs index b8a5439128e..cc9800fc55c 100644 --- a/swarm/src/behaviour.rs +++ b/swarm/src/behaviour.rs @@ -130,7 +130,7 @@ pub trait NetworkBehaviour: Send + 'static { } /// A listener closed. - fn inject_listener_closed(&mut self, _id: ListenerId, _err: Option) { + fn inject_listener_closed(&mut self, _id: ListenerId, _reason: Result<(), std::io::Error>) { } /// Polls for things that swarm should do. diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs index 4d561daeb38..dc05f0d0d4a 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -450,11 +450,7 @@ where TBehaviour: NetworkBehaviour, for addr in addresses.iter() { this.behaviour.inject_expired_listen_addr(addr); } - let err = match reason { - Ok(()) => None, - Err(e) => Some(e), - }; - this.behaviour.inject_listener_closed(listener_id, err); + this.behaviour.inject_listener_closed(listener_id, reason); } Poll::Ready(NetworkEvent::ListenerError { listener_id, error }) => this.behaviour.inject_listener_error(listener_id, &error), From ab59fcd5882002a4f4c08a6fc299ede1f558d390 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Wed, 1 Apr 2020 13:48:44 +0200 Subject: [PATCH 3/3] Finish change --- misc/core-derive/src/lib.rs | 6 +++--- swarm/src/behaviour.rs | 2 +- swarm/src/lib.rs | 5 ++++- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/misc/core-derive/src/lib.rs b/misc/core-derive/src/lib.rs index 205d4c45f0f..1153276f57a 100644 --- a/misc/core-derive/src/lib.rs +++ b/misc/core-derive/src/lib.rs @@ -271,8 +271,8 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream { return None } Some(match field.ident { - Some(ref i) => quote!(self.#i.inject_listener_closed(id);), - None => quote!(self.#field_n.inject_listener_closed(id);) + Some(ref i) => quote!(self.#i.inject_listener_closed(id, reason);), + None => quote!(self.#field_n.inject_listener_closed(id, reason);) }) }) }; @@ -469,7 +469,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream { #(#inject_listener_error_stmts);* } - fn inject_listener_closed(&mut self, id: #listener_id) { + fn inject_listener_closed(&mut self, id: #listener_id, reason: Result<(), &std::io::Error>) { #(#inject_listener_closed_stmts);* } diff --git a/swarm/src/behaviour.rs b/swarm/src/behaviour.rs index cf4bccdff7e..fdddc6a0ab9 100644 --- a/swarm/src/behaviour.rs +++ b/swarm/src/behaviour.rs @@ -146,7 +146,7 @@ pub trait NetworkBehaviour: Send + 'static { } /// A listener closed. - fn inject_listener_closed(&mut self, _id: ListenerId, _reason: Result<(), std::io::Error>) { + fn inject_listener_closed(&mut self, _id: ListenerId, _reason: Result<(), &std::io::Error>) { } /// Polls for things that swarm should do. diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs index 28c7761a17b..097bc991aba 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -593,7 +593,10 @@ where TBehaviour: NetworkBehaviour, for addr in addresses.iter() { this.behaviour.inject_expired_listen_addr(addr); } - this.behaviour.inject_listener_closed(listener_id, reason); + this.behaviour.inject_listener_closed(listener_id, match &reason { + Ok(()) => Ok(()), + Err(err) => Err(err), + }); return Poll::Ready(SwarmEvent::ListenerClosed { addresses, reason,