Update impls of abomonation.

This commit is contained in:
sebcrozet 2018-07-20 15:25:55 +02:00 committed by Sébastien Crozet
parent 0ec7dc4461
commit 378b39db9c
10 changed files with 78 additions and 54 deletions

View File

@ -1,5 +1,7 @@
use num::Zero; use num::Zero;
use num_complex::Complex; use num_complex::Complex;
#[cfg(feature = "abomonation-serialize")]
use std::io::{Result as IOResult, Write};
use approx::{AbsDiffEq, RelativeEq, UlpsEq}; use approx::{AbsDiffEq, RelativeEq, UlpsEq};
use std::any::TypeId; use std::any::TypeId;
@ -125,17 +127,17 @@ where
#[cfg(feature = "abomonation-serialize")] #[cfg(feature = "abomonation-serialize")]
impl<N: Scalar, R: Dim, C: Dim, S: Abomonation> Abomonation for Matrix<N, R, C, S> { impl<N: Scalar, R: Dim, C: Dim, S: Abomonation> Abomonation for Matrix<N, R, C, S> {
unsafe fn entomb(&self, writer: &mut Vec<u8>) { unsafe fn entomb<W: Write>(&self, writer: &mut W) -> IOResult<()> {
self.data.entomb(writer) 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]> { unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> {
self.data.exhume(bytes) self.data.exhume(bytes)
} }
fn extent(&self) -> usize {
self.data.extent()
}
} }
impl<N: Scalar, R: Dim, C: Dim, S> Matrix<N, R, C, S> { impl<N: Scalar, R: Dim, C: Dim, S> Matrix<N, R, C, S> {

View File

@ -1,29 +1,31 @@
use std::ops::{Deref, DerefMut, Mul};
use std::fmt::{self, Debug, Formatter}; use std::fmt::{self, Debug, Formatter};
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
#[cfg(feature = "abomonation-serialize")]
use std::io::{Result as IOResult, Write};
use std::ops::{Deref, DerefMut, Mul};
#[cfg(feature = "serde-serialize")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
#[cfg(feature = "serde-serialize")]
use serde::ser::SerializeSeq;
#[cfg(feature = "serde-serialize")] #[cfg(feature = "serde-serialize")]
use serde::de::{Error, SeqAccess, Visitor}; use serde::de::{Error, SeqAccess, Visitor};
#[cfg(feature = "serde-serialize")] #[cfg(feature = "serde-serialize")]
use std::mem; use serde::ser::SerializeSeq;
#[cfg(feature = "serde-serialize")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
#[cfg(feature = "serde-serialize")] #[cfg(feature = "serde-serialize")]
use std::marker::PhantomData; use std::marker::PhantomData;
#[cfg(feature = "serde-serialize")]
use std::mem;
#[cfg(feature = "abomonation-serialize")] #[cfg(feature = "abomonation-serialize")]
use abomonation::Abomonation; use abomonation::Abomonation;
use typenum::Prod;
use generic_array::{ArrayLength, GenericArray}; use generic_array::{ArrayLength, GenericArray};
use typenum::Prod;
use base::Scalar;
use base::dimension::{DimName, U1};
use base::storage::{ContiguousStorage, ContiguousStorageMut, Owned, Storage, StorageMut};
use base::allocator::Allocator; use base::allocator::Allocator;
use base::default_allocator::DefaultAllocator; use base::default_allocator::DefaultAllocator;
use base::dimension::{DimName, U1};
use base::storage::{ContiguousStorage, ContiguousStorageMut, Owned, Storage, StorageMut};
use base::Scalar;
/* /*
* *
@ -360,16 +362,12 @@ where
Prod<R::Value, C::Value>: ArrayLength<N>, Prod<R::Value, C::Value>: ArrayLength<N>,
N: Abomonation, N: Abomonation,
{ {
unsafe fn entomb(&self, writer: &mut Vec<u8>) { unsafe fn entomb<W: Write>(&self, writer: &mut W) -> IOResult<()> {
for element in self.data.as_slice() { for element in self.data.as_slice() {
element.entomb(writer); element.entomb(writer)?;
} }
}
unsafe fn embalm(&mut self) { Ok(())
for element in self.data.as_mut_slice() {
element.embalm();
}
} }
unsafe fn exhume<'a, 'b>(&'a mut self, mut bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { unsafe fn exhume<'a, 'b>(&'a mut self, mut bytes: &'b mut [u8]) -> Option<&'b mut [u8]> {
@ -383,4 +381,11 @@ where
} }
Some(bytes) Some(bytes)
} }
fn extent(&self) -> usize {
self.data
.as_slice()
.iter()
.fold(0, |acc, e| acc + e.extent())
}
} }

View File

@ -1,3 +1,5 @@
#[cfg(feature = "abomonation-serialize")]
use std::io::{Result as IOResult, Write};
use std::ops::Deref; use std::ops::Deref;
#[cfg(all(feature = "alloc", not(feature = "std")))] #[cfg(all(feature = "alloc", not(feature = "std")))]
@ -238,17 +240,17 @@ where
#[cfg(feature = "abomonation-serialize")] #[cfg(feature = "abomonation-serialize")]
impl<N: Abomonation, R: Dim, C: Dim> Abomonation for MatrixVec<N, R, C> { impl<N: Abomonation, R: Dim, C: Dim> Abomonation for MatrixVec<N, R, C> {
unsafe fn entomb(&self, writer: &mut Vec<u8>) { unsafe fn entomb<W: Write>(&self, writer: &mut W) -> IOResult<()> {
self.data.entomb(writer) 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]> { unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> {
self.data.exhume(bytes) self.data.exhume(bytes)
} }
fn extent(&self) -> usize {
self.data.extent()
}
} }
unsafe impl<N: Scalar, R: DimName> ContiguousStorage<N, R, Dynamic> for MatrixVec<N, R, Dynamic> unsafe impl<N: Scalar, R: DimName> ContiguousStorage<N, R, Dynamic> for MatrixVec<N, R, Dynamic>

View File

@ -1,4 +1,6 @@
use approx::RelativeEq; use approx::RelativeEq;
#[cfg(feature = "abomonation-serialize")]
use std::io::{Result as IOResult, Write};
use std::mem; use std::mem;
use std::ops::{Deref, Neg}; use std::ops::{Deref, Neg};
@ -42,12 +44,12 @@ impl<'de, T: Deserialize<'de>> Deserialize<'de> for Unit<T> {
#[cfg(feature = "abomonation-serialize")] #[cfg(feature = "abomonation-serialize")]
impl<T: Abomonation> Abomonation for Unit<T> { impl<T: Abomonation> Abomonation for Unit<T> {
unsafe fn entomb(&self, writer: &mut Vec<u8>) { unsafe fn entomb<W: Write>(&self, writer: &mut W) -> IOResult<()> {
self.value.entomb(writer); self.value.entomb(writer)
} }
unsafe fn embalm(&mut self) { fn extent(&self) -> usize {
self.value.embalm(); self.value.extent()
} }
unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> {

View File

@ -1,6 +1,8 @@
use approx::{AbsDiffEq, RelativeEq, UlpsEq}; use approx::{AbsDiffEq, RelativeEq, UlpsEq};
use std::fmt; use std::fmt;
use std::hash; use std::hash;
#[cfg(feature = "abomonation-serialize")]
use std::io::{Result as IOResult, Write};
use std::marker::PhantomData; use std::marker::PhantomData;
#[cfg(feature = "serde-serialize")] #[cfg(feature = "serde-serialize")]
@ -65,14 +67,13 @@ where
Translation<N, D>: Abomonation, Translation<N, D>: Abomonation,
DefaultAllocator: Allocator<N, D>, DefaultAllocator: Allocator<N, D>,
{ {
unsafe fn entomb(&self, writer: &mut Vec<u8>) { unsafe fn entomb<W: Write>(&self, writer: &mut W) -> IOResult<()> {
self.rotation.entomb(writer); self.rotation.entomb(writer)?;
self.translation.entomb(writer); self.translation.entomb(writer)
} }
unsafe fn embalm(&mut self) { fn extent(&self) -> usize {
self.rotation.embalm(); self.rotation.extent() + self.translation.extent()
self.translation.embalm();
} }
unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> {
@ -253,7 +254,8 @@ where
) -> bool { ) -> bool {
self.translation self.translation
.relative_eq(&other.translation, epsilon, max_relative) .relative_eq(&other.translation, epsilon, max_relative)
&& self.rotation && self
.rotation
.relative_eq(&other.rotation, epsilon, max_relative) .relative_eq(&other.rotation, epsilon, max_relative)
} }
} }

View File

@ -3,6 +3,8 @@ use num::One;
use std::cmp::Ordering; use std::cmp::Ordering;
use std::fmt; use std::fmt;
use std::hash; use std::hash;
#[cfg(feature = "abomonation-serialize")]
use std::io::{Result as IOResult, Write};
#[cfg(feature = "serde-serialize")] #[cfg(feature = "serde-serialize")]
use serde; use serde;
@ -92,12 +94,12 @@ where
VectorN<N, D>: Abomonation, VectorN<N, D>: Abomonation,
DefaultAllocator: Allocator<N, D>, DefaultAllocator: Allocator<N, D>,
{ {
unsafe fn entomb(&self, writer: &mut Vec<u8>) { unsafe fn entomb<W: Write>(&self, writer: &mut W) -> IOResult<()> {
self.coords.entomb(writer) self.coords.entomb(writer)
} }
unsafe fn embalm(&mut self) { fn extent(&self) -> usize {
self.coords.embalm() self.coords.extent()
} }
unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> {

View File

@ -2,6 +2,8 @@ use approx::{AbsDiffEq, RelativeEq, UlpsEq};
use num::Zero; use num::Zero;
use std::fmt; use std::fmt;
use std::hash; use std::hash;
#[cfg(feature = "abomonation-serialize")]
use std::io::{Result as IOResult, Write};
#[cfg(feature = "serde-serialize")] #[cfg(feature = "serde-serialize")]
use base::storage::Owned; use base::storage::Owned;
@ -33,12 +35,12 @@ impl<N: Real> Abomonation for Quaternion<N>
where where
Vector4<N>: Abomonation, Vector4<N>: Abomonation,
{ {
unsafe fn entomb(&self, writer: &mut Vec<u8>) { unsafe fn entomb<W: Write>(&self, writer: &mut W) -> IOResult<()> {
self.coords.entomb(writer) self.coords.entomb(writer)
} }
unsafe fn embalm(&mut self) { fn extent(&self) -> usize {
self.coords.embalm() self.coords.extent()
} }
unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> {

View File

@ -2,6 +2,8 @@ use approx::{AbsDiffEq, RelativeEq, UlpsEq};
use num::{One, Zero}; use num::{One, Zero};
use std::fmt; use std::fmt;
use std::hash; use std::hash;
#[cfg(feature = "abomonation-serialize")]
use std::io::{Result as IOResult, Write};
#[cfg(feature = "serde-serialize")] #[cfg(feature = "serde-serialize")]
use serde; use serde;
@ -64,12 +66,12 @@ where
MatrixN<N, D>: Abomonation, MatrixN<N, D>: Abomonation,
DefaultAllocator: Allocator<N, D, D>, DefaultAllocator: Allocator<N, D, D>,
{ {
unsafe fn entomb(&self, writer: &mut Vec<u8>) { unsafe fn entomb<W: Write>(&self, writer: &mut W) -> IOResult<()> {
self.matrix.entomb(writer) self.matrix.entomb(writer)
} }
unsafe fn embalm(&mut self) { fn extent(&self) -> usize {
self.matrix.embalm() self.matrix.extent()
} }
unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> {

View File

@ -1,6 +1,8 @@
use approx::{AbsDiffEq, RelativeEq, UlpsEq}; use approx::{AbsDiffEq, RelativeEq, UlpsEq};
use std::fmt; use std::fmt;
use std::hash; use std::hash;
#[cfg(feature = "abomonation-serialize")]
use std::io::{Result as IOResult, Write};
#[cfg(feature = "serde-serialize")] #[cfg(feature = "serde-serialize")]
use serde; use serde;
@ -58,12 +60,12 @@ where
Isometry<N, D, R>: Abomonation, Isometry<N, D, R>: Abomonation,
DefaultAllocator: Allocator<N, D>, DefaultAllocator: Allocator<N, D>,
{ {
unsafe fn entomb(&self, writer: &mut Vec<u8>) { unsafe fn entomb<W: Write>(&self, writer: &mut W) -> IOResult<()> {
self.isometry.entomb(writer) self.isometry.entomb(writer)
} }
unsafe fn embalm(&mut self) { fn extent(&self) -> usize {
self.isometry.embalm() self.isometry.extent()
} }
unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> {
@ -328,7 +330,8 @@ where
) -> bool { ) -> bool {
self.isometry self.isometry
.relative_eq(&other.isometry, epsilon, max_relative) .relative_eq(&other.isometry, epsilon, max_relative)
&& self.scaling && self
.scaling
.relative_eq(&other.scaling, epsilon, max_relative) .relative_eq(&other.scaling, epsilon, max_relative)
} }
} }

View File

@ -2,6 +2,8 @@ use approx::{AbsDiffEq, RelativeEq, UlpsEq};
use num::{One, Zero}; use num::{One, Zero};
use std::fmt; use std::fmt;
use std::hash; use std::hash;
#[cfg(feature = "abomonation-serialize")]
use std::io::{Result as IOResult, Write};
#[cfg(feature = "serde-serialize")] #[cfg(feature = "serde-serialize")]
use serde; use serde;
@ -64,12 +66,12 @@ where
VectorN<N, D>: Abomonation, VectorN<N, D>: Abomonation,
DefaultAllocator: Allocator<N, D>, DefaultAllocator: Allocator<N, D>,
{ {
unsafe fn entomb(&self, writer: &mut Vec<u8>) { unsafe fn entomb<W: Write>(&self, writer: &mut W) -> IOResult<()> {
self.vector.entomb(writer) self.vector.entomb(writer)
} }
unsafe fn embalm(&mut self) { fn extent(&self) -> usize {
self.vector.embalm() self.vector.extent()
} }
unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> {