Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for core::ops::Bound #655

Merged
merged 1 commit into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions serde_with/src/de/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,24 @@ where
}
}

impl<'de, T, U> DeserializeAs<'de, Bound<T>> for Bound<U>
where
U: DeserializeAs<'de, T>,
{
fn deserialize_as<D>(deserializer: D) -> Result<Bound<T>, D::Error>
where
D: Deserializer<'de>,
{
Ok(
match Bound::<DeserializeAsWrap<T, U>>::deserialize(deserializer)? {
Bound::Unbounded => Bound::Unbounded,
Bound::Included(v) => Bound::Included(v.into_inner()),
Bound::Excluded(v) => Bound::Excluded(v.into_inner()),
},
)
}
}

#[cfg(feature = "alloc")]
impl<'de, T, U> DeserializeAs<'de, Rc<T>> for Rc<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 @@ -403,6 +403,7 @@ pub(crate) mod prelude {
fmt::{self, Display},
hash::{BuildHasher, Hash},
marker::PhantomData,
ops::Bound,
option::Option,
result::Result,
str::FromStr,
Expand Down
18 changes: 18 additions & 0 deletions serde_with/src/ser/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,24 @@ where
}
}

impl<T, U> SerializeAs<Bound<T>> for Bound<U>
where
U: SerializeAs<T>,
T: Sized,
{
fn serialize_as<S>(source: &Bound<T>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match source {
Bound::Unbounded => Bound::Unbounded,
Bound::Included(ref v) => Bound::Included(SerializeAsWrap::<T, U>::new(v)),
Bound::Excluded(ref v) => Bound::Excluded(SerializeAsWrap::<T, U>::new(v)),
}
.serialize(serializer)
}
}

#[cfg(feature = "alloc")]
impl<T, U> SerializeAs<Rc<T>> for Rc<U>
where
Expand Down
36 changes: 36 additions & 0 deletions serde_with/tests/serde_as/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use alloc::{
sync::{Arc, Weak as ArcWeak},
};
use core::cell::{Cell, RefCell};
use core::ops::Bound;
use expect_test::expect;
use serde::{Deserialize, Serialize};
use serde_with::{
Expand Down Expand Up @@ -136,6 +137,41 @@ fn test_option() {
);
}

#[test]
fn test_bound() {
#[serde_as]
#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct S(#[serde_as(as = "Bound<DisplayFromStr>")] Bound<u32>);

is_equal(S(Bound::Unbounded), expect![[r#""Unbounded""#]]);
is_equal(
S(Bound::Included(42)),
expect![[r#"
{
"Included": "42"
}"#]],
);
is_equal(
S(Bound::Excluded(42)),
expect![[r#"
{
"Excluded": "42"
}"#]],
);
check_error_deserialization::<S>(r#"{}"#, expect![[r#"expected value at line 1 column 2"#]]);

#[serde_as]
#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct Struct {
#[serde_as(as = "Bound<DisplayFromStr>")]
value: Bound<u32>,
}
check_error_deserialization::<Struct>(
r#"{}"#,
expect![[r#"missing field `value` at line 1 column 2"#]],
);
}

#[test]
fn test_result() {
#[serde_as]
Expand Down