From 647455daddd4fdc3830c17a97282bcd43803a36e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20L=C3=B6schner?= Date: Wed, 15 Dec 2021 11:48:51 +0100 Subject: [PATCH] Move serialization code to submodules --- nalgebra-sparse/src/coo.rs | 84 ++++++++++++++++---------------- nalgebra-sparse/src/csc.rs | 89 +++++++++++++++++----------------- nalgebra-sparse/src/csr.rs | 89 +++++++++++++++++----------------- nalgebra-sparse/src/pattern.rs | 76 ++++++++++++++--------------- 4 files changed, 170 insertions(+), 168 deletions(-) diff --git a/nalgebra-sparse/src/coo.rs b/nalgebra-sparse/src/coo.rs index 3e3c7b4c..06788406 100644 --- a/nalgebra-sparse/src/coo.rs +++ b/nalgebra-sparse/src/coo.rs @@ -2,9 +2,6 @@ use crate::SparseFormatError; -#[cfg(feature = "serde-serialize")] -use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; - /// A COO representation of a sparse matrix. /// /// A COO matrix stores entries in coordinate-form, that is triplets `(i, j, v)`, where `i` and `j` @@ -278,52 +275,55 @@ impl CooMatrix { } #[cfg(feature = "serde-serialize")] -#[derive(Serialize, Deserialize)] -struct CooMatrixSerializationData { - nrows: usize, - ncols: usize, - row_indices: Indices, - col_indices: Indices, - values: Values, -} +mod serde_serialize { + use super::CooMatrix; + use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; -#[cfg(feature = "serde-serialize")] -impl Serialize for CooMatrix -where - T: Serialize + Clone, -{ - fn serialize(&self, serializer: S) -> Result + #[derive(Serialize, Deserialize)] + struct CooMatrixSerializationData { + nrows: usize, + ncols: usize, + row_indices: Indices, + col_indices: Indices, + values: Values, + } + + impl Serialize for CooMatrix where - S: Serializer, + T: Serialize + Clone, { - CooMatrixSerializationData::<&[usize], &[T]> { - nrows: self.nrows(), - ncols: self.ncols(), - row_indices: self.row_indices(), - col_indices: self.col_indices(), - values: self.values(), + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + CooMatrixSerializationData::<&[usize], &[T]> { + nrows: self.nrows(), + ncols: self.ncols(), + row_indices: self.row_indices(), + col_indices: self.col_indices(), + values: self.values(), + } + .serialize(serializer) } - .serialize(serializer) } -} -#[cfg(feature = "serde-serialize")] -impl<'de, T> Deserialize<'de> for CooMatrix -where - T: Deserialize<'de> + Clone, -{ - fn deserialize(deserializer: D) -> Result, D::Error> + impl<'de, T> Deserialize<'de> for CooMatrix where - D: Deserializer<'de>, + T: Deserialize<'de> + Clone, { - let de = CooMatrixSerializationData::, Vec>::deserialize(deserializer)?; - CooMatrix::try_from_triplets( - de.nrows, - de.ncols, - de.row_indices, - de.col_indices, - de.values, - ) - .map_err(|e| de::Error::custom(e)) + fn deserialize(deserializer: D) -> Result, D::Error> + where + D: Deserializer<'de>, + { + let de = CooMatrixSerializationData::, Vec>::deserialize(deserializer)?; + CooMatrix::try_from_triplets( + de.nrows, + de.ncols, + de.row_indices, + de.col_indices, + de.values, + ) + .map_err(|e| de::Error::custom(e)) + } } } diff --git a/nalgebra-sparse/src/csc.rs b/nalgebra-sparse/src/csc.rs index 7c7832b5..29861684 100644 --- a/nalgebra-sparse/src/csc.rs +++ b/nalgebra-sparse/src/csc.rs @@ -10,8 +10,6 @@ use crate::{SparseEntry, SparseEntryMut, SparseFormatError, SparseFormatErrorKin use nalgebra::Scalar; use num_traits::One; -#[cfg(feature = "serde-serialize")] -use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; use std::slice::{Iter, IterMut}; /// A CSC representation of a sparse matrix. @@ -523,53 +521,56 @@ impl CscMatrix { } #[cfg(feature = "serde-serialize")] -#[derive(Serialize, Deserialize)] -struct CscMatrixSerializationData { - nrows: usize, - ncols: usize, - col_offsets: Indices, - row_indices: Indices, - values: Values, -} +mod serde_serialize { + use super::CscMatrix; + use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; -#[cfg(feature = "serde-serialize")] -impl Serialize for CscMatrix -where - T: Serialize + Clone, -{ - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - CscMatrixSerializationData::<&[usize], &[T]> { - nrows: self.nrows(), - ncols: self.ncols(), - col_offsets: self.col_offsets(), - row_indices: self.row_indices(), - values: self.values(), - } - .serialize(serializer) + #[derive(Serialize, Deserialize)] + struct CscMatrixSerializationData { + nrows: usize, + ncols: usize, + col_offsets: Indices, + row_indices: Indices, + values: Values, } -} -#[cfg(feature = "serde-serialize")] -impl<'de, T> Deserialize<'de> for CscMatrix -where - T: Deserialize<'de> + Clone, -{ - fn deserialize(deserializer: D) -> Result, D::Error> + impl Serialize for CscMatrix where - D: Deserializer<'de>, + T: Serialize + Clone, { - let de = CscMatrixSerializationData::, Vec>::deserialize(deserializer)?; - CscMatrix::try_from_csc_data( - de.nrows, - de.ncols, - de.col_offsets, - de.row_indices, - de.values, - ) - .map_err(|e| de::Error::custom(e)) + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + CscMatrixSerializationData::<&[usize], &[T]> { + nrows: self.nrows(), + ncols: self.ncols(), + col_offsets: self.col_offsets(), + row_indices: self.row_indices(), + values: self.values(), + } + .serialize(serializer) + } + } + + impl<'de, T> Deserialize<'de> for CscMatrix + where + T: Deserialize<'de> + Clone, + { + fn deserialize(deserializer: D) -> Result, D::Error> + where + D: Deserializer<'de>, + { + let de = CscMatrixSerializationData::, Vec>::deserialize(deserializer)?; + CscMatrix::try_from_csc_data( + de.nrows, + de.ncols, + de.col_offsets, + de.row_indices, + de.values, + ) + .map_err(|e| de::Error::custom(e)) + } } } diff --git a/nalgebra-sparse/src/csr.rs b/nalgebra-sparse/src/csr.rs index 786736ff..6be91f94 100644 --- a/nalgebra-sparse/src/csr.rs +++ b/nalgebra-sparse/src/csr.rs @@ -9,8 +9,6 @@ use crate::{SparseEntry, SparseEntryMut, SparseFormatError, SparseFormatErrorKin use nalgebra::Scalar; use num_traits::One; -#[cfg(feature = "serde-serialize")] -use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; use std::iter::FromIterator; use std::slice::{Iter, IterMut}; @@ -594,53 +592,56 @@ impl CsrMatrix { } #[cfg(feature = "serde-serialize")] -#[derive(Serialize, Deserialize)] -struct CsrMatrixSerializationData { - nrows: usize, - ncols: usize, - row_offsets: Indices, - col_indices: Indices, - values: Values, -} +mod serde_serialize { + use super::CsrMatrix; + use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; -#[cfg(feature = "serde-serialize")] -impl Serialize for CsrMatrix -where - T: Serialize + Clone, -{ - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - CsrMatrixSerializationData::<&[usize], &[T]> { - nrows: self.nrows(), - ncols: self.ncols(), - row_offsets: self.row_offsets(), - col_indices: self.col_indices(), - values: self.values(), - } - .serialize(serializer) + #[derive(Serialize, Deserialize)] + struct CsrMatrixSerializationData { + nrows: usize, + ncols: usize, + row_offsets: Indices, + col_indices: Indices, + values: Values, } -} -#[cfg(feature = "serde-serialize")] -impl<'de, T> Deserialize<'de> for CsrMatrix -where - T: Deserialize<'de> + Clone, -{ - fn deserialize(deserializer: D) -> Result, D::Error> + impl Serialize for CsrMatrix where - D: Deserializer<'de>, + T: Serialize + Clone, { - let de = CsrMatrixSerializationData::, Vec>::deserialize(deserializer)?; - CsrMatrix::try_from_csr_data( - de.nrows, - de.ncols, - de.row_offsets, - de.col_indices, - de.values, - ) - .map_err(|e| de::Error::custom(e)) + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + CsrMatrixSerializationData::<&[usize], &[T]> { + nrows: self.nrows(), + ncols: self.ncols(), + row_offsets: self.row_offsets(), + col_indices: self.col_indices(), + values: self.values(), + } + .serialize(serializer) + } + } + + impl<'de, T> Deserialize<'de> for CsrMatrix + where + T: Deserialize<'de> + Clone, + { + fn deserialize(deserializer: D) -> Result, D::Error> + where + D: Deserializer<'de>, + { + let de = CsrMatrixSerializationData::, Vec>::deserialize(deserializer)?; + CsrMatrix::try_from_csr_data( + de.nrows, + de.ncols, + de.row_offsets, + de.col_indices, + de.values, + ) + .map_err(|e| de::Error::custom(e)) + } } } diff --git a/nalgebra-sparse/src/pattern.rs b/nalgebra-sparse/src/pattern.rs index f7289438..1253982b 100644 --- a/nalgebra-sparse/src/pattern.rs +++ b/nalgebra-sparse/src/pattern.rs @@ -4,9 +4,6 @@ use crate::SparseFormatError; use std::error::Error; use std::fmt; -#[cfg(feature = "serde-serialize")] -use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; - /// A representation of the sparsity pattern of a CSR or CSC matrix. /// /// CSR and CSC matrices store matrices in a very similar fashion. In fact, in a certain sense, @@ -293,44 +290,47 @@ pub enum SparsityPatternFormatError { } #[cfg(feature = "serde-serialize")] -#[derive(Serialize, Deserialize)] -struct SparsityPatternSerializationData { - major_dim: usize, - minor_dim: usize, - major_offsets: Indices, - minor_indices: Indices, -} +mod serde_serialize { + use super::SparsityPattern; + use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; -#[cfg(feature = "serde-serialize")] -impl Serialize for SparsityPattern { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - SparsityPatternSerializationData::<&[usize]> { - major_dim: self.major_dim(), - minor_dim: self.minor_dim(), - major_offsets: self.major_offsets(), - minor_indices: self.minor_indices(), - } - .serialize(serializer) + #[derive(Serialize, Deserialize)] + struct SparsityPatternSerializationData { + major_dim: usize, + minor_dim: usize, + major_offsets: Indices, + minor_indices: Indices, } -} -#[cfg(feature = "serde-serialize")] -impl<'de> Deserialize<'de> for SparsityPattern { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - let de = SparsityPatternSerializationData::>::deserialize(deserializer)?; - SparsityPattern::try_from_offsets_and_indices( - de.major_dim, - de.minor_dim, - de.major_offsets, - de.minor_indices, - ) - .map_err(|e| de::Error::custom(e)) + impl Serialize for SparsityPattern { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + SparsityPatternSerializationData::<&[usize]> { + major_dim: self.major_dim(), + minor_dim: self.minor_dim(), + major_offsets: self.major_offsets(), + minor_indices: self.minor_indices(), + } + .serialize(serializer) + } + } + + impl<'de> Deserialize<'de> for SparsityPattern { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let de = SparsityPatternSerializationData::>::deserialize(deserializer)?; + SparsityPattern::try_from_offsets_and_indices( + de.major_dim, + de.minor_dim, + de.major_offsets, + de.minor_indices, + ) + .map_err(|e| de::Error::custom(e)) + } } }