mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-23 17:38:04 +00:00
Compare commits
55 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 107018c628 | |||
| a398237930 | |||
| b63c65d7f5 | |||
| f60324e883 | |||
| 361c23a09a | |||
| 43b23c7ea0 | |||
| 6081497506 | |||
| 48e5753e76 | |||
| bbba632ab3 | |||
| e77db40b8d | |||
| 1aebdc2760 | |||
| 705e58be8c | |||
| 7c2c12aa43 | |||
| a0f850f15b | |||
| fccb9499bc | |||
| a139ab2572 | |||
| 1d910a484c | |||
| ee9166ec97 | |||
| b5a9eff32e | |||
| 9441a29663 | |||
| ab6588ef74 | |||
| 1d11f03449 | |||
| e11d01fe1d | |||
| a901f50850 | |||
| c399e9c368 | |||
| 25381be0c9 | |||
| ef2a7c753f | |||
| 99f165b45a | |||
| 2fb5560746 | |||
| bd653ab30c | |||
| b5d68aedaa | |||
| 624879c4c6 | |||
| bd9e9abf35 | |||
| 3e4a23cbd0 | |||
| 6326ceec3f | |||
| 8f4d37c7ec | |||
| 1b8290b318 | |||
| 48193fbccd | |||
| ac8ea72d88 | |||
| f583401284 | |||
| 2d88228b7d | |||
| 0c6a2bbf79 | |||
| a80d830f27 | |||
| 5f3fd9994e | |||
| d6de911855 | |||
| 04af32230e | |||
| 4cb8d079f8 | |||
| 6ab55a1e52 | |||
| acfd19cb46 | |||
| e3058105f0 | |||
| dc200a6450 | |||
| 2c0999a0b9 | |||
| dd460f82a1 | |||
| c3d637f397 | |||
| a803ec1c1f |
@@ -1 +0,0 @@
|
||||
test_suite/tests/expand/*.expanded.rs linguist-generated
|
||||
@@ -149,6 +149,7 @@ jobs:
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: dtolnay/rust-toolchain@miri
|
||||
- run: cargo miri setup
|
||||
- run: cd serde && cargo miri test --features derive,rc,unstable
|
||||
env:
|
||||
MIRIFLAGS: -Zmiri-strict-provenance
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "serde"
|
||||
version = "1.0.157" # remember to update html_root_url and serde_derive dependency
|
||||
version = "1.0.164" # remember to update html_root_url and serde_derive dependency
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
||||
build = "build.rs"
|
||||
categories = ["encoding", "no-std"]
|
||||
@@ -15,7 +15,7 @@ repository = "https://github.com/serde-rs/serde"
|
||||
rust-version = "1.19"
|
||||
|
||||
[dependencies]
|
||||
serde_derive = { version = "=1.0.157", optional = true, path = "../serde_derive" }
|
||||
serde_derive = { version = "=1.0.164", optional = true, path = "../serde_derive" }
|
||||
|
||||
[dev-dependencies]
|
||||
serde_derive = { version = "1.0", path = "../serde_derive" }
|
||||
|
||||
@@ -994,7 +994,8 @@ seq_impl!(
|
||||
HashSet::clear,
|
||||
HashSet::with_capacity_and_hasher(size_hint::cautious(seq.size_hint()), S::default()),
|
||||
HashSet::reserve,
|
||||
HashSet::insert);
|
||||
HashSet::insert
|
||||
);
|
||||
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
seq_impl!(
|
||||
@@ -1409,16 +1410,14 @@ macro_rules! map_impl {
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
map_impl!(
|
||||
BTreeMap<K: Ord, V>,
|
||||
map,
|
||||
BTreeMap::new());
|
||||
map_impl!(BTreeMap<K: Ord, V>, map, BTreeMap::new());
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
map_impl!(
|
||||
HashMap<K: Eq + Hash, V, S: BuildHasher + Default>,
|
||||
map,
|
||||
HashMap::with_capacity_and_hasher(size_hint::cautious(map.size_hint()), S::default()));
|
||||
HashMap::with_capacity_and_hasher(size_hint::cautious(map.size_hint()), S::default())
|
||||
);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
+1
-1
@@ -93,7 +93,7 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Serde types in rustdoc of other crates get linked to here.
|
||||
#![doc(html_root_url = "https://docs.rs/serde/1.0.157")]
|
||||
#![doc(html_root_url = "https://docs.rs/serde/1.0.164")]
|
||||
// Support using Serde without the standard library!
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
// Unstable functionality only if the user asks for it. For tracking and
|
||||
|
||||
+56
-98
@@ -982,9 +982,16 @@ mod content {
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
if field == self.tag {
|
||||
self.visit_bytes(field.as_bytes())
|
||||
}
|
||||
|
||||
fn visit_bytes<E>(self, field: &[u8]) -> Result<Self::Value, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
if field == self.tag.as_bytes() {
|
||||
Ok(TagContentOtherField::Tag)
|
||||
} else if field == self.content {
|
||||
} else if field == self.content.as_bytes() {
|
||||
Ok(TagContentOtherField::Content)
|
||||
} else {
|
||||
Ok(TagContentOtherField::Other)
|
||||
@@ -2186,6 +2193,14 @@ mod content {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'de: 'a, E> Copy for ContentRefDeserializer<'a, 'de, E> {}
|
||||
|
||||
impl<'a, 'de: 'a, E> Clone for ContentRefDeserializer<'a, 'de, E> {
|
||||
fn clone(&self) -> Self {
|
||||
*self
|
||||
}
|
||||
}
|
||||
|
||||
struct EnumRefDeserializer<'a, 'de: 'a, E>
|
||||
where
|
||||
E: de::Error,
|
||||
@@ -2731,11 +2746,7 @@ where
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
visitor.visit_map(FlatInternallyTaggedAccess {
|
||||
iter: self.0.iter_mut(),
|
||||
pending: None,
|
||||
_marker: PhantomData,
|
||||
})
|
||||
self.deserialize_map(visitor)
|
||||
}
|
||||
|
||||
fn deserialize_enum<V>(
|
||||
@@ -2747,17 +2758,8 @@ where
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
for item in self.0.iter_mut() {
|
||||
// items in the vector are nulled out when used. So we can only use
|
||||
// an item if it's still filled in and if the field is one we care
|
||||
// about.
|
||||
let use_item = match *item {
|
||||
None => false,
|
||||
Some((ref c, _)) => c.as_str().map_or(false, |x| variants.contains(&x)),
|
||||
};
|
||||
|
||||
if use_item {
|
||||
let (key, value) = item.take().unwrap();
|
||||
for entry in self.0.iter_mut() {
|
||||
if let Some((key, value)) = flat_map_take_entry(entry, variants) {
|
||||
return visitor.visit_enum(EnumDeserializer::new(key, Some(value)));
|
||||
}
|
||||
}
|
||||
@@ -2772,7 +2774,11 @@ where
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
visitor.visit_map(FlatMapAccess::new(self.0.iter()))
|
||||
visitor.visit_map(FlatMapAccess {
|
||||
iter: self.0.iter(),
|
||||
pending_content: None,
|
||||
_marker: PhantomData,
|
||||
})
|
||||
}
|
||||
|
||||
fn deserialize_struct<V>(
|
||||
@@ -2784,7 +2790,12 @@ where
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
visitor.visit_map(FlatStructAccess::new(self.0.iter_mut(), fields))
|
||||
visitor.visit_map(FlatStructAccess {
|
||||
iter: self.0.iter_mut(),
|
||||
pending_content: None,
|
||||
fields: fields,
|
||||
_marker: PhantomData,
|
||||
})
|
||||
}
|
||||
|
||||
fn deserialize_newtype_struct<V>(self, _name: &str, visitor: V) -> Result<V::Value, Self::Error>
|
||||
@@ -2838,25 +2849,12 @@ where
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
pub struct FlatMapAccess<'a, 'de: 'a, E> {
|
||||
struct FlatMapAccess<'a, 'de: 'a, E> {
|
||||
iter: slice::Iter<'a, Option<(Content<'de>, Content<'de>)>>,
|
||||
pending_content: Option<&'a Content<'de>>,
|
||||
_marker: PhantomData<E>,
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
impl<'a, 'de, E> FlatMapAccess<'a, 'de, E> {
|
||||
fn new(
|
||||
iter: slice::Iter<'a, Option<(Content<'de>, Content<'de>)>>,
|
||||
) -> FlatMapAccess<'a, 'de, E> {
|
||||
FlatMapAccess {
|
||||
iter: iter,
|
||||
pending_content: None,
|
||||
_marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
impl<'a, 'de, E> MapAccess<'de> for FlatMapAccess<'a, 'de, E>
|
||||
where
|
||||
@@ -2871,6 +2869,10 @@ where
|
||||
for item in &mut self.iter {
|
||||
// Items in the vector are nulled out when used by a struct.
|
||||
if let Some((ref key, ref content)) = *item {
|
||||
// Do not take(), instead borrow this entry. The internally tagged
|
||||
// enum does its own buffering so we can't tell whether this entry
|
||||
// is going to be consumed. Borrowing here leaves the entry
|
||||
// available for later flattened fields.
|
||||
self.pending_content = Some(content);
|
||||
return seed.deserialize(ContentRefDeserializer::new(key)).map(Some);
|
||||
}
|
||||
@@ -2890,28 +2892,13 @@ where
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
pub struct FlatStructAccess<'a, 'de: 'a, E> {
|
||||
struct FlatStructAccess<'a, 'de: 'a, E> {
|
||||
iter: slice::IterMut<'a, Option<(Content<'de>, Content<'de>)>>,
|
||||
pending_content: Option<Content<'de>>,
|
||||
fields: &'static [&'static str],
|
||||
_marker: PhantomData<E>,
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
impl<'a, 'de, E> FlatStructAccess<'a, 'de, E> {
|
||||
fn new(
|
||||
iter: slice::IterMut<'a, Option<(Content<'de>, Content<'de>)>>,
|
||||
fields: &'static [&'static str],
|
||||
) -> FlatStructAccess<'a, 'de, E> {
|
||||
FlatStructAccess {
|
||||
iter: iter,
|
||||
pending_content: None,
|
||||
fields: fields,
|
||||
_marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
impl<'a, 'de, E> MapAccess<'de> for FlatStructAccess<'a, 'de, E>
|
||||
where
|
||||
@@ -2923,17 +2910,8 @@ where
|
||||
where
|
||||
T: DeserializeSeed<'de>,
|
||||
{
|
||||
while let Some(item) = self.iter.next() {
|
||||
// items in the vector are nulled out when used. So we can only use
|
||||
// an item if it's still filled in and if the field is one we care
|
||||
// about. In case we do not know which fields we want, we take them all.
|
||||
let use_item = match *item {
|
||||
None => false,
|
||||
Some((ref c, _)) => c.as_str().map_or(false, |key| self.fields.contains(&key)),
|
||||
};
|
||||
|
||||
if use_item {
|
||||
let (key, content) = item.take().unwrap();
|
||||
for entry in self.iter.by_ref() {
|
||||
if let Some((key, content)) = flat_map_take_entry(entry, self.fields) {
|
||||
self.pending_content = Some(content);
|
||||
return seed.deserialize(ContentDeserializer::new(key)).map(Some);
|
||||
}
|
||||
@@ -2952,44 +2930,24 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
/// Claims one key-value pair from a FlatMapDeserializer's field buffer if the
|
||||
/// field name matches any of the recognized ones.
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
pub struct FlatInternallyTaggedAccess<'a, 'de: 'a, E> {
|
||||
iter: slice::IterMut<'a, Option<(Content<'de>, Content<'de>)>>,
|
||||
pending: Option<&'a Content<'de>>,
|
||||
_marker: PhantomData<E>,
|
||||
}
|
||||
fn flat_map_take_entry<'de>(
|
||||
entry: &mut Option<(Content<'de>, Content<'de>)>,
|
||||
recognized: &[&str],
|
||||
) -> Option<(Content<'de>, Content<'de>)> {
|
||||
// Entries in the FlatMapDeserializer buffer are nulled out as they get
|
||||
// claimed for deserialization. We only use an entry if it is still present
|
||||
// and if the field is one recognized by the current data structure.
|
||||
let is_recognized = match entry {
|
||||
None => false,
|
||||
Some((k, _v)) => k.as_str().map_or(false, |name| recognized.contains(&name)),
|
||||
};
|
||||
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
impl<'a, 'de, E> MapAccess<'de> for FlatInternallyTaggedAccess<'a, 'de, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
type Error = E;
|
||||
|
||||
fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
|
||||
where
|
||||
T: DeserializeSeed<'de>,
|
||||
{
|
||||
for item in &mut self.iter {
|
||||
if let Some((ref key, ref content)) = *item {
|
||||
// Do not take(), instead borrow this entry. The internally tagged
|
||||
// enum does its own buffering so we can't tell whether this entry
|
||||
// is going to be consumed. Borrowing here leaves the entry
|
||||
// available for later flattened fields.
|
||||
self.pending = Some(content);
|
||||
return seed.deserialize(ContentRefDeserializer::new(key)).map(Some);
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error>
|
||||
where
|
||||
T: DeserializeSeed<'de>,
|
||||
{
|
||||
match self.pending.take() {
|
||||
Some(value) => seed.deserialize(ContentRefDeserializer::new(value)),
|
||||
None => panic!("value is missing"),
|
||||
}
|
||||
if is_recognized {
|
||||
entry.take()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "serde_derive"
|
||||
version = "1.0.157" # remember to update html_root_url
|
||||
version = "1.0.164" # remember to update html_root_url
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
||||
categories = ["no-std"]
|
||||
description = "Macros 1.1 implementation of #[derive(Serialize, Deserialize)]"
|
||||
@@ -24,7 +24,7 @@ proc-macro = true
|
||||
[dependencies]
|
||||
proc-macro2 = "1.0"
|
||||
quote = "1.0"
|
||||
syn = "2.0"
|
||||
syn = "2.0.3"
|
||||
|
||||
[dev-dependencies]
|
||||
serde = { version = "1.0", path = "../serde" }
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
use std::env;
|
||||
use std::process::Command;
|
||||
use std::str;
|
||||
|
||||
// The rustc-cfg strings below are *not* public API. Please let us know by
|
||||
// opening a GitHub issue if your build environment requires some way to enable
|
||||
// these cfgs other than by executing our build script.
|
||||
fn main() {
|
||||
println!("cargo:rerun-if-changed=build.rs");
|
||||
|
||||
let minor = match rustc_minor_version() {
|
||||
Some(minor) => minor,
|
||||
None => return,
|
||||
};
|
||||
|
||||
// Underscore const names stabilized in Rust 1.37:
|
||||
// https://blog.rust-lang.org/2019/08/15/Rust-1.37.0.html#using-unnamed-const-items-for-macros
|
||||
if minor < 37 {
|
||||
println!("cargo:rustc-cfg=no_underscore_consts");
|
||||
}
|
||||
|
||||
// The ptr::addr_of! macro stabilized in Rust 1.51:
|
||||
// https://blog.rust-lang.org/2021/03/25/Rust-1.51.0.html#stabilized-apis
|
||||
if minor < 51 {
|
||||
println!("cargo:rustc-cfg=no_ptr_addr_of");
|
||||
}
|
||||
}
|
||||
|
||||
fn rustc_minor_version() -> Option<u32> {
|
||||
let rustc = env::var_os("RUSTC")?;
|
||||
let output = Command::new(rustc).arg("--version").output().ok()?;
|
||||
let version = str::from_utf8(&output.stdout).ok()?;
|
||||
let mut pieces = version.split('.');
|
||||
if pieces.next() != Some("rustc 1") {
|
||||
return None;
|
||||
}
|
||||
pieces.next()?.parse().ok()
|
||||
}
|
||||
+72
-38
@@ -15,9 +15,7 @@ use this;
|
||||
use std::collections::BTreeSet;
|
||||
use std::ptr;
|
||||
|
||||
pub fn expand_derive_deserialize(
|
||||
input: &mut syn::DeriveInput,
|
||||
) -> Result<TokenStream, Vec<syn::Error>> {
|
||||
pub fn expand_derive_deserialize(input: &mut syn::DeriveInput) -> syn::Result<TokenStream> {
|
||||
replace_receiver(input);
|
||||
|
||||
let ctxt = Ctxt::new();
|
||||
@@ -69,8 +67,6 @@ pub fn expand_derive_deserialize(
|
||||
|
||||
Ok(dummy::wrap_in_const(
|
||||
cont.attrs.custom_serde_path(),
|
||||
"DESERIALIZE",
|
||||
ident,
|
||||
impl_block,
|
||||
))
|
||||
}
|
||||
@@ -419,6 +415,7 @@ fn deserialize_unit_struct(params: &Parameters, cattrs: &attr::Container) -> Fra
|
||||
let expecting = cattrs.expecting().unwrap_or(&expecting);
|
||||
|
||||
quote_block! {
|
||||
#[doc(hidden)]
|
||||
struct __Visitor;
|
||||
|
||||
impl<'de> _serde::de::Visitor<'de> for __Visitor {
|
||||
@@ -515,6 +512,7 @@ fn deserialize_tuple(
|
||||
};
|
||||
|
||||
quote_block! {
|
||||
#[doc(hidden)]
|
||||
struct __Visitor #de_impl_generics #where_clause {
|
||||
marker: _serde::__private::PhantomData<#this_type #ty_generics>,
|
||||
lifetime: _serde::__private::PhantomData<&#delife ()>,
|
||||
@@ -605,6 +603,7 @@ fn deserialize_tuple_in_place(
|
||||
let place_life = place_lifetime();
|
||||
|
||||
quote_block! {
|
||||
#[doc(hidden)]
|
||||
struct __Visitor #in_place_impl_generics #where_clause {
|
||||
place: &#place_life mut #this_type #ty_generics,
|
||||
lifetime: _serde::__private::PhantomData<&#delife ()>,
|
||||
@@ -1020,6 +1019,7 @@ fn deserialize_struct(
|
||||
quote_block! {
|
||||
#field_visitor
|
||||
|
||||
#[doc(hidden)]
|
||||
struct __Visitor #de_impl_generics #where_clause {
|
||||
marker: _serde::__private::PhantomData<#this_type #ty_generics>,
|
||||
lifetime: _serde::__private::PhantomData<&#delife ()>,
|
||||
@@ -1132,6 +1132,7 @@ fn deserialize_struct_in_place(
|
||||
Some(quote_block! {
|
||||
#field_visitor
|
||||
|
||||
#[doc(hidden)]
|
||||
struct __Visitor #in_place_impl_generics #where_clause {
|
||||
place: &#place_life mut #this_type #ty_generics,
|
||||
lifetime: _serde::__private::PhantomData<&#delife ()>,
|
||||
@@ -1165,6 +1166,22 @@ fn deserialize_enum(
|
||||
params: &Parameters,
|
||||
variants: &[Variant],
|
||||
cattrs: &attr::Container,
|
||||
) -> Fragment {
|
||||
// The variants have already been checked (in ast.rs) that all untagged variants appear at the end
|
||||
match variants.iter().position(|var| var.attrs.untagged()) {
|
||||
Some(variant_idx) => {
|
||||
let (tagged, untagged) = variants.split_at(variant_idx);
|
||||
let tagged_frag = Expr(deserialize_homogeneous_enum(params, tagged, cattrs));
|
||||
deserialize_untagged_enum_after(params, untagged, cattrs, Some(tagged_frag))
|
||||
}
|
||||
None => deserialize_homogeneous_enum(params, variants, cattrs),
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_homogeneous_enum(
|
||||
params: &Parameters,
|
||||
variants: &[Variant],
|
||||
cattrs: &attr::Container,
|
||||
) -> Fragment {
|
||||
match cattrs.tag() {
|
||||
attr::TagType::External => deserialize_externally_tagged_enum(params, variants, cattrs),
|
||||
@@ -1203,6 +1220,7 @@ fn prepare_enum_variant_enum(
|
||||
let variants_stmt = {
|
||||
let variant_names = variant_names_idents.iter().map(|(name, _, _)| name);
|
||||
quote! {
|
||||
#[doc(hidden)]
|
||||
const VARIANTS: &'static [&'static str] = &[ #(#variant_names),* ];
|
||||
}
|
||||
};
|
||||
@@ -1275,6 +1293,7 @@ fn deserialize_externally_tagged_enum(
|
||||
quote_block! {
|
||||
#variant_visitor
|
||||
|
||||
#[doc(hidden)]
|
||||
struct __Visitor #de_impl_generics #where_clause {
|
||||
marker: _serde::__private::PhantomData<#this_type #ty_generics>,
|
||||
lifetime: _serde::__private::PhantomData<&#delife ()>,
|
||||
@@ -1522,6 +1541,7 @@ fn deserialize_adjacently_tagged_enum(
|
||||
|
||||
#variants_stmt
|
||||
|
||||
#[doc(hidden)]
|
||||
struct __Seed #de_impl_generics #where_clause {
|
||||
field: __Field,
|
||||
marker: _serde::__private::PhantomData<#this_type #ty_generics>,
|
||||
@@ -1541,6 +1561,7 @@ fn deserialize_adjacently_tagged_enum(
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
struct __Visitor #de_impl_generics #where_clause {
|
||||
marker: _serde::__private::PhantomData<#this_type #ty_generics>,
|
||||
lifetime: _serde::__private::PhantomData<&#delife ()>,
|
||||
@@ -1643,6 +1664,7 @@ fn deserialize_adjacently_tagged_enum(
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
const FIELDS: &'static [&'static str] = &[#tag, #content];
|
||||
_serde::Deserializer::deserialize_struct(
|
||||
__deserializer,
|
||||
@@ -1660,6 +1682,16 @@ fn deserialize_untagged_enum(
|
||||
params: &Parameters,
|
||||
variants: &[Variant],
|
||||
cattrs: &attr::Container,
|
||||
) -> Fragment {
|
||||
let first_attempt = None;
|
||||
deserialize_untagged_enum_after(params, variants, cattrs, first_attempt)
|
||||
}
|
||||
|
||||
fn deserialize_untagged_enum_after(
|
||||
params: &Parameters,
|
||||
variants: &[Variant],
|
||||
cattrs: &attr::Container,
|
||||
first_attempt: Option<Expr>,
|
||||
) -> Fragment {
|
||||
let attempts = variants
|
||||
.iter()
|
||||
@@ -1669,12 +1701,10 @@ fn deserialize_untagged_enum(
|
||||
params,
|
||||
variant,
|
||||
cattrs,
|
||||
quote!(
|
||||
_serde::__private::de::ContentRefDeserializer::<__D::Error>::new(&__content)
|
||||
),
|
||||
quote!(__deserializer),
|
||||
))
|
||||
});
|
||||
|
||||
let attempts = first_attempt.into_iter().chain(attempts);
|
||||
// TODO this message could be better by saving the errors from the failed
|
||||
// attempts. The heuristic used by TOML was to count the number of fields
|
||||
// processed before an error, and use the error that happened after the
|
||||
@@ -1689,6 +1719,7 @@ fn deserialize_untagged_enum(
|
||||
|
||||
quote_block! {
|
||||
let __content = try!(<_serde::__private::de::Content as _serde::Deserialize>::deserialize(__deserializer));
|
||||
let __deserializer = _serde::__private::de::ContentRefDeserializer::<__D::Error>::new(&__content);
|
||||
|
||||
#(
|
||||
if let _serde::__private::Ok(__ok) = #attempts {
|
||||
@@ -1954,11 +1985,13 @@ fn deserialize_generated_identifier(
|
||||
|
||||
quote_block! {
|
||||
#[allow(non_camel_case_types)]
|
||||
#[doc(hidden)]
|
||||
enum __Field #lifetime {
|
||||
#(#field_idents,)*
|
||||
#ignore_variant
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
struct __FieldVisitor;
|
||||
|
||||
impl<'de> _serde::de::Visitor<'de> for __FieldVisitor {
|
||||
@@ -2046,11 +2079,13 @@ fn deserialize_custom_identifier(
|
||||
None
|
||||
} else if is_variant {
|
||||
let variants = quote! {
|
||||
#[doc(hidden)]
|
||||
const VARIANTS: &'static [&'static str] = &[ #(#names),* ];
|
||||
};
|
||||
Some(variants)
|
||||
} else {
|
||||
let fields = quote! {
|
||||
#[doc(hidden)]
|
||||
const FIELDS: &'static [&'static str] = &[ #(#names),* ];
|
||||
};
|
||||
Some(fields)
|
||||
@@ -2072,6 +2107,7 @@ fn deserialize_custom_identifier(
|
||||
quote_block! {
|
||||
#names_const
|
||||
|
||||
#[doc(hidden)]
|
||||
struct __FieldVisitor #de_impl_generics #where_clause {
|
||||
marker: _serde::__private::PhantomData<#this_type #ty_generics>,
|
||||
lifetime: _serde::__private::PhantomData<&#delife ()>,
|
||||
@@ -2406,6 +2442,7 @@ fn deserialize_struct_as_struct_visitor(
|
||||
.flat_map(|(_, _, aliases)| aliases);
|
||||
|
||||
quote_block! {
|
||||
#[doc(hidden)]
|
||||
const FIELDS: &'static [&'static str] = &[ #(#field_names),* ];
|
||||
}
|
||||
};
|
||||
@@ -2684,6 +2721,7 @@ fn deserialize_struct_as_struct_in_place_visitor(
|
||||
let fields_stmt = {
|
||||
let field_names = field_names_idents.iter().map(|(name, _, _)| name);
|
||||
quote_block! {
|
||||
#[doc(hidden)]
|
||||
const FIELDS: &'static [&'static str] = &[ #(#field_names),* ];
|
||||
}
|
||||
};
|
||||
@@ -2864,6 +2902,7 @@ fn wrap_deserialize_with(
|
||||
let delife = params.borrowed.de_lifetime();
|
||||
|
||||
let wrapper = quote! {
|
||||
#[doc(hidden)]
|
||||
struct __DeserializeWith #de_impl_generics #where_clause {
|
||||
value: #value_ty,
|
||||
phantom: _serde::__private::PhantomData<#this_type #ty_generics>,
|
||||
@@ -3068,23 +3107,31 @@ struct DeTypeGenerics<'a>(&'a Parameters);
|
||||
#[cfg(feature = "deserialize_in_place")]
|
||||
struct InPlaceTypeGenerics<'a>(&'a Parameters);
|
||||
|
||||
fn de_type_generics_to_tokens(
|
||||
mut generics: syn::Generics,
|
||||
borrowed: &BorrowedLifetimes,
|
||||
tokens: &mut TokenStream,
|
||||
) {
|
||||
if borrowed.de_lifetime_param().is_some() {
|
||||
let def = syn::LifetimeParam {
|
||||
attrs: Vec::new(),
|
||||
lifetime: syn::Lifetime::new("'de", Span::call_site()),
|
||||
colon_token: None,
|
||||
bounds: Punctuated::new(),
|
||||
};
|
||||
// Prepend 'de lifetime to list of generics
|
||||
generics.params = Some(syn::GenericParam::Lifetime(def))
|
||||
.into_iter()
|
||||
.chain(generics.params)
|
||||
.collect();
|
||||
}
|
||||
let (_, ty_generics, _) = generics.split_for_impl();
|
||||
ty_generics.to_tokens(tokens);
|
||||
}
|
||||
|
||||
impl<'a> ToTokens for DeTypeGenerics<'a> {
|
||||
fn to_tokens(&self, tokens: &mut TokenStream) {
|
||||
let mut generics = self.0.generics.clone();
|
||||
if self.0.borrowed.de_lifetime_param().is_some() {
|
||||
let def = syn::LifetimeParam {
|
||||
attrs: Vec::new(),
|
||||
lifetime: syn::Lifetime::new("'de", Span::call_site()),
|
||||
colon_token: None,
|
||||
bounds: Punctuated::new(),
|
||||
};
|
||||
generics.params = Some(syn::GenericParam::Lifetime(def))
|
||||
.into_iter()
|
||||
.chain(generics.params)
|
||||
.collect();
|
||||
}
|
||||
let (_, ty_generics, _) = generics.split_for_impl();
|
||||
ty_generics.to_tokens(tokens);
|
||||
de_type_generics_to_tokens(self.0.generics.clone(), &self.0.borrowed, tokens);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3097,20 +3144,7 @@ impl<'a> ToTokens for InPlaceTypeGenerics<'a> {
|
||||
.chain(generics.params)
|
||||
.collect();
|
||||
|
||||
if self.0.borrowed.de_lifetime_param().is_some() {
|
||||
let def = syn::LifetimeParam {
|
||||
attrs: Vec::new(),
|
||||
lifetime: syn::Lifetime::new("'de", Span::call_site()),
|
||||
colon_token: None,
|
||||
bounds: Punctuated::new(),
|
||||
};
|
||||
generics.params = Some(syn::GenericParam::Lifetime(def))
|
||||
.into_iter()
|
||||
.chain(generics.params)
|
||||
.collect();
|
||||
}
|
||||
let (_, ty_generics, _) = generics.split_for_impl();
|
||||
ty_generics.to_tokens(tokens);
|
||||
de_type_generics_to_tokens(generics, &self.0.borrowed, tokens);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,23 +1,11 @@
|
||||
use proc_macro2::{Ident, TokenStream};
|
||||
use quote::format_ident;
|
||||
use proc_macro2::TokenStream;
|
||||
|
||||
use syn;
|
||||
use try;
|
||||
|
||||
pub fn wrap_in_const(
|
||||
serde_path: Option<&syn::Path>,
|
||||
trait_: &str,
|
||||
ty: &Ident,
|
||||
code: TokenStream,
|
||||
) -> TokenStream {
|
||||
pub fn wrap_in_const(serde_path: Option<&syn::Path>, code: TokenStream) -> TokenStream {
|
||||
let try_replacement = try::replacement();
|
||||
|
||||
let dummy_const = if cfg!(no_underscore_consts) {
|
||||
format_ident!("_IMPL_{}_FOR_{}", trait_, unraw(ty))
|
||||
} else {
|
||||
format_ident!("_")
|
||||
};
|
||||
|
||||
let use_serde = match serde_path {
|
||||
Some(path) => quote! {
|
||||
use #path as _serde;
|
||||
@@ -31,14 +19,10 @@ pub fn wrap_in_const(
|
||||
quote! {
|
||||
#[doc(hidden)]
|
||||
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
|
||||
const #dummy_const: () = {
|
||||
const _: () = {
|
||||
#use_serde
|
||||
#try_replacement
|
||||
#code
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
fn unraw(ident: &Ident) -> String {
|
||||
ident.to_string().trim_start_matches("r#").to_owned()
|
||||
}
|
||||
|
||||
@@ -140,7 +140,7 @@ fn enum_from_ast<'a>(
|
||||
variants: &'a Punctuated<syn::Variant, Token![,]>,
|
||||
container_default: &attr::Default,
|
||||
) -> Vec<Variant<'a>> {
|
||||
variants
|
||||
let variants: Vec<Variant> = variants
|
||||
.iter()
|
||||
.map(|variant| {
|
||||
let attrs = attr::Variant::from_ast(cx, variant);
|
||||
@@ -154,7 +154,20 @@ fn enum_from_ast<'a>(
|
||||
original: variant,
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
.collect();
|
||||
|
||||
let index_of_last_tagged_variant = variants
|
||||
.iter()
|
||||
.rposition(|variant| !variant.attrs.untagged());
|
||||
if let Some(index_of_last_tagged_variant) = index_of_last_tagged_variant {
|
||||
for variant in &variants[..index_of_last_tagged_variant] {
|
||||
if variant.attrs.untagged() {
|
||||
cx.error_spanned_by(&variant.ident, "all variants with the #[serde(untagged)] attribute must be placed at the end of the enum");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
variants
|
||||
}
|
||||
|
||||
fn struct_from_ast<'a>(
|
||||
|
||||
@@ -307,6 +307,12 @@ impl Container {
|
||||
continue;
|
||||
}
|
||||
|
||||
if let syn::Meta::List(meta) = &attr.meta {
|
||||
if meta.tokens.is_empty() {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if let Err(err) = attr.parse_nested_meta(|meta| {
|
||||
if meta.path == RENAME {
|
||||
// #[serde(rename = "foo")]
|
||||
@@ -734,6 +740,7 @@ pub struct Variant {
|
||||
serialize_with: Option<syn::ExprPath>,
|
||||
deserialize_with: Option<syn::ExprPath>,
|
||||
borrow: Option<BorrowAttribute>,
|
||||
untagged: bool,
|
||||
}
|
||||
|
||||
struct BorrowAttribute {
|
||||
@@ -756,12 +763,19 @@ impl Variant {
|
||||
let mut serialize_with = Attr::none(cx, SERIALIZE_WITH);
|
||||
let mut deserialize_with = Attr::none(cx, DESERIALIZE_WITH);
|
||||
let mut borrow = Attr::none(cx, BORROW);
|
||||
let mut untagged = BoolAttr::none(cx, UNTAGGED);
|
||||
|
||||
for attr in &variant.attrs {
|
||||
if attr.path() != SERDE {
|
||||
continue;
|
||||
}
|
||||
|
||||
if let syn::Meta::List(meta) = &attr.meta {
|
||||
if meta.tokens.is_empty() {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if let Err(err) = attr.parse_nested_meta(|meta| {
|
||||
if meta.path == RENAME {
|
||||
// #[serde(rename = "foo")]
|
||||
@@ -867,6 +881,8 @@ impl Variant {
|
||||
cx.error_spanned_by(variant, msg);
|
||||
}
|
||||
}
|
||||
} else if meta.path == UNTAGGED {
|
||||
untagged.set_true(&meta.path);
|
||||
} else {
|
||||
let path = meta.path.to_token_stream().to_string().replace(' ', "");
|
||||
return Err(
|
||||
@@ -893,6 +909,7 @@ impl Variant {
|
||||
serialize_with: serialize_with.get(),
|
||||
deserialize_with: deserialize_with.get(),
|
||||
borrow: borrow.get(),
|
||||
untagged: untagged.get(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -944,6 +961,10 @@ impl Variant {
|
||||
pub fn deserialize_with(&self) -> Option<&syn::ExprPath> {
|
||||
self.deserialize_with.as_ref()
|
||||
}
|
||||
|
||||
pub fn untagged(&self) -> bool {
|
||||
self.untagged
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents field attribute information
|
||||
@@ -1033,6 +1054,12 @@ impl Field {
|
||||
continue;
|
||||
}
|
||||
|
||||
if let syn::Meta::List(meta) = &attr.meta {
|
||||
if meta.tokens.is_empty() {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if let Err(err) = attr.parse_nested_meta(|meta| {
|
||||
if meta.path == RENAME {
|
||||
// #[serde(rename = "foo")]
|
||||
@@ -1381,21 +1408,26 @@ fn get_lit_str2(
|
||||
meta_item_name: Symbol,
|
||||
meta: &ParseNestedMeta,
|
||||
) -> syn::Result<Option<syn::LitStr>> {
|
||||
match meta.value()?.parse()? {
|
||||
syn::Expr::Lit(syn::ExprLit {
|
||||
lit: syn::Lit::Str(lit),
|
||||
..
|
||||
}) => Ok(Some(lit)),
|
||||
expr => {
|
||||
cx.error_spanned_by(
|
||||
expr,
|
||||
format!(
|
||||
"expected serde {} attribute to be a string: `{} = \"...\"`",
|
||||
attr_name, meta_item_name
|
||||
),
|
||||
);
|
||||
Ok(None)
|
||||
}
|
||||
let expr: syn::Expr = meta.value()?.parse()?;
|
||||
let mut value = &expr;
|
||||
while let syn::Expr::Group(e) = value {
|
||||
value = &e.expr;
|
||||
}
|
||||
if let syn::Expr::Lit(syn::ExprLit {
|
||||
lit: syn::Lit::Str(lit),
|
||||
..
|
||||
}) = value
|
||||
{
|
||||
Ok(Some(lit.clone()))
|
||||
} else {
|
||||
cx.error_spanned_by(
|
||||
expr,
|
||||
format!(
|
||||
"expected serde {} attribute to be a string: `{} = \"...\"`",
|
||||
attr_name, meta_item_name
|
||||
),
|
||||
);
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,8 +3,8 @@ use internals::attr::{Identifier, TagType};
|
||||
use internals::{ungroup, Ctxt, Derive};
|
||||
use syn::{Member, Type};
|
||||
|
||||
/// Cross-cutting checks that require looking at more than a single attrs
|
||||
/// object. Simpler checks should happen when parsing and building the attrs.
|
||||
// Cross-cutting checks that require looking at more than a single attrs object.
|
||||
// Simpler checks should happen when parsing and building the attrs.
|
||||
pub fn check(cx: &Ctxt, cont: &mut Container, derive: Derive) {
|
||||
check_remote_generic(cx, cont);
|
||||
check_getter(cx, cont);
|
||||
@@ -17,18 +17,18 @@ pub fn check(cx: &Ctxt, cont: &mut Container, derive: Derive) {
|
||||
check_from_and_try_from(cx, cont);
|
||||
}
|
||||
|
||||
/// Remote derive definition type must have either all of the generics of the
|
||||
/// remote type:
|
||||
///
|
||||
/// #[serde(remote = "Generic")]
|
||||
/// struct Generic<T> {…}
|
||||
///
|
||||
/// or none of them, i.e. defining impls for one concrete instantiation of the
|
||||
/// remote type only:
|
||||
///
|
||||
/// #[serde(remote = "Generic<T>")]
|
||||
/// struct ConcreteDef {…}
|
||||
///
|
||||
// Remote derive definition type must have either all of the generics of the
|
||||
// remote type:
|
||||
//
|
||||
// #[serde(remote = "Generic")]
|
||||
// struct Generic<T> {…}
|
||||
//
|
||||
// or none of them, i.e. defining impls for one concrete instantiation of the
|
||||
// remote type only:
|
||||
//
|
||||
// #[serde(remote = "Generic<T>")]
|
||||
// struct ConcreteDef {…}
|
||||
//
|
||||
fn check_remote_generic(cx: &Ctxt, cont: &Container) {
|
||||
if let Some(remote) = cont.attrs.remote() {
|
||||
let local_has_generic = !cont.generics.params.is_empty();
|
||||
@@ -39,8 +39,8 @@ fn check_remote_generic(cx: &Ctxt, cont: &Container) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Getters are only allowed inside structs (not enums) with the `remote`
|
||||
/// attribute.
|
||||
// Getters are only allowed inside structs (not enums) with the `remote`
|
||||
// attribute.
|
||||
fn check_getter(cx: &Ctxt, cont: &Container) {
|
||||
match cont.data {
|
||||
Data::Enum(_) => {
|
||||
@@ -62,7 +62,7 @@ fn check_getter(cx: &Ctxt, cont: &Container) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Flattening has some restrictions we can test.
|
||||
// Flattening has some restrictions we can test.
|
||||
fn check_flatten(cx: &Ctxt, cont: &Container) {
|
||||
match &cont.data {
|
||||
Data::Enum(variants) => {
|
||||
@@ -101,12 +101,12 @@ fn check_flatten_field(cx: &Ctxt, style: Style, field: &Field) {
|
||||
}
|
||||
}
|
||||
|
||||
/// The `other` attribute must be used at most once and it must be the last
|
||||
/// variant of an enum.
|
||||
///
|
||||
/// Inside a `variant_identifier` all variants must be unit variants. Inside a
|
||||
/// `field_identifier` all but possibly one variant must be unit variants. The
|
||||
/// last variant may be a newtype variant which is an implicit "other" case.
|
||||
// The `other` attribute must be used at most once and it must be the last
|
||||
// variant of an enum.
|
||||
//
|
||||
// Inside a `variant_identifier` all variants must be unit variants. Inside a
|
||||
// `field_identifier` all but possibly one variant must be unit variants. The
|
||||
// last variant may be a newtype variant which is an implicit "other" case.
|
||||
fn check_identifier(cx: &Ctxt, cont: &Container) {
|
||||
let variants = match &cont.data {
|
||||
Data::Enum(variants) => variants,
|
||||
@@ -189,8 +189,8 @@ fn check_identifier(cx: &Ctxt, cont: &Container) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Skip-(de)serializing attributes are not allowed on variants marked
|
||||
/// (de)serialize_with.
|
||||
// Skip-(de)serializing attributes are not allowed on variants marked
|
||||
// (de)serialize_with.
|
||||
fn check_variant_skip_attrs(cx: &Ctxt, cont: &Container) {
|
||||
let variants = match &cont.data {
|
||||
Data::Enum(variants) => variants,
|
||||
@@ -264,10 +264,9 @@ fn check_variant_skip_attrs(cx: &Ctxt, cont: &Container) {
|
||||
}
|
||||
}
|
||||
|
||||
/// The tag of an internally-tagged struct variant must not be
|
||||
/// the same as either one of its fields, as this would result in
|
||||
/// duplicate keys in the serialized output and/or ambiguity in
|
||||
/// the to-be-deserialized input.
|
||||
// The tag of an internally-tagged struct variant must not be the same as either
|
||||
// one of its fields, as this would result in duplicate keys in the serialized
|
||||
// output and/or ambiguity in the to-be-deserialized input.
|
||||
fn check_internal_tag_field_name_conflict(cx: &Ctxt, cont: &Container) {
|
||||
let variants = match &cont.data {
|
||||
Data::Enum(variants) => variants,
|
||||
@@ -313,8 +312,8 @@ fn check_internal_tag_field_name_conflict(cx: &Ctxt, cont: &Container) {
|
||||
}
|
||||
}
|
||||
|
||||
/// In the case of adjacently-tagged enums, the type and the
|
||||
/// contents tag must differ, for the same reason.
|
||||
// In the case of adjacently-tagged enums, the type and the contents tag must
|
||||
// differ, for the same reason.
|
||||
fn check_adjacent_tag_conflict(cx: &Ctxt, cont: &Container) {
|
||||
let (type_tag, content_tag) = match cont.attrs.tag() {
|
||||
TagType::Adjacent { tag, content } => (tag, content),
|
||||
@@ -332,7 +331,7 @@ fn check_adjacent_tag_conflict(cx: &Ctxt, cont: &Container) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Enums and unit structs cannot be transparent.
|
||||
// Enums and unit structs cannot be transparent.
|
||||
fn check_transparent(cx: &Ctxt, cont: &mut Container, derive: Derive) {
|
||||
if !cont.attrs.transparent() {
|
||||
return;
|
||||
|
||||
@@ -44,12 +44,19 @@ impl Ctxt {
|
||||
}
|
||||
|
||||
/// Consume this object, producing a formatted error string if there are errors.
|
||||
pub fn check(self) -> Result<(), Vec<syn::Error>> {
|
||||
let errors = self.errors.borrow_mut().take().unwrap();
|
||||
match errors.len() {
|
||||
0 => Ok(()),
|
||||
_ => Err(errors),
|
||||
pub fn check(self) -> syn::Result<()> {
|
||||
let mut errors = self.errors.borrow_mut().take().unwrap().into_iter();
|
||||
|
||||
let mut combined = match errors.next() {
|
||||
Some(first) => first,
|
||||
None => return Ok(()),
|
||||
};
|
||||
|
||||
for rest in errors {
|
||||
combined.combine(rest);
|
||||
}
|
||||
|
||||
Err(combined)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
//!
|
||||
//! [https://serde.rs/derive.html]: https://serde.rs/derive.html
|
||||
|
||||
#![doc(html_root_url = "https://docs.rs/serde_derive/1.0.157")]
|
||||
#![doc(html_root_url = "https://docs.rs/serde_derive/1.0.164")]
|
||||
#![allow(unknown_lints, bare_trait_objects)]
|
||||
// Ignored clippy lints
|
||||
#![allow(
|
||||
@@ -92,7 +92,7 @@ mod try;
|
||||
pub fn derive_serialize(input: TokenStream) -> TokenStream {
|
||||
let mut input = parse_macro_input!(input as DeriveInput);
|
||||
ser::expand_derive_serialize(&mut input)
|
||||
.unwrap_or_else(to_compile_errors)
|
||||
.unwrap_or_else(syn::Error::into_compile_error)
|
||||
.into()
|
||||
}
|
||||
|
||||
@@ -100,11 +100,6 @@ pub fn derive_serialize(input: TokenStream) -> TokenStream {
|
||||
pub fn derive_deserialize(input: TokenStream) -> TokenStream {
|
||||
let mut input = parse_macro_input!(input as DeriveInput);
|
||||
de::expand_derive_deserialize(&mut input)
|
||||
.unwrap_or_else(to_compile_errors)
|
||||
.unwrap_or_else(syn::Error::into_compile_error)
|
||||
.into()
|
||||
}
|
||||
|
||||
fn to_compile_errors(errors: Vec<syn::Error>) -> proc_macro2::TokenStream {
|
||||
let compile_errors = errors.iter().map(syn::Error::to_compile_error);
|
||||
quote!(#(#compile_errors)*)
|
||||
}
|
||||
|
||||
@@ -97,29 +97,14 @@ fn pretend_fields_used_struct_packed(cont: &Container, fields: &[Field]) -> Toke
|
||||
|
||||
let members = fields.iter().map(|field| &field.member).collect::<Vec<_>>();
|
||||
|
||||
#[cfg(not(no_ptr_addr_of))]
|
||||
{
|
||||
quote! {
|
||||
match _serde::__private::None::<&#type_ident #ty_generics> {
|
||||
_serde::__private::Some(__v @ #type_ident { #(#members: _),* }) => {
|
||||
#(
|
||||
let _ = _serde::__private::ptr::addr_of!(__v.#members);
|
||||
)*
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(no_ptr_addr_of)]
|
||||
{
|
||||
let placeholders = (0usize..).map(|i| format_ident!("__v{}", i));
|
||||
|
||||
quote! {
|
||||
match _serde::__private::None::<#type_ident #ty_generics> {
|
||||
_serde::__private::Some(#type_ident { #(#members: #placeholders),* }) => {}
|
||||
_ => {}
|
||||
quote! {
|
||||
match _serde::__private::None::<&#type_ident #ty_generics> {
|
||||
_serde::__private::Some(__v @ #type_ident { #(#members: _),* }) => {
|
||||
#(
|
||||
let _ = _serde::__private::ptr::addr_of!(__v.#members);
|
||||
)*
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+11
-10
@@ -10,9 +10,7 @@ use internals::{attr, replace_receiver, Ctxt, Derive};
|
||||
use pretend;
|
||||
use this;
|
||||
|
||||
pub fn expand_derive_serialize(
|
||||
input: &mut syn::DeriveInput,
|
||||
) -> Result<TokenStream, Vec<syn::Error>> {
|
||||
pub fn expand_derive_serialize(input: &mut syn::DeriveInput) -> syn::Result<TokenStream> {
|
||||
replace_receiver(input);
|
||||
|
||||
let ctxt = Ctxt::new();
|
||||
@@ -59,8 +57,6 @@ pub fn expand_derive_serialize(
|
||||
|
||||
Ok(dummy::wrap_in_const(
|
||||
cont.attrs.custom_serde_path(),
|
||||
"SERIALIZE",
|
||||
ident,
|
||||
impl_block,
|
||||
))
|
||||
}
|
||||
@@ -477,17 +473,19 @@ fn serialize_variant(
|
||||
}
|
||||
};
|
||||
|
||||
let body = Match(match cattrs.tag() {
|
||||
attr::TagType::External => {
|
||||
let body = Match(match (cattrs.tag(), variant.attrs.untagged()) {
|
||||
(attr::TagType::External, false) => {
|
||||
serialize_externally_tagged_variant(params, variant, variant_index, cattrs)
|
||||
}
|
||||
attr::TagType::Internal { tag } => {
|
||||
(attr::TagType::Internal { tag }, false) => {
|
||||
serialize_internally_tagged_variant(params, variant, cattrs, tag)
|
||||
}
|
||||
attr::TagType::Adjacent { tag, content } => {
|
||||
(attr::TagType::Adjacent { tag, content }, false) => {
|
||||
serialize_adjacently_tagged_variant(params, variant, cattrs, tag, content)
|
||||
}
|
||||
attr::TagType::None => serialize_untagged_variant(params, variant, cattrs),
|
||||
(attr::TagType::None, _) | (_, true) => {
|
||||
serialize_untagged_variant(params, variant, cattrs)
|
||||
}
|
||||
});
|
||||
|
||||
quote! {
|
||||
@@ -719,6 +717,7 @@ fn serialize_adjacently_tagged_variant(
|
||||
let (wrapper_impl_generics, wrapper_ty_generics, _) = wrapper_generics.split_for_impl();
|
||||
|
||||
quote_block! {
|
||||
#[doc(hidden)]
|
||||
struct __AdjacentlyTagged #wrapper_generics #where_clause {
|
||||
data: (#(&'__a #fields_ty,)*),
|
||||
phantom: _serde::__private::PhantomData<#this_type #ty_generics>,
|
||||
@@ -982,6 +981,7 @@ fn serialize_struct_variant_with_flatten(
|
||||
let (wrapper_impl_generics, wrapper_ty_generics, _) = wrapper_generics.split_for_impl();
|
||||
|
||||
quote_block! {
|
||||
#[doc(hidden)]
|
||||
struct __EnumFlatten #wrapper_generics #where_clause {
|
||||
data: (#(&'__a #fields_ty,)*),
|
||||
phantom: _serde::__private::PhantomData<#this_type #ty_generics>,
|
||||
@@ -1212,6 +1212,7 @@ fn wrap_serialize_with(
|
||||
});
|
||||
|
||||
quote!({
|
||||
#[doc(hidden)]
|
||||
struct __SerializeWith #wrapper_impl_generics #where_clause {
|
||||
values: (#(&'__a #field_tys, )*),
|
||||
phantom: _serde::__private::PhantomData<#this_type #ty_generics>,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "serde_derive_internals"
|
||||
version = "0.26.0" # remember to update html_root_url
|
||||
version = "0.28.0" # remember to update html_root_url
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
||||
description = "AST representation used by Serde derive macros. Unstable."
|
||||
documentation = "https://docs.rs/serde_derive_internals"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#![doc(html_root_url = "https://docs.rs/serde_derive_internals/0.26.0")]
|
||||
#![doc(html_root_url = "https://docs.rs/serde_derive_internals/0.28.0")]
|
||||
#![allow(unknown_lints, bare_trait_objects)]
|
||||
// Ignored clippy lints
|
||||
#![allow(
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "serde_test"
|
||||
version = "1.0.157" # remember to update html_root_url
|
||||
version = "1.0.164" # remember to update html_root_url
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
||||
build = "build.rs"
|
||||
categories = ["development-tools::testing"]
|
||||
|
||||
+83
-95
@@ -12,32 +12,29 @@ pub struct Deserializer<'de> {
|
||||
tokens: &'de [Token],
|
||||
}
|
||||
|
||||
macro_rules! assert_next_token {
|
||||
($de:expr, $expected:expr) => {
|
||||
match $de.next_token_opt() {
|
||||
Some(token) if token == $expected => {}
|
||||
Some(other) => panic!(
|
||||
"expected Token::{} but deserialization wants Token::{}",
|
||||
other, $expected
|
||||
),
|
||||
None => panic!(
|
||||
"end of tokens but deserialization wants Token::{}",
|
||||
$expected
|
||||
),
|
||||
}
|
||||
};
|
||||
fn assert_next_token(de: &mut Deserializer, expected: Token) -> Result<(), Error> {
|
||||
match de.next_token_opt() {
|
||||
Some(token) if token == expected => Ok(()),
|
||||
Some(other) => Err(de::Error::custom(format!(
|
||||
"expected Token::{} but deserialization wants Token::{}",
|
||||
other, expected,
|
||||
))),
|
||||
None => Err(de::Error::custom(format!(
|
||||
"end of tokens but deserialization wants Token::{}",
|
||||
expected,
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! unexpected {
|
||||
($token:expr) => {
|
||||
panic!("deserialization did not expect this token: {}", $token)
|
||||
};
|
||||
fn unexpected(token: Token) -> Error {
|
||||
de::Error::custom(format!(
|
||||
"deserialization did not expect this token: {}",
|
||||
token,
|
||||
))
|
||||
}
|
||||
|
||||
macro_rules! end_of_tokens {
|
||||
() => {
|
||||
panic!("ran out of tokens to deserialize")
|
||||
};
|
||||
fn end_of_tokens() -> Error {
|
||||
de::Error::custom("ran out of tokens to deserialize")
|
||||
}
|
||||
|
||||
impl<'de> Deserializer<'de> {
|
||||
@@ -49,11 +46,8 @@ impl<'de> Deserializer<'de> {
|
||||
self.tokens.first().cloned()
|
||||
}
|
||||
|
||||
fn peek_token(&self) -> Token {
|
||||
match self.peek_token_opt() {
|
||||
Some(token) => token,
|
||||
None => end_of_tokens!(),
|
||||
}
|
||||
fn peek_token(&self) -> Result<Token, Error> {
|
||||
self.peek_token_opt().ok_or_else(end_of_tokens)
|
||||
}
|
||||
|
||||
pub fn next_token_opt(&mut self) -> Option<Token> {
|
||||
@@ -66,14 +60,10 @@ impl<'de> Deserializer<'de> {
|
||||
}
|
||||
}
|
||||
|
||||
fn next_token(&mut self) -> Token {
|
||||
match self.tokens.split_first() {
|
||||
Some((&first, rest)) => {
|
||||
self.tokens = rest;
|
||||
first
|
||||
}
|
||||
None => end_of_tokens!(),
|
||||
}
|
||||
fn next_token(&mut self) -> Result<Token, Error> {
|
||||
let (&first, rest) = self.tokens.split_first().ok_or_else(end_of_tokens)?;
|
||||
self.tokens = rest;
|
||||
Ok(first)
|
||||
}
|
||||
|
||||
pub fn remaining(&self) -> usize {
|
||||
@@ -94,7 +84,7 @@ impl<'de> Deserializer<'de> {
|
||||
len: len,
|
||||
end: end,
|
||||
})?;
|
||||
assert_next_token!(self, end);
|
||||
assert_next_token(self, end)?;
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
@@ -112,7 +102,7 @@ impl<'de> Deserializer<'de> {
|
||||
len: len,
|
||||
end: end,
|
||||
})?;
|
||||
assert_next_token!(self, end);
|
||||
assert_next_token(self, end)?;
|
||||
Ok(value)
|
||||
}
|
||||
}
|
||||
@@ -129,7 +119,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
let token = self.next_token();
|
||||
let token = self.next_token()?;
|
||||
match token {
|
||||
Token::Bool(v) => visitor.visit_bool(v),
|
||||
Token::I8(v) => visitor.visit_i8(v),
|
||||
@@ -161,50 +151,50 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
||||
Token::Map { len } => self.visit_map(len, Token::MapEnd, visitor),
|
||||
Token::Struct { len, .. } => self.visit_map(Some(len), Token::StructEnd, visitor),
|
||||
Token::Enum { .. } => {
|
||||
let variant = self.next_token();
|
||||
let next = self.peek_token();
|
||||
let variant = self.next_token()?;
|
||||
let next = self.peek_token()?;
|
||||
match (variant, next) {
|
||||
(Token::Str(variant), Token::Unit) => {
|
||||
self.next_token();
|
||||
self.next_token()?;
|
||||
visitor.visit_str(variant)
|
||||
}
|
||||
(Token::BorrowedStr(variant), Token::Unit) => {
|
||||
self.next_token();
|
||||
self.next_token()?;
|
||||
visitor.visit_borrowed_str(variant)
|
||||
}
|
||||
(Token::String(variant), Token::Unit) => {
|
||||
self.next_token();
|
||||
self.next_token()?;
|
||||
visitor.visit_string(variant.to_string())
|
||||
}
|
||||
(Token::Bytes(variant), Token::Unit) => {
|
||||
self.next_token();
|
||||
self.next_token()?;
|
||||
visitor.visit_bytes(variant)
|
||||
}
|
||||
(Token::BorrowedBytes(variant), Token::Unit) => {
|
||||
self.next_token();
|
||||
self.next_token()?;
|
||||
visitor.visit_borrowed_bytes(variant)
|
||||
}
|
||||
(Token::ByteBuf(variant), Token::Unit) => {
|
||||
self.next_token();
|
||||
self.next_token()?;
|
||||
visitor.visit_byte_buf(variant.to_vec())
|
||||
}
|
||||
(Token::U8(variant), Token::Unit) => {
|
||||
self.next_token();
|
||||
self.next_token()?;
|
||||
visitor.visit_u8(variant)
|
||||
}
|
||||
(Token::U16(variant), Token::Unit) => {
|
||||
self.next_token();
|
||||
self.next_token()?;
|
||||
visitor.visit_u16(variant)
|
||||
}
|
||||
(Token::U32(variant), Token::Unit) => {
|
||||
self.next_token();
|
||||
self.next_token()?;
|
||||
visitor.visit_u32(variant)
|
||||
}
|
||||
(Token::U64(variant), Token::Unit) => {
|
||||
self.next_token();
|
||||
self.next_token()?;
|
||||
visitor.visit_u64(variant)
|
||||
}
|
||||
(variant, Token::Unit) => unexpected!(variant),
|
||||
(variant, Token::Unit) => Err(unexpected(variant)),
|
||||
(variant, _) => {
|
||||
visitor.visit_map(EnumMapVisitor::new(self, variant, EnumFormat::Any))
|
||||
}
|
||||
@@ -232,9 +222,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
||||
| Token::MapEnd
|
||||
| Token::StructEnd
|
||||
| Token::TupleVariantEnd
|
||||
| Token::StructVariantEnd => {
|
||||
unexpected!(token);
|
||||
}
|
||||
| Token::StructVariantEnd => Err(unexpected(token)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -242,13 +230,13 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.peek_token() {
|
||||
match self.peek_token()? {
|
||||
Token::Unit | Token::None => {
|
||||
self.next_token();
|
||||
self.next_token()?;
|
||||
visitor.visit_none()
|
||||
}
|
||||
Token::Some => {
|
||||
self.next_token();
|
||||
self.next_token()?;
|
||||
visitor.visit_some(self)
|
||||
}
|
||||
_ => self.deserialize_any(visitor),
|
||||
@@ -264,9 +252,9 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.peek_token() {
|
||||
match self.peek_token()? {
|
||||
Token::Enum { name: n } if name == n => {
|
||||
self.next_token();
|
||||
self.next_token()?;
|
||||
|
||||
visitor.visit_enum(DeserializerEnumVisitor { de: self })
|
||||
}
|
||||
@@ -286,9 +274,9 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.peek_token() {
|
||||
match self.peek_token()? {
|
||||
Token::UnitStruct { .. } => {
|
||||
assert_next_token!(self, Token::UnitStruct { name: name });
|
||||
assert_next_token(self, Token::UnitStruct { name: name })?;
|
||||
visitor.visit_unit()
|
||||
}
|
||||
_ => self.deserialize_any(visitor),
|
||||
@@ -303,9 +291,9 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.peek_token() {
|
||||
match self.peek_token()? {
|
||||
Token::NewtypeStruct { .. } => {
|
||||
assert_next_token!(self, Token::NewtypeStruct { name: name });
|
||||
assert_next_token(self, Token::NewtypeStruct { name: name })?;
|
||||
visitor.visit_newtype_struct(self)
|
||||
}
|
||||
_ => self.deserialize_any(visitor),
|
||||
@@ -316,21 +304,21 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.peek_token() {
|
||||
match self.peek_token()? {
|
||||
Token::Unit | Token::UnitStruct { .. } => {
|
||||
self.next_token();
|
||||
self.next_token()?;
|
||||
visitor.visit_unit()
|
||||
}
|
||||
Token::Seq { .. } => {
|
||||
self.next_token();
|
||||
self.next_token()?;
|
||||
self.visit_seq(Some(len), Token::SeqEnd, visitor)
|
||||
}
|
||||
Token::Tuple { .. } => {
|
||||
self.next_token();
|
||||
self.next_token()?;
|
||||
self.visit_seq(Some(len), Token::TupleEnd, visitor)
|
||||
}
|
||||
Token::TupleStruct { .. } => {
|
||||
self.next_token();
|
||||
self.next_token()?;
|
||||
self.visit_seq(Some(len), Token::TupleStructEnd, visitor)
|
||||
}
|
||||
_ => self.deserialize_any(visitor),
|
||||
@@ -346,25 +334,25 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.peek_token() {
|
||||
match self.peek_token()? {
|
||||
Token::Unit => {
|
||||
self.next_token();
|
||||
self.next_token()?;
|
||||
visitor.visit_unit()
|
||||
}
|
||||
Token::UnitStruct { .. } => {
|
||||
assert_next_token!(self, Token::UnitStruct { name: name });
|
||||
assert_next_token(self, Token::UnitStruct { name: name })?;
|
||||
visitor.visit_unit()
|
||||
}
|
||||
Token::Seq { .. } => {
|
||||
self.next_token();
|
||||
self.next_token()?;
|
||||
self.visit_seq(Some(len), Token::SeqEnd, visitor)
|
||||
}
|
||||
Token::Tuple { .. } => {
|
||||
self.next_token();
|
||||
self.next_token()?;
|
||||
self.visit_seq(Some(len), Token::TupleEnd, visitor)
|
||||
}
|
||||
Token::TupleStruct { len: n, .. } => {
|
||||
assert_next_token!(self, Token::TupleStruct { name: name, len: n });
|
||||
assert_next_token(self, Token::TupleStruct { name: name, len: n })?;
|
||||
self.visit_seq(Some(len), Token::TupleStructEnd, visitor)
|
||||
}
|
||||
_ => self.deserialize_any(visitor),
|
||||
@@ -380,13 +368,13 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.peek_token() {
|
||||
match self.peek_token()? {
|
||||
Token::Struct { len: n, .. } => {
|
||||
assert_next_token!(self, Token::Struct { name: name, len: n });
|
||||
assert_next_token(self, Token::Struct { name: name, len: n })?;
|
||||
self.visit_map(Some(fields.len()), Token::StructEnd, visitor)
|
||||
}
|
||||
Token::Map { .. } => {
|
||||
self.next_token();
|
||||
self.next_token()?;
|
||||
self.visit_map(Some(fields.len()), Token::MapEnd, visitor)
|
||||
}
|
||||
_ => self.deserialize_any(visitor),
|
||||
@@ -476,7 +464,7 @@ impl<'de, 'a> EnumAccess<'de> for DeserializerEnumVisitor<'a, 'de> {
|
||||
where
|
||||
V: DeserializeSeed<'de>,
|
||||
{
|
||||
match self.de.peek_token() {
|
||||
match self.de.peek_token()? {
|
||||
Token::UnitVariant { variant: v, .. }
|
||||
| Token::NewtypeVariant { variant: v, .. }
|
||||
| Token::TupleVariant { variant: v, .. }
|
||||
@@ -497,9 +485,9 @@ impl<'de, 'a> VariantAccess<'de> for DeserializerEnumVisitor<'a, 'de> {
|
||||
type Error = Error;
|
||||
|
||||
fn unit_variant(self) -> Result<(), Error> {
|
||||
match self.de.peek_token() {
|
||||
match self.de.peek_token()? {
|
||||
Token::UnitVariant { .. } => {
|
||||
self.de.next_token();
|
||||
self.de.next_token()?;
|
||||
Ok(())
|
||||
}
|
||||
_ => Deserialize::deserialize(self.de),
|
||||
@@ -510,9 +498,9 @@ impl<'de, 'a> VariantAccess<'de> for DeserializerEnumVisitor<'a, 'de> {
|
||||
where
|
||||
T: DeserializeSeed<'de>,
|
||||
{
|
||||
match self.de.peek_token() {
|
||||
match self.de.peek_token()? {
|
||||
Token::NewtypeVariant { .. } => {
|
||||
self.de.next_token();
|
||||
self.de.next_token()?;
|
||||
seed.deserialize(self.de)
|
||||
}
|
||||
_ => seed.deserialize(self.de),
|
||||
@@ -523,26 +511,26 @@ impl<'de, 'a> VariantAccess<'de> for DeserializerEnumVisitor<'a, 'de> {
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.de.peek_token() {
|
||||
match self.de.peek_token()? {
|
||||
Token::TupleVariant { len: enum_len, .. } => {
|
||||
let token = self.de.next_token();
|
||||
let token = self.de.next_token()?;
|
||||
|
||||
if len == enum_len {
|
||||
self.de
|
||||
.visit_seq(Some(len), Token::TupleVariantEnd, visitor)
|
||||
} else {
|
||||
unexpected!(token);
|
||||
Err(unexpected(token))
|
||||
}
|
||||
}
|
||||
Token::Seq {
|
||||
len: Some(enum_len),
|
||||
} => {
|
||||
let token = self.de.next_token();
|
||||
let token = self.de.next_token()?;
|
||||
|
||||
if len == enum_len {
|
||||
self.de.visit_seq(Some(len), Token::SeqEnd, visitor)
|
||||
} else {
|
||||
unexpected!(token);
|
||||
Err(unexpected(token))
|
||||
}
|
||||
}
|
||||
_ => de::Deserializer::deserialize_any(self.de, visitor),
|
||||
@@ -557,27 +545,27 @@ impl<'de, 'a> VariantAccess<'de> for DeserializerEnumVisitor<'a, 'de> {
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.de.peek_token() {
|
||||
match self.de.peek_token()? {
|
||||
Token::StructVariant { len: enum_len, .. } => {
|
||||
let token = self.de.next_token();
|
||||
let token = self.de.next_token()?;
|
||||
|
||||
if fields.len() == enum_len {
|
||||
self.de
|
||||
.visit_map(Some(fields.len()), Token::StructVariantEnd, visitor)
|
||||
} else {
|
||||
unexpected!(token);
|
||||
Err(unexpected(token))
|
||||
}
|
||||
}
|
||||
Token::Map {
|
||||
len: Some(enum_len),
|
||||
} => {
|
||||
let token = self.de.next_token();
|
||||
let token = self.de.next_token()?;
|
||||
|
||||
if fields.len() == enum_len {
|
||||
self.de
|
||||
.visit_map(Some(fields.len()), Token::MapEnd, visitor)
|
||||
} else {
|
||||
unexpected!(token);
|
||||
Err(unexpected(token))
|
||||
}
|
||||
}
|
||||
_ => de::Deserializer::deserialize_any(self.de, visitor),
|
||||
@@ -622,7 +610,7 @@ impl<'de, 'a> MapAccess<'de> for EnumMapVisitor<'a, 'de> {
|
||||
.deserialize(BytesDeserializer { value: variant })
|
||||
.map(Some),
|
||||
Some(Token::U32(variant)) => seed.deserialize(variant.into_deserializer()).map(Some),
|
||||
Some(other) => unexpected!(other),
|
||||
Some(other) => Err(unexpected(other)),
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
@@ -641,7 +629,7 @@ impl<'de, 'a> MapAccess<'de> for EnumMapVisitor<'a, 'de> {
|
||||
};
|
||||
seed.deserialize(SeqAccessDeserializer::new(visitor))?
|
||||
};
|
||||
assert_next_token!(self.de, Token::TupleVariantEnd);
|
||||
assert_next_token(self.de, Token::TupleVariantEnd)?;
|
||||
Ok(value)
|
||||
}
|
||||
EnumFormat::Map => {
|
||||
@@ -653,7 +641,7 @@ impl<'de, 'a> MapAccess<'de> for EnumMapVisitor<'a, 'de> {
|
||||
};
|
||||
seed.deserialize(MapAccessDeserializer::new(visitor))?
|
||||
};
|
||||
assert_next_token!(self.de, Token::StructVariantEnd);
|
||||
assert_next_token(self.de, Token::StructVariantEnd)?;
|
||||
Ok(value)
|
||||
}
|
||||
EnumFormat::Any => seed.deserialize(&mut *self.de),
|
||||
|
||||
@@ -140,7 +140,7 @@
|
||||
//! # }
|
||||
//! ```
|
||||
|
||||
#![doc(html_root_url = "https://docs.rs/serde_test/1.0.157")]
|
||||
#![doc(html_root_url = "https://docs.rs/serde_test/1.0.164")]
|
||||
#![cfg_attr(feature = "cargo-clippy", allow(renamed_and_removed_lints))]
|
||||
// Ignored clippy lints
|
||||
#![cfg_attr(feature = "cargo-clippy", allow(float_cmp, needless_doctest_main))]
|
||||
@@ -182,7 +182,3 @@ pub use assert::{
|
||||
pub use token::Token;
|
||||
|
||||
pub use configure::{Compact, Configure, Readable};
|
||||
|
||||
// Not public API.
|
||||
#[doc(hidden)]
|
||||
pub use de::Deserializer;
|
||||
|
||||
@@ -63,14 +63,12 @@ macro_rules! assert_next_token {
|
||||
($ser:expr, $actual:expr, $pat:pat, $guard:expr) => {
|
||||
match $ser.next_token() {
|
||||
Some($pat) if $guard => {}
|
||||
Some(expected) => {
|
||||
panic!("expected Token::{} but serialized as {}",
|
||||
expected, $actual);
|
||||
}
|
||||
None => {
|
||||
panic!("expected end of tokens, but {} was serialized",
|
||||
$actual);
|
||||
}
|
||||
Some(expected) => return Err(ser::Error::custom(
|
||||
format!("expected Token::{} but serialized as {}", expected, $actual)
|
||||
)),
|
||||
None => return Err(ser::Error::custom(
|
||||
format!("expected end of tokens, but {} was serialized", $actual)
|
||||
)),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
use serde::Deserialize;
|
||||
|
||||
macro_rules! bug {
|
||||
($serde_path:literal) => {
|
||||
#[derive(Deserialize)]
|
||||
#[serde(crate = $serde_path)]
|
||||
pub struct Struct;
|
||||
};
|
||||
}
|
||||
|
||||
bug!("serde");
|
||||
@@ -0,0 +1,5 @@
|
||||
use serde_derive::Serialize;
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde()]
|
||||
pub struct S;
|
||||
@@ -2317,6 +2317,53 @@ fn test_internally_tagged_enum_new_type_with_unit() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_adjacently_tagged_enum_bytes() {
|
||||
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
#[serde(tag = "t", content = "c")]
|
||||
enum Data {
|
||||
A { a: i32 },
|
||||
}
|
||||
|
||||
let data = Data::A { a: 0 };
|
||||
|
||||
assert_tokens(
|
||||
&data,
|
||||
&[
|
||||
Token::Struct {
|
||||
name: "Data",
|
||||
len: 2,
|
||||
},
|
||||
Token::Str("t"),
|
||||
Token::Str("A"),
|
||||
Token::Str("c"),
|
||||
Token::Struct { name: "A", len: 1 },
|
||||
Token::Str("a"),
|
||||
Token::I32(0),
|
||||
Token::StructEnd,
|
||||
Token::StructEnd,
|
||||
],
|
||||
);
|
||||
|
||||
assert_de_tokens(
|
||||
&data,
|
||||
&[
|
||||
Token::Struct {
|
||||
name: "Data",
|
||||
len: 2,
|
||||
},
|
||||
Token::Bytes(b"t"),
|
||||
Token::Str("A"),
|
||||
Token::Bytes(b"c"),
|
||||
Token::Struct { name: "A", len: 1 },
|
||||
Token::Str("a"),
|
||||
Token::I32(0),
|
||||
Token::StructEnd,
|
||||
Token::StructEnd,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_adjacently_tagged_enum_containing_flatten() {
|
||||
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
@@ -2395,6 +2442,162 @@ fn test_untagged_enum_containing_flatten() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_partially_untagged_enum() {
|
||||
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
enum Exp {
|
||||
Lambda(u32, Box<Exp>),
|
||||
#[serde(untagged)]
|
||||
App(Box<Exp>, Box<Exp>),
|
||||
#[serde(untagged)]
|
||||
Var(u32),
|
||||
}
|
||||
use Exp::*;
|
||||
|
||||
let data = Lambda(0, Box::new(App(Box::new(Var(0)), Box::new(Var(0)))));
|
||||
assert_tokens(
|
||||
&data,
|
||||
&[
|
||||
Token::TupleVariant {
|
||||
name: "Exp",
|
||||
variant: "Lambda",
|
||||
len: 2,
|
||||
},
|
||||
Token::U32(0),
|
||||
Token::Tuple { len: 2 },
|
||||
Token::U32(0),
|
||||
Token::U32(0),
|
||||
Token::TupleEnd,
|
||||
Token::TupleVariantEnd,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_partially_untagged_enum_generic() {
|
||||
trait Trait<T> {
|
||||
type Assoc;
|
||||
type Assoc2;
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
enum E<A, B, C>
|
||||
where
|
||||
A: Trait<C, Assoc2 = B>,
|
||||
{
|
||||
A(A::Assoc),
|
||||
#[serde(untagged)]
|
||||
B(A::Assoc2),
|
||||
}
|
||||
|
||||
impl<T> Trait<T> for () {
|
||||
type Assoc = T;
|
||||
type Assoc2 = bool;
|
||||
}
|
||||
|
||||
type MyE = E<(), bool, u32>;
|
||||
use E::*;
|
||||
|
||||
assert_tokens::<MyE>(&B(true), &[Token::Bool(true)]);
|
||||
|
||||
assert_tokens::<MyE>(
|
||||
&A(5),
|
||||
&[
|
||||
Token::NewtypeVariant {
|
||||
name: "E",
|
||||
variant: "A",
|
||||
},
|
||||
Token::U32(5),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_partially_untagged_enum_desugared() {
|
||||
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
enum Test {
|
||||
A(u32, u32),
|
||||
B(u32),
|
||||
#[serde(untagged)]
|
||||
C(u32),
|
||||
#[serde(untagged)]
|
||||
D(u32, u32),
|
||||
}
|
||||
use Test::*;
|
||||
|
||||
mod desugared {
|
||||
use super::*;
|
||||
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
pub(super) enum Test {
|
||||
A(u32, u32),
|
||||
B(u32),
|
||||
}
|
||||
}
|
||||
use desugared::Test as TestTagged;
|
||||
|
||||
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
#[serde(untagged)]
|
||||
enum TestUntagged {
|
||||
Tagged(TestTagged),
|
||||
C(u32),
|
||||
D(u32, u32),
|
||||
}
|
||||
|
||||
impl From<Test> for TestUntagged {
|
||||
fn from(test: Test) -> Self {
|
||||
match test {
|
||||
A(x, y) => TestUntagged::Tagged(TestTagged::A(x, y)),
|
||||
B(x) => TestUntagged::Tagged(TestTagged::B(x)),
|
||||
C(x) => TestUntagged::C(x),
|
||||
D(x, y) => TestUntagged::D(x, y),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_tokens_desugared(value: Test, tokens: &[Token]) {
|
||||
assert_tokens(&value, tokens);
|
||||
let desugared: TestUntagged = value.into();
|
||||
assert_tokens(&desugared, tokens);
|
||||
}
|
||||
|
||||
assert_tokens_desugared(
|
||||
A(0, 1),
|
||||
&[
|
||||
Token::TupleVariant {
|
||||
name: "Test",
|
||||
variant: "A",
|
||||
len: 2,
|
||||
},
|
||||
Token::U32(0),
|
||||
Token::U32(1),
|
||||
Token::TupleVariantEnd,
|
||||
],
|
||||
);
|
||||
|
||||
assert_tokens_desugared(
|
||||
B(1),
|
||||
&[
|
||||
Token::NewtypeVariant {
|
||||
name: "Test",
|
||||
variant: "B",
|
||||
},
|
||||
Token::U32(1),
|
||||
],
|
||||
);
|
||||
|
||||
assert_tokens_desugared(C(2), &[Token::U32(2)]);
|
||||
|
||||
assert_tokens_desugared(
|
||||
D(3, 5),
|
||||
&[
|
||||
Token::Tuple { len: 2 },
|
||||
Token::U32(3),
|
||||
Token::U32(5),
|
||||
Token::TupleEnd,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_flatten_untagged_enum() {
|
||||
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
clippy::used_underscore_binding
|
||||
)]
|
||||
|
||||
use serde::de::value::{BorrowedStrDeserializer, MapDeserializer};
|
||||
use serde::de::IntoDeserializer;
|
||||
use serde::{Deserialize, Deserializer};
|
||||
use serde_test::{assert_de_tokens, assert_de_tokens_error, Token};
|
||||
|
||||
@@ -130,20 +132,22 @@ fn test_cow() {
|
||||
borrowed: Cow<'b, str>,
|
||||
}
|
||||
|
||||
let tokens = &[
|
||||
Token::Struct {
|
||||
name: "Cows",
|
||||
len: 2,
|
||||
},
|
||||
Token::Str("copied"),
|
||||
Token::BorrowedStr("copied"),
|
||||
Token::Str("borrowed"),
|
||||
Token::BorrowedStr("borrowed"),
|
||||
Token::StructEnd,
|
||||
];
|
||||
struct BorrowedStr(&'static str);
|
||||
|
||||
let mut de = serde_test::Deserializer::new(tokens);
|
||||
let cows = Cows::deserialize(&mut de).unwrap();
|
||||
impl<'de> IntoDeserializer<'de> for BorrowedStr {
|
||||
type Deserializer = BorrowedStrDeserializer<'de, serde::de::value::Error>;
|
||||
|
||||
fn into_deserializer(self) -> Self::Deserializer {
|
||||
BorrowedStrDeserializer::new(self.0)
|
||||
}
|
||||
}
|
||||
|
||||
let de = MapDeserializer::new(IntoIterator::into_iter([
|
||||
("copied", BorrowedStr("copied")),
|
||||
("borrowed", BorrowedStr("borrowed")),
|
||||
]));
|
||||
|
||||
let cows = Cows::deserialize(de).unwrap();
|
||||
|
||||
match cows.copied {
|
||||
Cow::Owned(ref s) if s == "copied" => {}
|
||||
|
||||
+17
-23
@@ -33,7 +33,7 @@ use std::time::{Duration, UNIX_EPOCH};
|
||||
use std::sync::atomic::{AtomicI64, AtomicU64};
|
||||
|
||||
use fnv::FnvHasher;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::de::{DeserializeOwned, IntoDeserializer};
|
||||
use serde::{Deserialize, Deserializer};
|
||||
use serde_test::{assert_de_tokens, Configure, Token};
|
||||
|
||||
@@ -202,9 +202,8 @@ fn assert_de_tokens_ignore(ignorable_tokens: &[Token]) {
|
||||
.chain(vec![Token::MapEnd].into_iter())
|
||||
.collect();
|
||||
|
||||
let mut de = serde_test::Deserializer::new(&concated_tokens);
|
||||
let base = IgnoreBase::deserialize(&mut de).unwrap();
|
||||
assert_eq!(base, IgnoreBase { a: 1 });
|
||||
let expected = IgnoreBase { a: 1 };
|
||||
assert_de_tokens(&expected, &concated_tokens);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
@@ -2245,39 +2244,34 @@ fn test_cstr() {
|
||||
|
||||
#[test]
|
||||
fn test_atomics() {
|
||||
fn test<L, A, T>(load: L, val: T, token: Token)
|
||||
fn test<L, A, T>(load: L, val: T)
|
||||
where
|
||||
L: Fn(&A, Ordering) -> T,
|
||||
A: DeserializeOwned,
|
||||
T: PartialEq + Debug,
|
||||
T: PartialEq + Debug + Copy + for<'de> IntoDeserializer<'de>,
|
||||
{
|
||||
let tokens = &[token];
|
||||
let mut de = serde_test::Deserializer::new(tokens);
|
||||
match A::deserialize(&mut de) {
|
||||
match A::deserialize(val.into_deserializer()) {
|
||||
Ok(v) => {
|
||||
let loaded = load(&v, Ordering::Relaxed);
|
||||
assert_eq!(val, loaded);
|
||||
}
|
||||
Err(e) => panic!("tokens failed to deserialize: {}", e),
|
||||
};
|
||||
if de.remaining() > 0 {
|
||||
panic!("{} remaining tokens", de.remaining());
|
||||
}
|
||||
}
|
||||
|
||||
test(AtomicBool::load, true, Token::Bool(true));
|
||||
test(AtomicI8::load, -127, Token::I8(-127i8));
|
||||
test(AtomicI16::load, -510, Token::I16(-510i16));
|
||||
test(AtomicI32::load, -131072, Token::I32(-131072i32));
|
||||
test(AtomicIsize::load, -131072isize, Token::I32(-131072));
|
||||
test(AtomicU8::load, 127, Token::U8(127u8));
|
||||
test(AtomicU16::load, 510u16, Token::U16(510u16));
|
||||
test(AtomicU32::load, 131072u32, Token::U32(131072u32));
|
||||
test(AtomicUsize::load, 131072usize, Token::U32(131072));
|
||||
test(AtomicBool::load, true);
|
||||
test(AtomicI8::load, -127i8);
|
||||
test(AtomicI16::load, -510i16);
|
||||
test(AtomicI32::load, -131072i32);
|
||||
test(AtomicIsize::load, -131072isize);
|
||||
test(AtomicU8::load, 127u8);
|
||||
test(AtomicU16::load, 510u16);
|
||||
test(AtomicU32::load, 131072u32);
|
||||
test(AtomicUsize::load, 131072usize);
|
||||
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
{
|
||||
test(AtomicI64::load, -8589934592, Token::I64(-8589934592));
|
||||
test(AtomicU64::load, 8589934592u64, Token::U64(8589934592));
|
||||
test(AtomicI64::load, -8589934592i64);
|
||||
test(AtomicU64::load, 8589934592u64);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
use serde_derive::Serialize;
|
||||
|
||||
#[derive(Serialize)]
|
||||
enum E {
|
||||
#[serde(untagged)]
|
||||
A(u8),
|
||||
B(String),
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,5 @@
|
||||
error: all variants with the #[serde(untagged)] attribute must be placed at the end of the enum
|
||||
--> tests/ui/enum-representation/partially_tagged_wrong_order.rs:6:5
|
||||
|
|
||||
6 | A(u8),
|
||||
| ^
|
||||
Reference in New Issue
Block a user