Skip to content

Commit

Permalink
Implement (De)Serialization for Pinned Smart Pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
Astralchroma committed Apr 20, 2024
1 parent 9989d8e commit 9c2dac7
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
2 changes: 2 additions & 0 deletions serde_with/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

* Implement (De)Serialization for Pinned Smart Pointers by @Astralchroma (#733)

## [3.7.0] - 2024-03-11

### Added
Expand Down
28 changes: 28 additions & 0 deletions serde_with/src/de/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,25 @@ pub(crate) mod macros {
///////////////////////////////////////////////////////////////////////////////
// region: Simple Wrapper types (e.g., Box, Option)

#[allow(unused_macros)]
macro_rules! pinned_wrapper {
($wrapper:ident) => {
impl<'de, T, U> DeserializeAs<'de, Pin<$wrapper<T>>> for Pin<$wrapper<U>>
where
U: DeserializeAs<'de, T>,
{
fn deserialize_as<D>(deserializer: D) -> Result<Pin<$wrapper<T>>, D::Error>
where
D: Deserializer<'de>,
{
Ok($wrapper::pin(
DeserializeAsWrap::<T, U>::deserialize(deserializer)?.into_inner(),
))
}
}
};
}

#[cfg(feature = "alloc")]
impl<'de, T, U> DeserializeAs<'de, Box<T>> for Box<U>
where
Expand All @@ -125,6 +144,9 @@ where
}
}

#[cfg(feature = "alloc")]
pinned_wrapper!(Box);

impl<'de, T, U> DeserializeAs<'de, Option<T>> for Option<U>
where
U: DeserializeAs<'de, T>,
Expand Down Expand Up @@ -207,6 +229,9 @@ where
}
}

#[cfg(feature = "alloc")]
pinned_wrapper!(Rc);

#[cfg(feature = "alloc")]
impl<'de, T, U> DeserializeAs<'de, RcWeak<T>> for RcWeak<U>
where
Expand Down Expand Up @@ -236,6 +261,9 @@ where
}
}

#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))]
pinned_wrapper!(Arc);

#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))]
impl<'de, T, U> DeserializeAs<'de, ArcWeak<T>> for ArcWeak<U>
where
Expand Down
1 change: 1 addition & 0 deletions serde_with/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ pub(crate) mod prelude {
marker::PhantomData,
ops::Bound,
option::Option,
pin::Pin,
result::Result,
str::FromStr,
time::Duration,
Expand Down
26 changes: 26 additions & 0 deletions serde_with/src/ser/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@ pub(crate) mod macros {
///////////////////////////////////////////////////////////////////////////////
// region: Simple Wrapper types (e.g., Box, Option)

#[allow(unused_macros)]
macro_rules! pinned_wrapper {
($wrapper:ident) => {
impl<T, U> SerializeAs<Pin<$wrapper<T>>> for Pin<$wrapper<U>>
where
U: SerializeAs<T>,
{
fn serialize_as<S>(source: &Pin<$wrapper<T>>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
SerializeAsWrap::<T, U>::new(source).serialize(serializer)
}
}
};
}

impl<'a, T, U> SerializeAs<&'a T> for &'a U
where
U: SerializeAs<T>,
Expand Down Expand Up @@ -125,6 +142,9 @@ where
}
}

#[cfg(feature = "alloc")]
pinned_wrapper!(Box);

impl<T, U> SerializeAs<Option<T>> for Option<U>
where
U: SerializeAs<T>,
Expand Down Expand Up @@ -171,6 +191,9 @@ where
}
}

#[cfg(feature = "alloc")]
pinned_wrapper!(Rc);

#[cfg(feature = "alloc")]
impl<T, U> SerializeAs<RcWeak<T>> for RcWeak<U>
where
Expand Down Expand Up @@ -198,6 +221,9 @@ where
}
}

#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))]
pinned_wrapper!(Arc);

#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))]
impl<T, U> SerializeAs<ArcWeak<T>> for ArcWeak<U>
where
Expand Down
19 changes: 19 additions & 0 deletions serde_with/tests/serde_as/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use alloc::{
use core::{
cell::{Cell, RefCell},
ops::Bound,
pin::Pin,
};
use expect_test::expect;
use serde::{Deserialize, Serialize};
Expand All @@ -44,12 +45,24 @@ fn test_basic_wrappers() {

is_equal(SBox(Box::new(123)), expect![[r#""123""#]]);

#[serde_as]
#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct SPinBox(#[serde_as(as = "Pin<Box<DisplayFromStr>>")] Pin<Box<u32>>);

is_equal(SPinBox(Box::pin(123)), expect![[r#""123""#]]);

#[serde_as]
#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct SRc(#[serde_as(as = "Rc<DisplayFromStr>")] Rc<u32>);

is_equal(SRc(Rc::new(123)), expect![[r#""123""#]]);

#[serde_as]
#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct SPinRc(#[serde_as(as = "Pin<Rc<DisplayFromStr>>")] Pin<Rc<u32>>);

is_equal(SPinRc(Rc::pin(123)), expect![[r#""123""#]]);

#[serde_as]
#[derive(Debug, Serialize, Deserialize)]
struct SRcWeak(#[serde_as(as = "RcWeak<DisplayFromStr>")] RcWeak<u32>);
Expand All @@ -66,6 +79,12 @@ fn test_basic_wrappers() {

is_equal(SArc(Arc::new(123)), expect![[r#""123""#]]);

#[serde_as]
#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct SPinArc(#[serde_as(as = "Pin<Arc<DisplayFromStr>>")] Pin<Arc<u32>>);

is_equal(SPinArc(Arc::pin(123)), expect![[r#""123""#]]);

#[serde_as]
#[derive(Debug, Serialize, Deserialize)]
struct SArcWeak(#[serde_as(as = "ArcWeak<DisplayFromStr>")] ArcWeak<u32>);
Expand Down

0 comments on commit 9c2dac7

Please sign in to comment.