rust-embedded / embedded-dma

Apache License 2.0
35 stars 12 forks source link

No way to unsafely implement `Static{Read,Write}Buffer` #17

Closed andrewgazelka closed 2 years ago

andrewgazelka commented 2 years ago

According to the documentation, the end-user should be able to implement their own Static{Read,Write}Buffer for a non-static type. However, due to orphan rules, I do not think this is possible.

Take the case of StaticReadBuffer

pub unsafe trait StaticReadBuffer: ReadBuffer { }

If we want to implement this for our type we must also implement ReadBuffer, but there is already a blanket implementation

unsafe impl<B: WriteBuffer + 'static> StaticWriteBuffer for B {
    type Word = <Self as WriteBuffer>::Word;

    unsafe fn static_write_buffer(&mut self) -> (*mut <Self as StaticWriteBuffer>::Word, usize) {
        self.write_buffer()
    }
}

and afaik there is no way to denote a type as non-static. Ideally the end-user would be able to

unsafe impl StaticWriteBuffer for NotReallyStatic<'a> where 'a!: 'static {
  // ...
}

where ! is not.

Proposal

Remove the blanket implementations and perhaps only implement StaticWriteBuffer for slices and semi-concrete types.

andrewgazelka commented 2 years ago

This is also fixed if #13 is accepted