Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ arbitrary = { version = "1", optional = true }
borsh = { version = "1.2.0", optional = true, default-features = false }
# Implements the trait `Array` for `GenericArray` struct.
generic-array = { version = "1.1.1", optional = true, default-features = false }
# Provides derived `BitEncode` and `BitDecode` implementations
bin-proto = { version = "0.12.5", optional = true, default-features = false }
# Provides `Format` implementations
defmt = { version = "1.0", optional = true }

Expand Down Expand Up @@ -81,7 +83,7 @@ experimental_write_impl = []
real_blackbox = ["criterion/real_blackbox"]

[package.metadata.docs.rs]
features = ["alloc", "std", "grab_spare_slice", "latest_stable_rust", "serde", "borsh", "defmt"]
features = ["alloc", "std", "grab_spare_slice", "latest_stable_rust", "serde", "borsh", "defmt", "bin-proto"]
rustdoc-args = ["--cfg","docs_rs"]

[package.metadata.playground]
Expand Down
75 changes: 75 additions & 0 deletions src/arrayvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,81 @@ where
}
}

#[cfg(feature = "bin-proto")]
#[cfg_attr(docs_rs, doc(cfg(feature = "bin-proto")))]
impl<Ctx, A> bin_proto::BitEncode<Ctx, bin_proto::Untagged> for ArrayVec<A>
where
A: Array,
<A as Array>::Item: bin_proto::BitEncode<Ctx>,
{
fn encode<W, E>(
&self, write: &mut W, ctx: &mut Ctx, tag: bin_proto::Untagged,
) -> bin_proto::Result<()>
where
W: bin_proto::BitWrite,
E: bin_proto::Endianness,
{
<[<A as Array>::Item] as bin_proto::BitEncode<_, _>>::encode::<_, E>(
self.as_slice(),
write,
ctx,
tag,
)
}
}

#[cfg(feature = "bin-proto")]
#[cfg_attr(docs_rs, doc(cfg(feature = "bin-proto")))]
impl<Tag, Ctx, A> bin_proto::BitDecode<Ctx, bin_proto::Tag<Tag>> for ArrayVec<A>
where
A: Array,
<A as Array>::Item: bin_proto::BitDecode<Ctx>,
Tag: ::core::convert::TryInto<usize>,
{
fn decode<R, E>(
read: &mut R, ctx: &mut Ctx, tag: bin_proto::Tag<Tag>,
) -> bin_proto::Result<Self>
where
R: bin_proto::BitRead,
E: bin_proto::Endianness,
{
let item_count =
tag.0.try_into().map_err(|_| bin_proto::Error::TagConvert)?;
if item_count > A::CAPACITY {
return Err(bin_proto::Error::Other("insufficient capacity"));
}
let mut values = Self::default();
for _ in 0..item_count {
values.push(bin_proto::BitDecode::<_, _>::decode::<_, E>(read, ctx, ())?);
}
Ok(values)
}
}

#[cfg(feature = "bin-proto")]
#[cfg_attr(docs_rs, doc(cfg(feature = "bin-proto")))]
impl<Ctx, A> bin_proto::BitDecode<Ctx, bin_proto::Untagged> for ArrayVec<A>
where
A: Array,
<A as Array>::Item: bin_proto::BitDecode<Ctx>,
{
fn decode<R, E>(
read: &mut R, ctx: &mut Ctx, _tag: bin_proto::Untagged,
) -> bin_proto::Result<Self>
where
R: bin_proto::BitRead,
E: bin_proto::Endianness,
{
let mut values = Self::default();
for item in bin_proto::util::decode_items_to_eof::<_, E, _, _>(read, ctx) {
if values.try_push(item?).is_some() {
return Err(bin_proto::Error::Other("insufficient capacity"));
}
}
Ok(values)
}
}

