@@ -70,10 +70,9 @@ impl Multiaddr {
70
70
/// ```
71
71
///
72
72
pub fn push ( & mut self , p : Protocol < ' _ > ) {
73
- let mut w = io:: Cursor :: new ( self . to_vec ( ) ) ;
74
- w. set_position ( w. get_ref ( ) . len ( ) as u64 ) ;
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 ( ) ) ;
73
+ let mut w = Buffer :: from_ref ( self . as_ref ( ) ) ;
74
+ p. write_bytes ( & mut w) . expect ( "Writing to a `Buffer` never fails." ) ;
75
+ self . storage = w. to_storage ( ) ;
77
76
}
78
77
79
78
/// Pops the last `Protocol` of this multiaddr, or `None` if the multiaddr is empty.
@@ -105,10 +104,9 @@ impl Multiaddr {
105
104
106
105
/// Like [`Multiaddr::push`] but consumes `self`.
107
106
pub fn with ( mut self , p : Protocol < ' _ > ) -> Self {
108
- let mut w = io:: Cursor :: new ( self . to_vec ( ) ) ;
109
- w. set_position ( w. get_ref ( ) . len ( ) as u64 ) ;
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 ( ) ) ;
107
+ let mut w = Buffer :: from_ref ( self . as_ref ( ) ) ;
108
+ p. write_bytes ( & mut w) . expect ( "Writing to a `Buffer` never fails." ) ;
109
+ self . storage = w. to_storage ( ) ;
112
110
self
113
111
}
114
112
@@ -425,3 +423,26 @@ macro_rules! multiaddr {
425
423
}
426
424
}
427
425
}
426
+
427
+ struct Buffer ( smallvec:: SmallVec < [ u8 ; 32 ] > ) ;
428
+
429
+ impl Buffer {
430
+ fn from_ref ( data : & [ u8 ] ) -> Self {
431
+ Self ( data. into ( ) )
432
+ }
433
+
434
+ fn to_storage ( self ) -> Storage {
435
+ Storage :: from_slice ( & self . 0 )
436
+ }
437
+ }
438
+
439
+ impl io:: Write for Buffer {
440
+ fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
441
+ self . 0 . extend_from_slice ( buf) ;
442
+ Ok ( buf. len ( ) )
443
+ }
444
+
445
+ fn flush ( & mut self ) -> io:: Result < ( ) > {
446
+ Ok ( ( ) )
447
+ }
448
+ }
0 commit comments