Re: [PATCH v3 2/2] rust: add dma coherent allocator abstraction.

From: Alice Ryhl
Date: Mon Nov 04 2024 - 05:01:06 EST


On Mon, Nov 4, 2024 at 10:36 AM Abdiel Janulgue
<abdiel.janulgue@xxxxxxxxx> wrote:
>
>
>
> On 04/11/2024 11:31, Alice Ryhl wrote:
> > On Mon, Nov 4, 2024 at 10:07 AM Abdiel Janulgue
> > <abdiel.janulgue@xxxxxxxxx> wrote:
> >> +/// Abstraction of dma_alloc_coherent
> >> +///
> >> +/// # Invariants
> >> +///
> >> +/// For the lifetime of an instance of CoherentAllocation, the cpu address is a valid pointer
> >> +/// to an allocated region of consistent memory and we hold a reference to the device.
> >> +pub struct CoherentAllocation<T: Add> {
> >
> > Requiring `T: Add` is very unusual. Why?
> >
> > I don't even see any additions anywhere.
> >
> > Alice
>
> Background here:
> https://lore.kernel.org/all/ee45ae5f-133d-4d38-bb4a-d3515790feb4@xxxxxxxxx/
>
> Basically this aims to restrict the abstraction to non-ZST types. Are
> there better (clever) ways to do this?

That doesn't work:

struct MyZST {}

impl Add for MyZST {
type Output = MyZST;
fn add(&self, rhs: MyZST) -> MyZST {
MyZST
}
}

You'll need your own trait, or you can build_assert! that the size is
non-zero in the constructor. Also, you'll need to require the
FromBytes/AsBytes traits because you're converting the values to/from
bytes, which is not safe to do for all types.

Alice