Trait bounds : type of 'spi' object when passing to a function

Hi ,

So, I have a 'spi' object defined like this

    let mut spi = Spi::new(

intellisense says that the object is of type "Spi < SPI1 >"

I now want to pass that object to a function, but having trouble with the type.
Reading and trying and experimenting .... gave me this

fn ws28xx_writeled<T>(spi: &mut Spi<T>, color: u32) {
    let mut spidata: [u8;24] = [0;24];
    ws28xx_calc24(&mut spidata, color);
    spi.write(&mut spidata);

But I get:

error[E0599]: the method `write` exists for mutable reference `&mut Spi<T>`, but its trait bounds were not satisfied
  --> src/
55 |     spi.write(&mut spidata);
   |         ^^^^^ method cannot be called on `&mut Spi<T>` due to unsatisfied trait bounds
   = note: the following trait bounds were not satisfied:
           `<T as Deref>::Target = stm32_hal2::pac::spi1::RegisterBlock`
           `T: Deref`

Am I on the right track?


The documentation for Spi reads

impl<R> Spi<R> where
    R: Deref<Target = RegisterBlock>, 


But your ws28xx_writeled accept Spi<T> for any T. So you can either:

  • Don't make the function generic, since you know the type of T here
    fn ws28xx_writeled(spi: &mut Spi<SPI1>, color: u32) { /* */ }
  • Or, indicate the required bound:
    fn ws28xx_writeled<T>(spi: &mut Spi<T>, color: u32)
        where T: Deref<Target = stm32_hal2::pac::spi1::RegisterBlock>
    { /* */ }
    That's what the compiler is telling you to do, maybe in an unclear way :sweat_smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.