Skip to content

Commit

Permalink
Fix use of paste macro
Browse files Browse the repository at this point in the history
This uses the correct digest sizes, and adds a test for
a random CRC8 variant.
  • Loading branch information
jamesmunns committed Apr 21, 2023
1 parent 2c9ed55 commit e437a16
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/de/flavors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ pub mod crc {

/// Deserialize a message of type `T` from a byte slice with a Crc. The unused portion (if any)
/// of the byte slice is not returned.
pub fn [<from_bytes_ $int>]<'a, T>(s: &'a [u8], digest: Digest<'a, u32>) -> Result<T>
pub fn [<from_bytes_ $int>]<'a, T>(s: &'a [u8], digest: Digest<'a, $int>) -> Result<T>
where
T: Deserialize<'a>,
{
Expand All @@ -283,7 +283,7 @@ pub mod crc {

/// Deserialize a message of type `T` from a byte slice with a Crc. The unused portion (if any)
/// of the byte slice is returned for further usage
pub fn [<take_from_bytes_ $int>]<'a, T>(s: &'a [u8], digest: Digest<'a, u32>) -> Result<(T, &'a [u8])>
pub fn [<take_from_bytes_ $int>]<'a, T>(s: &'a [u8], digest: Digest<'a, $int>) -> Result<(T, &'a [u8])>
where
T: Deserialize<'a>,
{
Expand Down
6 changes: 3 additions & 3 deletions src/ser/flavors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ pub mod crc {
pub fn [<to_slice_ $int>]<'a, 'b, T>(
value: &'b T,
buf: &'a mut [u8],
digest: Digest<'a, u32>,
digest: Digest<'a, $int>,
) -> Result<&'a mut [u8]>
where
T: Serialize + ?Sized,
Expand All @@ -509,7 +509,7 @@ pub mod crc {
#[cfg(feature = "heapless")]
pub fn [<to_vec_ $int>]<'a, T, const B: usize>(
value: &T,
digest: Digest<'a, u32>,
digest: Digest<'a, $int>,
) -> Result<heapless::Vec<u8, B>>
where
T: Serialize + ?Sized,
Expand All @@ -522,7 +522,7 @@ pub mod crc {
/// Serialize a `T` to a `heapless::Vec<u8>`, with the `Vec` containing
/// data followed by a CRC. The CRC bytes are included in the output `Vec`.
#[cfg(feature = "alloc")]
pub fn [<to_allocvec_ $int>]<'a, T>(value: &T, digest: Digest<'a, u32>) -> Result<alloc::vec::Vec<u8>>
pub fn [<to_allocvec_ $int>]<'a, T>(value: &T, digest: Digest<'a, $int>) -> Result<alloc::vec::Vec<u8>>
where
T: Serialize + ?Sized,
{
Expand Down
20 changes: 20 additions & 0 deletions tests/crc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,23 @@ fn test_crc() {
let remaining_bytes = [];
assert_eq!(res, (expected_bytes, remaining_bytes.as_slice()));
}

#[test]
#[cfg(feature = "use-crc")]
fn test_crc_8() {
use crc::{Crc, CRC_8_SMBUS};

let data: &[u8] = &[0x01, 0x00, 0x20, 0x30];
let buffer = &mut [0u8; 32];
let crc = Crc::<u8>::new(&CRC_8_SMBUS);
let digest = crc.digest();
let res = postcard::ser_flavors::crc::to_slice_u8(data, buffer, digest).unwrap();
assert_eq!(res, &[0x04, 0x01, 0x00, 0x20, 0x30, 167]);

let digest = crc.digest();
let res = postcard::de_flavors::crc::take_from_bytes_u8::<[u8; 5]>(&res, digest).unwrap();

let expected_bytes = [0x04, 0x01, 0x00, 0x20, 0x30];
let remaining_bytes = [];
assert_eq!(res, (expected_bytes, remaining_bytes.as_slice()));
}

0 comments on commit e437a16

Please sign in to comment.