3 releases (1 stable)
Uses new Rust 2021
|1.0.0||Aug 22, 2022|
|0.0.0||Aug 21, 2022|
#249 in Rust patterns
6,668 downloads per month
Used in fewer than 8 crates
Statically allocated, initialized at runtime cell.
StaticCell provides a no-std-compatible, no-alloc way to reserve memory at compile time for
a value, but initialize it at runtime, and get a
'static reference to it.
This is useful in the following scenarios:
- You need
&'static T, but
Tcan't be constructed in const context so you can't simply use a
- You need
&'static mut T, not just
use static_cell::StaticCell; // Statically allocate memory for a `u32`. static SOME_INT: StaticCell<u32> = StaticCell::new(); // Initialize it at runtime. This returns a `&'static mut`. let x: &'static mut u32 = SOME_INT.init(42); assert_eq!(*x, 42); // Trying to call `.init()` again would panic, because the StaticCell is already initialized. // SOME_INT.init(42);
- If you can use
alloc, you can use
- If you're OK with
unsafe, you can use
static mut THING: MaybeUninit<T>.
Minimum Supported Rust Version (MSRV)
This crate is guaranteed to compile on stable Rust 1.56 and up. It might compile with older versions but that may change in any new patch release.
This work is licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.