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

#118 Add support for latest embedded-io #123

Merged
merged 1 commit into from
Jan 14, 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
20 changes: 17 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "postcard"
version = "1.0.8"
version = "1.0.9"
authors = ["James Munns <james@onevariable.com>"]
edition = "2018"
readme = "README.md"
Expand Down Expand Up @@ -47,10 +47,16 @@ default-features = false
version = "0.3"
optional = true

[dependencies.embedded-io]
[dependencies.embedded-io-04]
package = "embedded-io"
version = "0.4"
optional = true

[dependencies.embedded-io-06]
package = "embedded-io"
version = "0.6"
optional = true

[dependencies.postcard-derive]
path = "./postcard-derive"
version = "0.1.1"
Expand All @@ -67,9 +73,17 @@ optional = true
[features]
default = ["heapless-cas"]

# Enables support for `embedded-io` traits
# This feature will track the latest `embedded-io` when major releases are made
embedded-io = ["dep:embedded-io-04"]

# Specific versions of `embedded-io` can be selected through the features below
embedded-io-04 = ["dep:embedded-io-04"]
embedded-io-06 = ["dep:embedded-io-06"]

use-std = ["serde/std", "alloc"]
heapless-cas = ["heapless", "heapless/cas"]
alloc = ["serde/alloc", "embedded-io/alloc"]
alloc = ["serde/alloc", "embedded-io-04?/alloc", "embedded-io-06?/alloc"]
use-defmt = ["defmt"]
use-crc = ["crc", "paste"]

Expand Down
20 changes: 12 additions & 8 deletions src/de/flavors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,12 @@ impl<'de> Flavor<'de> for Slice<'de> {
}
}

/// Support for [std::io] or [embedded-io] traits
#[cfg(any(feature = "embedded-io", feature = "use-std"))]
/// Support for [std::io] or `embedded-io` traits
#[cfg(any(
feature = "embedded-io-04",
feature = "embedded-io-06",
feature = "use-std"
))]
pub mod io {
use crate::{Error, Result};
use core::marker::PhantomData;
Expand Down Expand Up @@ -206,25 +210,25 @@ pub mod io {
}
}

