box_swap

Crates.iobox_swap
lib.rsbox_swap
version1.1.1
created_at2025-05-18 03:12:46.898834+00
updated_at2025-05-18 04:19:19.759619+00
descriptionAn atomic verison of Option>.
homepage
repositoryhttps://github.com/HomelikeBrick42/box_swap
max_upload_size
id1678345
size5,738
(HomelikeBrick42)

documentation

README

An atomic verison of Option<Box<T>>.

Latest Version Rust Documentation GitHub license

One use case for this is being able to have one thread send many updates to another, but the other thread only cares about looking at the latest value.

use box_swap::BoxSwap;

struct Data {}

let value: BoxSwap<Data> = BoxSwap::empty();
std::thread::scope(|s| {
    s.spawn(|| {
        loop {
            if let Some(new_value) = value.take() {
                // update the value, maybe if this was a renderer you would upload the value to the gpu
                // if multiple values were "sent" before this happens, it doesnt matter, `new_value` is the most up-to-date value
            }

            // do whatever
        }
    });

    loop {
        // do whatever computation, maybe this could be a game loop

        value.store(Box::new(Data {})); // send the data to the other thread
    }
});
Commit count: 4

cargo fmt