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,52 +275,55 @@ 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;
nrows: usize, use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
ncols: usize,
row_indices: Indices,
col_indices: Indices,
values: Values,
}
#[cfg(feature = "serde-serialize")] #[derive(Serialize, Deserialize)]
impl<T> Serialize for CooMatrix<T> struct CooMatrixSerializationData<Indices, Values> {
where nrows: usize,
T: Serialize + Clone, ncols: usize,
{ row_indices: Indices,
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> col_indices: Indices,
values: Values,
}
impl<T> Serialize for CooMatrix<T>
where where
S: Serializer, T: Serialize + Clone,
{ {
CooMatrixSerializationData::<&[usize], &[T]> { fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
nrows: self.nrows(), where
ncols: self.ncols(), S: Serializer,
row_indices: self.row_indices(), {
col_indices: self.col_indices(), CooMatrixSerializationData::<&[usize], &[T]> {
values: self.values(), 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<T>
impl<'de, T> Deserialize<'de> for CooMatrix<T>
where
T: Deserialize<'de> + Clone,
{
fn deserialize<D>(deserializer: D) -> Result<CooMatrix<T>, D::Error>
where where
D: Deserializer<'de>, T: Deserialize<'de> + Clone,
{ {
let de = CooMatrixSerializationData::<Vec<usize>, Vec<T>>::deserialize(deserializer)?; fn deserialize<D>(deserializer: D) -> Result<CooMatrix<T>, D::Error>
CooMatrix::try_from_triplets( where
de.nrows, D: Deserializer<'de>,
de.ncols, {
de.row_indices, let de = CooMatrixSerializationData::<Vec<usize>, Vec<T>>::deserialize(deserializer)?;
de.col_indices, CooMatrix::try_from_triplets(
de.values, de.nrows,
) de.ncols,
.map_err(|e| de::Error::custom(e)) de.row_indices,
de.col_indices,
de.values,
)
.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,53 +521,56 @@ 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;
nrows: usize, use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
ncols: usize,
col_offsets: Indices,
row_indices: Indices,
values: Values,
}
#[cfg(feature = "serde-serialize")] #[derive(Serialize, Deserialize)]
impl<T> Serialize for CscMatrix<T> struct CscMatrixSerializationData<Indices, Values> {
where nrows: usize,
T: Serialize + Clone, ncols: usize,
{ col_offsets: Indices,
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> row_indices: Indices,
where values: Values,
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)
} }
}
#[cfg(feature = "serde-serialize")] impl<T> Serialize for CscMatrix<T>
impl<'de, T> Deserialize<'de> for CscMatrix<T>
where
T: Deserialize<'de> + Clone,
{
fn deserialize<D>(deserializer: D) -> Result<CscMatrix<T>, D::Error>
where where
D: Deserializer<'de>, T: Serialize + Clone,
{ {
let de = CscMatrixSerializationData::<Vec<usize>, Vec<T>>::deserialize(deserializer)?; fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
CscMatrix::try_from_csc_data( where
de.nrows, S: Serializer,
de.ncols, {
de.col_offsets, CscMatrixSerializationData::<&[usize], &[T]> {
de.row_indices, nrows: self.nrows(),
de.values, ncols: self.ncols(),
) col_offsets: self.col_offsets(),
.map_err(|e| de::Error::custom(e)) row_indices: self.row_indices(),
values: self.values(),
}
.serialize(serializer)
}
}
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>,
{
let de = CscMatrixSerializationData::<Vec<usize>, Vec<T>>::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))
}
} }
} }

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,53 +592,56 @@ 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;
nrows: usize, use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
ncols: usize,
row_offsets: Indices,
col_indices: Indices,
values: Values,
}
#[cfg(feature = "serde-serialize")] #[derive(Serialize, Deserialize)]
impl<T> Serialize for CsrMatrix<T> struct CsrMatrixSerializationData<Indices, Values> {
where nrows: usize,
T: Serialize + Clone, ncols: usize,
{ row_offsets: Indices,
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> col_indices: Indices,
where values: Values,
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)
} }
}
#[cfg(feature = "serde-serialize")] impl<T> Serialize for CsrMatrix<T>
impl<'de, T> Deserialize<'de> for CsrMatrix<T>
where
T: Deserialize<'de> + Clone,
{
fn deserialize<D>(deserializer: D) -> Result<CsrMatrix<T>, D::Error>
where where
D: Deserializer<'de>, T: Serialize + Clone,
{ {
let de = CsrMatrixSerializationData::<Vec<usize>, Vec<T>>::deserialize(deserializer)?; fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
CsrMatrix::try_from_csr_data( where
de.nrows, S: Serializer,
de.ncols, {
de.row_offsets, CsrMatrixSerializationData::<&[usize], &[T]> {
de.col_indices, nrows: self.nrows(),
de.values, ncols: self.ncols(),
) row_offsets: self.row_offsets(),
.map_err(|e| de::Error::custom(e)) col_indices: self.col_indices(),
values: self.values(),
}
.serialize(serializer)
}
}
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>,
{
let de = CsrMatrixSerializationData::<Vec<usize>, Vec<T>>::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))
}
} }
} }

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,44 +290,47 @@ pub enum SparsityPatternFormatError {
} }
#[cfg(feature = "serde-serialize")] #[cfg(feature = "serde-serialize")]
#[derive(Serialize, Deserialize)] mod serde_serialize {
struct SparsityPatternSerializationData<Indices> { use super::SparsityPattern;
major_dim: usize, use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
minor_dim: usize,
major_offsets: Indices,
minor_indices: Indices,
}
#[cfg(feature = "serde-serialize")] #[derive(Serialize, Deserialize)]
impl Serialize for SparsityPattern { struct SparsityPatternSerializationData<Indices> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> major_dim: usize,
where minor_dim: usize,
S: Serializer, major_offsets: Indices,
{ minor_indices: Indices,
SparsityPatternSerializationData::<&[usize]> {
major_dim: self.major_dim(),
minor_dim: self.minor_dim(),
major_offsets: self.major_offsets(),
minor_indices: self.minor_indices(),
}
.serialize(serializer)
} }
}
#[cfg(feature = "serde-serialize")] impl Serialize for SparsityPattern {
impl<'de> Deserialize<'de> for SparsityPattern { fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
fn deserialize<D>(deserializer: D) -> Result<SparsityPattern, D::Error> where
where S: Serializer,
D: Deserializer<'de>, {
{ SparsityPatternSerializationData::<&[usize]> {
let de = SparsityPatternSerializationData::<Vec<usize>>::deserialize(deserializer)?; major_dim: self.major_dim(),
SparsityPattern::try_from_offsets_and_indices( minor_dim: self.minor_dim(),
de.major_dim, major_offsets: self.major_offsets(),
de.minor_dim, minor_indices: self.minor_indices(),
de.major_offsets, }
de.minor_indices, .serialize(serializer)
) }
.map_err(|e| de::Error::custom(e)) }
impl<'de> Deserialize<'de> for SparsityPattern {
fn deserialize<D>(deserializer: D) -> Result<SparsityPattern, D::Error>
where
D: Deserializer<'de>,
{
let de = SparsityPatternSerializationData::<Vec<usize>>::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))
}
} }
} }