init_cell 0.1.0

A safe-to-access, unsafely-initialized cell.
Documentation
  • Coverage
  • 100%
    7 out of 7 items documented1 out of 7 items with examples
  • Size
  • Source code size: 18.84 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 997.11 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • meltah/init_cell
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • meltah

Crates.io

init_cell

A cell which can be unsafely initialized or interiorly mutated, but safely accessed.

This is mostly intended for use in statics. The cell is safe to access, but must be initialized before any access. There is no synchronization to ensure initialization is observed, so you should initialize at the beginning of the main function or using something like the ctor crate.

Example

use init_cell::InitCell;

// SAFETY: We will initialize the cell before using it.
pub static MY_VAL: InitCell<Vec<i32>> = unsafe { InitCell::new() };

fn main() {
	// SAFETY: Nothing is accessing the cell.
	unsafe {
		InitCell::init(&MY_VAL, vec![1, 2, 3]);
	}
	assert_eq!(MY_VAL.iter().sum::<i32>(), 6);

	// The cell can be mutated, too, which drops the previous value.
	unsafe {
		InitCell::set(&MY_VAL, vec![4, 5, 6]);
	}
	assert_eq!(MY_VAL.iter().sum::<i32>(), 15);
}

License

Licensed under either of

at your option.

Contribution

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.