embassy-rs / static-cell

Statically allocated, runtime initialized cell.
Apache License 2.0
30 stars 15 forks source link

Add `StaticCell::uninit()` #3

Closed zRedShift closed 1 year ago

zRedShift commented 1 year ago

Sometimes I find myself using something that could be distilled to ARRAY.init([0; BIG_NUMBER]), where

let arr = ARRAY.uninit();
unsafe {
    arr.as_mut_ptr().write_bytes(0x00, BIG_NUMBER);
    arr.assume_init_mut()
}

Would guarantee no unnecessary memcpys or stack usage.

IsaacDynamo commented 1 year ago

I think this might already possible with the current API.

static ARRAY: StaticCell<MaybeUninit<[u8; BIG_NUMBER]>> = StaticCell::new();

let arr = ARRAY.init(MaybeUninit::uninit());
unsafe {
    arr.as_mut_ptr().write_bytes(0x00, BIG_NUMBER);
    arr.assume_init_mut()
}

But the proposed uninit() does clean-up the code above.