rust-embedded / embedded-dma

Apache License 2.0
35 stars 12 forks source link

Cannot implement `WriteTarget` for multiple word sizes #16

Open jonas-schievink opened 3 years ago

jonas-schievink commented 3 years ago

I have a use case where I'd like to reuse a buffer type for multiple different DMA operations that require different word sizes and alignments. Basically what I'd like to use as the buffer type is

union Buffer {
    u8: [u8; 1024],
    i16: [i16; 512],
}

Now I'd like to implement WriteTarget twice for this type, with a u8 and i16 word. Unfortunately that doesn't work, since the word type is an associated type of the WriteTarget trait. Maybe it would be better if it was a type parameter instead?

Sympatron commented 3 years ago

You could add NewType wrappers around your buffer type and implement WriteTarget on those. The trait could be changed, too, but this seems to be a rather niche use case.

jonas-schievink commented 3 years ago

I'm not sure how I would turn a Box<Buffer> into a Box<NewtypedBuffer> though, at least when Box is some library-provided smart pointer and not alloc::boxed::Box. For example heapless' pool::singleton::Box is generic over the pool it came from, not the pointee, so I don't see a convenient way to use a newtype there.