Skip to content

Commit

Permalink
Implement CsMatBase serialization manually
Browse files Browse the repository at this point in the history
This works around a bug encountered with the combination of serde and
bincode when there is a flatten attribute, see
bincode-org/bincode#245
  • Loading branch information
vbarrielle committed Dec 21, 2020
1 parent e4d5794 commit b5ef5c5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/sparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub use self::csmat::CompressedStorage;
/// [`bmat`]: fn.bmat.html
#[derive(Eq, PartialEq, Debug, Copy, Clone, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(Deserialize))]
#[cfg_attr(
feature = "serde",
serde(
Expand Down
29 changes: 28 additions & 1 deletion src/sparse/serde_traits.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
use super::*;
pub(crate) use serde::{Deserialize, Serialize};
pub(crate) use serde::ser::SerializeStruct;
pub(crate) use serde::{Deserialize, Serialize, Serializer};
use std::convert::TryFrom;
use std::ops::Deref;

impl<N, I: SpIndex, Iptr: SpIndex, IptrStorage, IStorage, DStorage> Serialize
for CsMatBase<N, I, IptrStorage, IStorage, DStorage, Iptr>
where
Iptr: Serialize,
I: Serialize,
N: Serialize,
IptrStorage: Deref<Target = [Iptr]>,
IStorage: Deref<Target = [I]>,
DStorage: Deref<Target = [N]>,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut state = serializer.serialize_struct("CsMatBase", 6)?;
state.serialize_field("storage", &self.storage)?;
state.serialize_field("nrows", &self.nrows)?;
state.serialize_field("ncols", &self.ncols)?;
state.serialize_field("indptr", &self.indptr.raw_storage())?;
state.serialize_field("indices", &self.indices[..])?;
state.serialize_field("data", &self.data[..])?;
state.end()
}
}

#[derive(Deserialize)]
pub(crate) struct CsVecBaseShadow<IStorage, DStorage, N, I: SpIndex = usize>
Expand Down

0 comments on commit b5ef5c5

Please sign in to comment.