Crates.io | cursedcontainer |
lib.rs | cursedcontainer |
version | 0.1.2 |
source | src |
created_at | 2021-12-18 07:59:51.066888 |
updated_at | 2021-12-19 00:22:51.041738 |
description | A "cursed" container with opaque keys, usable as a static variable |
homepage | |
repository | https://github.com/u1f408/cursedcontainer |
max_upload_size | |
id | 499938 |
size | 10,912 |
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));
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
.
This crate was developed for the author's hobby operating system project, and the lack of safety in here is designed for that purpose.
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.