Skip to content

Commit 555771c

Browse files
committed
feat: add missing trait implementations
Signed-off-by: Roman Volosatovs <[email protected]>
1 parent d71cd0f commit 555771c

File tree

8 files changed

+113
-91
lines changed

8 files changed

+113
-91
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "wasm-tokio"
3-
version = "0.5.7"
3+
version = "0.5.8"
44
description = "Streaming WebAssembly codec based on Tokio"
55

66
authors.workspace = true

leb128-tokio/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "leb128-tokio"
3-
version = "0.1.5"
3+
version = "0.1.6"
44
description = "Streaming LEB128 codec based on Tokio"
55

66
authors.workspace = true

leb128-tokio/src/lib.rs

Lines changed: 33 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,95 +1101,45 @@ impl Decoder for Leb128DecoderI128 {
11011101

11021102
pub struct Leb128Encoder;
11031103

1104-
impl Encoder<u8> for Leb128Encoder {
1105-
type Error = std::io::Error;
1106-
1107-
fn encode(&mut self, x: u8, dst: &mut BytesMut) -> Result<(), Self::Error> {
1108-
dst.extend_from_slice(put_u8_leb128(&mut Default::default(), x));
1109-
Ok(())
1110-
}
1111-
}
1112-
1113-
impl Encoder<u16> for Leb128Encoder {
1114-
type Error = std::io::Error;
1115-
1116-
fn encode(&mut self, x: u16, dst: &mut BytesMut) -> Result<(), Self::Error> {
1117-
dst.extend_from_slice(put_u16_leb128(&mut Default::default(), x));
1118-
Ok(())
1119-
}
1120-
}
1121-
1122-
impl Encoder<u32> for Leb128Encoder {
1123-
type Error = std::io::Error;
1124-
1125-
fn encode(&mut self, x: u32, dst: &mut BytesMut) -> Result<(), Self::Error> {
1126-
dst.extend_from_slice(put_u32_leb128(&mut Default::default(), x));
1127-
Ok(())
1128-
}
1129-
}
1130-
1131-
impl Encoder<u64> for Leb128Encoder {
1132-
type Error = std::io::Error;
1133-
1134-
fn encode(&mut self, x: u64, dst: &mut BytesMut) -> Result<(), Self::Error> {
1135-
dst.extend_from_slice(put_u64_leb128(&mut Default::default(), x));
1136-
Ok(())
1137-
}
1138-
}
1139-
1140-
impl Encoder<u128> for Leb128Encoder {
1141-
type Error = std::io::Error;
1142-
1143-
fn encode(&mut self, x: u128, dst: &mut BytesMut) -> Result<(), Self::Error> {
1144-
dst.extend_from_slice(put_u128_leb128(&mut Default::default(), x));
1145-
Ok(())
1146-
}
1147-
}
1148-
1149-
impl Encoder<i8> for Leb128Encoder {
1150-
type Error = std::io::Error;
1151-
1152-
fn encode(&mut self, x: i8, dst: &mut BytesMut) -> Result<(), Self::Error> {
1153-
dst.extend_from_slice(put_i8_leb128(&mut Default::default(), x));
1154-
Ok(())
1155-
}
1156-
}
1157-
1158-
impl Encoder<i16> for Leb128Encoder {
1159-
type Error = std::io::Error;
1160-
1161-
fn encode(&mut self, x: i16, dst: &mut BytesMut) -> Result<(), Self::Error> {
1162-
dst.extend_from_slice(put_i16_leb128(&mut Default::default(), x));
1163-
Ok(())
1164-
}
1165-
}
1104+
macro_rules! impl_encode {
1105+
($t:ty, $f:ident) => {
1106+
impl Encoder<$t> for Leb128Encoder {
1107+
type Error = std::io::Error;
1108+
1109+
fn encode(&mut self, item: $t, dst: &mut BytesMut) -> Result<(), Self::Error> {
1110+
dst.extend_from_slice($f(&mut Default::default(), item));
1111+
Ok(())
1112+
}
1113+
}
11661114

1167-
impl Encoder<i32> for Leb128Encoder {
1168-
type Error = std::io::Error;
1115+
impl Encoder<&$t> for Leb128Encoder {
1116+
type Error = std::io::Error;
11691117

1170-
fn encode(&mut self, x: i32, dst: &mut BytesMut) -> Result<(), Self::Error> {
1171-
dst.extend_from_slice(put_i32_leb128(&mut Default::default(), x));
1172-
Ok(())
1173-
}
1174-
}
1118+
fn encode(&mut self, item: &$t, dst: &mut BytesMut) -> Result<(), Self::Error> {
1119+
self.encode(*item, dst)
1120+
}
1121+
}
11751122

1176-
impl Encoder<i64> for Leb128Encoder {
1177-
type Error = std::io::Error;
1123+
impl Encoder<&&$t> for Leb128Encoder {
1124+
type Error = std::io::Error;
11781125

1179-
fn encode(&mut self, x: i64, dst: &mut BytesMut) -> Result<(), Self::Error> {
1180-
dst.extend_from_slice(put_i64_leb128(&mut Default::default(), x));
1181-
Ok(())
1182-
}
1126+
fn encode(&mut self, item: &&$t, dst: &mut BytesMut) -> Result<(), Self::Error> {
1127+
self.encode(*item, dst)
1128+
}
1129+
}
1130+
};
11831131
}
11841132

1185-
impl Encoder<i128> for Leb128Encoder {
1186-
type Error = std::io::Error;
1187-
1188-
fn encode(&mut self, x: i128, dst: &mut BytesMut) -> Result<(), Self::Error> {
1189-
dst.extend_from_slice(put_i128_leb128(&mut Default::default(), x));
1190-
Ok(())
1191-
}
1192-
}
1133+
impl_encode!(u8, put_u8_leb128);
1134+
impl_encode!(u16, put_u16_leb128);
1135+
impl_encode!(u32, put_u32_leb128);
1136+
impl_encode!(u64, put_u64_leb128);
1137+
impl_encode!(u128, put_u128_leb128);
1138+
impl_encode!(i8, put_i8_leb128);
1139+
impl_encode!(i16, put_i16_leb128);
1140+
impl_encode!(i32, put_i32_leb128);
1141+
impl_encode!(i64, put_i64_leb128);
1142+
impl_encode!(i128, put_i128_leb128);
11931143

11941144
#[cfg(test)]
11951145
mod tests {

src/cm/values.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,25 @@ macro_rules! impl_encode_copy_ref {
2727
type Error = std::io::Error;
2828

2929
#[cfg_attr(
30-
feature = "tracing",
31-
tracing::instrument(level = "trace", ret, fields(ty = stringify!($t)))
30+
feature = "tracing",
31+
tracing::instrument(level = "trace", ret, fields(ty = stringify!($t)))
3232
)]
3333
fn encode(&mut self, item: &$t, dst: &mut BytesMut) -> Result<(), Self::Error> {
3434
self.encode(*item, dst)
3535
}
3636
}
37+
38+
impl Encoder<&&$t> for $enc {
39+
type Error = std::io::Error;
40+
41+
#[cfg_attr(
42+
feature = "tracing",
43+
tracing::instrument(level = "trace", ret, fields(ty = stringify!($t)))
44+
)]
45+
fn encode(&mut self, item: &&$t, dst: &mut BytesMut) -> Result<(), Self::Error> {
46+
self.encode(**item, dst)
47+
}
48+
}
3749
};
3850
}
3951

