mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-24 18:07:59 +00:00
Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6502838f27 | |||
| c93a0f335a | |||
| 8264e002a7 | |||
| 117ef22142 | |||
| 3fb5e71c33 | |||
| 296db177e2 | |||
| e4a4389177 | |||
| 7aa0453c3b | |||
| 09b78b24e9 | |||
| a622b8a74a | |||
| 399ef081ec | |||
| 3686277e14 | |||
| c604bdbfe4 | |||
| 9fef892f6d | |||
| b1c7db47b8 | |||
| 51799dd654 | |||
| 732ac49321 |
+2
-2
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "serde"
|
||||
version = "1.0.167" # remember to update html_root_url and serde_derive dependency
|
||||
version = "1.0.170" # 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", "no-std::no-alloc"]
|
||||
@@ -15,7 +15,7 @@ repository = "https://github.com/serde-rs/serde"
|
||||
rust-version = "1.19"
|
||||
|
||||
[dependencies]
|
||||
serde_derive = { version = "=1.0.167", optional = true, path = "../serde_derive" }
|
||||
serde_derive = { version = "=1.0.170", optional = true, path = "../serde_derive" }
|
||||
|
||||
[dev-dependencies]
|
||||
serde_derive = { version = "1.0", path = "../serde_derive" }
|
||||
|
||||
@@ -107,7 +107,7 @@ use de::{
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
#[derive(Copy, Clone, Debug, Default)]
|
||||
#[derive(Copy, Clone, Debug, Default, PartialEq)]
|
||||
pub struct IgnoredAny;
|
||||
|
||||
impl<'de> Visitor<'de> for IgnoredAny {
|
||||
|
||||
+10
-9
@@ -681,8 +681,8 @@ impl<'de> Visitor<'de> for CStringVisitor {
|
||||
where
|
||||
A: SeqAccess<'de>,
|
||||
{
|
||||
let len = size_hint::cautious(seq.size_hint());
|
||||
let mut values = Vec::with_capacity(len);
|
||||
let capacity = size_hint::cautious::<u8>(seq.size_hint());
|
||||
let mut values = Vec::<u8>::with_capacity(capacity);
|
||||
|
||||
while let Some(value) = try!(seq.next_element()) {
|
||||
values.push(value);
|
||||
@@ -936,7 +936,7 @@ macro_rules! seq_impl {
|
||||
A: SeqAccess<'de>,
|
||||
{
|
||||
$clear(&mut self.0);
|
||||
$reserve(&mut self.0, size_hint::cautious($access.size_hint()));
|
||||
$reserve(&mut self.0, size_hint::cautious::<T>($access.size_hint()));
|
||||
|
||||
// FIXME: try to overwrite old values here? (Vec, VecDeque, LinkedList)
|
||||
while let Some(value) = try!($access.next_element()) {
|
||||
@@ -962,7 +962,7 @@ seq_impl!(
|
||||
BinaryHeap<T: Ord>,
|
||||
seq,
|
||||
BinaryHeap::clear,
|
||||
BinaryHeap::with_capacity(size_hint::cautious(seq.size_hint())),
|
||||
BinaryHeap::with_capacity(size_hint::cautious::<T>(seq.size_hint())),
|
||||
BinaryHeap::reserve,
|
||||
BinaryHeap::push
|
||||
);
|
||||
@@ -992,7 +992,7 @@ seq_impl!(
|
||||
HashSet<T: Eq + Hash, S: BuildHasher + Default>,
|
||||
seq,
|
||||
HashSet::clear,
|
||||
HashSet::with_capacity_and_hasher(size_hint::cautious(seq.size_hint()), S::default()),
|
||||
HashSet::with_capacity_and_hasher(size_hint::cautious::<T>(seq.size_hint()), S::default()),
|
||||
HashSet::reserve,
|
||||
HashSet::insert
|
||||
);
|
||||
@@ -1002,7 +1002,7 @@ seq_impl!(
|
||||
VecDeque<T>,
|
||||
seq,
|
||||
VecDeque::clear,
|
||||
VecDeque::with_capacity(size_hint::cautious(seq.size_hint())),
|
||||
VecDeque::with_capacity(size_hint::cautious::<T>(seq.size_hint())),
|
||||
VecDeque::reserve,
|
||||
VecDeque::push_back
|
||||
);
|
||||
@@ -1036,7 +1036,8 @@ where
|
||||
where
|
||||
A: SeqAccess<'de>,
|
||||
{
|
||||
let mut values = Vec::with_capacity(size_hint::cautious(seq.size_hint()));
|
||||
let capacity = size_hint::cautious::<T>(seq.size_hint());
|
||||
let mut values = Vec::<T>::with_capacity(capacity);
|
||||
|
||||
while let Some(value) = try!(seq.next_element()) {
|
||||
values.push(value);
|
||||
@@ -1072,7 +1073,7 @@ where
|
||||
where
|
||||
A: SeqAccess<'de>,
|
||||
{
|
||||
let hint = size_hint::cautious(seq.size_hint());
|
||||
let hint = size_hint::cautious::<T>(seq.size_hint());
|
||||
if let Some(additional) = hint.checked_sub(self.0.len()) {
|
||||
self.0.reserve(additional);
|
||||
}
|
||||
@@ -1416,7 +1417,7 @@ map_impl!(BTreeMap<K: Ord, V>, map, BTreeMap::new());
|
||||
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::<(K, V)>(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.167")]
|
||||
#![doc(html_root_url = "https://docs.rs/serde/1.0.170")]
|
||||
// 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
|
||||
|
||||
+57
-6
@@ -474,7 +474,8 @@ mod content {
|
||||
where
|
||||
V: SeqAccess<'de>,
|
||||
{
|
||||
let mut vec = Vec::with_capacity(size_hint::cautious(visitor.size_hint()));
|
||||
let mut vec =
|
||||
Vec::<Content>::with_capacity(size_hint::cautious::<Content>(visitor.size_hint()));
|
||||
while let Some(e) = try!(visitor.next_element()) {
|
||||
vec.push(e);
|
||||
}
|
||||
@@ -485,7 +486,10 @@ mod content {
|
||||
where
|
||||
V: MapAccess<'de>,
|
||||
{
|
||||
let mut vec = Vec::with_capacity(size_hint::cautious(visitor.size_hint()));
|
||||
let mut vec =
|
||||
Vec::<(Content, Content)>::with_capacity(
|
||||
size_hint::cautious::<(Content, Content)>(visitor.size_hint()),
|
||||
);
|
||||
while let Some(kv) = try!(visitor.next_entry()) {
|
||||
vec.push(kv);
|
||||
}
|
||||
@@ -844,7 +848,10 @@ mod content {
|
||||
M: MapAccess<'de>,
|
||||
{
|
||||
let mut tag = None;
|
||||
let mut vec = Vec::with_capacity(size_hint::cautious(map.size_hint()));
|
||||
let mut vec = Vec::<(Content, Content)>::with_capacity(size_hint::cautious::<(
|
||||
Content,
|
||||
Content,
|
||||
)>(map.size_hint()));
|
||||
while let Some(k) = try!(map.next_key_seed(TagOrContentVisitor::new(self.tag_name))) {
|
||||
match k {
|
||||
TagOrContent::Tag => {
|
||||
@@ -887,7 +894,7 @@ mod content {
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_str(self)
|
||||
deserializer.deserialize_identifier(self)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -898,6 +905,20 @@ mod content {
|
||||
write!(formatter, "{:?} or {:?}", self.tag, self.content)
|
||||
}
|
||||
|
||||
fn visit_u64<E>(self, field_index: u64) -> Result<Self::Value, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
match field_index {
|
||||
0 => Ok(TagOrContentField::Tag),
|
||||
1 => Ok(TagOrContentField::Content),
|
||||
_ => Err(de::Error::invalid_value(
|
||||
Unexpected::Unsigned(field_index),
|
||||
&self,
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, field: &str) -> Result<Self::Value, E>
|
||||
where
|
||||
E: de::Error,
|
||||
@@ -910,6 +931,19 @@ mod content {
|
||||
Err(de::Error::invalid_value(Unexpected::Str(field), &self))
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_bytes<E>(self, field: &[u8]) -> Result<Self::Value, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
if field == self.tag.as_bytes() {
|
||||
Ok(TagOrContentField::Tag)
|
||||
} else if field == self.content.as_bytes() {
|
||||
Ok(TagOrContentField::Content)
|
||||
} else {
|
||||
Err(de::Error::invalid_value(Unexpected::Bytes(field), &self))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Used by generated code to deserialize an adjacently tagged enum when
|
||||
@@ -935,7 +969,7 @@ mod content {
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_str(self)
|
||||
deserializer.deserialize_identifier(self)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -950,6 +984,17 @@ mod content {
|
||||
)
|
||||
}
|
||||
|
||||
fn visit_u64<E>(self, field_index: u64) -> Result<Self::Value, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
match field_index {
|
||||
0 => Ok(TagContentOtherField::Tag),
|
||||
1 => Ok(TagContentOtherField::Content),
|
||||
_ => Ok(TagContentOtherField::Other),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, field: &str) -> Result<Self::Value, E>
|
||||
where
|
||||
E: de::Error,
|
||||
@@ -2794,6 +2839,13 @@ where
|
||||
visitor.visit_unit()
|
||||
}
|
||||
|
||||
fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
visitor.visit_unit()
|
||||
}
|
||||
|
||||
forward_to_deserialize_other! {
|
||||
deserialize_bool()
|
||||
deserialize_i8()
|
||||
@@ -2816,7 +2868,6 @@ where
|
||||
deserialize_tuple(usize)
|
||||
deserialize_tuple_struct(&'static str, usize)
|
||||
deserialize_identifier()
|
||||
deserialize_ignored_any()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,9 +8,17 @@ where
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
#[inline]
|
||||
pub fn cautious(hint: Option<usize>) -> usize {
|
||||
cmp::min(hint.unwrap_or(0), 4096)
|
||||
pub fn cautious<Element>(hint: Option<usize>) -> usize {
|
||||
const MAX_PREALLOC_BYTES: usize = 1024 * 1024;
|
||||
|
||||
if mem::size_of::<Element>() == 0 {
|
||||
0
|
||||
} else {
|
||||
cmp::min(
|
||||
hint.unwrap_or(0),
|
||||
MAX_PREALLOC_BYTES / mem::size_of::<Element>(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn helper(bounds: (usize, Option<usize>)) -> Option<usize> {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "serde_derive"
|
||||
version = "1.0.167" # remember to update html_root_url
|
||||
version = "1.0.170" # remember to update html_root_url
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
||||
categories = ["no-std", "no-std::no-alloc"]
|
||||
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.21"
|
||||
syn = "2.0.25"
|
||||
|
||||
[dev-dependencies]
|
||||
serde = { version = "1.0", path = "../serde" }
|
||||
|
||||
@@ -1418,6 +1418,13 @@ fn get_lit_str2(
|
||||
..
|
||||
}) = value
|
||||
{
|
||||
let suffix = lit.suffix();
|
||||
if !suffix.is_empty() {
|
||||
cx.error_spanned_by(
|
||||
lit,
|
||||
format!("unexpected suffix `{}` on string literal", suffix),
|
||||
);
|
||||
}
|
||||
Ok(Some(lit.clone()))
|
||||
} else {
|
||||
cx.error_spanned_by(
|
||||
|
||||
@@ -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.167")]
|
||||
#![doc(html_root_url = "https://docs.rs/serde_derive/1.0.170")]
|
||||
#![allow(unknown_lints, bare_trait_objects)]
|
||||
// Ignored clippy lints
|
||||
#![allow(
|
||||
|
||||
@@ -17,7 +17,7 @@ path = "lib.rs"
|
||||
[dependencies]
|
||||
proc-macro2 = "1.0"
|
||||
quote = "1.0"
|
||||
syn = { version = "2.0.21", default-features = false, features = ["clone-impls", "derive", "parsing", "printing"] }
|
||||
syn = { version = "2.0.25", default-features = false, features = ["clone-impls", "derive", "parsing", "printing"] }
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "serde_test"
|
||||
version = "1.0.167" # remember to update html_root_url
|
||||
version = "1.0.170" # 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"]
|
||||
|
||||
@@ -144,7 +144,7 @@
|
||||
//! # }
|
||||
//! ```
|
||||
|
||||
#![doc(html_root_url = "https://docs.rs/serde_test/1.0.167")]
|
||||
#![doc(html_root_url = "https://docs.rs/serde_test/1.0.170")]
|
||||
#![cfg_attr(feature = "cargo-clippy", allow(renamed_and_removed_lints))]
|
||||
// Ignored clippy lints
|
||||
#![cfg_attr(feature = "cargo-clippy", allow(float_cmp, needless_doctest_main))]
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
clippy::uninlined_format_args,
|
||||
)]
|
||||
|
||||
use serde::de::{self, MapAccess, Unexpected, Visitor};
|
||||
use serde::de::{self, IgnoredAny, MapAccess, Unexpected, Visitor};
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
@@ -2697,6 +2697,31 @@ fn test_flatten_option() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_flatten_ignored_any() {
|
||||
#[derive(Deserialize, PartialEq, Debug)]
|
||||
struct Outer {
|
||||
#[serde(flatten)]
|
||||
inner: IgnoredAny,
|
||||
}
|
||||
|
||||
assert_de_tokens(
|
||||
&Outer { inner: IgnoredAny },
|
||||
&[Token::Map { len: None }, Token::MapEnd],
|
||||
);
|
||||
|
||||
assert_de_tokens(
|
||||
&Outer { inner: IgnoredAny },
|
||||
&[
|
||||
Token::Struct {
|
||||
name: "DoNotMatter",
|
||||
len: 0,
|
||||
},
|
||||
Token::StructEnd,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_transparent_struct() {
|
||||
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
|
||||
@@ -1267,6 +1267,38 @@ fn test_adjacently_tagged_enum() {
|
||||
Token::StructEnd,
|
||||
],
|
||||
);
|
||||
|
||||
// integer field keys
|
||||
assert_de_tokens(
|
||||
&AdjacentlyTagged::Newtype::<u8>(1),
|
||||
&[
|
||||
Token::Struct {
|
||||
name: "AdjacentlyTagged",
|
||||
len: 2,
|
||||
},
|
||||
Token::U64(1), // content field
|
||||
Token::U8(1),
|
||||
Token::U64(0), // tag field
|
||||
Token::Str("Newtype"),
|
||||
Token::StructEnd,
|
||||
],
|
||||
);
|
||||
|
||||
// byte-array field keys
|
||||
assert_de_tokens(
|
||||
&AdjacentlyTagged::Newtype::<u8>(1),
|
||||
&[
|
||||
Token::Struct {
|
||||
name: "AdjacentlyTagged",
|
||||
len: 2,
|
||||
},
|
||||
Token::Bytes(b"c"),
|
||||
Token::U8(1),
|
||||
Token::Bytes(b"t"),
|
||||
Token::Str("Newtype"),
|
||||
Token::StructEnd,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1330,6 +1362,32 @@ fn test_adjacently_tagged_enum_deny_unknown_fields() {
|
||||
],
|
||||
r#"invalid value: string "h", expected "t" or "c""#,
|
||||
);
|
||||
|
||||
assert_de_tokens_error::<AdjacentlyTagged>(
|
||||
&[
|
||||
Token::Struct {
|
||||
name: "AdjacentlyTagged",
|
||||
len: 2,
|
||||
},
|
||||
Token::U64(0), // tag field
|
||||
Token::Str("Unit"),
|
||||
Token::U64(3),
|
||||
],
|
||||
r#"invalid value: integer `3`, expected "t" or "c""#,
|
||||
);
|
||||
|
||||
assert_de_tokens_error::<AdjacentlyTagged>(
|
||||
&[
|
||||
Token::Struct {
|
||||
name: "AdjacentlyTagged",
|
||||
len: 2,
|
||||
},
|
||||
Token::Bytes(b"c"),
|
||||
Token::Unit,
|
||||
Token::Bytes(b"h"),
|
||||
],
|
||||
r#"invalid value: byte array, expected "t" or "c""#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde(bound = ""huh)]
|
||||
pub struct Struct {
|
||||
#[serde(rename = ""what)]
|
||||
pub field: i32,
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,11 @@
|
||||
error: unexpected suffix `huh` on string literal
|
||||
--> tests/ui/malformed/str_suffix.rs:4:17
|
||||
|
|
||||
4 | #[serde(bound = ""huh)]
|
||||
| ^^^^^
|
||||
|
||||
error: unexpected suffix `what` on string literal
|
||||
--> tests/ui/malformed/str_suffix.rs:6:22
|
||||
|
|
||||
6 | #[serde(rename = ""what)]
|
||||
| ^^^^^^
|
||||
Reference in New Issue
Block a user