Skip to content

Commit

Permalink
Remove unnecessary lifetimes of CRC digest (#129)
Browse files Browse the repository at this point in the history
The CRC digest lifetime was too strict for using it in a method and it can be elided.
  • Loading branch information
cdunster authored May 13, 2024
1 parent 41d14ef commit 66f3f67
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
12 changes: 6 additions & 6 deletions src/ser/flavors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,10 +638,10 @@ pub mod crc {
///
/// When successful, this function returns the slice containing the
/// serialized and encoded message.
pub fn [<to_slice_ $int>]<'a, 'b, T>(
value: &'b T,
pub fn [<to_slice_ $int>]<'a, T>(
value: &T,
buf: &'a mut [u8],
digest: Digest<'a, $int>,
digest: Digest<'_, $int>,
) -> Result<&'a mut [u8]>
where
T: Serialize + ?Sized,
Expand All @@ -653,9 +653,9 @@ pub mod crc {
/// data followed by a CRC. The CRC bytes are included in the output `Vec`.
#[cfg(feature = "heapless")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "heapless")))]
pub fn [<to_vec_ $int>]<'a, T, const B: usize>(
pub fn [<to_vec_ $int>]<T, const B: usize>(
value: &T,
digest: Digest<'a, $int>,
digest: Digest<'_, $int>,
) -> Result<heapless::Vec<u8, B>>
where
T: Serialize + ?Sized,
Expand All @@ -669,7 +669,7 @@ pub mod crc {
/// data followed by a CRC. The CRC bytes are included in the output `Vec`.
#[cfg(feature = "alloc")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
pub fn [<to_allocvec_ $int>]<'a, T>(value: &T, digest: Digest<'a, $int>) -> Result<alloc::vec::Vec<u8>>
pub fn [<to_allocvec_ $int>]<T>(value: &T, digest: Digest<'_, $int>) -> Result<alloc::vec::Vec<u8>>
where
T: Serialize + ?Sized,
{
Expand Down
16 changes: 8 additions & 8 deletions src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,10 @@ where
#[cfg(feature = "use-crc")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "use-crc")))]
#[inline]
pub fn to_slice_crc32<'a, 'b, T>(
value: &'b T,
pub fn to_slice_crc32<'a, T>(
value: &T,
buf: &'a mut [u8],
digest: crc::Digest<'a, u32>,
digest: crc::Digest<'_, u32>,
) -> Result<&'a mut [u8]>
where
T: Serialize + ?Sized,
Expand Down Expand Up @@ -375,9 +375,9 @@ where
#[cfg(all(feature = "use-crc", feature = "heapless"))]
#[cfg_attr(doc_cfg, doc(cfg(all(feature = "use-crc", feature = "heapless"))))]
#[inline]
pub fn to_vec_crc32<'a, T, const B: usize>(
pub fn to_vec_crc32<T, const B: usize>(
value: &T,
digest: crc::Digest<'a, u32>,
digest: crc::Digest<'_, u32>,
) -> Result<heapless::Vec<u8, B>>
where
T: Serialize + ?Sized,
Expand Down Expand Up @@ -409,7 +409,7 @@ where
#[cfg(all(feature = "use-crc", feature = "use-std"))]
#[cfg_attr(doc_cfg, doc(cfg(all(feature = "use-crc", feature = "use-std"))))]
#[inline]
pub fn to_stdvec_crc32<'a, T>(value: &T, digest: crc::Digest<'a, u32>) -> Result<std::vec::Vec<u8>>
pub fn to_stdvec_crc32<T>(value: &T, digest: crc::Digest<'_, u32>) -> Result<std::vec::Vec<u8>>
where
T: Serialize + ?Sized,
{
Expand Down Expand Up @@ -440,9 +440,9 @@ where
#[cfg(all(feature = "use-crc", feature = "alloc"))]
#[cfg_attr(doc_cfg, doc(cfg(all(feature = "use-crc", feature = "alloc"))))]
#[inline]
pub fn to_allocvec_crc32<'a, T>(
pub fn to_allocvec_crc32<T>(
value: &T,
digest: crc::Digest<'a, u32>,
digest: crc::Digest<'_, u32>,
) -> Result<alloc::vec::Vec<u8>>
where
T: Serialize + ?Sized,
Expand Down
25 changes: 25 additions & 0 deletions tests/crc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,28 @@ fn test_crc_error() {

assert_eq!(res, Err(postcard::Error::DeserializeBadCrc));
}

#[test]
#[cfg(feature = "use-crc")]
fn test_crc_in_method() {
use crc::{Crc, CRC_32_ISCSI};
use postcard::{to_slice_crc32, Result};
use serde::Serialize;

#[derive(Debug, Serialize)]
pub struct Thing {
value: u32,
}

impl Thing {
pub fn to_bytes<'a>(&self, buf: &'a mut [u8]) -> Result<&'a mut [u8]> {
let crc = Crc::<u32>::new(&CRC_32_ISCSI);
to_slice_crc32(self, buf, crc.digest())
}
}

let buffer = &mut [0u8; 5];
let thing = Thing { value: 42 };
let slice = thing.to_bytes(buffer).unwrap();
assert_eq!(slice, &[0x2A, 0xB7, 0xF5, 0x22, 0x19]);
}

0 comments on commit 66f3f67

Please sign in to comment.