diff --git a/source/postcard/Cargo.toml b/source/postcard/Cargo.toml index 1646429..fb6391a 100644 --- a/source/postcard/Cargo.toml +++ b/source/postcard/Cargo.toml @@ -2,7 +2,7 @@ name = "postcard" version = "1.1.0" authors = ["James Munns "] -edition = "2018" +edition = "2021" readme = "README.md" repository = "https://github.com/jamesmunns/postcard" description = "A no_std + serde compatible message library for Rust" @@ -87,7 +87,7 @@ embedded-io-04 = ["dep:embedded-io-04"] embedded-io-06 = ["dep:embedded-io-06"] use-std = ["serde/std", "alloc"] -heapless-cas = ["heapless", "heapless/cas"] +heapless-cas = ["heapless", "dep:heapless", "heapless/cas"] alloc = ["serde/alloc", "embedded-io-04?/alloc", "embedded-io-06?/alloc"] use-defmt = ["defmt"] use-crc = ["crc", "paste"] @@ -99,3 +99,8 @@ chrono-v0_4 = ["chrono"] # # NOT subject to SemVer guarantees! experimental-derive = ["postcard-derive"] +crc = ["dep:crc"] +defmt = ["dep:defmt"] +heapless = ["dep:heapless"] +paste = ["dep:paste"] +postcard-derive = ["dep:postcard-derive"] diff --git a/source/postcard/src/de/deserializer.rs b/source/postcard/src/de/deserializer.rs index ff00959..e09c64a 100644 --- a/source/postcard/src/de/deserializer.rs +++ b/source/postcard/src/de/deserializer.rs @@ -139,7 +139,7 @@ impl<'de, F: Flavor<'de>> Deserializer<'de, F> { } } -struct SeqAccess<'a, 'b: 'a, F: Flavor<'b>> { +struct SeqAccess<'a, 'b, F: Flavor<'b>> { deserializer: &'a mut Deserializer<'b, F>, len: usize, } @@ -169,7 +169,7 @@ impl<'a, 'b: 'a, F: Flavor<'b>> serde::de::SeqAccess<'b> for SeqAccess<'a, 'b, F } } -struct MapAccess<'a, 'b: 'a, F: Flavor<'b>> { +struct MapAccess<'a, 'b, F: Flavor<'b>> { deserializer: &'a mut Deserializer<'b, F>, len: usize, } @@ -201,7 +201,7 @@ impl<'a, 'b: 'a, F: Flavor<'b>> serde::de::MapAccess<'b> for MapAccess<'a, 'b, F } } -impl<'de, 'a, F: Flavor<'de>> de::Deserializer<'de> for &'a mut Deserializer<'de, F> { +impl<'de, F: Flavor<'de>> de::Deserializer<'de> for &mut Deserializer<'de, F> { type Error = Error; #[inline] @@ -539,7 +539,7 @@ impl<'de, 'a, F: Flavor<'de>> de::Deserializer<'de> for &'a mut Deserializer<'de } } -impl<'de, 'a, F: Flavor<'de>> serde::de::VariantAccess<'de> for &'a mut Deserializer<'de, F> { +impl<'de, F: Flavor<'de>> serde::de::VariantAccess<'de> for &mut Deserializer<'de, F> { type Error = Error; #[inline] @@ -567,7 +567,7 @@ impl<'de, 'a, F: Flavor<'de>> serde::de::VariantAccess<'de> for &'a mut Deserial } } -impl<'de, 'a, F: Flavor<'de>> serde::de::EnumAccess<'de> for &'a mut Deserializer<'de, F> { +impl<'de, F: Flavor<'de>> serde::de::EnumAccess<'de> for &mut Deserializer<'de, F> { type Error = Error; type Variant = Self; diff --git a/source/postcard/src/de/flavors.rs b/source/postcard/src/de/flavors.rs index 844fd00..8bf363d 100644 --- a/source/postcard/src/de/flavors.rs +++ b/source/postcard/src/de/flavors.rs @@ -189,7 +189,7 @@ impl<'de> Flavor<'de> for Slice<'de> { } } -/// Support for [std::io] or `embedded-io` traits +/// Support for [`std::io`] or `embedded-io` traits #[cfg(any( feature = "embedded-io-04", feature = "embedded-io-06", @@ -248,7 +248,7 @@ pub mod io { use super::SlidingBuffer; use crate::{Error, Result}; - /// Wrapper over a [`embedded_io`](crate::eio::embedded_io)::[`Read`](crate::eio::Read) and a sliding buffer to implement the [Flavor] trait + /// Wrapper over a [`embedded_io`](crate::eio::embedded_io)::[`Read`](crate::eio::Read) and a sliding buffer to implement the [`Flavor`] trait pub struct EIOReader<'de, T> where T: crate::eio::Read, @@ -307,7 +307,7 @@ pub mod io { } } - /// Support for [std::io] traits + /// Support for [`std::io`] traits #[allow(clippy::module_inception)] #[cfg(feature = "use-std")] pub mod io { @@ -315,7 +315,7 @@ pub mod io { use super::SlidingBuffer; use crate::{Error, Result}; - /// Wrapper over a [std::io::Read] and a sliding buffer to implement the [Flavor] trait + /// Wrapper over a [`std::io::Read`] and a sliding buffer to implement the [Flavor] trait pub struct IOReader<'de, T> where T: std::io::Read, @@ -380,11 +380,11 @@ pub mod io { //////////////////////////////////////// /// This Cyclic Redundancy Check flavor applies [the CRC crate's `Algorithm`](https://docs.rs/crc/latest/crc/struct.Algorithm.html) struct on -/// the serialized data. The flavor will check the CRC assuming that it has been appended to the bytes. +/// the serialized data. /// +/// The flavor will check the CRC assuming that it has been appended to the bytes. /// CRCs are used for error detection when reading data back. -/// -/// The `crc` feature requires enabling to use this module. +/// Requires the `crc` feature. /// /// More on CRCs: . #[cfg(feature = "use-crc")] diff --git a/source/postcard/src/de/mod.rs b/source/postcard/src/de/mod.rs index a519e19..b174b9c 100644 --- a/source/postcard/src/de/mod.rs +++ b/source/postcard/src/de/mod.rs @@ -18,8 +18,9 @@ where Ok(t) } -/// Deserialize a message of type `T` from a cobs-encoded byte slice. The -/// unused portion (if any) of the byte slice is not returned. +/// Deserialize a message of type `T` from a cobs-encoded byte slice. +/// +/// The unused portion (if any) of the byte slice is not returned. /// The used portion of the input slice is modified during deserialization (even if an error is returned). /// Therefore, if this is not desired, pass a clone of the original slice. pub fn from_bytes_cobs<'a, T>(s: &'a mut [u8]) -> Result @@ -30,8 +31,9 @@ where from_bytes::(&s[..sz]) } -/// Deserialize a message of type `T` from a cobs-encoded byte slice. The -/// unused portion (if any) of the byte slice is returned for further usage. +/// Deserialize a message of type `T` from a cobs-encoded byte slice. +/// +/// The unused portion (if any) of the byte slice is returned for further usage. /// The used portion of the input slice is modified during deserialization (even if an error is returned). /// Therefore, if this is not desired, pass a clone of the original slice. pub fn take_from_bytes_cobs<'a, T>(s: &'a mut [u8]) -> Result<(T, &'a mut [u8])> @@ -80,7 +82,7 @@ where Ok((t, deserializer.finalize()?)) } -/// Deserialize a message of type `T` from a [std::io::Read]. +/// Deserialize a message of type `T` from a [`std::io::Read`]. #[cfg(feature = "use-std")] pub fn from_io<'a, T, R>(val: (R, &'a mut [u8])) -> Result<(T, (R, &'a mut [u8]))> where @@ -427,7 +429,7 @@ mod test_heapless { let x = ByteSliceStruct { bytes: &[0u8; 32] }; let output: Vec = to_vec(&x).unwrap(); assert_eq!(output.len(), 33); - let out: ByteSliceStruct = from_bytes(output.deref()).unwrap(); + let out: ByteSliceStruct<'_> = from_bytes(output.deref()).unwrap(); assert_eq!(out, ByteSliceStruct { bytes: &[0u8; 32] }); } @@ -488,7 +490,7 @@ mod test_heapless { output.deref() ); - let out: RefStruct = from_bytes(output.deref()).unwrap(); + let out: RefStruct<'_> = from_bytes(output.deref()).unwrap(); assert_eq!( out, RefStruct { @@ -548,7 +550,7 @@ mod test_heapless { let mut encode_buf = [0u8; 32]; let sz = cobs::encode(output.deref(), &mut encode_buf); - let out = from_bytes_cobs::(&mut encode_buf[..sz]).unwrap(); + let out = from_bytes_cobs::>(&mut encode_buf[..sz]).unwrap(); assert_eq!(input, out); } diff --git a/source/postcard/src/error.rs b/source/postcard/src/error.rs index 0d5e484..5fd6996 100644 --- a/source/postcard/src/error.rs +++ b/source/postcard/src/error.rs @@ -5,9 +5,9 @@ use core::fmt::{Display, Formatter}; #[cfg_attr(feature = "use-defmt", derive(defmt::Format))] #[non_exhaustive] pub enum Error { - /// This is a feature that PostCard will never implement + /// This is a feature that postcard will never implement WontImplement, - /// This is a feature that Postcard intends to support, but does not yet + /// This is a feature that postcard intends to support, but does not yet NotYetImplemented, /// The serialize buffer is full SerializeBufferFull, @@ -25,7 +25,7 @@ pub enum Error { DeserializeBadUtf8, /// Found an Option discriminant that wasn't 0 or 1 DeserializeBadOption, - /// Found an enum discriminant that was > u32::max_value() + /// Found an enum discriminant that was > `u32::MAX` DeserializeBadEnum, /// The original data was not well encoded DeserializeBadEncoding, @@ -40,7 +40,7 @@ pub enum Error { } impl Display for Error { - fn fmt(&self, f: &mut Formatter) -> core::fmt::Result { + fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { use Error::*; write!( f, diff --git a/source/postcard/src/fixint.rs b/source/postcard/src/fixint.rs index 5392c42..fffcc22 100644 --- a/source/postcard/src/fixint.rs +++ b/source/postcard/src/fixint.rs @@ -16,6 +16,7 @@ use serde::{Deserialize, Serialize, Serializer}; /// Use with the `#[serde(with = "postcard::fixint::le")]` field attribute. +/// /// Disables varint serialization/deserialization for the specified integer /// field. The integer will always be serialized in the same way as a fixed /// size array, in **Little Endian** order on the wire. @@ -53,9 +54,10 @@ pub mod le { } } +/// Disables varint serialization/deserialization for the specified integer field. +/// /// Use with the `#[serde(with = "postcard::fixint::be")]` field attribute. -/// Disables varint serialization/deserialization for the specified integer -/// field. The integer will always be serialized in the same way as a fixed +/// The integer will always be serialized in the same way as a fixed /// size array, in **Big Endian** order on the wire. /// /// ```rust diff --git a/source/postcard/src/ser/flavors.rs b/source/postcard/src/ser/flavors.rs index b749e68..7bca90e 100644 --- a/source/postcard/src/ser/flavors.rs +++ b/source/postcard/src/ser/flavors.rs @@ -115,18 +115,18 @@ pub trait Flavor { /// such as a slice or a Vec of some sort. type Output; - /// The try_extend() trait method can be implemented when there is a more efficient way of processing - /// multiple bytes at once, such as copying a slice to the output, rather than iterating over one byte - /// at a time. + /// Override this method when you want to customize processing + /// multiple bytes at once, such as copying a slice to the output, + /// rather than iterating over one byte at a time. #[inline] fn try_extend(&mut self, data: &[u8]) -> Result<()> { data.iter().try_for_each(|d| self.try_push(*d)) } - /// The try_push() trait method can be used to push a single byte to be modified and/or stored + /// Push a single byte to be modified and/or stored. fn try_push(&mut self, data: u8) -> Result<()>; - /// Finalize the serialization process + /// Finalize the serialization process. fn finalize(self) -> Result; } @@ -194,7 +194,7 @@ impl<'a> Flavor for Slice<'a> { } } -impl<'a> Index for Slice<'a> { +impl Index for Slice<'_> { type Output = u8; fn index(&self, idx: usize) -> &u8 { @@ -204,7 +204,7 @@ impl<'a> Index for Slice<'a> { } } -impl<'a> IndexMut for Slice<'a> { +impl IndexMut for Slice<'_> { fn index_mut(&mut self, idx: usize) -> &mut u8 { let len = (self.end as usize) - (self.start as usize); assert!(idx < len); @@ -212,7 +212,7 @@ impl<'a> IndexMut for Slice<'a> { } } -/// Wrapper over a [`std::iter::Extend`] that implements the flavor trait +/// Wrapper over a [`core::iter::Extend`] that implements the flavor trait pub struct ExtendFlavor { iter: T, } @@ -221,7 +221,7 @@ impl ExtendFlavor where T: core::iter::Extend, { - /// Create a new [Self] flavor from a given [`std::iter::Extend`] + /// Create a new [`Self`] flavor from a given [`core::iter::Extend`] pub fn new(iter: T) -> Self { Self { iter } } @@ -241,7 +241,7 @@ where #[inline(always)] fn try_extend(&mut self, b: &[u8]) -> Result<()> { - self.iter.extend(b.iter().cloned()); + self.iter.extend(b.iter().copied()); Ok(()) } @@ -266,7 +266,7 @@ pub mod eio { where T: crate::eio::Write, { - /// Create a new [Self] flavor from a given [`embedded_io Write`](crate::eio::Write) + /// Create a new [`Self`] flavor from a given [`embedded_io Write`](crate::eio::Write) pub fn new(writer: T) -> Self { Self { writer } } @@ -303,14 +303,14 @@ pub mod eio { } } -/// Support for the [std::io] traits +/// Support for the [`std::io`] traits #[cfg(feature = "use-std")] pub mod io { use super::Flavor; use crate::{Error, Result}; - /// Wrapper over a [std::io::Write] that implements the flavor trait + /// Wrapper over a [`std::io::Write`] that implements the flavor trait pub struct WriteFlavor { writer: T, } @@ -319,7 +319,7 @@ pub mod io { where T: std::io::Write, { - /// Create a new [Self] flavor from a given [std::io::Write] + /// Create a new [`Self`] flavor from a given [`std::io::Write`] pub fn new(writer: T) -> Self { Self { writer } } @@ -377,7 +377,7 @@ mod heapless_vec { } impl HVec { - /// Create a new, currently empty, [heapless::Vec] to be used for storing serialized + /// Create a new, currently empty, [`heapless::Vec`] to be used for storing serialized /// output data. pub fn new() -> Self { Self::default() @@ -436,7 +436,7 @@ mod alloc_vec { use crate::Result; use alloc::vec::Vec; - /// The `AllocVec` flavor is a wrapper type around an [alloc::vec::Vec]. + /// The `AllocVec` flavor is a wrapper type around an [`alloc::vec::Vec`]. /// /// This type is only available when the (non-default) `alloc` feature is active #[derive(Default)] @@ -446,7 +446,7 @@ mod alloc_vec { } impl AllocVec { - /// Create a new, currently empty, [alloc::vec::Vec] to be used for storing serialized + /// Create a new, currently empty, [`alloc::vec::Vec`] to be used for storing serialized /// output data. pub fn new() -> Self { Self::default() @@ -564,11 +564,11 @@ where //////////////////////////////////////// /// This Cyclic Redundancy Check flavor applies [the CRC crate's `Algorithm`](https://docs.rs/crc/latest/crc/struct.Algorithm.html) struct on -/// the serialized data. The output of this flavor receives the CRC appended to the bytes. +/// the serialized data. /// +/// The output of this flavor receives the CRC appended to the bytes. /// CRCs are used for error detection when reading data back. -/// -/// The `crc` feature requires enabling to use this module. +/// Requires the `crc` feature. /// /// More on CRCs: . #[cfg(feature = "use-crc")] diff --git a/source/postcard/src/ser/mod.rs b/source/postcard/src/ser/mod.rs index c2b2050..e2fa32a 100644 --- a/source/postcard/src/ser/mod.rs +++ b/source/postcard/src/ser/mod.rs @@ -255,7 +255,7 @@ where ) } -/// Serialize a `T` to a [core::iter::Extend], +/// Serialize a `T` to a [`core::iter::Extend`], /// ## Example /// /// ```rust @@ -287,7 +287,7 @@ where /// assert_eq!(&buf[0..5], &[0x01, 0x03, b'H', b'i', b'!']); /// ``` #[cfg(any(feature = "embedded-io-04", feature = "embedded-io-06"))] -pub fn to_eio<'b, T, W>(value: &'b T, writer: W) -> Result +pub fn to_eio(value: &T, writer: W) -> Result where T: Serialize + ?Sized, W: crate::eio::Write, @@ -295,7 +295,7 @@ where serialize_with_flavor::(value, flavors::eio::WriteFlavor::new(writer)) } -/// Serialize a `T` to a [std::io::Write], +/// Serialize a `T` to a [`std::io::Write`], /// ## Example /// /// ```rust @@ -856,7 +856,7 @@ mod test { let sz = cobs::decode_in_place(output.deref_mut()).unwrap(); - let x = crate::from_bytes::(&output.deref_mut()[..sz]).unwrap(); + let x = crate::from_bytes::>(&output.deref_mut()[..sz]).unwrap(); assert_eq!(input, x); } diff --git a/source/postcard/src/ser/serializer.rs b/source/postcard/src/ser/serializer.rs index fa7729f..1bccedf 100644 --- a/source/postcard/src/ser/serializer.rs +++ b/source/postcard/src/ser/serializer.rs @@ -63,7 +63,7 @@ impl Serializer { } } -impl<'a, F> ser::Serializer for &'a mut Serializer +impl ser::Serializer for &mut Serializer where F: Flavor, { @@ -347,7 +347,7 @@ where } impl Write for CountWriter { fn write_str(&mut self, s: &str) -> core::result::Result<(), core::fmt::Error> { - self.ct += s.as_bytes().len(); + self.ct += s.len(); Ok(()) } } @@ -356,7 +356,7 @@ where // This is the first pass through, where we just count the length of the // data that we are given - write!(&mut ctr, "{}", value).map_err(|_| Error::CollectStrError)?; + write!(&mut ctr, "{value}").map_err(|_| Error::CollectStrError)?; let len = ctr.ct; self.try_push_varint_usize(len) .map_err(|_| Error::SerializeBufferFull)?; @@ -367,7 +367,7 @@ where { output: &'a mut IF, } - impl<'a, IF> Write for FmtWriter<'a, IF> + impl Write for FmtWriter<'_, IF> where IF: Flavor, { @@ -382,13 +382,13 @@ where let mut fw = FmtWriter { output: &mut self.output, }; - write!(&mut fw, "{}", value).map_err(|_| Error::CollectStrError)?; + write!(&mut fw, "{value}").map_err(|_| Error::CollectStrError)?; Ok(()) } } -impl<'a, F> ser::SerializeSeq for &'a mut Serializer +impl ser::SerializeSeq for &mut Serializer where F: Flavor, { @@ -413,7 +413,7 @@ where } } -impl<'a, F> ser::SerializeTuple for &'a mut Serializer +impl ser::SerializeTuple for &mut Serializer where F: Flavor, { @@ -434,7 +434,7 @@ where } } -impl<'a, F> ser::SerializeTupleStruct for &'a mut Serializer +impl ser::SerializeTupleStruct for &mut Serializer where F: Flavor, { @@ -455,7 +455,7 @@ where } } -impl<'a, F> ser::SerializeTupleVariant for &'a mut Serializer +impl ser::SerializeTupleVariant for &mut Serializer where F: Flavor, { @@ -476,7 +476,7 @@ where } } -impl<'a, F> ser::SerializeMap for &'a mut Serializer +impl ser::SerializeMap for &mut Serializer where F: Flavor, { @@ -505,7 +505,7 @@ where } } -impl<'a, F> ser::SerializeStruct for &'a mut Serializer +impl ser::SerializeStruct for &mut Serializer where F: Flavor, { @@ -526,7 +526,7 @@ where } } -impl<'a, F> ser::SerializeStructVariant for &'a mut Serializer +impl ser::SerializeStructVariant for &mut Serializer where F: Flavor, { diff --git a/source/postcard/tests/loopback.rs b/source/postcard/tests/loopback.rs index d9fff5d..b54b5b3 100644 --- a/source/postcard/tests/loopback.rs +++ b/source/postcard/tests/loopback.rs @@ -219,7 +219,7 @@ fn std_eio_loopback() { use postcard::from_eio; use postcard::to_eio; - fn test_io<'a, 'de, T>(data: T, ser_rep: &'a [u8]) + fn test_io(data: T, ser_rep: &[u8]) where T: Serialize + DeserializeOwned + Eq + PartialEq + Debug, {