@@ -36,7 +36,14 @@ use std::num::NonZeroU8;
36
36
///
37
37
/// - [`DialOpts::unknown_peer_id`] dialing an unknown peer
38
38
#[ 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
+ }
40
47
41
48
impl DialOpts {
42
49
/// Dial a known peer.
@@ -73,13 +80,7 @@ impl DialOpts {
73
80
74
81
/// Get the [`PeerId`] specified in a [`DialOpts`] if any.
75
82
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
83
84
}
84
85
85
86
/// Retrieves the [`PeerId`] from the [`DialOpts`] if specified or otherwise tries to parse it
@@ -92,92 +93,48 @@ impl DialOpts {
92
93
///
93
94
/// See <https://github.com/multiformats/rust-multiaddr/issues/73>.
94
95
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) ) ;
117
98
}
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)
118
118
}
119
119
120
120
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 ( )
130
122
}
131
123
132
124
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
141
126
}
142
127
143
128
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
153
130
}
154
131
155
132
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
167
134
}
168
135
169
136
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
181
138
}
182
139
}
183
140
@@ -193,25 +150,12 @@ impl From<PeerId> for DialOpts {
193
150
}
194
151
}
195
152
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
-
209
153
#[ derive( Debug ) ]
210
154
pub 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 > ,
215
159
}
216
160
217
161
impl WithPeerId {
@@ -256,18 +200,25 @@ impl WithPeerId {
256
200
/// Addresses to dial the peer are retrieved via
257
201
/// [`NetworkBehaviour::addresses_of_peer`](crate::behaviour::NetworkBehaviour::addresses_of_peer).
258
202
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
+ }
260
211
}
261
212
}
262
213
263
214
#[ derive( Debug ) ]
264
215
pub 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 > ,
271
222
}
272
223
273
224
impl WithPeerIdWithAddresses {
@@ -304,7 +255,14 @@ impl WithPeerIdWithAddresses {
304
255
305
256
/// Build the final [`DialOpts`].
306
257
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
+ }
308
266
}
309
267
}
310
268
@@ -323,8 +281,8 @@ impl WithoutPeerId {
323
281
324
282
#[ derive( Debug ) ]
325
283
pub struct WithoutPeerIdWithAddress {
326
- pub ( crate ) address : Multiaddr ,
327
- pub ( crate ) role_override : Endpoint ,
284
+ address : Multiaddr ,
285
+ role_override : Endpoint ,
328
286
}
329
287
330
288
impl WithoutPeerIdWithAddress {
@@ -340,7 +298,14 @@ impl WithoutPeerIdWithAddress {
340
298
}
341
299
/// Build the final [`DialOpts`].
342
300
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
+ }
344
309
}
345
310
}
346
311
0 commit comments