Move serialization code to submodules

This commit is contained in:
Fabian Löschner 2021-12-15 11:48:51 +01:00 committed by Fabian Loeschner
parent 837ded932e
commit 647455dadd
4 changed files with 170 additions and 168 deletions

View File

@ -2,9 +2,6 @@
use crate::SparseFormatError; use crate::SparseFormatError;
#[cfg(feature = "serde-serialize")]
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
/// A COO representation of a sparse matrix. /// 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` /// A COO matrix stores entries in coordinate-form, that is triplets `(i, j, v)`, where `i` and `j`
@ -278,6 +275,10 @@ impl<T> CooMatrix<T> {
} }
#[cfg(feature = "serde-serialize")] #[cfg(feature = "serde-serialize")]
mod serde_serialize {
use super::CooMatrix;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct CooMatrixSerializationData<Indices, Values> { struct CooMatrixSerializationData<Indices, Values> {
nrows: usize, nrows: usize,
@ -287,7 +288,6 @@ struct CooMatrixSerializationData<Indices, Values> {
values: Values, values: Values,
} }
#[cfg(feature = "serde-serialize")]
impl<T> Serialize for CooMatrix<T> impl<T> Serialize for CooMatrix<T>
where where
T: Serialize + Clone, T: Serialize + Clone,
@ -307,7 +307,6 @@ where
} }
} }
#[cfg(feature = "serde-serialize")]
impl<'de, T> Deserialize<'de> for CooMatrix<T> impl<'de, T> Deserialize<'de> for CooMatrix<T>
where where
T: Deserialize<'de> + Clone, T: Deserialize<'de> + Clone,
@ -327,3 +326,4 @@ where
.map_err(|e| de::Error::custom(e)) .map_err(|e| de::Error::custom(e))
} }
} }
}

View File

@ -10,8 +10,6 @@ use crate::{SparseEntry, SparseEntryMut, SparseFormatError, SparseFormatErrorKin
use nalgebra::Scalar; use nalgebra::Scalar;
use num_traits::One; use num_traits::One;
#[cfg(feature = "serde-serialize")]
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use std::slice::{Iter, IterMut}; use std::slice::{Iter, IterMut};
/// A CSC representation of a sparse matrix. /// A CSC representation of a sparse matrix.
@ -523,6 +521,10 @@ impl<T> CscMatrix<T> {
} }
#[cfg(feature = "serde-serialize")] #[cfg(feature = "serde-serialize")]
mod serde_serialize {
use super::CscMatrix;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct CscMatrixSerializationData<Indices, Values> { struct CscMatrixSerializationData<Indices, Values> {
nrows: usize, nrows: usize,
@ -532,7 +534,6 @@ struct CscMatrixSerializationData<Indices, Values> {
values: Values, values: Values,
} }
#[cfg(feature = "serde-serialize")]
impl<T> Serialize for CscMatrix<T> impl<T> Serialize for CscMatrix<T>
where where
T: Serialize + Clone, T: Serialize + Clone,
@ -552,7 +553,6 @@ where
} }
} }
#[cfg(feature = "serde-serialize")]
impl<'de, T> Deserialize<'de> for CscMatrix<T> impl<'de, T> Deserialize<'de> for CscMatrix<T>
where where
T: Deserialize<'de> + Clone, T: Deserialize<'de> + Clone,
@ -572,6 +572,7 @@ where
.map_err(|e| de::Error::custom(e)) .map_err(|e| de::Error::custom(e))
} }
} }
}
/// Convert pattern format errors into more meaningful CSC-specific errors. /// Convert pattern format errors into more meaningful CSC-specific errors.
/// ///

View File

@ -9,8 +9,6 @@ use crate::{SparseEntry, SparseEntryMut, SparseFormatError, SparseFormatErrorKin
use nalgebra::Scalar; use nalgebra::Scalar;
use num_traits::One; use num_traits::One;
#[cfg(feature = "serde-serialize")]
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use std::iter::FromIterator; use std::iter::FromIterator;
use std::slice::{Iter, IterMut}; use std::slice::{Iter, IterMut};
@ -594,6 +592,10 @@ impl<T> CsrMatrix<T> {
} }
#[cfg(feature = "serde-serialize")] #[cfg(feature = "serde-serialize")]
mod serde_serialize {
use super::CsrMatrix;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct CsrMatrixSerializationData<Indices, Values> { struct CsrMatrixSerializationData<Indices, Values> {
nrows: usize, nrows: usize,
@ -603,7 +605,6 @@ struct CsrMatrixSerializationData<Indices, Values> {
values: Values, values: Values,
} }
#[cfg(feature = "serde-serialize")]
impl<T> Serialize for CsrMatrix<T> impl<T> Serialize for CsrMatrix<T>
where where
T: Serialize + Clone, T: Serialize + Clone,
@ -623,7 +624,6 @@ where
} }
} }
#[cfg(feature = "serde-serialize")]
impl<'de, T> Deserialize<'de> for CsrMatrix<T> impl<'de, T> Deserialize<'de> for CsrMatrix<T>
where where
T: Deserialize<'de> + Clone, T: Deserialize<'de> + Clone,
@ -643,6 +643,7 @@ where
.map_err(|e| de::Error::custom(e)) .map_err(|e| de::Error::custom(e))
} }
} }
}
/// Convert pattern format errors into more meaningful CSR-specific errors. /// Convert pattern format errors into more meaningful CSR-specific errors.
/// ///

View File

@ -4,9 +4,6 @@ use crate::SparseFormatError;
use std::error::Error; use std::error::Error;
use std::fmt; 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. /// 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, /// CSR and CSC matrices store matrices in a very similar fashion. In fact, in a certain sense,
@ -293,6 +290,10 @@ pub enum SparsityPatternFormatError {
} }
#[cfg(feature = "serde-serialize")] #[cfg(feature = "serde-serialize")]
mod serde_serialize {
use super::SparsityPattern;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct SparsityPatternSerializationData<Indices> { struct SparsityPatternSerializationData<Indices> {
major_dim: usize, major_dim: usize,
@ -301,7 +302,6 @@ struct SparsityPatternSerializationData<Indices> {
minor_indices: Indices, minor_indices: Indices,
} }
#[cfg(feature = "serde-serialize")]
impl Serialize for SparsityPattern { impl Serialize for SparsityPattern {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where where
@ -317,7 +317,6 @@ impl Serialize for SparsityPattern {
} }
} }
#[cfg(feature = "serde-serialize")]
impl<'de> Deserialize<'de> for SparsityPattern { impl<'de> Deserialize<'de> for SparsityPattern {
fn deserialize<D>(deserializer: D) -> Result<SparsityPattern, D::Error> fn deserialize<D>(deserializer: D) -> Result<SparsityPattern, D::Error>
where where
@ -333,6 +332,7 @@ impl<'de> Deserialize<'de> for SparsityPattern {
.map_err(|e| de::Error::custom(e)) .map_err(|e| de::Error::custom(e))
} }
} }
}
impl From<SparsityPatternFormatError> for SparseFormatError { impl From<SparsityPatternFormatError> for SparseFormatError {
fn from(err: SparsityPatternFormatError) -> Self { fn from(err: SparsityPatternFormatError) -> Self {