module::math_adapter
Collection of math adapters to decouple your application from math libraries' implementations and to provide both inter-libraries compatibility and affordable exchangeability.
Conversion vs reinterpretation
To apply functions from another library you may either convert or reinterpret math object. What is difference?
Reinterpretation take place during compile-time. Reinterpretation is possible if layout( size, alignment, padding and order ) of two similar structures are the same. For example structures cgmath::Vector2 and nalgebra::Vector2 are different structures, but them both have exactly same layout. That's why it's safe to reinterpret one to another and vise-versa. Reinterpretation says compiler to use one data structure as if it was another. It has zero run-time and compile-time cost. Such methods as as_native(), as_native_mut(), as_cgmath(), as_cgmath_mut(), as_nalgebra(), as_nalgebra_mut() reintepret math objects.
Conversion is different. Conversion says to rebuild a new instance of structure from components of another. It has non-zero run-time and compile-time cost. Although it is often optimized into reinterpretation by compiler. Also argument to use conversion instead of reinterpretation is safety. Thing is reinterpretation is safe based on several assumption about layout, which may be changed by either an author of a math library or by authors of the compiler. In theory! On practice it is unlikely. Even more most math objects are declared with #[ repr( C ) ], what restricts layout of such structure and protects it from changes in the future.
Every structure could be converted into another semantically similar structure even with different layout, but reinterpreation is possible only in case of the same layour. Because of that severa traits are implemented.
*NominalInterface- interface exposing function to convert and to access elements.*BasicInterface- interface extendingX2NominalInterfaceand exposing functions to make a new instance of such.*CanonicalInterface- interface extendingX2BasicInterfaceand exposing functions of reinterpretation.
Relation: Canonical > Basic > Nominal.
Sample : elements
Number of elements of a vector is coded in name of type, X2 for vector of length 2, X3 for vector of length 3 and so on. Each structure implements constructors make(), make_nan(), and make_default() to construct a new instance of the type. To get access to elements use either methods x(), y(), z() or _0(), _1(), _2().
use *;
use X2;
Sample : operators
Select a feature *_ops to reuse operators and function of math lib of choice.
use *;
use X2;
Sample : interoperability
Feature *_ops means to request to use operators and function of math lib of choice. But instead of choosing a single back-end math lib, you may use several. Use methods as_*_clone(), as_*, as_*_mut to either convert or reinterpret original math object into analog of such of chosen back-end. You don't have to use the same back-end in every call, you may choose which math lib for a specific call and combine the best of each math lib.
use *;
use X2;
To add to your project
Try out from the repository