diff --git a/.travis.yml b/.travis.yml index ffc7cfa5..add11426 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,9 +22,10 @@ script: - cargo --version - cargo build --verbose - cargo build --verbose --features arbitrary + - cargo build --verbose --features mint - cargo build --verbose --features serde-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 env: diff --git a/Cargo.toml b/Cargo.toml index 9152a0a1..17edf061 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,7 +32,8 @@ alga = "0.5" matrixmultiply = "0.1" serde = { 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] optional = true diff --git a/src/core/conversion.rs b/src/core/conversion.rs index 738857d5..247a7ee5 100644 --- a/src/core/conversion.rs +++ b/src/core/conversion.rs @@ -2,6 +2,8 @@ use std::ptr; use std::mem; use std::convert::{From, Into, AsRef, AsMut}; use alga::general::{SubsetOf, SupersetOf}; +#[cfg(feature = "mint")] +use mint; use core::{DefaultAllocator, Scalar, Matrix, MatrixMN}; 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); (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 From> for Matrix + where N: Scalar, + S: OwnedStorage, + S::Alloc: OwnedAllocator { + #[inline] + fn from(v: mint::$VT) -> Self { + unsafe { + let mut res = Self::new_uninitialized(); + ptr::copy_nonoverlapping(&v.x, res.data.ptr_mut(), $SZ); + + res + } + } + } + + impl Into> for Matrix + where N: Scalar, + S: OwnedStorage, + S::Alloc: OwnedAllocator { + #[inline] + fn into(self) -> mint::$VT { + unsafe { + let mut res: mint::$VT = mem::uninitialized(); + ptr::copy_nonoverlapping(self.data.ptr(), &mut res.x, $SZ); + + res + } + } + } + + impl AsRef> for Matrix + where N: Scalar, + S: OwnedStorage, + S::Alloc: OwnedAllocator { + #[inline] + fn as_ref(&self) -> &mint::$VT { + unsafe { + mem::transmute(self.data.ptr()) + } + } + } + + impl AsMut> for Matrix + where N: Scalar, + S: OwnedStorage, + S::Alloc: OwnedAllocator { + #[inline] + fn as_mut(&mut self) -> &mut mint::$VT { + 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]; +); diff --git a/src/lib.rs b/src/lib.rs index 6a87d081..2bcdf063 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -100,6 +100,9 @@ extern crate serde_derive; #[cfg(feature = "abomonation-serialize")] extern crate abomonation; +#[cfg(feature = "mint")] +extern crate mint; + extern crate num_traits as num; extern crate num_complex; extern crate rand;