Implement Abomonation for static matrices
This commit is contained in:
parent
e0cc7ff21b
commit
65fa4cf740
|
@ -18,6 +18,7 @@ path = "src/lib.rs"
|
|||
[features]
|
||||
arbitrary = [ "quickcheck" ]
|
||||
serde-serialize = [ "serde", "serde_derive", "num-complex/serde" ]
|
||||
abomonation-serialize = [ "abomonation", "abomonation_derive" ]
|
||||
|
||||
[dependencies]
|
||||
typenum = "1.4"
|
||||
|
@ -29,6 +30,8 @@ approx = "0.1"
|
|||
alga = "0.5"
|
||||
serde = { version = "1.0", optional = true }
|
||||
serde_derive = { version = "1.0", optional = true }
|
||||
abomonation = { version = "0.4", optional = true }
|
||||
abomonation_derive = { version = "0.2", optional = true }
|
||||
# clippy = "*"
|
||||
|
||||
[dependencies.quickcheck]
|
||||
|
|
|
@ -10,6 +10,9 @@ use approx::ApproxEq;
|
|||
#[cfg(feature = "serde-serialize")]
|
||||
use serde::{Serialize, Serializer, Deserialize, Deserializer};
|
||||
|
||||
#[cfg(feature = "abomonation-serialize")]
|
||||
use abomonation::Abomonation;
|
||||
|
||||
use alga::general::{Ring, Real};
|
||||
|
||||
use core::{Scalar, Unit};
|
||||
|
@ -122,6 +125,21 @@ impl<'de, N, R, C, S> Deserialize<'de> for Matrix<N, R, C, S>
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "abomonation-serialize")]
|
||||
impl<N: Scalar, R: Dim, C: Dim, S: Abomonation> Abomonation for Matrix<N, R, C, S> {
|
||||
unsafe fn entomb(&self, writer: &mut Vec<u8>) {
|
||||
self.data.entomb(writer)
|
||||
}
|
||||
|
||||
unsafe fn embalm(&mut self) {
|
||||
self.data.embalm()
|
||||
}
|
||||
|
||||
unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> {
|
||||
self.data.exhume(bytes)
|
||||
}
|
||||
}
|
||||
|
||||
impl<N: Scalar, R: Dim, C: Dim, S> Matrix<N, R, C, S> {
|
||||
/// Creates a new matrix with the given data without statically checking that the matrix
|
||||
/// dimension matches the storage dimension.
|
||||
|
|
|
@ -13,6 +13,9 @@ use std::mem;
|
|||
#[cfg(feature = "serde-serialize")]
|
||||
use std::marker::PhantomData;
|
||||
|
||||
#[cfg(feature = "abomonation-serialize")]
|
||||
use abomonation::Abomonation;
|
||||
|
||||
use typenum::Prod;
|
||||
use generic_array::{ArrayLength, GenericArray};
|
||||
|
||||
|
@ -299,3 +302,24 @@ where N: Scalar + Deserialize<'a>,
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "abomonation-serialize")]
|
||||
impl<N, R, C> Abomonation for MatrixArray<N, R, C>
|
||||
where R: DimName,
|
||||
C: DimName,
|
||||
R::Value: Mul<C::Value>,
|
||||
Prod<R::Value, C::Value>: ArrayLength<N>,
|
||||
N: Abomonation
|
||||
{
|
||||
unsafe fn entomb(&self, writer: &mut Vec<u8>) {
|
||||
self.data.as_slice().entomb(writer)
|
||||
}
|
||||
|
||||
unsafe fn embalm(&mut self) {
|
||||
self.data.as_slice().embalm()
|
||||
}
|
||||
|
||||
unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> {
|
||||
self.data.as_slice().exhume(bytes)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -94,6 +94,8 @@ extern crate serde;
|
|||
#[cfg(feature = "serde")]
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
#[cfg(feature = "abomonation")]
|
||||
extern crate abomonation;
|
||||
extern crate num_traits as num;
|
||||
extern crate num_complex;
|
||||
extern crate rand;
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
extern crate rand;
|
||||
extern crate nalgebra;
|
||||
extern crate abomonation;
|
||||
|
||||
use rand::random;
|
||||
use abomonation::{Abomonation, encode, decode};
|
||||
use nalgebra::Matrix3x4;
|
||||
|
||||
#[test]
|
||||
fn abomonate_matrix3x4() {
|
||||
assert_encode_and_decode(&random::<Matrix3x4<f32>>());
|
||||
}
|
||||
|
||||
fn assert_encode_and_decode<T: Abomonation + PartialEq>(data: &T) {
|
||||
let mut bytes = Vec::new();
|
||||
unsafe { encode(data, &mut bytes); }
|
||||
|
||||
if let Some((result, rest)) = unsafe { decode::<T>(&mut bytes) } {
|
||||
assert!(result == data);
|
||||
assert!(rest.len() == 0, "binary data was not decoded completely");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue