AtomicBox - Safe atomic boxes for Rust.
This crate provides AtomicBox<T>
and AtomicOptionBox<T>
types: safe, owning
versions of the standard library's AtomicPtr
.
Unfortunately, the only operations you can perform on an atomic box are
swaps and stores: you can't just use the box without taking ownership of it.
Imagine a Box
without Deref
or DerefMut
implementations, and you'll
get the idea. Still, this is sufficient for some lock-free data structures,
so here it is!
Deref
?
Why no The reason it's safe for a normal Box
to implement Deref
is apparent
from the deref
method's signature:
fn deref(&self) -> &Self::Target;
Dereferencing a box returns a reference, call it r
, whose lifetime is
enclosed by the lifetime of &self
, the borrow of the box itself. Since the
box is borrowed by a shared reference, nobody can change the box's value
while it exists. This ensures that r
remains valid throughout its
lifetime.
But the point of an atomic type is that it permits mutation even while borrowed by a shared reference. So the fact that you've got a shared reference to an atomic box doesn't mean nobody will store some new pointer in it and free the old one you're borrowing.
This is the same reason you can't borrow a reference to the contents of any other atomic type. The only difference here is that those contents happen to be on the heap.
License
AtomicBox is distributed under the terms of both the MIT license and the Apache License (Version 2.0).
See LICENSE-APACHE and LICENSE-MIT, and COPYRIGHT for details.