Skip to content

Commit 48def4c

Browse files
committed
Use write feature of smallvec instead of Buffer wrapper
1 parent 85ebcea commit 48def4c

File tree

2 files changed

+4
-30
lines changed

2 files changed

+4
-30
lines changed

misc/multiaddr/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ serde = "1.0.70"
1919
static_assertions = "1.1"
2020
unsigned-varint = "0.3"
2121
url = { version = "2.1.0", default-features = false }
22-
smallvec = "1.0"
22+
smallvec = { version = "1.0", features = ["write"] }
2323

2424
[dev-dependencies]
2525
bincode = "1"

misc/multiaddr/src/lib.rs

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use serde::{
1818
use std::{
1919
convert::TryFrom,
2020
fmt,
21-
io,
2221
hash,
2322
iter::FromIterator,
2423
net::{IpAddr, Ipv4Addr, Ipv6Addr},
@@ -70,9 +69,9 @@ impl Multiaddr {
7069
/// ```
7170
///
7271
pub fn push(&mut self, p: Protocol<'_>) {
73-
let mut w = Buffer::from_ref(self.as_ref());
72+
let mut w: smallvec::SmallVec<[u8; 32]> = self.as_ref().into();
7473
p.write_bytes(&mut w).expect("Writing to a `Buffer` never fails.");
75-
self.storage = w.to_storage();
74+
self.storage = Storage::from_slice(&w);
7675
}
7776

7877
/// Pops the last `Protocol` of this multiaddr, or `None` if the multiaddr is empty.
@@ -104,9 +103,7 @@ impl Multiaddr {
104103

105104
/// Like [`Multiaddr::push`] but consumes `self`.
106105
pub fn with(mut self, p: Protocol<'_>) -> Self {
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();
106+
self.push(p);
110107
self
111108
}
112109

@@ -423,26 +420,3 @@ macro_rules! multiaddr {
423420
}
424421
}
425422
}
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

Comments
 (0)