#reference #variables #container #key #opaque #mutable #object

no-std cursedcontainer

A "cursed" container with opaque keys, usable as a static variable

3 releases

0.1.2 Dec 19, 2021
0.1.1 Dec 18, 2021
0.1.0 Dec 18, 2021

#12 in #opaque

MIT license

9KB
79 lines

cursedcontainer

Crates.io Workflow status

Please, for the love of all things good in the world, do not use this crate unless you've read the code thoroughly and understand the implications of using such an utterly unsafe piece of software. You have been warned.

A "cursed" container with an opaque key type, that allows for retrieving mutable references to the objects contained within.

The CursedContainer is a synchronized init-on-first-use Vec<T> wrapper, where the objects within the inner Vec are themselves contained within an UnsafeCell, allowing for retrieval of mutable references to those objects without a mutable reference to the CursedContainer itself.

This design allows for assigning a CursedContainer to a static variable, like so:

static CONTAINER: CursedContainer<usize> = CursedContainer::new();

let key = CONTAINER.insert(69420);
assert_eq!(CONTAINER.get(key), Some(&mut 69420));

Safety

Hahah, good joke.

There is some safety built into CursedContainer around initialization race conditions, but accessing items within the container is unsafe by design - it allows for retrieving multiple mutable references to the same object stored within the container.

It is the responsibility of your application code to make sure that things don't go horribly wrong when using a CursedContainer.

But... why?

This crate was developed for the author's hobby operating system project, and the lack of safety in here is designed for that purpose.

License

cursedcontainer is licensed under the MIT license, the text of which can be found in the LICENSE file, or at https://opensource.org/licenses/MIT.

Dependencies

~150KB