In your second version, you use the type Box<&'a mut [T]>
, which means there are two levels of indirection to reach a T
, because both Box
and &
are pointers.
What you want instead is a Box<[T]>
. I think the only sane way to construct such a value is from a Vec<T>
, using the into_boxed_slice
method. Note that the only benefit is that you lose the capacity
field that a Vec
would have. Unless you need to have a lot of these arrays in memory at the same time, the overhead is likely to be insignificant.
pub fn linspace_vec<'a, T: 'a>(start: T, stop: T, len: usize) -> Box<[T]>
where
T: Float,
{
// get 0, 1 and the increment dx as T
let (one, zero, dx) = get_values_as_type_t::<T>(start, stop, len);
let mut v = vec![zero; len].into_boxed_slice();
let mut c = zero;
let ptr: *mut T = v.as_mut_ptr();
unsafe {
for ii in 0..len {
let x = ptr.offset((ii as isize));
*x = start + c * dx;
c = c + one;
}
}
v
}