Attribute Macro dst

Source
#[dst]
Expand description

§Usage:

Add #[dst] ahead of struct item as below:

#[dst]
struct Foo{
    a:u8,
    b:[usize],
}

after expansion:

use std::marker::PhantomData;
#[repr(C)]
struct Foo{
    a:u8,
    b:[usize],
}

#[repr(C)]
struct FooInit<INIT:EmplaceInitializer<Output=[usize]>>{
    a:u8,
    b:INIT,
}

#[repr(C)]
struct FooFst{
    a:u8,
    b:PhantomData<[usize]>,
}

You can also use it in nestly. With above Foo:

#[dst]
struct Bar{
    c:usize,
    d:Foo
}

For bar there will be 3 structs Bar,BarInit,BarFst after expansion. The BarInit looks like this:

#[repr(C)]
struct BarInit<INIT:EmplaceInitializer<Output=Foo>>{
    c:usize,
    d:INIT,
}

§Use Case:

  • 1 add simpler api

    we usually provide a function to create the initializer

#[dst]
struct SomePacket{
    src:u32,
    dst:u32,
    options:[u8],
}
impl SomePacket{
    fn initializer<Init:EmplaceInitializer<Output=[u8]>>(src:u32,dst:u32,init:Init)->SomePacketInit<Init>{
        SomePacketInit{
            src,
            dst,
            options:init
        }
    }
}