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

async/spi: Add a transaction helper macro #404

Merged
merged 11 commits into from
Sep 21, 2022

Conversation

GrantM11235
Copy link
Contributor

Based on #403

This macro allows users to use SpiDevice::transaction in a completely safe way, without needing to dereference a raw pointer.

The macro is exported as embedded_hal_async::spi_transaction_helper because exported macros always go in the root of the crate.
It is also publicly reexported as embedded_hal_async::spi::transaction_helper because I think that most people would expect to find it in the spi module.
It is not possible to apply doc(hidden) to the instance in the root of the crate because that would also hide the reexport, see rust-lang/rust#59368.

@rust-highfive
Copy link

r? @ryankurte

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added S-waiting-on-review Review is incomplete T-hal labels Sep 3, 2022
Clippy doesn't like it when you `drop` a reference
Dirbaio added a commit to embassy-rs/cyw43 that referenced this pull request Sep 20, 2022
Copy link
Member

@Dirbaio Dirbaio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated a driver to use the macro, and it worked very nicely! HUGE improvement in ergonomics. https://github.com/embassy-rs/cyw43/pull/19/files

I've commented a few naming/doc nits, LGTM otherwise!

@@ -35,13 +35,117 @@ where
Word: Copy + 'static,
= impl Future<Output = Result<(), T::Error>>;

#[macro_export]
/// This macro is a workaround for the workaround in [`SpiDevice::transaction`]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps a more descriptive docs on why you might want to use it? Something like "Do an SPI transaction on a bus. This is a safe wrapper for [SpiDevice::transaction], which handles dereferencing the raw pointer for you."

/// .await
/// # }
/// ```
macro_rules! spi_transaction_helper {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if the name should be simply spi_transaction. I think users will end up using this 100% of the time instead of the unsafe transaction() method.

@@ -69,9 +173,13 @@ pub trait SpiDevice: ErrorType {
/// On bus errors the implementation should try to deassert CS.
/// If an error occurs while deasserting CS the bus error should take priority as the return value.
///
/// # Safety
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mention that the transaction! safe wrapper macro exist and that using it if possible is recommended.

Copy link
Member

@Dirbaio Dirbaio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

bors r+

@bors bors bot merged commit 8790887 into rust-embedded:master Sep 21, 2022
@GrantM11235 GrantM11235 deleted the async-transaction-helper branch September 21, 2022 02:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Review is incomplete T-hal
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants