@@ -36,7 +36,14 @@ use std::num::NonZeroU8;
3636///
3737/// - [`DialOpts::unknown_peer_id`] dialing an unknown peer
3838#[ derive( Debug ) ]
39- pub struct DialOpts ( pub ( super ) Opts ) ;
39+ pub struct DialOpts {
40+ peer_id : Option < PeerId > ,
41+ condition : PeerCondition ,
42+ addresses : Vec < Multiaddr > ,
43+ extend_addresses_through_behaviour : bool ,
44+ role_override : Endpoint ,
45+ dial_concurrency_factor_override : Option < NonZeroU8 > ,
46+ }
4047
4148impl DialOpts {
4249 /// Dial a known peer.
@@ -73,13 +80,7 @@ impl DialOpts {
7380
7481 /// Get the [`PeerId`] specified in a [`DialOpts`] if any.
7582 pub fn get_peer_id ( & self ) -> Option < PeerId > {
76- match self {
77- DialOpts ( Opts :: WithPeerId ( WithPeerId { peer_id, .. } ) ) => Some ( * peer_id) ,
78- DialOpts ( Opts :: WithPeerIdWithAddresses ( WithPeerIdWithAddresses {
79- peer_id, ..
80- } ) ) => Some ( * peer_id) ,
81- DialOpts ( Opts :: WithoutPeerIdWithAddress ( _) ) => None ,
82- }
83+ self . peer_id
8384 }
8485
8586 /// Retrieves the [`PeerId`] from the [`DialOpts`] if specified or otherwise tries to parse it
@@ -92,92 +93,48 @@ impl DialOpts {
9293 ///
9394 /// See <https://github.com/multiformats/rust-multiaddr/issues/73>.
9495 pub ( crate ) fn get_or_parse_peer_id ( & self ) -> Result < Option < PeerId > , Multihash > {
95- match self {
96- DialOpts ( Opts :: WithPeerId ( WithPeerId { peer_id, .. } ) ) => Ok ( Some ( * peer_id) ) ,
97- DialOpts ( Opts :: WithPeerIdWithAddresses ( WithPeerIdWithAddresses {
98- peer_id, ..
99- } ) ) => Ok ( Some ( * peer_id) ) ,
100- DialOpts ( Opts :: WithoutPeerIdWithAddress ( WithoutPeerIdWithAddress {
101- address, ..
102- } ) ) => {
103- let peer_id = address
104- . iter ( )
105- . last ( )
106- . and_then ( |p| {
107- if let Protocol :: P2p ( ma) = p {
108- Some ( PeerId :: try_from ( ma) )
109- } else {
110- None
111- }
112- } )
113- . transpose ( ) ?;
114-
115- Ok ( peer_id)
116- }
96+ if let Some ( peer_id) = self . peer_id {
97+ return Ok ( Some ( peer_id) ) ;
11798 }
99+
100+ let first_address = match self . addresses . first ( ) {
101+ Some ( first_address) => first_address,
102+ None => return Ok ( None ) ,
103+ } ;
104+
105+ let maybe_peer_id = first_address
106+ . iter ( )
107+ . last ( )
108+ . and_then ( |p| {
109+ if let Protocol :: P2p ( ma) = p {
110+ Some ( PeerId :: try_from ( ma) )
111+ } else {
112+ None
113+ }
114+ } )
115+ . transpose ( ) ?;
116+
117+ Ok ( maybe_peer_id)
118118 }
119119
120120 pub ( crate ) fn get_addresses ( & self ) -> Vec < Multiaddr > {
121- match self {
122- DialOpts ( Opts :: WithPeerId ( WithPeerId { .. } ) ) => vec ! [ ] ,
123- DialOpts ( Opts :: WithPeerIdWithAddresses ( WithPeerIdWithAddresses {
124- addresses, ..
125- } ) ) => addresses. clone ( ) ,
126- DialOpts ( Opts :: WithoutPeerIdWithAddress ( WithoutPeerIdWithAddress {
127- address, ..
128- } ) ) => vec ! [ address. clone( ) ] ,
129- }
121+ self . addresses . clone ( )
130122 }
131123
132124 pub ( crate ) fn extend_addresses_through_behaviour ( & self ) -> bool {
133- match self {
134- DialOpts ( Opts :: WithPeerId ( WithPeerId { .. } ) ) => true ,
135- DialOpts ( Opts :: WithPeerIdWithAddresses ( WithPeerIdWithAddresses {
136- extend_addresses_through_behaviour,
137- ..
138- } ) ) => * extend_addresses_through_behaviour,
139- DialOpts ( Opts :: WithoutPeerIdWithAddress ( WithoutPeerIdWithAddress { .. } ) ) => true ,
140- }
125+ self . extend_addresses_through_behaviour
141126 }
142127
143128 pub ( crate ) fn peer_condition ( & self ) -> PeerCondition {
144- match self {
145- DialOpts (
146- Opts :: WithPeerId ( WithPeerId { condition, .. } )
147- | Opts :: WithPeerIdWithAddresses ( WithPeerIdWithAddresses { condition, .. } ) ,
148- ) => * condition,
149- DialOpts ( Opts :: WithoutPeerIdWithAddress ( WithoutPeerIdWithAddress { .. } ) ) => {
150- PeerCondition :: Always
151- }
152- }
129+ self . condition
153130 }
154131
155132 pub ( crate ) fn dial_concurrency_override ( & self ) -> Option < NonZeroU8 > {
156- match self {
157- DialOpts ( Opts :: WithPeerId ( WithPeerId {
158- dial_concurrency_factor_override,
159- ..
160- } ) ) => * dial_concurrency_factor_override,
161- DialOpts ( Opts :: WithPeerIdWithAddresses ( WithPeerIdWithAddresses {
162- dial_concurrency_factor_override,
163- ..
164- } ) ) => * dial_concurrency_factor_override,
165- DialOpts ( Opts :: WithoutPeerIdWithAddress ( WithoutPeerIdWithAddress { .. } ) ) => None ,
166- }
133+ self . dial_concurrency_factor_override
167134 }
168135
169136 pub ( crate ) fn role_override ( & self ) -> Endpoint {
170- match self {
171- DialOpts ( Opts :: WithPeerId ( WithPeerId { role_override, .. } ) ) => * role_override,
172- DialOpts ( Opts :: WithPeerIdWithAddresses ( WithPeerIdWithAddresses {
173- role_override,
174- ..
175- } ) ) => * role_override,
176- DialOpts ( Opts :: WithoutPeerIdWithAddress ( WithoutPeerIdWithAddress {
177- role_override,
178- ..
179- } ) ) => * role_override,
180- }
137+ self . role_override
181138 }
182139}
183140
@@ -193,25 +150,12 @@ impl From<PeerId> for DialOpts {
193150 }
194151}
195152
196- /// Internal options type.
197- ///
198- /// Not to be constructed manually. Use either of the below instead:
199- ///
200- /// - [`DialOpts::peer_id`] dialing a known peer
201- /// - [`DialOpts::unknown_peer_id`] dialing an unknown peer
202- #[ derive( Debug ) ]
203- pub ( super ) enum Opts {
204- WithPeerId ( WithPeerId ) ,
205- WithPeerIdWithAddresses ( WithPeerIdWithAddresses ) ,
206- WithoutPeerIdWithAddress ( WithoutPeerIdWithAddress ) ,
207- }
208-
209153#[ derive( Debug ) ]
210154pub struct WithPeerId {
211- pub ( crate ) peer_id : PeerId ,
212- pub ( crate ) condition : PeerCondition ,
213- pub ( crate ) role_override : Endpoint ,
214- pub ( crate ) dial_concurrency_factor_override : Option < NonZeroU8 > ,
155+ peer_id : PeerId ,
156+ condition : PeerCondition ,
157+ role_override : Endpoint ,
158+ dial_concurrency_factor_override : Option < NonZeroU8 > ,
215159}
216160
217161impl WithPeerId {
@@ -256,18 +200,25 @@ impl WithPeerId {
256200 /// Addresses to dial the peer are retrieved via
257201 /// [`NetworkBehaviour::addresses_of_peer`](crate::behaviour::NetworkBehaviour::addresses_of_peer).
258202 pub fn build ( self ) -> DialOpts {
259- DialOpts ( Opts :: WithPeerId ( self ) )
203+ DialOpts {
204+ peer_id : Some ( self . peer_id ) ,
205+ condition : self . condition ,
206+ addresses : vec ! [ ] ,
207+ extend_addresses_through_behaviour : true ,
208+ role_override : self . role_override ,
209+ dial_concurrency_factor_override : self . dial_concurrency_factor_override ,
210+ }
260211 }
261212}
262213
263214#[ derive( Debug ) ]
264215pub struct WithPeerIdWithAddresses {
265- pub ( crate ) peer_id : PeerId ,
266- pub ( crate ) condition : PeerCondition ,
267- pub ( crate ) addresses : Vec < Multiaddr > ,
268- pub ( crate ) extend_addresses_through_behaviour : bool ,
269- pub ( crate ) role_override : Endpoint ,
270- pub ( crate ) dial_concurrency_factor_override : Option < NonZeroU8 > ,
216+ peer_id : PeerId ,
217+ condition : PeerCondition ,
218+ addresses : Vec < Multiaddr > ,
219+ extend_addresses_through_behaviour : bool ,
220+ role_override : Endpoint ,
221+ dial_concurrency_factor_override : Option < NonZeroU8 > ,
271222}
272223
273224impl WithPeerIdWithAddresses {
@@ -304,7 +255,14 @@ impl WithPeerIdWithAddresses {
304255
305256 /// Build the final [`DialOpts`].
306257 pub fn build ( self ) -> DialOpts {
307- DialOpts ( Opts :: WithPeerIdWithAddresses ( self ) )
258+ DialOpts {
259+ peer_id : Some ( self . peer_id ) ,
260+ condition : self . condition ,
261+ addresses : self . addresses ,
262+ extend_addresses_through_behaviour : self . extend_addresses_through_behaviour ,
263+ role_override : self . role_override ,
264+ dial_concurrency_factor_override : self . dial_concurrency_factor_override ,
265+ }
308266 }
309267}
310268
@@ -323,8 +281,8 @@ impl WithoutPeerId {
323281
324282#[ derive( Debug ) ]
325283pub struct WithoutPeerIdWithAddress {
326- pub ( crate ) address : Multiaddr ,
327- pub ( crate ) role_override : Endpoint ,
284+ address : Multiaddr ,
285+ role_override : Endpoint ,
328286}
329287
330288impl WithoutPeerIdWithAddress {
@@ -340,7 +298,14 @@ impl WithoutPeerIdWithAddress {
340298 }
341299 /// Build the final [`DialOpts`].
342300 pub fn build ( self ) -> DialOpts {
343- DialOpts ( Opts :: WithoutPeerIdWithAddress ( self ) )
301+ DialOpts {
302+ peer_id : None ,
303+ condition : PeerCondition :: Always ,
304+ addresses : vec ! [ self . address] ,
305+ extend_addresses_through_behaviour : false ,
306+ role_override : self . role_override ,
307+ dial_concurrency_factor_override : None ,
308+ }
344309 }
345310}
346311
0 commit comments