ftdi-embedded-hal icon indicating copy to clipboard operation
ftdi-embedded-hal copied to clipboard

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

Open alexkl-rugged-controls opened this issue 1 year ago • 0 comments

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

alexkl-rugged-controls avatar Oct 09 '24 17:10 alexkl-rugged-controls