@@ -183,6 +195,8 @@ impl Encoder<bool> for BoolCodec {
183195
}
184196
}
185197

198+
impl_encode_copy_ref!(BoolCodec, bool);
199+
186200
impl Decoder for BoolCodec {
187201
type Item = bool;
188202
type Error = std::io::Error;
@@ -215,6 +229,8 @@ impl Encoder<i8> for S8Codec {
215229
}
216230
}
217231

232+
impl_encode_copy_ref!(S8Codec, i8);
233+
218234
impl Decoder for S8Codec {
219235
type Item = i8;
220236
type Error = std::io::Error;

src/core.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use ::core::future::Future;
22
use ::core::mem;
33
use ::core::str;
44

5+
use std::sync::Arc;
6+
57
use leb128_tokio::{AsyncReadLeb128, Leb128DecoderU32, Leb128Encoder};
68
use tokio::io::{AsyncRead, AsyncReadExt as _, AsyncWrite, AsyncWriteExt as _};
79
use tokio_util::bytes::{BufMut as _, Bytes, BytesMut};
@@ -91,6 +93,16 @@ impl Encoder<&String> for CoreNameEncoder {
9193
}
9294
}
9395

96+
impl Encoder<Arc<str>> for CoreNameEncoder {
97+
type Error = std::io::Error;
98+
99+
fn encode(&mut self, item: Arc<str>, dst: &mut BytesMut) -> Result<(), Self::Error> {
100+
let item: &str = item.as_ref();
101+
self.encode(item, dst)
102+
}
103+
}
104+
105+
94106
/// [`core:name`](https://webassembly.github.io/spec/core/binary/values.html#names) decoder
95107
#[derive(Debug, Default)]
96108
pub struct CoreNameDecoder(CoreVecDecoderBytes);
@@ -226,6 +238,15 @@ impl Encoder<Vec<u8>> for CoreVecEncoderBytes {
226238
}
227239
}
228240

