Skip to content

Commit b3cc43e

Browse files
feat: add encoded_len and bytes written (#252)
BREAKING CHANGE: `Multihash::write()` returns bytes written Prior to this change it returned an empty tuple `()`, now it returns the bytes written.
1 parent dcd8e39 commit b3cc43e

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

src/multihash.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,17 +159,30 @@ impl<const S: usize> Multihash<S> {
159159
Ok(result)
160160
}
161161

162-
/// Writes a multihash to a byte stream.
163-
pub fn write<W: io::Write>(&self, w: W) -> Result<(), Error> {
162+
/// Writes a multihash to a byte stream, returning the written size.
163+
pub fn write<W: io::Write>(&self, w: W) -> Result<usize, Error> {
164164
write_multihash(w, self.code(), self.size(), self.digest())
165165
}
166166

167+
/// Returns the length in bytes needed to encode this multihash into bytes.
168+
pub fn encoded_len(&self) -> usize {
169+
let mut code_buf = varint_encode::u64_buffer();
170+
let code = varint_encode::u64(self.code, &mut code_buf);
171+
172+
let mut size_buf = varint_encode::u8_buffer();
173+
let size = varint_encode::u8(self.size, &mut size_buf);
174+
175+
code.len() + size.len() + usize::from(self.size)
176+
}
177+
167178
#[cfg(feature = "alloc")]
168179
/// Returns the bytes of a multihash.
169180
pub fn to_bytes(&self) -> Vec<u8> {
170181
let mut bytes = Vec::with_capacity(self.size().into());
171-
self.write(&mut bytes)
182+
let written = self
183+
.write(&mut bytes)
172184
.expect("writing to a vec should never fail");
185+
debug_assert_eq!(written, bytes.len());
173186
bytes
174187
}
175188

@@ -293,7 +306,7 @@ impl<const S: usize> parity_scale_codec::Decode for Multihash<S> {
293306
}
294307

295308
/// Writes the multihash to a byte stream.
296-
pub fn write_multihash<W>(mut w: W, code: u64, size: u8, digest: &[u8]) -> Result<(), Error>
309+
pub fn write_multihash<W>(mut w: W, code: u64, size: u8, digest: &[u8]) -> Result<usize, Error>
297310
where
298311
W: io::Write,
299312
{
@@ -303,10 +316,13 @@ where
303316
let mut size_buf = varint_encode::u8_buffer();
304317
let size = varint_encode::u8(size, &mut size_buf);
305318

319+
let written = code.len() + size.len() + digest.len();
320+
306321
w.write_all(code)?;
307322
w.write_all(size)?;
308323
w.write_all(digest)?;
309-
Ok(())
324+
325+
Ok(written)
310326
}
311327

312328
/// Reads a multihash from a byte stream that contains a full multihash (code, size and the digest)
@@ -361,9 +377,10 @@ mod tests {
361377
fn roundtrip() {
362378
let hash = Code::Sha2_256.digest(b"hello world");
363379
let mut buf = [0u8; 35];
364-
hash.write(&mut buf[..]).unwrap();
380+
let written = hash.write(&mut buf[..]).unwrap();
365381
let hash2 = Multihash::<32>::read(&buf[..]).unwrap();
366382
assert_eq!(hash, hash2);
383+
assert_eq!(hash.encoded_len(), written);
367384
}
368385

369386
#[test]

0 commit comments

Comments
 (0)