@@ -6,6 +6,7 @@ mod protocol;
6
6
mod onion_addr;
7
7
mod errors;
8
8
mod from_url;
9
+ mod storage;
9
10
10
11
use serde:: {
11
12
Deserialize ,
@@ -18,12 +19,13 @@ use std::{
18
19
convert:: TryFrom ,
19
20
fmt,
20
21
io,
22
+ hash,
21
23
iter:: FromIterator ,
22
24
net:: { IpAddr , Ipv4Addr , Ipv6Addr } ,
23
25
result:: Result as StdResult ,
24
26
str:: FromStr ,
25
- sync:: Arc
26
27
} ;
28
+ use storage:: Storage ;
27
29
pub use self :: errors:: { Result , Error } ;
28
30
pub use self :: from_url:: { FromUrlErr , from_url, from_url_lossy} ;
29
31
pub use self :: protocol:: Protocol ;
@@ -36,28 +38,23 @@ static_assertions::const_assert! {
36
38
}
37
39
38
40
/// Representation of a Multiaddr.
39
- #[ derive( PartialEq , Eq , Clone , Hash ) ]
40
- pub struct Multiaddr { bytes : Arc < Vec < u8 > > }
41
+ #[ derive( Clone ) ]
42
+ pub struct Multiaddr { storage : Storage }
41
43
42
44
impl Multiaddr {
43
45
/// Create a new, empty multiaddress.
44
46
pub fn empty ( ) -> Self {
45
- Self { bytes : Arc :: new ( Vec :: new ( ) ) }
46
- }
47
-
48
- /// Create a new, empty multiaddress with the given capacity.
49
- pub fn with_capacity ( n : usize ) -> Self {
50
- Self { bytes : Arc :: new ( Vec :: with_capacity ( n) ) }
47
+ Self { storage : Storage :: from_slice ( & [ ] ) }
51
48
}
52
49
53
50
/// Return the length in bytes of this multiaddress.
54
51
pub fn len ( & self ) -> usize {
55
- self . bytes . len ( )
52
+ self . as_ref ( ) . len ( )
56
53
}
57
54
58
55
/// Return a copy of this [`Multiaddr`]'s byte representation.
59
56
pub fn to_vec ( & self ) -> Vec < u8 > {
60
- Vec :: from ( & self . bytes [ .. ] )
57
+ self . as_ref ( ) . to_vec ( )
61
58
}
62
59
63
60
/// Adds an already-parsed address component to the end of this multiaddr.
@@ -73,9 +70,10 @@ impl Multiaddr {
73
70
/// ```
74
71
///
75
72
pub fn push ( & mut self , p : Protocol < ' _ > ) {
76
- let mut w = io:: Cursor :: < & mut Vec < u8 > > :: new ( Arc :: make_mut ( & mut self . bytes ) ) ;
73
+ let mut w = io:: Cursor :: new ( self . to_vec ( ) ) ;
77
74
w. set_position ( w. get_ref ( ) . len ( ) as u64 ) ;
78
- p. write_bytes ( & mut w) . expect ( "Writing to a `io::Cursor<&mut Vec<u8>>` never fails." )
75
+ p. write_bytes ( & mut w) . expect ( "Writing to a `io::Cursor<&mut Vec<u8>>` never fails." ) ;
76
+ self . storage = Storage :: from_slice ( & w. into_inner ( ) ) ;
79
77
}
80
78
81
79
/// Pops the last `Protocol` of this multiaddr, or `None` if the multiaddr is empty.
@@ -89,7 +87,7 @@ impl Multiaddr {
89
87
/// ```
90
88
///
91
89
pub fn pop < ' a > ( & mut self ) -> Option < Protocol < ' a > > {
92
- let mut slice = & self . bytes [ .. ] ; // the remaining multiaddr slice
90
+ let mut slice = self . as_ref ( ) ; // the remaining multiaddr slice
93
91
if slice. is_empty ( ) {
94
92
return None
95
93
}
@@ -100,16 +98,17 @@ impl Multiaddr {
100
98
}
101
99
slice = s
102
100
} ;
103
- let remaining_len = self . bytes . len ( ) - slice. len ( ) ;
104
- Arc :: make_mut ( & mut self . bytes ) . truncate ( remaining_len) ;
101
+ let remaining_len = self . as_ref ( ) . len ( ) - slice. len ( ) ;
102
+ self . storage = Storage :: from_slice ( & self . as_ref ( ) [ .. remaining_len] ) ;
105
103
Some ( protocol)
106
104
}
107
105
108
106
/// Like [`Multiaddr::push`] but consumes `self`.
109
107
pub fn with ( mut self , p : Protocol < ' _ > ) -> Self {
110
- let mut w = io:: Cursor :: < & mut Vec < u8 > > :: new ( Arc :: make_mut ( & mut self . bytes ) ) ;
108
+ let mut w = io:: Cursor :: new ( self . to_vec ( ) ) ;
111
109
w. set_position ( w. get_ref ( ) . len ( ) as u64 ) ;
112
110
p. write_bytes ( & mut w) . expect ( "Writing to a `io::Cursor<&mut Vec<u8>>` never fails." ) ;
111
+ self . storage = Storage :: from_slice ( & w. into_inner ( ) ) ;
113
112
self
114
113
}
115
114
@@ -130,7 +129,7 @@ impl Multiaddr {
130
129
/// ```
131
130
///
132
131
pub fn iter ( & self ) -> Iter < ' _ > {
133
- Iter ( & self . bytes )
132
+ Iter ( & self . as_ref ( ) )
134
133
}
135
134
136
135
/// Replace a [`Protocol`] at some position in this `Multiaddr`.
@@ -145,7 +144,7 @@ impl Multiaddr {
145
144
where
146
145
F : FnOnce ( & Protocol ) -> Option < Protocol < ' a > >
147
146
{
148
- let mut address = Multiaddr :: with_capacity ( self . len ( ) ) ;
147
+ let mut address = Multiaddr :: empty ( ) ;
149
148
let mut fun = Some ( by) ;
150
149
let mut replaced = false ;
151
150
@@ -192,9 +191,23 @@ impl fmt::Display for Multiaddr {
192
191
}
193
192
}
194
193
194
+ impl PartialEq for Multiaddr {
195
+ fn eq ( & self , other : & Self ) -> bool {
196
+ self . as_ref ( ) == other. as_ref ( )
197
+ }
198
+ }
199
+
200
+ impl Eq for Multiaddr { }
201
+
202
+ impl hash:: Hash for Multiaddr {
203
+ fn hash < H : hash:: Hasher > ( & self , state : & mut H ) {
204
+ self . as_ref ( ) . hash ( state) ;
205
+ }
206
+ }
207
+
195
208
impl AsRef < [ u8 ] > for Multiaddr {
196
209
fn as_ref ( & self ) -> & [ u8 ] {
197
- self . bytes . as_ref ( )
210
+ self . storage . bytes ( )
198
211
}
199
212
}
200
213
@@ -203,7 +216,7 @@ impl<'a> IntoIterator for &'a Multiaddr {
203
216
type IntoIter = Iter < ' a > ;
204
217
205
218
fn into_iter ( self ) -> Iter < ' a > {
206
- Iter ( & self . bytes )
219
+ Iter ( & self . as_ref ( ) )
207
220
}
208
221
}
209
222
@@ -216,7 +229,7 @@ impl<'a> FromIterator<Protocol<'a>> for Multiaddr {
216
229
for cmp in iter {
217
230
cmp. write_bytes ( & mut writer) . expect ( "Writing to a `Vec` never fails." ) ;
218
231
}
219
- Multiaddr { bytes : Arc :: new ( writer) }
232
+ Multiaddr { storage : Storage :: from_slice ( & writer) }
220
233
}
221
234
}
222
235
@@ -237,7 +250,7 @@ impl FromStr for Multiaddr {
237
250
p. write_bytes ( & mut writer) . expect ( "Writing to a `Vec` never fails." ) ;
238
251
}
239
252
240
- Ok ( Multiaddr { bytes : Arc :: new ( writer) } )
253
+ Ok ( Multiaddr { storage : Storage :: from_slice ( & writer) } )
241
254
}
242
255
}
243
256
@@ -264,7 +277,7 @@ impl<'a> From<Protocol<'a>> for Multiaddr {
264
277
fn from ( p : Protocol < ' a > ) -> Multiaddr {
265
278
let mut w = Vec :: new ( ) ;
266
279
p. write_bytes ( & mut w) . expect ( "Writing to a `Vec` never fails." ) ;
267
- Multiaddr { bytes : Arc :: new ( w) }
280
+ Multiaddr { storage : Storage :: from_slice ( & w) }
268
281
}
269
282
}
270
283
@@ -299,7 +312,7 @@ impl TryFrom<Vec<u8>> for Multiaddr {
299
312
let ( _, s) = Protocol :: from_bytes ( slice) ?;
300
313
slice = s
301
314
}
302
- Ok ( Multiaddr { bytes : Arc :: new ( v) } )
315
+ Ok ( Multiaddr { storage : Storage :: from_slice ( & v) } )
303
316
}
304
317
}
305
318
0 commit comments