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,52 +275,55 @@ impl<T> CooMatrix<T> {
}
#[cfg(feature = "serde-serialize")]
#[derive(Serialize, Deserialize)]
struct CooMatrixSerializationData<Indices, Values> {
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<T> Serialize for CooMatrix<T>
where
T: Serialize + Clone,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
#[derive(Serialize, Deserialize)]
struct CooMatrixSerializationData<Indices, Values> {
nrows: usize,
ncols: usize,
row_indices: Indices,
col_indices: Indices,
values: Values,
}
impl<T> Serialize for CooMatrix<T>
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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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<T>
where
T: Deserialize<'de> + Clone,
{
fn deserialize<D>(deserializer: D) -> Result<CooMatrix<T>, D::Error>
impl<'de, T> Deserialize<'de> for CooMatrix<T>
where
D: Deserializer<'de>,
T: Deserialize<'de> + Clone,
{
let de = CooMatrixSerializationData::<Vec<usize>, Vec<T>>::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<D>(deserializer: D) -> Result<CooMatrix<T>, D::Error>
where
D: Deserializer<'de>,
{
let de = CooMatrixSerializationData::<Vec<usize>, Vec<T>>::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))
}
}
}

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,53 +521,56 @@ impl<T> CscMatrix<T> {
}
#[cfg(feature = "serde-serialize")]
#[derive(Serialize, Deserialize)]
struct CscMatrixSerializationData<Indices, Values> {
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<T> Serialize for CscMatrix<T>
where
T: Serialize + Clone,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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<Indices, Values> {
nrows: usize,
ncols: usize,
col_offsets: Indices,
row_indices: Indices,
values: Values,
}
}
#[cfg(feature = "serde-serialize")]
impl<'de, T> Deserialize<'de> for CscMatrix<T>
where
T: Deserialize<'de> + Clone,
{
fn deserialize<D>(deserializer: D) -> Result<CscMatrix<T>, D::Error>
impl<T> Serialize for CscMatrix<T>
where
D: Deserializer<'de>,
T: Serialize + Clone,
{
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))
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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<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 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<T> CsrMatrix<T> {
}
#[cfg(feature = "serde-serialize")]
#[derive(Serialize, Deserialize)]
struct CsrMatrixSerializationData<Indices, Values> {
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<T> Serialize for CsrMatrix<T>
where
T: Serialize + Clone,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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<Indices, Values> {
nrows: usize,
ncols: usize,
row_offsets: Indices,
col_indices: Indices,
values: Values,
}
}
#[cfg(feature = "serde-serialize")]
impl<'de, T> Deserialize<'de> for CsrMatrix<T>
where
T: Deserialize<'de> + Clone,
{
fn deserialize<D>(deserializer: D) -> Result<CsrMatrix<T>, D::Error>
impl<T> Serialize for CsrMatrix<T>
where
D: Deserializer<'de>,
T: Serialize + Clone,
{
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))
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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<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::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<Indices> {
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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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<Indices> {
major_dim: usize,
minor_dim: usize,
major_offsets: Indices,
minor_indices: Indices,
}
}
#[cfg(feature = "serde-serialize")]
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))
impl Serialize for SparsityPattern {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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<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))
}
}
}