From 18b694dad282c543cc2eb786f783f7b94afa3a0d Mon Sep 17 00:00:00 2001 From: Fabian Loeschner Date: Mon, 8 Nov 2021 11:10:58 +0100 Subject: [PATCH] Move serialization helper structs into trait impls --- nalgebra-sparse/src/csr.rs | 48 +++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/nalgebra-sparse/src/csr.rs b/nalgebra-sparse/src/csr.rs index b98717b8..bc2a3df0 100644 --- a/nalgebra-sparse/src/csr.rs +++ b/nalgebra-sparse/src/csr.rs @@ -599,15 +599,6 @@ impl CsrMatrix { } } -#[cfg_attr(feature = "serde-serialize", derive(Serialize))] -struct CsrMatrixSerializationHelper<'a, T> { - nrows: usize, - ncols: usize, - row_offsets: &'a [usize], - col_indices: &'a [usize], - values: &'a [T], -} - #[cfg(feature = "serde-serialize")] impl Serialize for CsrMatrix where @@ -617,7 +608,16 @@ where where S: Serializer, { - CsrMatrixSerializationHelper { + #[derive(Serialize)] + struct CsrMatrixSerializationData<'a, T> { + nrows: usize, + ncols: usize, + row_offsets: &'a [usize], + col_indices: &'a [usize], + values: &'a [T], + } + + CsrMatrixSerializationData { nrows: self.nrows(), ncols: self.ncols(), row_offsets: self.row_offsets(), @@ -628,15 +628,6 @@ where } } -#[cfg_attr(feature = "serde-serialize", derive(Deserialize))] -struct CsrMatrixDeserializationHelper { - nrows: usize, - ncols: usize, - row_offsets: Vec, - col_indices: Vec, - values: Vec, -} - #[cfg(feature = "serde-serialize")] impl<'de, T> Deserialize<'de> for CsrMatrix where @@ -646,14 +637,17 @@ where where D: Deserializer<'de>, { - let CsrMatrixDeserializationHelper { - nrows, - ncols, - row_offsets, - col_indices, - values, - } = CsrMatrixDeserializationHelper::deserialize(deserializer)?; - CsrMatrix::try_from_csr_data(nrows, ncols, row_offsets, col_indices, values) + #[derive(Deserialize)] + struct CsrMatrixDeserializationData { + nrows: usize, + ncols: usize, + row_offsets: Vec, + col_indices: Vec, + values: Vec, + } + + let de = CsrMatrixDeserializationData::deserialize(deserializer)?; + CsrMatrix::try_from_csr_data(de.nrows, de.ncols, de.row_offsets, de.col_indices, de.values) .map(|m| m.into()) // TODO: More specific error .map_err(|_e| de::Error::invalid_value(de::Unexpected::Other("invalid CSR matrix"), &"a valid CSR matrix"))