News for hackers. Stuff that matters.
Re:Allowing Matlab to Talk to Rust by masklinn on Saturday February 06, 2016 @10:32AM (#11048105)
There are plenty of other things to improve/rustify.

* The function should take slices as inputs for instance, that way not only doesn't it take ownership of the inputs you don't need to copy the C arrays to vecs in the C-exposed function, you can just convert them to slice (which is essentially 0-cost, you just create a std::raw::Slice on the stack)

* The function also shouldn't panic it should return a Result (or an Option)

* Maybe less rustic but more efficient would be to take the output as an &mut [f64] (as the extern function does), that way the whole thing can be allocation-free

1 and 3 also simplify the glue code, instead of having to copy data from the input f64 to brand new vecs and from the output vec to the output f64, just convert the three input pointers with from_raw_parts and from_raw_parts_mut. That makes for much simpler and easier to review unsafe code as well:

    let a = unsafe { from_raw_parts(a_double, elements) };
    let b = unsafe { from_raw_parts(b_double, elements) };
    let c = unsafe { from_raw_parts_mut(c_double, elements) };
that's it, no mucking around with pointer offsets, and the rest if safe Rust.

Something like this: http://is.gd/uFhx2J

There are no comments on this comment yet.