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

Implementing spi traits on the struct as opposed to a reference to the struct #57

Open
alexkl-rugged-controls opened this issue Oct 9, 2024 · 0 comments

Comments

@alexkl-rugged-controls
Copy link

alexkl-rugged-controls commented Oct 9, 2024

Lets say I have a struct which is supposed to store a generic SpiDevice:

struct Foo<E> {
    device: Box<dyn SpiDevice<Error = E>>
}

I then want to store a device into it:

    let f = Foo {
        device: Box::new(spi_device_value),
    };

This works with linux_embedded_hal but not with ftdi_embedded_hal, because it appears the SpiDevice trait is only implemented on references to SpiDevice<Device>.

I'm fairly new to rust, but it seems that simply implementing it for the struct directly works for both references and owned values:

impl<Device, E> eh1::spi::SpiDevice for &'a SpiDevice<Device>
// into
impl<Device, E> eh1::spi::SpiDevice for SpiDevice<Device>

I did this transformation (along with removing the one lifetime reference to 'a in the body, and it works just fine.

Is there a reason why it's only implemented for references?

It seems like you get the impl on &mut T for free:
https://rtic.rs/dev/api/embedded_hal/spi/trait.SpiDevice.html#impl-SpiDevice%3CWord%3E-for-%26mut+T

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant