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,20 +275,23 @@ impl<T> CooMatrix<T> {
} }
#[cfg(feature = "serde-serialize")] #[cfg(feature = "serde-serialize")]
#[derive(Serialize, Deserialize)] mod serde_serialize {
struct CooMatrixSerializationData<Indices, Values> { use super::CooMatrix;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
#[derive(Serialize, Deserialize)]
struct CooMatrixSerializationData<Indices, Values> {
nrows: usize, nrows: usize,
ncols: usize, ncols: usize,
row_indices: Indices, row_indices: Indices,
col_indices: Indices, col_indices: Indices,
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,
{ {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where where
S: Serializer, S: Serializer,
@ -305,13 +305,12 @@ where
} }
.serialize(serializer) .serialize(serializer)
} }
} }
#[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,
{ {
fn deserialize<D>(deserializer: D) -> Result<CooMatrix<T>, D::Error> fn deserialize<D>(deserializer: D) -> Result<CooMatrix<T>, D::Error>
where where
D: Deserializer<'de>, D: Deserializer<'de>,
@ -326,4 +325,5 @@ 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,20 +521,23 @@ impl<T> CscMatrix<T> {
} }
#[cfg(feature = "serde-serialize")] #[cfg(feature = "serde-serialize")]
#[derive(Serialize, Deserialize)] mod serde_serialize {
struct CscMatrixSerializationData<Indices, Values> { use super::CscMatrix;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
#[derive(Serialize, Deserialize)]
struct CscMatrixSerializationData<Indices, Values> {
nrows: usize, nrows: usize,
ncols: usize, ncols: usize,
col_offsets: Indices, col_offsets: Indices,
row_indices: Indices, row_indices: Indices,
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,
{ {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where where
S: Serializer, S: Serializer,
@ -550,13 +551,12 @@ where
} }
.serialize(serializer) .serialize(serializer)
} }
} }
#[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,
{ {
fn deserialize<D>(deserializer: D) -> Result<CscMatrix<T>, D::Error> fn deserialize<D>(deserializer: D) -> Result<CscMatrix<T>, D::Error>
where where
D: Deserializer<'de>, D: Deserializer<'de>,
@ -571,6 +571,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,20 +592,23 @@ impl<T> CsrMatrix<T> {
} }
#[cfg(feature = "serde-serialize")] #[cfg(feature = "serde-serialize")]
#[derive(Serialize, Deserialize)] mod serde_serialize {
struct CsrMatrixSerializationData<Indices, Values> { use super::CsrMatrix;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
#[derive(Serialize, Deserialize)]
struct CsrMatrixSerializationData<Indices, Values> {
nrows: usize, nrows: usize,
ncols: usize, ncols: usize,
row_offsets: Indices, row_offsets: Indices,
col_indices: Indices, col_indices: Indices,
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,
{ {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where where
S: Serializer, S: Serializer,
@ -621,13 +622,12 @@ where
} }
.serialize(serializer) .serialize(serializer)
} }
} }
#[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,
{ {
fn deserialize<D>(deserializer: D) -> Result<CsrMatrix<T>, D::Error> fn deserialize<D>(deserializer: D) -> Result<CsrMatrix<T>, D::Error>
where where
D: Deserializer<'de>, D: Deserializer<'de>,
@ -642,6 +642,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,16 +290,19 @@ pub enum SparsityPatternFormatError {
} }
#[cfg(feature = "serde-serialize")] #[cfg(feature = "serde-serialize")]
#[derive(Serialize, Deserialize)] mod serde_serialize {
struct SparsityPatternSerializationData<Indices> { use super::SparsityPattern;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
#[derive(Serialize, Deserialize)]
struct SparsityPatternSerializationData<Indices> {
major_dim: usize, major_dim: usize,
minor_dim: usize, minor_dim: usize,
major_offsets: Indices, major_offsets: 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
S: Serializer, S: Serializer,
@ -315,10 +315,9 @@ impl Serialize for SparsityPattern {
} }
.serialize(serializer) .serialize(serializer)
} }
} }
#[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
D: Deserializer<'de>, D: Deserializer<'de>,
@ -332,6 +331,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 {