impl<A: Array> ArrayVec<A> {
/// Move all values from `other` into this vec.
///
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
//! * `borsh` provides a `BorshSerialize` and `BorshDeserialize` implementation
//! for [`TinyVec`] and [`ArrayVec`] types, provided the inner item also has
//! an implementation.
//! * `bin-proto` provides a `BitEncode` and `BitDecode` implementation for
//! [`TinyVec`] and [`ArrayVec`] types, provided the inner item also has an
//! implementation.
//! * `defmt` provides a `Format` implementation for all types, provided the
//! inner item also has an implementation.
//!
Expand Down
66 changes: 66 additions & 0 deletions src/tinyvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,72 @@ where
}
}

#[cfg(feature = "bin-proto")]
#[cfg_attr(docs_rs, doc(cfg(feature = "bin-proto")))]
impl<Ctx, A> bin_proto::BitEncode<Ctx, bin_proto::Untagged> for TinyVec<A>
where
A: Array,
<A as Array>::Item: bin_proto::BitEncode<Ctx>,
{
fn encode<W, E>(
&self, write: &mut W, ctx: &mut Ctx, tag: bin_proto::Untagged,
) -> bin_proto::Result<()>
where
W: bin_proto::BitWrite,
E: bin_proto::Endianness,
{
<[<A as Array>::Item] as bin_proto::BitEncode<_, _>>::encode::<_, E>(
self.as_slice(),
write,
ctx,
tag,
)
}
}

#[cfg(feature = "bin-proto")]
#[cfg_attr(docs_rs, doc(cfg(feature = "bin-proto")))]
impl<Tag, Ctx, A> bin_proto::BitDecode<Ctx, bin_proto::Tag<Tag>> for TinyVec<A>
where
A: Array,
<A as Array>::Item: bin_proto::BitDecode<Ctx>,
Tag: ::core::convert::TryInto<usize>,
{
fn decode<R, E>(
read: &mut R, ctx: &mut Ctx, tag: bin_proto::Tag<Tag>,
) -> bin_proto::Result<Self>
where
R: bin_proto::BitRead,
E: bin_proto::Endianness,
{
let item_count =
tag.0.try_into().map_err(|_| bin_proto::Error::TagConvert)?;
let mut values = Self::with_capacity(item_count);
for _ in 0..item_count {
values.push(bin_proto::BitDecode::<_, _>::decode::<_, E>(read, ctx, ())?);
}
Ok(values)
}
}

#[cfg(feature = "bin-proto")]
#[cfg_attr(docs_rs, doc(cfg(feature = "bin-proto")))]
impl<Ctx, A> bin_proto::BitDecode<Ctx, bin_proto::Untagged> for TinyVec<A>
where
A: Array,
<A as Array>::Item: bin_proto::BitDecode<Ctx>,
{
fn decode<R, E>(
read: &mut R, ctx: &mut Ctx, _tag: bin_proto::Untagged,
) -> bin_proto::Result<Self>
where
R: bin_proto::BitRead,
E: bin_proto::Endianness,
{
bin_proto::util::decode_items_to_eof::<_, E, _, _>(read, ctx).collect()
}
}

