Expand description
§EmptyBox, a way to safely move values in and out of Boxs without
reallocations
EmptyBox is similar to a statically checked Box<Option<T>>:
use empty_box::EmptyBox;
// A box with a string!
let boxed = Box::new("Hello!".to_string());
// Oh no, we don't like that string.
let (string, empty) = EmptyBox::take(boxed);
// Let's make an objectively superior string, and put it into the original
// box.
let superior = "Objectively superior string!".to_string();
// Now we have our superior string in the box!
let boxed = empty.put(superior);
assert_eq!("Hello!", string);
assert_eq!("Objectively superior string!", &*boxed);Creating an EmptyBox from a Box and then putting a T back into the
EmptyBox will avoid allocating a new Box, instead reusing whatever old
Box the T was EmptyBox::taken from.
Structs§
- Empty
Box - An “emptied”
Box. Constructed viaEmptyBox::take(), anEmptyBox<T>is aBoxfrom which the contents have been moved. This allows for reuse of theBoxviaEmptyBox::put(), which moves the contents back in, turning theEmptyBoxback into aBox<T>.