Function bip::map_in_place
[−]
[src]
pub fn map_in_place<T, U, F>(x: Box<T>, f: F) -> Box<U> where F: FnOnce(T) -> UExecute f on the data in x, replacing the output into the same
allocation.
This is semantically equivalent to Box::new(f(*x)), but avoids
the allocation by reusing the memory of x directly. map will
not cause unsafety or leak memory if f panics.
T and U must have the same size, and the alignment (measured
by std::mem::min_align_of) of T must be at least as large as
that of U. A violation of either of these requirements will
result in a runtime panic.
Example
let x = Box::new(1_i32); let address = &*x as *const _ as usize; let new_x = bip::map_in_place(x, |a| a as f32 + 1.0); assert_eq!(*new_x, 2.0); assert_eq!(address, &*new_x as *const _ as usize);