Function inplace_it::inplace
source · Expand description
Places uninitialized memory for the T
type on the stack
and passes the reference to it into the consumer
closure.
This function is being used for placing fixed-size arrays
on the stack in cases when the type
of array ([T; $size]
generically)
is selects in runtime.
R
type is used to pass the result of the consumer
back when it return
control back.
consumer
’s result will be returned.
Example
use inplace_it::inplace;
unsafe {
inplace(|memory: &mut [u8; 12]| {
for i in 0u8..12 {
memory[i as usize] = i;
}
let mut sum = 0;
for i in 0..12 {
sum += memory[i];
}
assert_eq!(sum, 66);
});
}
Safety
Because of some purposes we don’t want to initialize the memory allocated
on the stack so we use core::mem::uninitialized
which is unsafe
so inplace
is unsafe too.