mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-26 02:47:56 +00:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3ae2bee272 | |||
| d0fb958e99 | |||
| b941c63a53 | |||
| cf70c3fb05 | |||
| f249e72162 | |||
| 92e0b62c6b | |||
| 4c29eea790 |
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde"
|
name = "serde"
|
||||||
version = "1.0.96" # remember to update html_root_url
|
version = "1.0.97" # remember to update html_root_url
|
||||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
description = "A generic serialization/deserialization framework"
|
description = "A generic serialization/deserialization framework"
|
||||||
|
|||||||
@@ -69,7 +69,12 @@ fn main() {
|
|||||||
println!("cargo:rustc-cfg=num_nonzero");
|
println!("cargo:rustc-cfg=num_nonzero");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TryFrom and Atomic types stabilized in Rust 1.34:
|
||||||
|
// https://blog.rust-lang.org/2019/04/11/Rust-1.34.0.html#tryfrom-and-tryinto
|
||||||
|
// https://blog.rust-lang.org/2019/04/11/Rust-1.34.0.html#library-stabilizations
|
||||||
if minor >= 34 {
|
if minor >= 34 {
|
||||||
|
println!("cargo:rustc-cfg=core_try_from");
|
||||||
|
|
||||||
// Whitelist of archs that support std::sync::atomic module. Ideally we
|
// Whitelist of archs that support std::sync::atomic module. Ideally we
|
||||||
// would use #[cfg(target_has_atomic = "...")] but it is not stable yet.
|
// would use #[cfg(target_has_atomic = "...")] but it is not stable yet.
|
||||||
// Instead this is based on rustc's src/librustc_target/spec/*.rs.
|
// Instead this is based on rustc's src/librustc_target/spec/*.rs.
|
||||||
|
|||||||
@@ -11,6 +11,9 @@ pub use self::string::from_utf8_lossy;
|
|||||||
#[cfg(any(feature = "alloc", feature = "std"))]
|
#[cfg(any(feature = "alloc", feature = "std"))]
|
||||||
pub use lib::{ToString, Vec};
|
pub use lib::{ToString, Vec};
|
||||||
|
|
||||||
|
#[cfg(core_try_from)]
|
||||||
|
pub use lib::convert::TryFrom;
|
||||||
|
|
||||||
mod string {
|
mod string {
|
||||||
use lib::*;
|
use lib::*;
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -73,7 +73,7 @@
|
|||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
// Serde types in rustdoc of other crates get linked to here.
|
// Serde types in rustdoc of other crates get linked to here.
|
||||||
#![doc(html_root_url = "https://docs.rs/serde/1.0.96")]
|
#![doc(html_root_url = "https://docs.rs/serde/1.0.97")]
|
||||||
// Support using Serde without the standard library!
|
// Support using Serde without the standard library!
|
||||||
#![cfg_attr(not(feature = "std"), no_std)]
|
#![cfg_attr(not(feature = "std"), no_std)]
|
||||||
// Unstable functionality only if the user asks for it. For tracking and
|
// Unstable functionality only if the user asks for it. For tracking and
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde_derive"
|
name = "serde_derive"
|
||||||
version = "1.0.96" # remember to update html_root_url
|
version = "1.0.97" # remember to update html_root_url
|
||||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
description = "Macros 1.1 implementation of #[derive(Serialize, Deserialize)]"
|
description = "Macros 1.1 implementation of #[derive(Serialize, Deserialize)]"
|
||||||
|
|||||||
@@ -269,6 +269,8 @@ fn deserialize_body(cont: &Container, params: &Parameters) -> Fragment {
|
|||||||
deserialize_transparent(cont, params)
|
deserialize_transparent(cont, params)
|
||||||
} else if let Some(type_from) = cont.attrs.type_from() {
|
} else if let Some(type_from) = cont.attrs.type_from() {
|
||||||
deserialize_from(type_from)
|
deserialize_from(type_from)
|
||||||
|
} else if let Some(type_try_from) = cont.attrs.type_try_from() {
|
||||||
|
deserialize_try_from(type_try_from)
|
||||||
} else if let attr::Identifier::No = cont.attrs.identifier() {
|
} else if let attr::Identifier::No = cont.attrs.identifier() {
|
||||||
match cont.data {
|
match cont.data {
|
||||||
Data::Enum(ref variants) => deserialize_enum(params, variants, &cont.attrs),
|
Data::Enum(ref variants) => deserialize_enum(params, variants, &cont.attrs),
|
||||||
@@ -298,6 +300,7 @@ fn deserialize_in_place_body(cont: &Container, params: &Parameters) -> Option<St
|
|||||||
|
|
||||||
if cont.attrs.transparent()
|
if cont.attrs.transparent()
|
||||||
|| cont.attrs.type_from().is_some()
|
|| cont.attrs.type_from().is_some()
|
||||||
|
|| cont.attrs.type_try_from().is_some()
|
||||||
|| cont.attrs.identifier().is_some()
|
|| cont.attrs.identifier().is_some()
|
||||||
|| cont
|
|| cont
|
||||||
.data
|
.data
|
||||||
@@ -390,6 +393,14 @@ fn deserialize_from(type_from: &syn::Type) -> Fragment {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn deserialize_try_from(type_try_from: &syn::Type) -> Fragment {
|
||||||
|
quote_block! {
|
||||||
|
_serde::export::Result::and_then(
|
||||||
|
<#type_try_from as _serde::Deserialize>::deserialize(__deserializer),
|
||||||
|
|v| _serde::export::TryFrom::try_from(v).map_err(_serde::de::Error::custom))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn deserialize_unit_struct(params: &Parameters, cattrs: &attr::Container) -> Fragment {
|
fn deserialize_unit_struct(params: &Parameters, cattrs: &attr::Container) -> Fragment {
|
||||||
let this = ¶ms.this;
|
let this = ¶ms.this;
|
||||||
let type_name = cattrs.name().deserialize_name();
|
let type_name = cattrs.name().deserialize_name();
|
||||||
|
|||||||
@@ -215,6 +215,7 @@ pub struct Container {
|
|||||||
de_bound: Option<Vec<syn::WherePredicate>>,
|
de_bound: Option<Vec<syn::WherePredicate>>,
|
||||||
tag: TagType,
|
tag: TagType,
|
||||||
type_from: Option<syn::Type>,
|
type_from: Option<syn::Type>,
|
||||||
|
type_try_from: Option<syn::Type>,
|
||||||
type_into: Option<syn::Type>,
|
type_into: Option<syn::Type>,
|
||||||
remote: Option<syn::Path>,
|
remote: Option<syn::Path>,
|
||||||
identifier: Identifier,
|
identifier: Identifier,
|
||||||
@@ -296,6 +297,7 @@ impl Container {
|
|||||||
let mut internal_tag = Attr::none(cx, "tag");
|
let mut internal_tag = Attr::none(cx, "tag");
|
||||||
let mut content = Attr::none(cx, "content");
|
let mut content = Attr::none(cx, "content");
|
||||||
let mut type_from = Attr::none(cx, "from");
|
let mut type_from = Attr::none(cx, "from");
|
||||||
|
let mut type_try_from = Attr::none(cx, "try_from");
|
||||||
let mut type_into = Attr::none(cx, "into");
|
let mut type_into = Attr::none(cx, "into");
|
||||||
let mut remote = Attr::none(cx, "remote");
|
let mut remote = Attr::none(cx, "remote");
|
||||||
let mut field_identifier = BoolAttr::none(cx, "field_identifier");
|
let mut field_identifier = BoolAttr::none(cx, "field_identifier");
|
||||||
@@ -557,6 +559,13 @@ impl Container {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Parse `#[serde(try_from = "Type")]
|
||||||
|
Meta(NameValue(ref m)) if m.ident == "try_from" => {
|
||||||
|
if let Ok(try_from_ty) = parse_lit_into_ty(cx, &m.ident, &m.lit) {
|
||||||
|
type_try_from.set_opt(&m.ident, Some(try_from_ty));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Parse `#[serde(into = "Type")]
|
// Parse `#[serde(into = "Type")]
|
||||||
Meta(NameValue(ref m)) if m.ident == "into" => {
|
Meta(NameValue(ref m)) if m.ident == "into" => {
|
||||||
if let Ok(into_ty) = parse_lit_into_ty(cx, &m.ident, &m.lit) {
|
if let Ok(into_ty) = parse_lit_into_ty(cx, &m.ident, &m.lit) {
|
||||||
@@ -619,6 +628,7 @@ impl Container {
|
|||||||
de_bound: de_bound.get(),
|
de_bound: de_bound.get(),
|
||||||
tag: decide_tag(cx, item, untagged, internal_tag, content),
|
tag: decide_tag(cx, item, untagged, internal_tag, content),
|
||||||
type_from: type_from.get(),
|
type_from: type_from.get(),
|
||||||
|
type_try_from: type_try_from.get(),
|
||||||
type_into: type_into.get(),
|
type_into: type_into.get(),
|
||||||
remote: remote.get(),
|
remote: remote.get(),
|
||||||
identifier: decide_identifier(cx, item, field_identifier, variant_identifier),
|
identifier: decide_identifier(cx, item, field_identifier, variant_identifier),
|
||||||
@@ -663,6 +673,10 @@ impl Container {
|
|||||||
self.type_from.as_ref()
|
self.type_from.as_ref()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn type_try_from(&self) -> Option<&syn::Type> {
|
||||||
|
self.type_try_from.as_ref()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn type_into(&self) -> Option<&syn::Type> {
|
pub fn type_into(&self) -> Option<&syn::Type> {
|
||||||
self.type_into.as_ref()
|
self.type_into.as_ref()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ pub fn check(cx: &Ctxt, cont: &mut Container, derive: Derive) {
|
|||||||
check_internal_tag_field_name_conflict(cx, cont);
|
check_internal_tag_field_name_conflict(cx, cont);
|
||||||
check_adjacent_tag_conflict(cx, cont);
|
check_adjacent_tag_conflict(cx, cont);
|
||||||
check_transparent(cx, cont, derive);
|
check_transparent(cx, cont, derive);
|
||||||
|
check_from_and_try_from(cx, cont);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Getters are only allowed inside structs (not enums) with the `remote`
|
/// Getters are only allowed inside structs (not enums) with the `remote`
|
||||||
@@ -330,6 +331,13 @@ fn check_transparent(cx: &Ctxt, cont: &mut Container, derive: Derive) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cont.attrs.type_try_from().is_some() {
|
||||||
|
cx.error_spanned_by(
|
||||||
|
cont.original,
|
||||||
|
"#[serde(transparent)] is not allowed with #[serde(try_from = \"...\")]",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if cont.attrs.type_into().is_some() {
|
if cont.attrs.type_into().is_some() {
|
||||||
cx.error_spanned_by(
|
cx.error_spanned_by(
|
||||||
cont.original,
|
cont.original,
|
||||||
@@ -410,3 +418,12 @@ fn allow_transparent(field: &Field, derive: Derive) -> bool {
|
|||||||
Derive::Deserialize => !field.attrs.skip_deserializing() && field.attrs.default().is_none(),
|
Derive::Deserialize => !field.attrs.skip_deserializing() && field.attrs.default().is_none(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn check_from_and_try_from(cx: &Ctxt, cont: &mut Container) {
|
||||||
|
if cont.attrs.type_from().is_some() && cont.attrs.type_try_from().is_some() {
|
||||||
|
cx.error_spanned_by(
|
||||||
|
cont.original,
|
||||||
|
"#[serde(from = \"...\")] and #[serde(try_from = \"...\")] conflict with each other",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@
|
|||||||
//!
|
//!
|
||||||
//! [https://serde.rs/derive.html]: https://serde.rs/derive.html
|
//! [https://serde.rs/derive.html]: https://serde.rs/derive.html
|
||||||
|
|
||||||
#![doc(html_root_url = "https://docs.rs/serde_derive/1.0.96")]
|
#![doc(html_root_url = "https://docs.rs/serde_derive/1.0.97")]
|
||||||
#![allow(unknown_lints, bare_trait_objects)]
|
#![allow(unknown_lints, bare_trait_objects)]
|
||||||
#![cfg_attr(feature = "cargo-clippy", allow(renamed_and_removed_lints))]
|
#![cfg_attr(feature = "cargo-clippy", allow(renamed_and_removed_lints))]
|
||||||
#![cfg_attr(feature = "cargo-clippy", deny(clippy, clippy_pedantic))]
|
#![cfg_attr(feature = "cargo-clippy", deny(clippy, clippy_pedantic))]
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde_test"
|
name = "serde_test"
|
||||||
version = "1.0.96" # remember to update html_root_url
|
version = "1.0.97" # remember to update html_root_url
|
||||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
description = "Token De/Serializer for testing De/Serialize implementations"
|
description = "Token De/Serializer for testing De/Serialize implementations"
|
||||||
|
|||||||
@@ -144,7 +144,7 @@
|
|||||||
//! # }
|
//! # }
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
#![doc(html_root_url = "https://docs.rs/serde_test/1.0.96")]
|
#![doc(html_root_url = "https://docs.rs/serde_test/1.0.97")]
|
||||||
#![cfg_attr(feature = "cargo-clippy", allow(renamed_and_removed_lints))]
|
#![cfg_attr(feature = "cargo-clippy", allow(renamed_and_removed_lints))]
|
||||||
#![cfg_attr(feature = "cargo-clippy", deny(clippy, clippy_pedantic))]
|
#![cfg_attr(feature = "cargo-clippy", deny(clippy, clippy_pedantic))]
|
||||||
// Ignored clippy lints
|
// Ignored clippy lints
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ use serde::de::{self, MapAccess, Unexpected, Visitor};
|
|||||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||||
|
|
||||||
use std::collections::{BTreeMap, HashMap};
|
use std::collections::{BTreeMap, HashMap};
|
||||||
|
use std::convert::TryFrom;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
@@ -1588,14 +1589,35 @@ impl From<Option<u32>> for EnumToU32 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Deserialize, PartialEq, Debug)]
|
||||||
|
#[serde(try_from = "u32")]
|
||||||
|
enum TryFromU32 {
|
||||||
|
One,
|
||||||
|
Two,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<u32> for TryFromU32 {
|
||||||
|
type Error = String;
|
||||||
|
|
||||||
|
fn try_from(value: u32) -> Result<Self, Self::Error> {
|
||||||
|
match value {
|
||||||
|
1 => Ok(TryFromU32::One),
|
||||||
|
2 => Ok(TryFromU32::Two),
|
||||||
|
_ => Err("out of range".to_owned()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_from_into_traits() {
|
fn test_from_into_traits() {
|
||||||
assert_ser_tokens::<EnumToU32>(&EnumToU32::One, &[Token::Some, Token::U32(1)]);
|
assert_ser_tokens(&EnumToU32::One, &[Token::Some, Token::U32(1)]);
|
||||||
assert_ser_tokens::<EnumToU32>(&EnumToU32::Nothing, &[Token::None]);
|
assert_ser_tokens(&EnumToU32::Nothing, &[Token::None]);
|
||||||
assert_de_tokens::<EnumToU32>(&EnumToU32::Two, &[Token::Some, Token::U32(2)]);
|
assert_de_tokens(&EnumToU32::Two, &[Token::Some, Token::U32(2)]);
|
||||||
assert_ser_tokens::<StructFromEnum>(&StructFromEnum(Some(5)), &[Token::None]);
|
assert_ser_tokens(&StructFromEnum(Some(5)), &[Token::None]);
|
||||||
assert_ser_tokens::<StructFromEnum>(&StructFromEnum(None), &[Token::None]);
|
assert_ser_tokens(&StructFromEnum(None), &[Token::None]);
|
||||||
assert_de_tokens::<StructFromEnum>(&StructFromEnum(Some(2)), &[Token::Some, Token::U32(2)]);
|
assert_de_tokens(&StructFromEnum(Some(2)), &[Token::Some, Token::U32(2)]);
|
||||||
|
assert_de_tokens(&TryFromU32::Two, &[Token::U32(2)]);
|
||||||
|
assert_de_tokens_error::<TryFromU32>(&[Token::U32(5)], "out of range");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
use serde_derive::Serialize;
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
#[serde(from = "u64", try_from = "u64")]
|
||||||
|
struct S {
|
||||||
|
a: u8,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
error: #[serde(from = "...")] and #[serde(try_from = "...")] conflict with each other
|
||||||
|
--> $DIR/from-try-from.rs:4:1
|
||||||
|
|
|
||||||
|
4 | / #[serde(from = "u64", try_from = "u64")]
|
||||||
|
5 | | struct S {
|
||||||
|
6 | | a: u8,
|
||||||
|
7 | | }
|
||||||
|
| |_^
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
use serde_derive::Serialize;
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
#[serde(transparent, try_from = "u64")]
|
||||||
|
struct S {
|
||||||
|
a: u8,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
error: #[serde(transparent)] is not allowed with #[serde(try_from = "...")]
|
||||||
|
--> $DIR/with_try_from.rs:4:1
|
||||||
|
|
|
||||||
|
4 | / #[serde(transparent, try_from = "u64")]
|
||||||
|
5 | | struct S {
|
||||||
|
6 | | a: u8,
|
||||||
|
7 | | }
|
||||||
|
| |_^
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
use serde_derive::Deserialize;
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
#[serde(try_from = "Option<T")]
|
||||||
|
enum TestOne {
|
||||||
|
Testing,
|
||||||
|
One,
|
||||||
|
Two,
|
||||||
|
Three,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
error: failed to parse type: try_from = "Option<T"
|
||||||
|
--> $DIR/try_from.rs:4:20
|
||||||
|
|
|
||||||
|
4 | #[serde(try_from = "Option<T")]
|
||||||
|
| ^^^^^^^^^^
|
||||||
Reference in New Issue
Block a user