From 378b39db9c34ec44040e6ca357382d5220a129bf Mon Sep 17 00:00:00 2001 From: sebcrozet Date: Fri, 20 Jul 2018 15:25:55 +0200 Subject: [PATCH] Update impls of abomonation. --- src/base/matrix.rs | 12 +++++++----- src/base/matrix_array.rs | 39 +++++++++++++++++++++---------------- src/base/matrix_vec.rs | 12 +++++++----- src/base/unit.rs | 10 ++++++---- src/geometry/isometry.rs | 16 ++++++++------- src/geometry/point.rs | 8 +++++--- src/geometry/quaternion.rs | 8 +++++--- src/geometry/rotation.rs | 8 +++++--- src/geometry/similarity.rs | 11 +++++++---- src/geometry/translation.rs | 8 +++++--- 10 files changed, 78 insertions(+), 54 deletions(-) diff --git a/src/base/matrix.rs b/src/base/matrix.rs index b8c28721..fae56d70 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -1,5 +1,7 @@ use num::Zero; use num_complex::Complex; +#[cfg(feature = "abomonation-serialize")] +use std::io::{Result as IOResult, Write}; use approx::{AbsDiffEq, RelativeEq, UlpsEq}; use std::any::TypeId; @@ -125,17 +127,17 @@ where #[cfg(feature = "abomonation-serialize")] impl Abomonation for Matrix { - unsafe fn entomb(&self, writer: &mut Vec) { + unsafe fn entomb(&self, writer: &mut W) -> IOResult<()> { 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) } + + fn extent(&self) -> usize { + self.data.extent() + } } impl Matrix { diff --git a/src/base/matrix_array.rs b/src/base/matrix_array.rs index 59bdb9e0..023c7c7b 100644 --- a/src/base/matrix_array.rs +++ b/src/base/matrix_array.rs @@ -1,29 +1,31 @@ -use std::ops::{Deref, DerefMut, Mul}; use std::fmt::{self, Debug, Formatter}; 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")] use serde::de::{Error, SeqAccess, Visitor}; #[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")] use std::marker::PhantomData; +#[cfg(feature = "serde-serialize")] +use std::mem; #[cfg(feature = "abomonation-serialize")] use abomonation::Abomonation; -use typenum::Prod; 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::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: ArrayLength, N: Abomonation, { - unsafe fn entomb(&self, writer: &mut Vec) { + unsafe fn entomb(&self, writer: &mut W) -> IOResult<()> { for element in self.data.as_slice() { - element.entomb(writer); + element.entomb(writer)?; } - } - unsafe fn embalm(&mut self) { - for element in self.data.as_mut_slice() { - element.embalm(); - } + Ok(()) } unsafe fn exhume<'a, 'b>(&'a mut self, mut bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { @@ -383,4 +381,11 @@ where } Some(bytes) } + + fn extent(&self) -> usize { + self.data + .as_slice() + .iter() + .fold(0, |acc, e| acc + e.extent()) + } } diff --git a/src/base/matrix_vec.rs b/src/base/matrix_vec.rs index 3667b1ff..7f5fdd35 100644 --- a/src/base/matrix_vec.rs +++ b/src/base/matrix_vec.rs @@ -1,3 +1,5 @@ +#[cfg(feature = "abomonation-serialize")] +use std::io::{Result as IOResult, Write}; use std::ops::Deref; #[cfg(all(feature = "alloc", not(feature = "std")))] @@ -238,17 +240,17 @@ where #[cfg(feature = "abomonation-serialize")] impl Abomonation for MatrixVec { - unsafe fn entomb(&self, writer: &mut Vec) { + unsafe fn entomb(&self, writer: &mut W) -> IOResult<()> { 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) } + + fn extent(&self) -> usize { + self.data.extent() + } } unsafe impl ContiguousStorage for MatrixVec diff --git a/src/base/unit.rs b/src/base/unit.rs index d8df3e82..6924f6fd 100644 --- a/src/base/unit.rs +++ b/src/base/unit.rs @@ -1,4 +1,6 @@ use approx::RelativeEq; +#[cfg(feature = "abomonation-serialize")] +use std::io::{Result as IOResult, Write}; use std::mem; use std::ops::{Deref, Neg}; @@ -42,12 +44,12 @@ impl<'de, T: Deserialize<'de>> Deserialize<'de> for Unit { #[cfg(feature = "abomonation-serialize")] impl Abomonation for Unit { - unsafe fn entomb(&self, writer: &mut Vec) { - self.value.entomb(writer); + unsafe fn entomb(&self, writer: &mut W) -> IOResult<()> { + self.value.entomb(writer) } - unsafe fn embalm(&mut self) { - self.value.embalm(); + fn extent(&self) -> usize { + self.value.extent() } unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { diff --git a/src/geometry/isometry.rs b/src/geometry/isometry.rs index a8357181..80283c54 100644 --- a/src/geometry/isometry.rs +++ b/src/geometry/isometry.rs @@ -1,6 +1,8 @@ use approx::{AbsDiffEq, RelativeEq, UlpsEq}; use std::fmt; use std::hash; +#[cfg(feature = "abomonation-serialize")] +use std::io::{Result as IOResult, Write}; use std::marker::PhantomData; #[cfg(feature = "serde-serialize")] @@ -65,14 +67,13 @@ where Translation: Abomonation, DefaultAllocator: Allocator, { - unsafe fn entomb(&self, writer: &mut Vec) { - self.rotation.entomb(writer); - self.translation.entomb(writer); + unsafe fn entomb(&self, writer: &mut W) -> IOResult<()> { + self.rotation.entomb(writer)?; + self.translation.entomb(writer) } - unsafe fn embalm(&mut self) { - self.rotation.embalm(); - self.translation.embalm(); + fn extent(&self) -> usize { + self.rotation.extent() + self.translation.extent() } unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { @@ -253,7 +254,8 @@ where ) -> bool { self.translation .relative_eq(&other.translation, epsilon, max_relative) - && self.rotation + && self + .rotation .relative_eq(&other.rotation, epsilon, max_relative) } } diff --git a/src/geometry/point.rs b/src/geometry/point.rs index 56f8be45..6cd0a3a3 100644 --- a/src/geometry/point.rs +++ b/src/geometry/point.rs @@ -3,6 +3,8 @@ use num::One; use std::cmp::Ordering; use std::fmt; use std::hash; +#[cfg(feature = "abomonation-serialize")] +use std::io::{Result as IOResult, Write}; #[cfg(feature = "serde-serialize")] use serde; @@ -92,12 +94,12 @@ where VectorN: Abomonation, DefaultAllocator: Allocator, { - unsafe fn entomb(&self, writer: &mut Vec) { + unsafe fn entomb(&self, writer: &mut W) -> IOResult<()> { self.coords.entomb(writer) } - unsafe fn embalm(&mut self) { - self.coords.embalm() + fn extent(&self) -> usize { + self.coords.extent() } unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { diff --git a/src/geometry/quaternion.rs b/src/geometry/quaternion.rs index 41dcde24..d79997f0 100644 --- a/src/geometry/quaternion.rs +++ b/src/geometry/quaternion.rs @@ -2,6 +2,8 @@ use approx::{AbsDiffEq, RelativeEq, UlpsEq}; use num::Zero; use std::fmt; use std::hash; +#[cfg(feature = "abomonation-serialize")] +use std::io::{Result as IOResult, Write}; #[cfg(feature = "serde-serialize")] use base::storage::Owned; @@ -33,12 +35,12 @@ impl Abomonation for Quaternion where Vector4: Abomonation, { - unsafe fn entomb(&self, writer: &mut Vec) { + unsafe fn entomb(&self, writer: &mut W) -> IOResult<()> { self.coords.entomb(writer) } - unsafe fn embalm(&mut self) { - self.coords.embalm() + fn extent(&self) -> usize { + self.coords.extent() } unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { diff --git a/src/geometry/rotation.rs b/src/geometry/rotation.rs index 68a5342f..ffcb2f75 100644 --- a/src/geometry/rotation.rs +++ b/src/geometry/rotation.rs @@ -2,6 +2,8 @@ use approx::{AbsDiffEq, RelativeEq, UlpsEq}; use num::{One, Zero}; use std::fmt; use std::hash; +#[cfg(feature = "abomonation-serialize")] +use std::io::{Result as IOResult, Write}; #[cfg(feature = "serde-serialize")] use serde; @@ -64,12 +66,12 @@ where MatrixN: Abomonation, DefaultAllocator: Allocator, { - unsafe fn entomb(&self, writer: &mut Vec) { + unsafe fn entomb(&self, writer: &mut W) -> IOResult<()> { self.matrix.entomb(writer) } - unsafe fn embalm(&mut self) { - self.matrix.embalm() + fn extent(&self) -> usize { + self.matrix.extent() } unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { diff --git a/src/geometry/similarity.rs b/src/geometry/similarity.rs index 1a9a0d30..e339d283 100644 --- a/src/geometry/similarity.rs +++ b/src/geometry/similarity.rs @@ -1,6 +1,8 @@ use approx::{AbsDiffEq, RelativeEq, UlpsEq}; use std::fmt; use std::hash; +#[cfg(feature = "abomonation-serialize")] +use std::io::{Result as IOResult, Write}; #[cfg(feature = "serde-serialize")] use serde; @@ -58,12 +60,12 @@ where Isometry: Abomonation, DefaultAllocator: Allocator, { - unsafe fn entomb(&self, writer: &mut Vec) { + unsafe fn entomb(&self, writer: &mut W) -> IOResult<()> { self.isometry.entomb(writer) } - unsafe fn embalm(&mut self) { - self.isometry.embalm() + fn extent(&self) -> usize { + self.isometry.extent() } unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { @@ -328,7 +330,8 @@ where ) -> bool { self.isometry .relative_eq(&other.isometry, epsilon, max_relative) - && self.scaling + && self + .scaling .relative_eq(&other.scaling, epsilon, max_relative) } } diff --git a/src/geometry/translation.rs b/src/geometry/translation.rs index af7fceba..317e8177 100644 --- a/src/geometry/translation.rs +++ b/src/geometry/translation.rs @@ -2,6 +2,8 @@ use approx::{AbsDiffEq, RelativeEq, UlpsEq}; use num::{One, Zero}; use std::fmt; use std::hash; +#[cfg(feature = "abomonation-serialize")] +use std::io::{Result as IOResult, Write}; #[cfg(feature = "serde-serialize")] use serde; @@ -64,12 +66,12 @@ where VectorN: Abomonation, DefaultAllocator: Allocator, { - unsafe fn entomb(&self, writer: &mut Vec) { + unsafe fn entomb(&self, writer: &mut W) -> IOResult<()> { self.vector.entomb(writer) } - unsafe fn embalm(&mut self) { - self.vector.embalm() + fn extent(&self) -> usize { + self.vector.extent() } unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> {