Skip to content

Commit 3578d7e

Browse files
thomastaylor312rvolosatovs
authored andcommitted
fix(core): Updates VecEncoder to support anything that can be bytes
This came from an issue when trying to pass a vec of tuples that had `Bytes` ended up as `&&Bytes` which there wasn't an impl for. When I looked at the implementation, it looked like pretty much everything was calling `AsRef` anyway, so I collapsed everything down to the single impl. Signed-off-by: Taylor Thomas <[email protected]>
1 parent c0e684b commit 3578d7e

File tree

3 files changed

+11
-72
lines changed

3 files changed

+11
-72
lines changed

Cargo.lock

+7-24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "wasm-tokio"
3-
version = "0.5.16"
3+
version = "0.6.0"
44
description = "Streaming WebAssembly codec based on Tokio"
55

66
authors.workspace = true

src/core.rs

+3-47
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,11 @@ where
367367
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
368368
pub struct CoreVecEncoderBytes;
369369

370-
impl Encoder<&[u8]> for CoreVecEncoderBytes {
370+
impl<T: AsRef<[u8]>> Encoder<T> for CoreVecEncoderBytes {
371371
type Error = std::io::Error;
372372

373-
fn encode(&mut self, item: &[u8], dst: &mut BytesMut) -> Result<(), Self::Error> {
373+
fn encode(&mut self, item: T, dst: &mut BytesMut) -> Result<(), Self::Error> {
374+
let item = item.as_ref();
374375
let n = item.len();
375376
let n = u32::try_from(n)
376377
.map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidInput, err))?;
@@ -381,51 +382,6 @@ impl Encoder<&[u8]> for CoreVecEncoderBytes {
381382
}
382383
}
383384

384-
impl Encoder<Vec<u8>> for CoreVecEncoderBytes {
385-
type Error = std::io::Error;
386-
387-
fn encode(&mut self, item: Vec<u8>, dst: &mut BytesMut) -> Result<(), Self::Error> {
388-
let item: &[u8] = item.as_ref();
389-
self.encode(item, dst)
390-
}
391-
}
392-
393-
impl Encoder<&Vec<u8>> for CoreVecEncoderBytes {
394-
type Error = std::io::Error;
395-
396-
fn encode(&mut self, item: &Vec<u8>, dst: &mut BytesMut) -> Result<(), Self::Error> {
397-
let item: &[u8] = item.as_ref();
398-
self.encode(item, dst)
399-
}
400-
}
401-
402-
impl Encoder<Bytes> for CoreVecEncoderBytes {
403-
type Error = std::io::Error;
404-
405-
fn encode(&mut self, item: Bytes, dst: &mut BytesMut) -> Result<(), Self::Error> {
406-
let item: &[u8] = item.as_ref();
407-
self.encode(item, dst)
408-
}
409-
}
410-
411-
impl Encoder<&Bytes> for CoreVecEncoderBytes {
412-
type Error = std::io::Error;
413-
414-
fn encode(&mut self, item: &Bytes, dst: &mut BytesMut) -> Result<(), Self::Error> {
415-
let item: &[u8] = item.as_ref();
416-
self.encode(item, dst)
417-
}
418-
}
419-
420-
impl Encoder<Arc<[u8]>> for CoreVecEncoderBytes {
421-
type Error = std::io::Error;
422-
423-
fn encode(&mut self, item: Arc<[u8]>, dst: &mut BytesMut) -> Result<(), Self::Error> {
424-
let item: &[u8] = item.as_ref();
425-
self.encode(item, dst)
426-
}
427-
}
428-
429385
/// [`core:vec`](https://webassembly.github.io/spec/core/binary/conventions.html#binary-vec)
430386
/// decoder optimized for vectors of byte-sized values
431387
#[derive(Debug, Default)]

0 commit comments

Comments
 (0)