embedded-dma icon indicating copy to clipboard operation
embedded-dma copied to clipboard

How implement ReadBuffer for custom structs, newtype for example?

Open qwerty19106 opened this issue 2 years ago • 1 comments

ReadTarget allows implement ReadBuffer for some standard generic types, such as alloc::Box<[u8; N]>, heapless::Box<[u8; N]> et. al.

unsafe impl<B, T> ReadBuffer for B
where
    B: Deref<Target = T> + StableDeref + 'static,
    T: ReadTarget + ?Sized,
{...}

But it not works automatically with newtype structs with Deref:

pub struct MyArray([u8; 100]);

impl Deref for MyArray {
   type Target = [u8];
   fn deref(&self) -> &[u8] {
       &self.0
   }
}

#[entry]
fn main() -> ! {
   let a = alloc::Box::new(MyArray([0; 100]));
   unsafe {
       a.read_buffer();
   }

   loop {}
}

error[E0599]: the method read_bufferexists for structBox<MyArray>, but its trait bounds were not satisfied.

I can implement ReadTarget manually, and then code will be compiled:

unsafe impl ReadTarget for MyArray {
    type Word = u8;

    fn as_read_buffer(&self) -> (*const Self::Word, usize) {
        self.deref().as_read_buffer()
    }
}

Why my code not works by using Deref only?

P.S. Full example

qwerty19106 avatar Feb 26 '24 19:02 qwerty19106

This repo is dead?

qwerty19106 avatar Mar 13 '24 08:03 qwerty19106