Expand description
Reuse alloations when mapping the elements of a Vec
, Box<[T]>
or Box<T>
if possible.
To map in place the types must have identical alignment and:
- for boxes and boxed slices the sizes must be equal,
- for vectors the size of in must be a multiple of the out type. so out cannot be bigger than in)
The ..._in_place()
methods will panic if not possible,
while the others will fall back to iterating and collecting.
I might add methods to the traits without a default impl or a major version bump; implement them for other types at your own risk.
§Examples:
extern crate map_in_place;
use map_in_place::MapVecInPlace;
fn main() {
let v = vec![8_u32,29,14,5];
let v = v.filter_map(|n| if n < 10 {Some( (n as u8+b'0') as char)}
else {None}
);// happens in place
assert_eq!(&v, &['8','5']);
let v = v.map(|c| c as u8);// falls back to iterators
assert_eq!(&v[..], &b"85"[..]);
}