impl<A: Array> TinyVec<A> {
/// Returns whether elements are on heap
#[inline(always)]
Expand Down
75 changes: 75 additions & 0 deletions tests/arrayvec.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![allow(bad_style)]
#![allow(clippy::clone_on_copy)]

#[cfg(feature = "bin-proto")]
use bin_proto::{BigEndian, BitDecodeExt, BitEncodeExt, Tag, Untagged};
#[cfg(feature = "serde")]
use serde_test::{assert_tokens, Token};
use std::iter::FromIterator;
Expand Down Expand Up @@ -469,6 +471,79 @@ fn ArrayVec_borsh_de() {
assert_eq!(tv, des);
}

#[cfg(feature = "bin-proto")]
#[test]
fn ArrayVec_bin_proto_encode_untagged() {
let mut values = ArrayVec::<[u8; 4]>::new();
values.push(0x12);
values.push(0x34);
values.push(0x56);
let mut data = [0u8; 16];
let n_bytes = values
.encode_bytes_ctx_buf(BigEndian, &mut (), Untagged, &mut data)
.unwrap() as usize;
assert_eq!(&[0x12, 0x34, 0x56], &data[0..n_bytes]);
}

#[cfg(feature = "bin-proto")]
#[test]
fn ArrayVec_bin_proto_decode_tagged_too_long() {
assert!(ArrayVec::<[u8; 4]>::decode_bytes_ctx(
&[0x12, 0x34, 0x56, 0x78, 0x9A],
BigEndian,
&mut (),
Tag(5usize),
)
.is_err());
}

#[cfg(feature = "bin-proto")]
#[test]
fn ArrayVec_bin_proto_decode_tagged() {
let (decoded, read_bits) = ArrayVec::<[u8; 4]>::decode_bytes_ctx(
&[0x12, 0x34, 0x56, 0x78],
BigEndian,
&mut (),
Tag(3usize),
)
.unwrap();
let mut expected = ArrayVec::<[u8; 4]>::new();
expected.push(0x12);
expected.push(0x34);
expected.push(0x56);
assert_eq!(24, read_bits);
assert_eq!(expected, decoded);
}

#[cfg(feature = "bin-proto")]
#[test]
fn ArrayVec_bin_proto_decode_untagged_too_long() {
let result = ArrayVec::<[u8; 4]>::decode_all_bytes_ctx(
&[0x12, 0x34, 0x56, 0x78, 0x9A],
BigEndian,
&mut (),
Untagged,
);
assert!(result.is_err());
}

#[cfg(feature = "bin-proto")]
#[test]
fn ArrayVec_bin_proto_decode_untagged() {
let decoded = ArrayVec::<[u8; 4]>::decode_all_bytes_ctx(
&[0x12, 0x34, 0x56],
BigEndian,
&mut (),
Untagged,
)
.unwrap();
let mut expected = ArrayVec::<[u8; 4]>::new();
expected.push(0x12);
expected.push(0x34);
expected.push(0x56);
assert_eq!(expected, decoded);
}

#[test]
fn ArrayVec_try_from_slice() {
use std::convert::TryFrom;
Expand Down
51 changes: 51 additions & 0 deletions tests/tinyvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#![allow(bad_style)]
#![allow(clippy::redundant_clone)]

#[cfg(feature = "bin-proto")]
use bin_proto::{BigEndian, BitDecodeExt, BitEncodeExt, Tag, Untagged};
#[cfg(feature = "serde")]
use serde_test::{assert_tokens, Token};
use std::iter::FromIterator;
Expand Down Expand Up @@ -476,6 +478,55 @@ fn TinyVec_borsh_de_heap() {
assert_eq!(tv, des);
}

#[cfg(feature = "bin-proto")]
#[test]
fn TinyVec_bin_proto_encode_untagged() {
let mut values = TinyVec::<[u8; 2]>::new();
values.push(0x12);
values.push(0x34);
values.push(0x56);
let mut data = [0u8; 16];
let n_bytes = values
.encode_bytes_ctx_buf(BigEndian, &mut (), Untagged, &mut data)
.unwrap() as usize;
assert_eq!(&[0x12, 0x34, 0x56], &data[0..n_bytes]);
}

#[cfg(feature = "bin-proto")]
#[test]
fn TinyVec_bin_proto_decode_tagged() {
let (decoded, read_bits) = TinyVec::<[u8; 2]>::decode_bytes_ctx(
&[0x12, 0x34, 0x56, 0x78],
BigEndian,
&mut (),
Tag(3usize),
)
.unwrap();
let mut expected = TinyVec::<[u8; 2]>::new();
expected.push(0x12);
expected.push(0x34);
expected.push(0x56);
assert_eq!(24, read_bits);
assert_eq!(expected, decoded);
}

#[cfg(feature = "bin-proto")]
#[test]
fn TinyVec_bin_proto_decode_untagged() {
let decoded = TinyVec::<[u8; 2]>::decode_all_bytes_ctx(
&[0x12, 0x34, 0x56],
BigEndian,
&mut (),
Untagged,
)
.unwrap();
let mut expected = TinyVec::<[u8; 2]>::new();
expected.push(0x12);
expected.push(0x34);
expected.push(0x56);
assert_eq!(expected, decoded);
}

#[test]
fn TinyVec_pretty_debug() {
let tv: TinyVec<[i32; 6]> = tiny_vec![1, 2, 3];
Expand Down
Loading