From 2e5301def1889e268b2345f45344e2de784275af Mon Sep 17 00:00:00 2001 From: Liam Kinne Date: Tue, 2 Apr 2024 23:40:06 +1000 Subject: [PATCH 1/6] add async API --- embedded-can/src/asynch.rs | 25 +++++++++++++++++++++++++ embedded-can/src/lib.rs | 2 ++ 2 files changed, 27 insertions(+) create mode 100644 embedded-can/src/asynch.rs diff --git a/embedded-can/src/asynch.rs b/embedded-can/src/asynch.rs new file mode 100644 index 00000000..2285bed6 --- /dev/null +++ b/embedded-can/src/asynch.rs @@ -0,0 +1,25 @@ +//! Async CAN API + +/// An async CAN interface that is able to transmit and receive frames. +pub trait Can { + /// Associated frame type. + type Frame: crate::Frame; + + /// Associated error type. + type Error: crate::Error; + + /// Puts a frame in the transmit buffer. + /// Awaits until space is available in the transmit buffer. + async fn transmit(&mut self, frame: &Self::Frame) -> Result<(), Self::Error>; + + /// Tries to put a frame in the transmit buffer. + /// If no space is available in the transmit buffer `None` is returned. + fn try_transmit(&mut self, frame: &Self::Frame) -> Option>; + + /// Awaits until a frame was received or an error occurred. + async fn receive(&mut self) -> Result; + + /// Tries to receive a frame from the receive buffer. + /// If no frame is available `None` is returned. + fn try_receive(&mut self) -> Option>; +} diff --git a/embedded-can/src/lib.rs b/embedded-can/src/lib.rs index bb010b96..9f349000 100644 --- a/embedded-can/src/lib.rs +++ b/embedded-can/src/lib.rs @@ -2,7 +2,9 @@ #![warn(missing_docs)] #![no_std] +#![allow(async_fn_in_trait)] +pub mod asynch; pub mod blocking; pub mod nb; From d6315c1a2894efe253ebfd28d37bd8278bd15532 Mon Sep 17 00:00:00 2001 From: Liam Kinne Date: Tue, 2 Apr 2024 23:42:04 +1000 Subject: [PATCH 2/6] add changelog entry --- embedded-can/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/embedded-can/CHANGELOG.md b/embedded-can/CHANGELOG.md index f8719d63..3699d355 100644 --- a/embedded-can/CHANGELOG.md +++ b/embedded-can/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Added `core::error::Error` implementations for every custom `impl Error` - Increased MSRV to 1.81 due to `core::error::Error` +- Add async API. ## [v0.4.1] - 2022-09-28 From f772c8aa9dc5e0ccc87bf5ae51feb5c118dda1a9 Mon Sep 17 00:00:00 2001 From: Liam Kinne Date: Wed, 3 Apr 2024 00:20:02 +1000 Subject: [PATCH 3/6] fix ci warning --- embedded-can/src/lib.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/embedded-can/src/lib.rs b/embedded-can/src/lib.rs index 9f349000..88e7ffe9 100644 --- a/embedded-can/src/lib.rs +++ b/embedded-can/src/lib.rs @@ -2,6 +2,12 @@ #![warn(missing_docs)] #![no_std] +// disable warning for already-stabilized features. +// Needed to pass CI, because we deny warnings. +// We don't immediately remove them to not immediately break older nightlies. +// When all features are stable, we'll remove them. +#![cfg_attr(nightly, allow(stable_features, unknown_lints))] +#![cfg_attr(nightly, feature(async_fn_in_trait, impl_trait_projections))] #![allow(async_fn_in_trait)] pub mod asynch; From 0fbbbd60eee856e7635e745a305cf8b5d5c97ede Mon Sep 17 00:00:00 2001 From: Liam Kinne Date: Fri, 28 Jun 2024 08:30:29 +1000 Subject: [PATCH 4/6] remove try_ methods --- embedded-can/src/asynch.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/embedded-can/src/asynch.rs b/embedded-can/src/asynch.rs index 2285bed6..cec27479 100644 --- a/embedded-can/src/asynch.rs +++ b/embedded-can/src/asynch.rs @@ -12,14 +12,6 @@ pub trait Can { /// Awaits until space is available in the transmit buffer. async fn transmit(&mut self, frame: &Self::Frame) -> Result<(), Self::Error>; - /// Tries to put a frame in the transmit buffer. - /// If no space is available in the transmit buffer `None` is returned. - fn try_transmit(&mut self, frame: &Self::Frame) -> Option>; - /// Awaits until a frame was received or an error occurred. async fn receive(&mut self) -> Result; - - /// Tries to receive a frame from the receive buffer. - /// If no frame is available `None` is returned. - fn try_receive(&mut self) -> Option>; } From 94b2c2dbfb3d2f9a84fa609c5f1dc787c62ab7b8 Mon Sep 17 00:00:00 2001 From: Liam Kinne Date: Thu, 4 Jul 2024 20:15:50 +1000 Subject: [PATCH 5/6] Revert "fix ci warning" This reverts commit 81bc3673ab828d580569f0c7fc2dae39e1182ae0. --- embedded-can/src/lib.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/embedded-can/src/lib.rs b/embedded-can/src/lib.rs index 88e7ffe9..9f349000 100644 --- a/embedded-can/src/lib.rs +++ b/embedded-can/src/lib.rs @@ -2,12 +2,6 @@ #![warn(missing_docs)] #![no_std] -// disable warning for already-stabilized features. -// Needed to pass CI, because we deny warnings. -// We don't immediately remove them to not immediately break older nightlies. -// When all features are stable, we'll remove them. -#![cfg_attr(nightly, allow(stable_features, unknown_lints))] -#![cfg_attr(nightly, feature(async_fn_in_trait, impl_trait_projections))] #![allow(async_fn_in_trait)] pub mod asynch; From 5d35622e6c246445895c92573589300ea906d3e2 Mon Sep 17 00:00:00 2001 From: Liam Kinne Date: Mon, 11 Aug 2025 19:00:31 +1000 Subject: [PATCH 6/6] split into separate tx/rx traits --- embedded-can/src/asynch.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/embedded-can/src/asynch.rs b/embedded-can/src/asynch.rs index cec27479..3a1939c7 100644 --- a/embedded-can/src/asynch.rs +++ b/embedded-can/src/asynch.rs @@ -1,17 +1,26 @@ //! Async CAN API -/// An async CAN interface that is able to transmit and receive frames. -pub trait Can { +/// An async CAN interface that is able to transmit frames. +pub trait CanTx { /// Associated frame type. type Frame: crate::Frame; /// Associated error type. type Error: crate::Error; - /// Puts a frame in the transmit buffer. - /// Awaits until space is available in the transmit buffer. + /// Puts a frame in the transmit buffer or awaits until space is available + /// in the transmit buffer. async fn transmit(&mut self, frame: &Self::Frame) -> Result<(), Self::Error>; +} + +/// An async CAN interface that is able to receive frames. +pub trait CanRx { + /// Associated frame type. + type Frame: crate::Frame; + + /// Associated error type. + type Error: crate::Error; - /// Awaits until a frame was received or an error occurred. + /// Awaits until a frame was received or an error occurs. async fn receive(&mut self) -> Result; }