diff --git a/Cargo.toml b/Cargo.toml index 5cdde88..cde89e4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "veccell" -version = "0.3.0" +version = "0.4.0" edition = "2021" authors = [ "Shad Amethyst " ] description = "Provides VecCell, a variant of Vec with interior mutability." @@ -10,3 +10,10 @@ keywords = ["vec", "refcell", "interior-mutability"] categories = ["rust-patterns"] [dependencies] +serde = { version = "1", optional = true } + +[dev-dependencies] +serde_json = "1.0" + +[features] +serde = ["dep:serde"] diff --git a/src/lib.rs b/src/lib.rs index 707cd09..9085752 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -779,6 +779,28 @@ impl From> for Vec { } } +#[cfg(feature = "serde")] +impl serde::Serialize for VecCell { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer + { + let range = self.borrow_range(..).expect("Cannot borrow immutably VecCell: already borrowed mutably."); + (*range).serialize(serializer) + } +} + +#[cfg(feature = "serde")] +impl<'de, T: serde::Deserialize<'de>> serde::Deserialize<'de> for VecCell { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de> + { + let vec: Vec = Vec::deserialize(deserializer)?; + Ok(Self::from(vec)) + } +} + #[cfg(test)] mod test; diff --git a/src/test.rs b/src/test.rs index e38b8c8..9daa35d 100644 --- a/src/test.rs +++ b/src/test.rs @@ -296,3 +296,26 @@ fn test_try_map() { assert!(borrow.is_ok()); assert_eq!(borrow.unwrap(), -6); } + +#[cfg(feature = "serde")] +#[test] +fn test_serde() { + let mut vec: VecCell = VecCell::new(); + + vec.push(2); + vec.push(5); + + let range = vec.borrow_range(..); + + let s: String = serde_json::to_string(&vec).unwrap(); + + assert_eq!(serde_json::from_str::>(&s).unwrap(), vec![2, 5]); + + let new_vec: VecCell = serde_json::from_str(&s).unwrap(); + + assert_eq!(new_vec.len(), 2); + assert_eq!(new_vec.borrows(), 0); + drop(range); + assert!(new_vec.mut_borrow().is_none()); + assert_eq!(new_vec, vec![2, 5]); +}