Rustmex
A library providing convenient Rust bindings to Matlab's MEX C api.
Rustmex makes writing MEX functions in Rust a bit easier. It convert Matlab types, and
the arguments it provides to mexFunction into more Rusty types, which can then be used
to interface with other Rust code easily.
For installation and documtoentation & linkage examples, refer to the documentation.
v0.6 Update Notes
Rustmex v0.6 constitutes a major redesign of the crate. It has been broken up into a set of crates, such that each crate is compilable separately; feature flags are no longer required for Rustmex itself to compile. However, the easiest installation method is still to use feature flags to select the appropriate backend; confer to the documentation of the backend module for instructions. The 'normal' user facing API of Rustmex should however still mostly be the same as before; only the internals have changed.
Usage Examples
This section contains some examples of how Rustmex can be used. Note however that these are not tested, since testing MEX functions involve starting a Matlab/Octave interpreter, and so this cannot be done automatically. Future versions of Rustmex will come with runnable examples.
That being said, each MEX function file has an entrypoint called mexFunction. This is a
C FFI function, which, preferably, you do not want to write yourself. Instead, rustmex
provides the entrypoint macro; a macro to mark your Rust entrypoint with. For example:
use *;
Note that this example mirrors the definition of mexFunction itself: Matlab has already
allocated a return value array, you just need to place your results into it.
The FromMatlabError is for when you want to convert an mxArray into a more Rusty data
type. These conversions are not infallible: Matlab is dynamically typed, and the provided
mxArray must have the same type and size as the type we want to convert it into.
As a more convoluted example:
use *;
This example computes the Euclidean length of an input vector. Note that type annotations
are (almost always) needed for the return type of data_slice() and from_matlab().
Regarding [FromMatlab][convert::FromMatlab], this library also supports building
NDarrays from mxArrays. It will ensure that NDarray understands the data the same way
matlab does. For example, the following prints out a debug representation of the array:
use *;
use ArrayViewD;
Calling back into Matlab is also supported. For example, to compute the square root of the sum of squares (i.e. nd-pythagoras):
% Call Rust MEX file
=
use *
use Function;
prints:
x = 19.621
If you assume something about the data you receive, but it might not yet be in the right
shape, you might want to reshape. Rustmex enables ergonomically reshaping data: you can
apply the ? operator on a Result with a ShapeError in functions which return a
rustmex::Result:
use *;
use ;
Licence
This is licensed to you under the Mozilla Public License, version 2. You can the licence in the LICENCE file in this project's source tree root folder.
Authors
- Niels ter Meer (maintainer)