Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary lifetimes of CRC digest #129

Merged
merged 1 commit into from
May 13, 2024
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
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]);
}