embassy-rs / static-cell

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

Add `ConstInitCell` #14

Closed jamesmunns closed 5 months ago

jamesmunns commented 5 months ago

This adds a "const initialized" variant of StaticCell.

I decided to add a second type, rather than extending StaticCell, because I couldn't see a way to do this without potentially making the value non-zero-initialized.

My other plan was to change the atomicbool into an AtomicU8 with three states:

But then one of the two flavors would have to start with a non-zero init value, which could "infect" the whole type into .data instead of .bss.

ivmarkov commented 5 months ago

I think I have exactly the use case that needs this. The main, fat rs-matter structure is now const-initializable, yet it is not thread-safe (contains RefCells and Mutex<NoopRawMutex, ...> stuff), that might or might not become configurable in future. I.e. use CriticalSectionRawMutex or NoopRawMutex depending on build features - yet - the single threaded model would remain the most appealing one.

Yet, I would really like to put it in a static context with static MATTER: SomethingSomething<Matter<'static>> = SomethingSomething::new(Matter::new(...)), as this way (I hope) there would be no allocate-it-first-on-stack-and-then-move-into-rwdata-static` memory blolwups.

(Don't pay attention to the 'a lifetime. It can be 'static too, as the stuff that carries 'a can also be const-initialized this way.)

jamesmunns commented 5 months ago

btw @ivmarkov, you might like some of the "Generic over RawMutex" shenanigans I'm doing over in the new postcard-rpc PR, to allow users to pick their mutex:

https://github.com/jamesmunns/postcard-rpc/blob/5b642d959cafc9b3822a056110f7e6e948bf3d02/source/postcard-rpc/src/target_server/mod.rs#L84-L101

It looks like this at the device level:

https://github.com/jamesmunns/postcard-rpc/blob/5b642d959cafc9b3822a056110f7e6e948bf3d02/example/full-setup/firmware/src/main.rs#L34-L37

ivmarkov commented 5 months ago

@jamesmunns How about naming it StaticConstCell instead?

(I considered StaticConstInitCell too but that's a bit too long for my taste.).

Reasons:

jamesmunns commented 5 months ago

@ivmarkov I don't love it! IMO the "const" part only applies at "init", the static itself isn't const. I don't feel that strongly about it if dirbaio prefers that tho.

Dirbaio commented 5 months ago

ConstStaticCell perhaps?

Dirbaio commented 5 months ago

Released v2.1.0