Skip to content

Commit

Permalink
Merge pull request #169 from barafael/edition-2021
Browse files Browse the repository at this point in the history
Update to edition 2021 (and clippy fixes)
  • Loading branch information
jamesmunns authored Nov 29, 2024
2 parents bffc76c + eaa410d commit 0dd176a
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 65 deletions.
9 changes: 7 additions & 2 deletions source/postcard/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "postcard"
version = "1.1.0"
authors = ["James Munns <james@onevariable.com>"]
edition = "2018"
edition = "2021"
readme = "README.md"
repository = "https://github.com/jamesmunns/postcard"
description = "A no_std + serde compatible message library for Rust"
Expand Down Expand Up @@ -77,7 +77,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"]
Expand All @@ -86,3 +86,8 @@ use-crc = ["crc", "paste"]
#
# 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"]
10 changes: 5 additions & 5 deletions source/postcard/src/de/deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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;

Expand Down
14 changes: 7 additions & 7 deletions source/postcard/src/de/flavors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -310,15 +310,15 @@ pub mod io {
}
}

/// Support for [std::io] traits
/// Support for [`std::io`] traits
#[allow(clippy::module_inception)]
#[cfg(feature = "use-std")]
pub mod io {
use super::super::Flavor;
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,
Expand Down Expand Up @@ -386,11 +386,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: <https://en.wikipedia.org/wiki/Cyclic_redundancy_check>.
#[cfg(feature = "use-crc")]
Expand Down
18 changes: 10 additions & 8 deletions source/postcard/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>
Expand All @@ -30,8 +31,9 @@ where
from_bytes::<T>(&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])>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -427,7 +429,7 @@ mod test_heapless {
let x = ByteSliceStruct { bytes: &[0u8; 32] };
let output: Vec<u8, 128> = 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] });
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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::<RefStruct>(&mut encode_buf[..sz]).unwrap();
let out = from_bytes_cobs::<RefStruct<'_>>(&mut encode_buf[..sz]).unwrap();

assert_eq!(input, out);
}
Expand Down
8 changes: 4 additions & 4 deletions source/postcard/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions source/postcard/src/fixint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
40 changes: 20 additions & 20 deletions source/postcard/src/ser/flavors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self::Output>;
}

Expand Down Expand Up @@ -194,7 +194,7 @@ impl<'a> Flavor for Slice<'a> {
}
}

impl<'a> Index<usize> for Slice<'a> {
impl Index<usize> for Slice<'_> {
type Output = u8;

fn index(&self, idx: usize) -> &u8 {
Expand All @@ -204,15 +204,15 @@ impl<'a> Index<usize> for Slice<'a> {
}
}

impl<'a> IndexMut<usize> for Slice<'a> {
impl IndexMut<usize> for Slice<'_> {
fn index_mut(&mut self, idx: usize) -> &mut u8 {
let len = (self.end as usize) - (self.start as usize);
assert!(idx < len);
unsafe { &mut *self.start.add(idx) }
}
}

/// Wrapper over a [`std::iter::Extend<u8>`] that implements the flavor trait
/// Wrapper over a [`core::iter::Extend<u8>`] that implements the flavor trait
pub struct ExtendFlavor<T> {
iter: T,
}
Expand All @@ -221,7 +221,7 @@ impl<T> ExtendFlavor<T>
where
T: core::iter::Extend<u8>,
{
/// Create a new [Self] flavor from a given [`std::iter::Extend<u8>`]
/// Create a new [`Self`] flavor from a given [`core::iter::Extend<u8>`]
pub fn new(iter: T) -> Self {
Self { iter }
}
Expand All @@ -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(())
}

Expand All @@ -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 }
}
Expand Down Expand Up @@ -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<T> {
writer: T,
}
Expand All @@ -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 }
}
Expand Down Expand Up @@ -377,7 +377,7 @@ mod heapless_vec {
}

impl<const B: usize> HVec<B> {
/// 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()
Expand Down Expand Up @@ -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)]
Expand All @@ -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()
Expand Down Expand Up @@ -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: <https://en.wikipedia.org/wiki/Cyclic_redundancy_check>.
#[cfg(feature = "use-crc")]
Expand Down
Loading

0 comments on commit 0dd176a

Please sign in to comment.