map_box 0.2.2

Map the value in a Box, re-using the allocation when possible.
Documentation
  • Coverage
  • 16.67%
    1 out of 6 items documented0 out of 3 items with examples
  • Size
  • Source code size: 7.98 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 245.82 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • JonathanBrouwer/map_box
    2 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • JonathanBrouwer

Map Box

githubcrates-iodocs-rs

Map the value in a Box, re-using the allocation when possible.

For example, this code will not re-allocate.

use map_box::Map;

let b = Box::new(42u64);
let b = b.map_box(|v| v as i64);

The signature of map is:

impl<T1> Box<T1> {
    fn map_box<T2>(self, f: impl FnMut(T1) -> T2) -> Box<T2>;
}

Limitations

If the alignment requirements of the type changes, even if the alignment becomes lower, the Box needs to be reallocated. This is because:

  1. alloc::dealloc requires the layout to be identical to the layout that the allocation was made with
  2. alloc::realloc only takes a new size, it cannot change the layout of the allocation downwards