Mint vector conversions

This commit is contained in:
Dzmitry Malyshau 2017-07-25 11:04:12 -04:00 committed by Sébastien Crozet
parent 3a1fb0ed88
commit 7d96007bdb
4 changed files with 76 additions and 2 deletions

View File

@ -22,9 +22,10 @@ script:
- cargo --version - cargo --version
- cargo build --verbose - cargo build --verbose
- cargo build --verbose --features arbitrary - cargo build --verbose --features arbitrary
- cargo build --verbose --features mint
- cargo build --verbose --features serde-serialize - cargo build --verbose --features serde-serialize
- cargo build --verbose --features abomonation-serialize - cargo build --verbose --features abomonation-serialize
- cargo test --verbose --features "debug arbitrary serde-serialize abomonation-serialize" - cargo test --verbose --features "debug arbitrary mint serde-serialize abomonation-serialize"
- cd nalgebra-lapack; cargo test --verbose - cd nalgebra-lapack; cargo test --verbose
env: env:

View File

@ -32,7 +32,8 @@ alga = "0.5"
matrixmultiply = "0.1" matrixmultiply = "0.1"
serde = { version = "1.0", optional = true } serde = { version = "1.0", optional = true }
serde_derive = { version = "1.0", optional = true } serde_derive = { version = "1.0", optional = true }
abomonation = { version = "0.4", optional = true } abomonation = { version = "0.4", optional = true }
mint = { version = "0.4.2", optional = true }
[dependencies.quickcheck] [dependencies.quickcheck]
optional = true optional = true

View File

@ -2,6 +2,8 @@ use std::ptr;
use std::mem; use std::mem;
use std::convert::{From, Into, AsRef, AsMut}; use std::convert::{From, Into, AsRef, AsMut};
use alga::general::{SubsetOf, SupersetOf}; use alga::general::{SubsetOf, SupersetOf};
#[cfg(feature = "mint")]
use mint;
use core::{DefaultAllocator, Scalar, Matrix, MatrixMN}; use core::{DefaultAllocator, Scalar, Matrix, MatrixMN};
use core::dimension::{Dim, use core::dimension::{Dim,
@ -216,3 +218,70 @@ impl_from_into_asref_2D!(
(U5, U2) => (5, 2); (U5, U3) => (5, 3); (U5, U4) => (5, 4); (U5, U5) => (5, 5); (U5, U6) => (5, 6); (U5, U2) => (5, 2); (U5, U3) => (5, 3); (U5, U4) => (5, 4); (U5, U5) => (5, 5); (U5, U6) => (5, 6);
(U6, U2) => (6, 2); (U6, U3) => (6, 3); (U6, U4) => (6, 4); (U6, U5) => (6, 5); (U6, U6) => (6, 6); (U6, U2) => (6, 2); (U6, U3) => (6, 3); (U6, U4) => (6, 4); (U6, U5) => (6, 5); (U6, U6) => (6, 6);
); );
#[cfg(feature = "mint")]
macro_rules! impl_from_into_mint_1D(
($($NRows: ident => $VT:ident [$SZ: expr]);* $(;)*) => {$(
impl<N, S> From<mint::$VT<N>> for Matrix<N, $NRows, U1, S>
where N: Scalar,
S: OwnedStorage<N, $NRows, U1>,
S::Alloc: OwnedAllocator<N, $NRows, U1, S> {
#[inline]
fn from(v: mint::$VT<N>) -> Self {
unsafe {
let mut res = Self::new_uninitialized();
ptr::copy_nonoverlapping(&v.x, res.data.ptr_mut(), $SZ);
res
}
}
}
impl<N, S> Into<mint::$VT<N>> for Matrix<N, $NRows, U1, S>
where N: Scalar,
S: OwnedStorage<N, $NRows, U1>,
S::Alloc: OwnedAllocator<N, $NRows, U1, S> {
#[inline]
fn into(self) -> mint::$VT<N> {
unsafe {
let mut res: mint::$VT<N> = mem::uninitialized();
ptr::copy_nonoverlapping(self.data.ptr(), &mut res.x, $SZ);
res
}
}
}
impl<N, S> AsRef<mint::$VT<N>> for Matrix<N, $NRows, U1, S>
where N: Scalar,
S: OwnedStorage<N, $NRows, U1>,
S::Alloc: OwnedAllocator<N, $NRows, U1, S> {
#[inline]
fn as_ref(&self) -> &mint::$VT<N> {
unsafe {
mem::transmute(self.data.ptr())
}
}
}
impl<N, S> AsMut<mint::$VT<N>> for Matrix<N, $NRows, U1, S>
where N: Scalar,
S: OwnedStorage<N, $NRows, U1>,
S::Alloc: OwnedAllocator<N, $NRows, U1, S> {
#[inline]
fn as_mut(&mut self) -> &mut mint::$VT<N> {
unsafe {
mem::transmute(self.data.ptr_mut())
}
}
}
)*}
);
// Implement for vectors of dimension 2 .. 4.
#[cfg(feature = "mint")]
impl_from_into_mint_1D!( // Column vectors.
U2 => Vector2[2];
U3 => Vector3[3];
U4 => Vector4[4];
);

View File

@ -100,6 +100,9 @@ extern crate serde_derive;
#[cfg(feature = "abomonation-serialize")] #[cfg(feature = "abomonation-serialize")]
extern crate abomonation; extern crate abomonation;
#[cfg(feature = "mint")]
extern crate mint;
extern crate num_traits as num; extern crate num_traits as num;
extern crate num_complex; extern crate num_complex;
extern crate rand; extern crate rand;