mirror of
https://github.com/serde-rs/serde.git
synced 2025-10-02 23:35:26 +00:00
Merge pull request #765 from serde-rs/with
Pair serialize_with and deserialize_with into one attribute
This commit is contained in:
commit
afa6dfbbe2
@ -453,6 +453,18 @@ impl Field {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Parse `#[serde(with="...")]`
|
||||||
|
MetaItem(NameValue(ref name, ref lit)) if name == "with" => {
|
||||||
|
if let Ok(path) = parse_lit_into_path(cx, name.as_ref(), lit) {
|
||||||
|
let mut ser_path = path.clone();
|
||||||
|
ser_path.segments.push("serialize".into());
|
||||||
|
serialize_with.set(ser_path);
|
||||||
|
let mut de_path = path;
|
||||||
|
de_path.segments.push("deserialize".into());
|
||||||
|
deserialize_with.set(de_path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Parse `#[serde(bound="D: Serialize")]`
|
// Parse `#[serde(bound="D: Serialize")]`
|
||||||
MetaItem(NameValue(ref name, ref lit)) if name == "bound" => {
|
MetaItem(NameValue(ref name, ref lit)) if name == "bound" => {
|
||||||
if let Ok(where_predicates) =
|
if let Ok(where_predicates) =
|
||||||
|
@ -30,6 +30,14 @@ fn test_gen() {
|
|||||||
}
|
}
|
||||||
assert::<With<i32>>();
|
assert::<With<i32>>();
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
struct WithTogether<T> {
|
||||||
|
t: T,
|
||||||
|
#[serde(with="both_x")]
|
||||||
|
x: X,
|
||||||
|
}
|
||||||
|
assert::<WithTogether<i32>>();
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct WithRef<'a, T: 'a> {
|
struct WithRef<'a, T: 'a> {
|
||||||
#[serde(skip_deserializing)]
|
#[serde(skip_deserializing)]
|
||||||
@ -307,16 +315,20 @@ trait DeserializeWith: Sized {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Implements neither Serialize nor Deserialize
|
// Implements neither Serialize nor Deserialize
|
||||||
struct X;
|
pub struct X;
|
||||||
|
|
||||||
fn ser_x<S: Serializer>(_: &X, _: S) -> StdResult<S::Ok, S::Error> {
|
pub fn ser_x<S: Serializer>(_: &X, _: S) -> StdResult<S::Ok, S::Error> {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn de_x<D: Deserializer>(_: D) -> StdResult<X, D::Error> {
|
pub fn de_x<D: Deserializer>(_: D) -> StdResult<X, D::Error> {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mod both_x {
|
||||||
|
pub use super::{ser_x as serialize, de_x as deserialize};
|
||||||
|
}
|
||||||
|
|
||||||
impl SerializeWith for X {
|
impl SerializeWith for X {
|
||||||
fn serialize_with<S: Serializer>(_: &Self, _: S) -> StdResult<S::Ok, S::Error> {
|
fn serialize_with<S: Serializer>(_: &Self, _: S) -> StdResult<S::Ok, S::Error> {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user