241+
impl Encoder<&Vec<u8>> for CoreVecEncoderBytes {
242+
type Error = std::io::Error;
243+
244+
fn encode(&mut self, item: &Vec<u8>, dst: &mut BytesMut) -> Result<(), Self::Error> {
245+
let item: &[u8] = item.as_ref();
246+
self.encode(item, dst)
247+
}
248+
}
249+
229250
impl Encoder<Bytes> for CoreVecEncoderBytes {
230251
type Error = std::io::Error;
231252

@@ -235,6 +256,24 @@ impl Encoder<Bytes> for CoreVecEncoderBytes {
235256
}
236257
}
237258

259+
impl Encoder<&Bytes> for CoreVecEncoderBytes {
260+
type Error = std::io::Error;
261+
262+
fn encode(&mut self, item: &Bytes, dst: &mut BytesMut) -> Result<(), Self::Error> {
263+
let item: &[u8] = item.as_ref();
264+
self.encode(item, dst)
265+
}
266+
}
267+
268+
impl Encoder<Arc<[u8]>> for CoreVecEncoderBytes {
269+
type Error = std::io::Error;
270+
271+
fn encode(&mut self, item: Arc<[u8]>, dst: &mut BytesMut) -> Result<(), Self::Error> {
272+
let item: &[u8] = item.as_ref();
273+
self.encode(item, dst)
274+
}
275+
}
276+
238277
/// [`core:vec`](https://webassembly.github.io/spec/core/binary/conventions.html#binary-vec)
239278
/// decoder optimized for vectors of byte-sized values
240279
#[derive(Debug, Default)]

utf8-tokio/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "utf8-tokio"
3-
version = "0.2.0"
3+
version = "0.2.1"
44
description = "Streaming UTF-8 codec based on Tokio"
55

66
authors.workspace = true

utf8-tokio/src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ pub trait AsyncWriteUtf8: AsyncWrite {
7575

7676
impl<T: AsyncWrite> AsyncWriteUtf8 for T {}
7777

78+
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
7879
pub struct Utf8Codec;
7980

8081
impl Decoder for Utf8Codec {
@@ -158,6 +159,22 @@ impl Encoder<char> for Utf8Codec {
158159
}
159160
}
160161

162+
impl Encoder<&char> for Utf8Codec {
163+
type Error = std::io::Error;
164+
165+
fn encode(&mut self, x: &char, dst: &mut BytesMut) -> Result<(), Self::Error> {
166+
self.encode(*x, dst)
167+
}
168+
}
169+
170+
impl Encoder<&&char> for Utf8Codec {
171+
type Error = std::io::Error;
172+
173+
fn encode(&mut self, x: &&char, dst: &mut BytesMut) -> Result<(), Self::Error> {
174+
self.encode(**x, dst)
175+
}
176+
}
177+
161178
#[cfg(test)]
162179
mod tests {
163180
use super::*;

0 commit comments

Comments
 (0)