/// Support for [embedded_io] traits
#[cfg(feature = "embedded-io")]
/// Support for [`embedded_io`](crate::eio::embedded_io) traits
#[cfg(any(feature = "embedded-io-04", feature = "embedded-io-06"))]
pub mod eio {
use super::super::Flavor;
use super::SlidingBuffer;
use crate::{Error, Result};

/// Wrapper over a [embedded_io::blocking::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: embedded_io::blocking::Read,
T: crate::eio::Read,
{
reader: T,
buff: SlidingBuffer<'de>,
}

impl<'de, T> EIOReader<'de, T>
where
T: embedded_io::blocking::Read,
T: crate::eio::Read,
{
pub(crate) fn new(reader: T, buff: &'de mut [u8]) -> Self {
Self {
Expand All @@ -236,7 +240,7 @@ pub mod io {

impl<'de, T> Flavor<'de> for EIOReader<'de, T>
where
T: embedded_io::blocking::Read + 'de,
T: crate::eio::Read + 'de,
{
type Remainder = (T, &'de mut [u8]);
type Source = &'de [u8];
Expand Down
6 changes: 3 additions & 3 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ where
Ok((t, deserializer.finalize()?))
}

/// Deserialize a message of type `T` from a [embedded_io::blocking::Read].
#[cfg(feature = "embedded-io")]
/// Deserialize a message of type `T` from a [`embedded_io`](crate::eio::embedded_io)::[`Read`](crate::eio::Read).
#[cfg(any(feature = "embedded-io-04", feature = "embedded-io-06"))]
pub fn from_eio<'a, T, R>(val: (R, &'a mut [u8])) -> Result<(T, (R, &'a mut [u8]))>
where
T: Deserialize<'a>,
R: embedded_io::blocking::Read + 'a,
R: crate::eio::Read + 'a,
{
let flavor = flavors::io::eio::EIOReader::new(val.0, val.1);
let mut deserializer = Deserializer::from_flavor(flavor);
Expand Down
18 changes: 18 additions & 0 deletions src/eio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#[cfg(all(feature = "embedded-io-04", feature = "embedded-io-06"))]
compile_error!("Only one version of `embedded-io` must be enabled through features");

#[cfg(feature = "embedded-io-04")]
mod version_impl {
pub use embedded_io_04 as embedded_io;
pub use embedded_io_04::blocking::{Read, Write};
}

#[cfg(feature = "embedded-io-06")]
mod version_impl {
pub use embedded_io_06 as embedded_io;
pub use embedded_io_06::{Read, Write};
}

// All versions should export the appropriate items
#[cfg(any(feature = "embedded-io-04", feature = "embedded-io-06"))]
pub use version_impl::{embedded_io, Read, Write};
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

pub mod accumulator;
mod de;

mod eio;

mod error;
pub mod fixint;
mod ser;
Expand Down Expand Up @@ -88,10 +91,10 @@ pub use ser::{serialize_with_flavor, serializer::Serializer, to_extend, to_slice
#[cfg(feature = "heapless")]
pub use ser::{to_vec, to_vec_cobs};

#[cfg(feature = "embedded-io")]
#[cfg(any(feature = "embedded-io-04", feature = "embedded-io-06"))]
pub use ser::to_eio;

#[cfg(feature = "embedded-io")]
#[cfg(any(feature = "embedded-io-04", feature = "embedded-io-06"))]
pub use de::from_eio;

#[cfg(feature = "use-std")]
Expand Down
12 changes: 6 additions & 6 deletions src/ser/flavors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,31 +250,31 @@ where
}
}

/// Support for the [embedded-io] traits
#[cfg(feature = "embedded-io")]
/// Support for the [`embedded-io`](crate::eio::embedded_io) traits
#[cfg(any(feature = "embedded-io-04", feature = "embedded-io-06"))]
pub mod eio {

use super::Flavor;
use crate::{Error, Result};

/// Wrapper over a [embedded_io::blocking::Write] that implements the flavor trait
/// Wrapper over a [`embedded_io Write`](crate::eio::Write) that implements the flavor trait
pub struct WriteFlavor<T> {
writer: T,
}

impl<T> WriteFlavor<T>
where
T: embedded_io::blocking::Write,
T: crate::eio::Write,
{
/// Create a new [Self] flavor from a given [std::io::Write]
/// Create a new [Self] flavor from a given [`embedded_io Write`](crate::eio::Write)
pub fn new(writer: T) -> Self {
Self { writer }
}
}

impl<T> Flavor for WriteFlavor<T>
where
T: embedded_io::blocking::Write,
T: crate::eio::Write,
{
type Output = T;

Expand Down
6 changes: 3 additions & 3 deletions src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ where
serialize_with_flavor::<T, _, _>(value, flavors::ExtendFlavor::new(writer))
}

/// Serialize a `T` to a [embedded_io::blocking::Write],
/// Serialize a `T` to an [`embedded_io Write`](crate::eio::Write),
/// ## Example
///
/// ```rust
Expand All @@ -286,11 +286,11 @@ where
/// to_eio("Hi!", ser).unwrap();
/// assert_eq!(&buf[0..5], &[0x01, 0x03, b'H', b'i', b'!']);
/// ```
#[cfg(feature = "embedded-io")]
#[cfg(any(feature = "embedded-io-04", feature = "embedded-io-06"))]
pub fn to_eio<'b, T, W>(value: &'b T, writer: W) -> Result<W>
where
T: Serialize + ?Sized,
W: embedded_io::blocking::Write,
W: crate::eio::Write,
{
serialize_with_flavor::<T, _, _>(value, flavors::eio::WriteFlavor::new(writer))
}
Expand Down
5 changes: 4 additions & 1 deletion tests/loopback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,10 @@ fn std_io_loopback() {
);
}

#[cfg(all(feature = "embedded-io", feature = "alloc"))]
#[cfg(all(
any(feature = "embedded-io-04", feature = "embedded-io-06"),
feature = "alloc"
))]
#[test]
fn std_eio_loopback() {
use postcard::from_eio;
Expand Down