box_swap 1.1.1

An atomic verison of Option<Box<T>>.
Documentation
  • Coverage
  • 100%
    12 out of 12 items documented1 out of 12 items with examples
  • Size
  • Source code size: 5.27 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 980.59 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 10s Average build duration of successful builds.
  • all releases: 9s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • HomelikeBrick42/box_swap
    1 1 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • HomelikeBrick42

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
    }
});