Skip to content

Commit

Permalink
Add support for core::ops::Bound
Browse files Browse the repository at this point in the history
  • Loading branch information
qsantos committed Oct 14, 2023
1 parent 04bf8f3 commit e144133
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
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

0 comments on commit e144133

Please sign in to comment.