From 837ded932e8e2724cb2c29317a90566793e56256 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20L=C3=B6schner?= Date: Wed, 15 Dec 2021 11:43:35 +0100 Subject: [PATCH] Replace usage of Cow with generic type --- nalgebra-sparse/src/coo.rs | 26 ++++++++++++-------------- nalgebra-sparse/src/csc.rs | 26 ++++++++++++-------------- nalgebra-sparse/src/csr.rs | 26 ++++++++++++-------------- nalgebra-sparse/src/pattern.rs | 20 +++++++++----------- 4 files changed, 45 insertions(+), 53 deletions(-) diff --git a/nalgebra-sparse/src/coo.rs b/nalgebra-sparse/src/coo.rs index 0cebb725..3e3c7b4c 100644 --- a/nalgebra-sparse/src/coo.rs +++ b/nalgebra-sparse/src/coo.rs @@ -4,8 +4,6 @@ use crate::SparseFormatError; #[cfg(feature = "serde-serialize")] use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; -#[cfg(feature = "serde-serialize")] -use std::borrow::Cow; /// A COO representation of a sparse matrix. /// @@ -281,12 +279,12 @@ impl CooMatrix { #[cfg(feature = "serde-serialize")] #[derive(Serialize, Deserialize)] -struct CooMatrixSerializationData<'a, T: Clone> { +struct CooMatrixSerializationData { nrows: usize, ncols: usize, - row_indices: Cow<'a, [usize]>, - col_indices: Cow<'a, [usize]>, - values: Cow<'a, [T]>, + row_indices: Indices, + col_indices: Indices, + values: Values, } #[cfg(feature = "serde-serialize")] @@ -298,12 +296,12 @@ where where S: Serializer, { - CooMatrixSerializationData { + CooMatrixSerializationData::<&[usize], &[T]> { nrows: self.nrows(), ncols: self.ncols(), - row_indices: self.row_indices().into(), - col_indices: self.col_indices().into(), - values: self.values().into(), + row_indices: self.row_indices(), + col_indices: self.col_indices(), + values: self.values(), } .serialize(serializer) } @@ -318,13 +316,13 @@ where where D: Deserializer<'de>, { - let de = CooMatrixSerializationData::deserialize(deserializer)?; + let de = CooMatrixSerializationData::, Vec>::deserialize(deserializer)?; CooMatrix::try_from_triplets( de.nrows, de.ncols, - de.row_indices.into(), - de.col_indices.into(), - de.values.into(), + de.row_indices, + de.col_indices, + de.values, ) .map_err(|e| de::Error::custom(e)) } diff --git a/nalgebra-sparse/src/csc.rs b/nalgebra-sparse/src/csc.rs index bd70d4c8..7c7832b5 100644 --- a/nalgebra-sparse/src/csc.rs +++ b/nalgebra-sparse/src/csc.rs @@ -12,8 +12,6 @@ use nalgebra::Scalar; use num_traits::One; #[cfg(feature = "serde-serialize")] use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; -#[cfg(feature = "serde-serialize")] -use std::borrow::Cow; use std::slice::{Iter, IterMut}; /// A CSC representation of a sparse matrix. @@ -526,12 +524,12 @@ impl CscMatrix { #[cfg(feature = "serde-serialize")] #[derive(Serialize, Deserialize)] -struct CscMatrixSerializationData<'a, T: Clone> { +struct CscMatrixSerializationData { nrows: usize, ncols: usize, - col_offsets: Cow<'a, [usize]>, - row_indices: Cow<'a, [usize]>, - values: Cow<'a, [T]>, + col_offsets: Indices, + row_indices: Indices, + values: Values, } #[cfg(feature = "serde-serialize")] @@ -543,12 +541,12 @@ where where S: Serializer, { - CscMatrixSerializationData { + CscMatrixSerializationData::<&[usize], &[T]> { nrows: self.nrows(), ncols: self.ncols(), - col_offsets: Cow::Borrowed(self.col_offsets()), - row_indices: Cow::Borrowed(self.row_indices()), - values: Cow::Borrowed(self.values()), + col_offsets: self.col_offsets(), + row_indices: self.row_indices(), + values: self.values(), } .serialize(serializer) } @@ -563,13 +561,13 @@ where where D: Deserializer<'de>, { - let de = CscMatrixSerializationData::deserialize(deserializer)?; + let de = CscMatrixSerializationData::, Vec>::deserialize(deserializer)?; CscMatrix::try_from_csc_data( de.nrows, de.ncols, - de.col_offsets.into(), - de.row_indices.into(), - de.values.into(), + de.col_offsets, + de.row_indices, + de.values, ) .map_err(|e| de::Error::custom(e)) } diff --git a/nalgebra-sparse/src/csr.rs b/nalgebra-sparse/src/csr.rs index bd43927d..786736ff 100644 --- a/nalgebra-sparse/src/csr.rs +++ b/nalgebra-sparse/src/csr.rs @@ -11,8 +11,6 @@ use nalgebra::Scalar; use num_traits::One; #[cfg(feature = "serde-serialize")] use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; -#[cfg(feature = "serde-serialize")] -use std::borrow::Cow; use std::iter::FromIterator; use std::slice::{Iter, IterMut}; @@ -597,12 +595,12 @@ impl CsrMatrix { #[cfg(feature = "serde-serialize")] #[derive(Serialize, Deserialize)] -struct CsrMatrixSerializationData<'a, T: Clone> { +struct CsrMatrixSerializationData { nrows: usize, ncols: usize, - row_offsets: Cow<'a, [usize]>, - col_indices: Cow<'a, [usize]>, - values: Cow<'a, [T]>, + row_offsets: Indices, + col_indices: Indices, + values: Values, } #[cfg(feature = "serde-serialize")] @@ -614,12 +612,12 @@ where where S: Serializer, { - CsrMatrixSerializationData { + CsrMatrixSerializationData::<&[usize], &[T]> { nrows: self.nrows(), ncols: self.ncols(), - row_offsets: Cow::Borrowed(self.row_offsets()), - col_indices: Cow::Borrowed(self.col_indices()), - values: Cow::Borrowed(self.values()), + row_offsets: self.row_offsets(), + col_indices: self.col_indices(), + values: self.values(), } .serialize(serializer) } @@ -634,13 +632,13 @@ where where D: Deserializer<'de>, { - let de = CsrMatrixSerializationData::deserialize(deserializer)?; + let de = CsrMatrixSerializationData::, Vec>::deserialize(deserializer)?; CsrMatrix::try_from_csr_data( de.nrows, de.ncols, - de.row_offsets.into(), - de.col_indices.into(), - de.values.into(), + de.row_offsets, + de.col_indices, + de.values, ) .map_err(|e| de::Error::custom(e)) } diff --git a/nalgebra-sparse/src/pattern.rs b/nalgebra-sparse/src/pattern.rs index a833250c..f7289438 100644 --- a/nalgebra-sparse/src/pattern.rs +++ b/nalgebra-sparse/src/pattern.rs @@ -6,8 +6,6 @@ use std::fmt; #[cfg(feature = "serde-serialize")] use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; -#[cfg(feature = "serde-serialize")] -use std::borrow::Cow; /// A representation of the sparsity pattern of a CSR or CSC matrix. /// @@ -296,11 +294,11 @@ pub enum SparsityPatternFormatError { #[cfg(feature = "serde-serialize")] #[derive(Serialize, Deserialize)] -struct SparsityPatternSerializationData<'a> { +struct SparsityPatternSerializationData { major_dim: usize, minor_dim: usize, - major_offsets: Cow<'a, [usize]>, - minor_indices: Cow<'a, [usize]>, + major_offsets: Indices, + minor_indices: Indices, } #[cfg(feature = "serde-serialize")] @@ -309,11 +307,11 @@ impl Serialize for SparsityPattern { where S: Serializer, { - SparsityPatternSerializationData { + SparsityPatternSerializationData::<&[usize]> { major_dim: self.major_dim(), minor_dim: self.minor_dim(), - major_offsets: Cow::Borrowed(self.major_offsets()), - minor_indices: Cow::Borrowed(self.minor_indices()), + major_offsets: self.major_offsets(), + minor_indices: self.minor_indices(), } .serialize(serializer) } @@ -325,12 +323,12 @@ impl<'de> Deserialize<'de> for SparsityPattern { where D: Deserializer<'de>, { - let de = SparsityPatternSerializationData::deserialize(deserializer)?; + let de = SparsityPatternSerializationData::>::deserialize(deserializer)?; SparsityPattern::try_from_offsets_and_indices( de.major_dim, de.minor_dim, - de.major_offsets.into(), - de.minor_indices.into(), + de.major_offsets, + de.minor_indices, ) .map_err(|e| de::Error::custom(e)) }