I’m porting old C audio processing code to Rust. All over the place there are functions that receive two-dimensional float arrays (float**), along with their respective lengths (in other words, audio sample buffers, with their number of channels and samples).
In C those are accessed like
for (int c=0; c<channels; c++) {
for (int s=0; s<samples; s++) {
buffer[c][s] = 0.0f;
}
}
In Rust, I suppose one could go with the usual pointer arithmetic (*(*buffer.add(c)).add(s)) just as well considering the length of the arrays are known at all times, but if there is a method that
- incurs no considerable extra cost/extra allocations relative to the C/pointer arithmetic method (considering the cost of bound checks negligible, since it would also incur if the function was pure Rust without FFI in the first place)
- involves no pointer arithmetic (or is at least safer than direct raw pointer manipulation)
I would like to know of such a method.
At first I assumed C arrays of arrays would translate to Rust slices of slices (&mut [&mut [f32]]) in Rust, but that conversion seems to be neither trivial nor ideal, hence the new question.