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;
#[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,20 +275,23 @@ impl<T> CooMatrix<T> {
}
#[cfg(feature = "serde-serialize")]
#[derive(Serialize, Deserialize)]
struct CooMatrixSerializationData<Indices, Values> {
mod serde_serialize {
use super::CooMatrix;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
#[derive(Serialize, Deserialize)]
struct CooMatrixSerializationData<Indices, Values> {
nrows: usize,
ncols: usize,
row_indices: Indices,
col_indices: Indices,
values: Values,
}
}
#[cfg(feature = "serde-serialize")]
impl<T> Serialize for CooMatrix<T>
where
impl<T> Serialize for CooMatrix<T>
where
T: Serialize + Clone,
{
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
@ -305,13 +305,12 @@ where
}
.serialize(serializer)
}
}
}
#[cfg(feature = "serde-serialize")]
impl<'de, T> Deserialize<'de> for CooMatrix<T>
where
impl<'de, T> Deserialize<'de> for CooMatrix<T>
where
T: Deserialize<'de> + Clone,
{
{
fn deserialize<D>(deserializer: D) -> Result<CooMatrix<T>, D::Error>
where
D: Deserializer<'de>,
@ -326,4 +325,5 @@ where
)
.map_err(|e| de::Error::custom(e))
}
}
}

View File

@ -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,20 +521,23 @@ impl<T> CscMatrix<T> {
}
#[cfg(feature = "serde-serialize")]
#[derive(Serialize, Deserialize)]
struct CscMatrixSerializationData<Indices, Values> {
mod serde_serialize {
use super::CscMatrix;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
#[derive(Serialize, Deserialize)]
struct CscMatrixSerializationData<Indices, Values> {
nrows: usize,
ncols: usize,
col_offsets: Indices,
row_indices: Indices,
values: Values,
}
}
#[cfg(feature = "serde-serialize")]
impl<T> Serialize for CscMatrix<T>
where
impl<T> Serialize for CscMatrix<T>
where
T: Serialize + Clone,
{
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
@ -550,13 +551,12 @@ where
}
.serialize(serializer)
}
}
}
#[cfg(feature = "serde-serialize")]
impl<'de, T> Deserialize<'de> for CscMatrix<T>
where
impl<'de, T> Deserialize<'de> for CscMatrix<T>
where
T: Deserialize<'de> + Clone,
{
{
fn deserialize<D>(deserializer: D) -> Result<CscMatrix<T>, D::Error>
where
D: Deserializer<'de>,
@ -571,6 +571,7 @@ where
)
.map_err(|e| de::Error::custom(e))
}
}
}
/// 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 num_traits::One;
#[cfg(feature = "serde-serialize")]
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use std::iter::FromIterator;
use std::slice::{Iter, IterMut};
@ -594,20 +592,23 @@ impl<T> CsrMatrix<T> {
}
#[cfg(feature = "serde-serialize")]
#[derive(Serialize, Deserialize)]
struct CsrMatrixSerializationData<Indices, Values> {
mod serde_serialize {
use super::CsrMatrix;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
#[derive(Serialize, Deserialize)]
struct CsrMatrixSerializationData<Indices, Values> {
nrows: usize,
ncols: usize,
row_offsets: Indices,
col_indices: Indices,
values: Values,
}
}
#[cfg(feature = "serde-serialize")]
impl<T> Serialize for CsrMatrix<T>
where
impl<T> Serialize for CsrMatrix<T>
where
T: Serialize + Clone,
{
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
@ -621,13 +622,12 @@ where
}
.serialize(serializer)
}
}
}
#[cfg(feature = "serde-serialize")]
impl<'de, T> Deserialize<'de> for CsrMatrix<T>
where
impl<'de, T> Deserialize<'de> for CsrMatrix<T>
where
T: Deserialize<'de> + Clone,
{
{
fn deserialize<D>(deserializer: D) -> Result<CsrMatrix<T>, D::Error>
where
D: Deserializer<'de>,
@ -642,6 +642,7 @@ where
)
.map_err(|e| de::Error::custom(e))
}
}
}
/// 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::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,16 +290,19 @@ pub enum SparsityPatternFormatError {
}
#[cfg(feature = "serde-serialize")]
#[derive(Serialize, Deserialize)]
struct SparsityPatternSerializationData<Indices> {
mod serde_serialize {
use super::SparsityPattern;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
#[derive(Serialize, Deserialize)]
struct SparsityPatternSerializationData<Indices> {
major_dim: usize,
minor_dim: usize,
major_offsets: 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>
where
S: Serializer,
@ -315,10 +315,9 @@ impl Serialize for SparsityPattern {
}
.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>
where
D: Deserializer<'de>,
@ -332,6 +331,7 @@ impl<'de> Deserialize<'de> for SparsityPattern {
)
.map_err(|e| de::Error::custom(e))
}
}
}
impl From<SparsityPatternFormatError> for SparseFormatError {