From 3e70dd2f1f5fd86304b6470da7e0d62a73f84066 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 86248f3c..65b0500e 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 9ae2e6e7e62cb83b34a5bbe62fd90d8778dd485a 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 6ef70c03..96ecbcdb 100644 --- a/embedded-can/CHANGELOG.md +++ b/embedded-can/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added - `as_raw_unchecked` getter function for `Id` +- Add async API. ## [v0.4.1] - 2022-09-28 From 22725ff1b0258d10edf72df67a11623ff9da8203 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 65b0500e..e8d3fe54 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 a5c34295a90ac302bd0467d7ee8043727df99855 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 a226a0c1a0e43eb686203e57c7793edf9185b57c 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 e8d3fe54..65b0500e 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 68d91c8cc8f7063e6430e23f1786f8552e91839c 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; }