From 71256237ee246dce6617ea55bb9695ac66fa3334 Mon Sep 17 00:00:00 2001 From: Diogo Sousa Date: Tue, 20 Feb 2024 21:37:02 +0000 Subject: [PATCH] Implement serde support. Closes #31. --- Cargo.toml | 7 +++++-- README.md | 9 +++++++++ src/lib.rs | 9 +++++++++ src/shared_pointer/mod.rs | 29 +++++++++++++++++++++++++++++ src/shared_pointer/test.rs | 11 +++++++++++ 5 files changed, 63 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ac1a99c..e30abe0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,16 +41,19 @@ codecov = { repository = "orium/archery", branch = "main", service = "github" } [dependencies] static_assertions = "1.1.0" -triomphe = { version = "0.1.9", default-features = false, optional = true } +triomphe = { version = "0.1.9", optional = true, default-features = false } +serde = { version = "1.0.197", optional = true, default-features = false } [dev-dependencies] criterion = { version = "0.5.1", features = ["html_reports"] } pretty_assertions = "1.2.1" compiletest_rs = "0.10.2" +bincode = "1.3.3" [features] fatal-warnings = [] triomphe = ["dep:triomphe"] +serde = ["dep:serde"] [lints.clippy] all = { level = "warn", priority = -1 } @@ -102,4 +105,4 @@ harness = false required-features = ["triomphe"] [package.metadata.docs.rs] -features = ["triomphe"] +features = ["triomphe", "serde"] diff --git a/README.md b/README.md index 02c8fe5..70d18bd 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,15 @@ about it. To use it you need to enable the `triomphe` feature in `archery`. Use `ArcTK` as the pointer kind in [`SharedPointer`](https://docs.rs/archery/latest/archery/shared_pointer/struct.SharedPointer.html). +### Serialization + +We support serialization through [serde](https://crates.io/crates/serde). To use it +enable the `serde` feature. To do so change the archery dependency in your `Cargo.toml` to + +```toml +[dependencies] +archery = { version = "", features = ["serde"] } +``` ## Limitations Currently it is not possible to have unsized types inside a diff --git a/src/lib.rs b/src/lib.rs index b077f94..74dc111 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -98,6 +98,15 @@ //! To use it you need to enable the `triomphe` feature in `archery`. Use `ArcTK` as the pointer //! kind in [`SharedPointer`](crate::shared_pointer::SharedPointer). //! +//! ## Serialization +//! +//! We support serialization through [serde](https://crates.io/crates/serde). To use it +//! enable the `serde` feature. To do so change the archery dependency in your `Cargo.toml` to +//! +//! ```toml +//! [dependencies] +//! archery = { version = "", features = ["serde"] } +//! ``` //! # Limitations //! //! Currently it is not possible to have unsized types inside a diff --git a/src/shared_pointer/mod.rs b/src/shared_pointer/mod.rs index 9c42901..7f9e407 100644 --- a/src/shared_pointer/mod.rs +++ b/src/shared_pointer/mod.rs @@ -354,5 +354,34 @@ where pub mod kind; +#[cfg(feature = "serde")] +pub mod serde { + use super::*; + use ::serde::de::{Deserialize, Deserializer}; + use ::serde::ser::{Serialize, Serializer}; + + impl Serialize for SharedPointer + where + T: Serialize, + P: SharedPointerKind, + { + fn serialize(&self, serializer: S) -> Result { + self.as_ref().serialize(serializer) + } + } + + impl<'de, T, P> Deserialize<'de> for SharedPointer + where + T: Deserialize<'de>, + P: SharedPointerKind, + { + fn deserialize>( + deserializer: D, + ) -> Result, D::Error> { + T::deserialize(deserializer).map(SharedPointer::new) + } + } +} + #[cfg(test)] mod test; diff --git a/src/shared_pointer/test.rs b/src/shared_pointer/test.rs index 361060b..1853dd4 100644 --- a/src/shared_pointer/test.rs +++ b/src/shared_pointer/test.rs @@ -290,3 +290,14 @@ fn test_display() { assert_eq!(format!("{ptr}"), "hello"); } + +#[cfg(feature = "serde")] +#[test] +fn test_serde() { + use bincode::{deserialize, serialize}; + let ptr: SharedPointer<_, RcK> = SharedPointer::new("hello"); + let encoded = serialize(&ptr).unwrap(); + let decoded: SharedPointer<_, RcK> = deserialize(&encoded).unwrap(); + + pretty_assertions::assert_eq!(ptr, decoded); +}