mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-29 04:57:54 +00:00
Compare commits
57 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5d036515ed | |||
| a741293886 | |||
| f8d0b26d2f | |||
| 7007c1bea5 | |||
| 0d8ebac7e1 | |||
| 212c42c74f | |||
| 919f6be887 | |||
| c0f70428ba | |||
| e797c90ebd | |||
| fc04d1219a | |||
| df4ad5884d | |||
| 3c7dd6fc1e | |||
| 8b196ea1c8 | |||
| 1f8c8ad5a3 | |||
| 870925d503 | |||
| d593215ef7 | |||
| 110af31b48 | |||
| 360606b9a6 | |||
| 151b45ae36 | |||
| 2ea7e1300f | |||
| 4617c957b9 | |||
| 2547ed83ca | |||
| bfcd44704f | |||
| 7b548db91e | |||
| d39dea85ad | |||
| 5e56c9fba8 | |||
| 8d3a03288b | |||
| cb490ec16d | |||
| 45271c3676 | |||
| 05a5b7e3c6 | |||
| 3bff326fb3 | |||
| aaadd93878 | |||
| 9c864f0b02 | |||
| 070cce0d9c | |||
| b58e8bac12 | |||
| ada50b077e | |||
| 5e313a7330 | |||
| 2a36d11238 | |||
| b6685cf9dd | |||
| fc273c6763 | |||
| bd7b0e257e | |||
| 73931692b2 | |||
| 4d93e9f44c | |||
| da55ed7e8d | |||
| e3617e1f28 | |||
| 431636af0d | |||
| 891ced598a | |||
| 5c33931422 | |||
| f709fc05b0 | |||
| 089aae1292 | |||
| 855acaf112 | |||
| 7ca7720262 | |||
| 78fea3aa4a | |||
| 1efb8b6a53 | |||
| bc1960b106 | |||
| 967023b755 | |||
| bb51e68f16 |
@@ -98,7 +98,6 @@ jobs:
|
|||||||
- run: cd serde && cargo check --no-default-features
|
- run: cd serde && cargo check --no-default-features
|
||||||
- run: cd serde && cargo check
|
- run: cd serde && cargo check
|
||||||
- run: cd serde_derive && cargo check
|
- run: cd serde_derive && cargo check
|
||||||
- run: cd precompiled/serde_derive && cargo check
|
|
||||||
|
|
||||||
alloc:
|
alloc:
|
||||||
name: Rust 1.36.0
|
name: Rust 1.36.0
|
||||||
@@ -109,38 +108,6 @@ jobs:
|
|||||||
- uses: dtolnay/rust-toolchain@1.36.0
|
- uses: dtolnay/rust-toolchain@1.36.0
|
||||||
- run: cd serde && cargo build --no-default-features --features alloc
|
- run: cd serde && cargo build --no-default-features --features alloc
|
||||||
|
|
||||||
precompiled:
|
|
||||||
name: Precompiled
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
timeout-minutes: 45
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v3
|
|
||||||
- uses: dtolnay/rust-toolchain@nightly
|
|
||||||
with:
|
|
||||||
components: rust-src
|
|
||||||
targets: x86_64-unknown-linux-musl
|
|
||||||
- run: precompiled/build.sh
|
|
||||||
- name: replace serde_derive dependency with precompiled
|
|
||||||
run: |
|
|
||||||
# FIXME: consider using `cargo rm serde_derive` but it's currently broken
|
|
||||||
# https://github.com/rust-lang/cargo/issues/12419
|
|
||||||
sed -i '/serde_derive =/d' serde/Cargo.toml
|
|
||||||
sed -i '/derive = \["serde_derive"\]/d' serde/Cargo.toml
|
|
||||||
sed -i '/"serde_derive"/d' Cargo.toml
|
|
||||||
sed -i '/\[workspace\]/d' precompiled/serde_derive/Cargo.toml
|
|
||||||
cargo add --dev serde_derive --path precompiled/serde_derive --manifest-path test_suite/Cargo.toml
|
|
||||||
git diff
|
|
||||||
- run: cd test_suite && cargo test --features unstable -- --skip ui --exact
|
|
||||||
|
|
||||||
macos:
|
|
||||||
name: macOS
|
|
||||||
runs-on: macos-latest
|
|
||||||
timeout-minutes: 45
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v3
|
|
||||||
- uses: dtolnay/rust-toolchain@stable
|
|
||||||
- run: cd precompiled/serde_derive && cargo check
|
|
||||||
|
|
||||||
minimal:
|
minimal:
|
||||||
name: Minimal versions
|
name: Minimal versions
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
/serde_derive/serde_derive-x86_64-unknown-linux-gnu
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
[workspace]
|
|
||||||
members = ["bin", "proc-macro2"]
|
|
||||||
resolver = "2"
|
|
||||||
|
|
||||||
[patch.crates-io]
|
|
||||||
proc-macro2 = { path = "proc-macro2" }
|
|
||||||
|
|
||||||
[profile.precompiled]
|
|
||||||
inherits = "release"
|
|
||||||
codegen-units = 1
|
|
||||||
lto = true
|
|
||||||
opt-level = "z"
|
|
||||||
panic = "abort"
|
|
||||||
strip = true
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "serde_derive"
|
|
||||||
version = "1.0.181"
|
|
||||||
authors = ["David Tolnay <dtolnay@gmail.com>"]
|
|
||||||
publish = false
|
|
||||||
|
|
||||||
[lib]
|
|
||||||
doctest = false
|
|
||||||
|
|
||||||
[[bin]]
|
|
||||||
name = "serde_derive"
|
|
||||||
path = "main.rs"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
proc-macro2 = "1"
|
|
||||||
quote = { version = "1", default-features = false }
|
|
||||||
syn = { version = "2.0.28", default-features = false, features = ["clone-impls", "derive", "full", "parsing", "printing"] }
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
fn main() {
|
|
||||||
println!("cargo:rustc-cfg=precompiled");
|
|
||||||
println!("cargo:rustc-cfg=feature=\"deserialize_in_place\"");
|
|
||||||
}
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
extern crate proc_macro2;
|
|
||||||
|
|
||||||
use proc_macro2::watt;
|
|
||||||
use proc_macro2::watt::buffer::InputBuffer;
|
|
||||||
use std::alloc::{GlobalAlloc, Layout, System};
|
|
||||||
use std::io::{self, Read, Write};
|
|
||||||
use std::sync::atomic::Ordering;
|
|
||||||
|
|
||||||
struct MonotonicAllocator;
|
|
||||||
|
|
||||||
#[global_allocator]
|
|
||||||
static ALLOCATOR: MonotonicAllocator = MonotonicAllocator;
|
|
||||||
|
|
||||||
unsafe impl GlobalAlloc for MonotonicAllocator {
|
|
||||||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
|
||||||
System.alloc(layout)
|
|
||||||
}
|
|
||||||
|
|
||||||
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {
|
|
||||||
// Leak: this cuts 3% of code size from the precompiled macro binary.
|
|
||||||
// There is no way that serde_derive would fill up all memory on the
|
|
||||||
// host. When the subprocess exits, operating system will clean this up.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
let mut buf = Vec::new();
|
|
||||||
io::stdin().read_to_end(&mut buf).unwrap();
|
|
||||||
|
|
||||||
let mut buf = InputBuffer::new(&buf);
|
|
||||||
let derive = match buf.read_u8() {
|
|
||||||
0 => serde_derive::derive_serialize,
|
|
||||||
1 => serde_derive::derive_deserialize,
|
|
||||||
2 => {
|
|
||||||
serde_derive::DESERIALIZE_IN_PLACE.store(true, Ordering::Relaxed);
|
|
||||||
serde_derive::derive_deserialize
|
|
||||||
}
|
|
||||||
_ => unreachable!(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let input = watt::load(&mut buf);
|
|
||||||
let output = derive(input);
|
|
||||||
let bytes = watt::linearize(output);
|
|
||||||
io::stdout().write_all(&bytes).unwrap();
|
|
||||||
}
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../serde_derive/src
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null
|
|
||||||
set -e -x
|
|
||||||
|
|
||||||
# TODO: Sanitize host filesystem paths. https://github.com/rust-lang/cargo/issues/12137
|
|
||||||
|
|
||||||
cargo +nightly build \
|
|
||||||
--manifest-path bin/Cargo.toml \
|
|
||||||
--bin serde_derive \
|
|
||||||
--profile precompiled \
|
|
||||||
-Z unstable-options \
|
|
||||||
-Z build-std=std,panic_abort \
|
|
||||||
-Z build-std-features=panic_immediate_abort \
|
|
||||||
--target x86_64-unknown-linux-musl \
|
|
||||||
--out-dir serde_derive
|
|
||||||
|
|
||||||
rm -f serde_derive/serde_derive-x86_64-unknown-linux-gnu
|
|
||||||
mv serde_derive/serde_derive{,-x86_64-unknown-linux-gnu}
|
|
||||||
|
|
||||||
#upx --best --lzma serde_derive/serde_derive-x86_64-unknown-linux-gnu
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "proc-macro2"
|
|
||||||
version = "1.0.66"
|
|
||||||
edition = "2021"
|
|
||||||
publish = false
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
proc-macro2 = { package = "proc-macro2-fallback", version = "1" }
|
|
||||||
@@ -1,815 +0,0 @@
|
|||||||
#[doc(hidden)]
|
|
||||||
pub mod watt;
|
|
||||||
|
|
||||||
use crate::extra::DelimSpan;
|
|
||||||
use crate::watt::Identity;
|
|
||||||
use std::cmp::Ordering;
|
|
||||||
use std::fmt::{self, Debug, Display};
|
|
||||||
use std::hash::{Hash, Hasher};
|
|
||||||
use std::ops::RangeBounds;
|
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
pub use proc_macro2::{Delimiter, Spacing};
|
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
|
||||||
pub struct Span {
|
|
||||||
lo: u32,
|
|
||||||
hi: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Span {
|
|
||||||
pub fn call_site() -> Self {
|
|
||||||
Span { lo: 0, hi: 0 }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn join(&self, other: Self) -> Option<Self> {
|
|
||||||
Some(Span {
|
|
||||||
lo: self.lo,
|
|
||||||
hi: other.hi,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub enum TokenTree {
|
|
||||||
Group(Group),
|
|
||||||
Ident(Ident),
|
|
||||||
Punct(Punct),
|
|
||||||
Literal(Literal),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TokenTree {
|
|
||||||
pub fn span(&self) -> Span {
|
|
||||||
match self {
|
|
||||||
TokenTree::Group(group) => group.span(),
|
|
||||||
TokenTree::Ident(ident) => ident.span(),
|
|
||||||
TokenTree::Punct(punct) => punct.span(),
|
|
||||||
TokenTree::Literal(literal) => literal.span(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_span(&mut self, span: Span) {
|
|
||||||
match self {
|
|
||||||
TokenTree::Group(group) => group.set_span(span),
|
|
||||||
TokenTree::Ident(ident) => ident.set_span(span),
|
|
||||||
TokenTree::Punct(punct) => punct.set_span(span),
|
|
||||||
TokenTree::Literal(literal) => literal.set_span(span),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Group> for TokenTree {
|
|
||||||
fn from(group: Group) -> Self {
|
|
||||||
TokenTree::Group(group)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Ident> for TokenTree {
|
|
||||||
fn from(ident: Ident) -> Self {
|
|
||||||
TokenTree::Ident(ident)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Punct> for TokenTree {
|
|
||||||
fn from(punct: Punct) -> Self {
|
|
||||||
TokenTree::Punct(punct)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Literal> for TokenTree {
|
|
||||||
fn from(literal: Literal) -> Self {
|
|
||||||
TokenTree::Literal(literal)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for TokenTree {
|
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
match self {
|
|
||||||
TokenTree::Group(group) => Debug::fmt(group, formatter),
|
|
||||||
TokenTree::Ident(ident) => {
|
|
||||||
let mut debug = formatter.debug_struct("Ident");
|
|
||||||
debug.field("sym", &format_args!("{}", ident));
|
|
||||||
debug.finish()
|
|
||||||
}
|
|
||||||
TokenTree::Punct(punct) => Debug::fmt(punct, formatter),
|
|
||||||
TokenTree::Literal(literal) => Debug::fmt(literal, formatter),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Group {
|
|
||||||
delimiter: Delimiter,
|
|
||||||
stream: Vec<TokenTree>,
|
|
||||||
span: Span,
|
|
||||||
span_open: Span,
|
|
||||||
span_close: Span,
|
|
||||||
identity: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Group {
|
|
||||||
pub fn new(delimiter: Delimiter, stream: TokenStream) -> Self {
|
|
||||||
Group {
|
|
||||||
delimiter,
|
|
||||||
stream: stream.content,
|
|
||||||
span: Span::call_site(),
|
|
||||||
span_open: Span::call_site(),
|
|
||||||
span_close: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn stream(&self) -> TokenStream {
|
|
||||||
TokenStream {
|
|
||||||
content: self.stream.clone(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn delimiter(&self) -> Delimiter {
|
|
||||||
self.delimiter
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn span(&self) -> Span {
|
|
||||||
self.span
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn span_open(&self) -> Span {
|
|
||||||
self.span_open
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn span_close(&self) -> Span {
|
|
||||||
self.span_close
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn delim_span(&self) -> DelimSpan {
|
|
||||||
DelimSpan {
|
|
||||||
join: self.span,
|
|
||||||
open: self.span_open,
|
|
||||||
close: self.span_close,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_span(&mut self, span: Span) {
|
|
||||||
self.span = span;
|
|
||||||
self.identity |= Identity::RESPANNED;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for Group {
|
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
let (open, close) = match self.delimiter {
|
|
||||||
Delimiter::Parenthesis => ("(", ")"),
|
|
||||||
Delimiter::Brace => ("{ ", "}"),
|
|
||||||
Delimiter::Bracket => ("[", "]"),
|
|
||||||
Delimiter::None => ("", ""),
|
|
||||||
};
|
|
||||||
|
|
||||||
formatter.write_str(open)?;
|
|
||||||
display_tokens(&self.stream, formatter)?;
|
|
||||||
if self.delimiter == Delimiter::Brace && !self.stream.is_empty() {
|
|
||||||
formatter.write_str(" ")?;
|
|
||||||
}
|
|
||||||
formatter.write_str(close)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for Group {
|
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
let mut debug = formatter.debug_struct("Group");
|
|
||||||
debug.field("delimiter", &self.delimiter);
|
|
||||||
debug.field("stream", &self.stream);
|
|
||||||
debug.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Ident {
|
|
||||||
fallback: proc_macro2::Ident,
|
|
||||||
span: Span,
|
|
||||||
identity: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Ident {
|
|
||||||
pub fn new(string: &str, span: Span) -> Self {
|
|
||||||
Ident {
|
|
||||||
fallback: proc_macro2::Ident::new(string, proc_macro2::Span::call_site()),
|
|
||||||
span,
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new_raw(string: &str, span: Span) -> Self {
|
|
||||||
Ident {
|
|
||||||
fallback: proc_macro2::Ident::new_raw(string, proc_macro2::Span::call_site()),
|
|
||||||
span,
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn span(&self) -> Span {
|
|
||||||
self.span
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_span(&mut self, span: Span) {
|
|
||||||
self.span = span;
|
|
||||||
self.identity |= Identity::RESPANNED;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for Ident {
|
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
Display::fmt(&self.fallback, formatter)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for Ident {
|
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
Debug::fmt(&self.fallback, formatter)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Eq for Ident {}
|
|
||||||
|
|
||||||
impl PartialEq for Ident {
|
|
||||||
fn eq(&self, other: &Self) -> bool {
|
|
||||||
PartialEq::eq(&self.fallback, &other.fallback)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> PartialEq<T> for Ident
|
|
||||||
where
|
|
||||||
T: ?Sized + AsRef<str>,
|
|
||||||
{
|
|
||||||
fn eq(&self, other: &T) -> bool {
|
|
||||||
PartialEq::eq(&self.fallback, other)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Ord for Ident {
|
|
||||||
fn cmp(&self, other: &Self) -> Ordering {
|
|
||||||
Ord::cmp(&self.fallback, &other.fallback)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialOrd for Ident {
|
|
||||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
||||||
PartialOrd::partial_cmp(&self.fallback, &other.fallback)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Hash for Ident {
|
|
||||||
fn hash<H: Hasher>(&self, hasher: &mut H) {
|
|
||||||
Hash::hash(&self.fallback, hasher);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Punct {
|
|
||||||
fallback: proc_macro2::Punct,
|
|
||||||
span: Span,
|
|
||||||
identity: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Punct {
|
|
||||||
pub fn new(ch: char, spacing: Spacing) -> Self {
|
|
||||||
Punct {
|
|
||||||
fallback: proc_macro2::Punct::new(ch, spacing),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn as_char(&self) -> char {
|
|
||||||
self.fallback.as_char()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn spacing(&self) -> Spacing {
|
|
||||||
self.fallback.spacing()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn span(&self) -> Span {
|
|
||||||
self.span
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_span(&mut self, span: Span) {
|
|
||||||
self.span = span;
|
|
||||||
self.identity |= Identity::RESPANNED;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for Punct {
|
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
Display::fmt(&self.fallback, formatter)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for Punct {
|
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
Debug::fmt(&self.fallback, formatter)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Literal {
|
|
||||||
fallback: proc_macro2::Literal,
|
|
||||||
span: Span,
|
|
||||||
identity: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Literal {
|
|
||||||
pub fn u8_suffixed(n: u8) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::u8_suffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn u16_suffixed(n: u16) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::u16_suffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn u32_suffixed(n: u32) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::u32_suffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn u64_suffixed(n: u64) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::u64_suffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn u128_suffixed(n: u128) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::u128_suffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn usize_suffixed(n: usize) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::usize_suffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn i8_suffixed(n: i8) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::i8_suffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn i16_suffixed(n: i16) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::i16_suffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn i32_suffixed(n: i32) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::i32_suffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn i64_suffixed(n: i64) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::i64_suffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn i128_suffixed(n: i128) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::i128_suffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn isize_suffixed(n: isize) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::isize_suffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn u8_unsuffixed(n: u8) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::u8_unsuffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn u16_unsuffixed(n: u16) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::u16_unsuffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn u32_unsuffixed(n: u32) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::u32_unsuffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn u64_unsuffixed(n: u64) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::u64_unsuffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn u128_unsuffixed(n: u128) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::u128_unsuffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn usize_unsuffixed(n: usize) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::usize_unsuffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn i8_unsuffixed(n: i8) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::i8_unsuffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn i16_unsuffixed(n: i16) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::i16_unsuffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn i32_unsuffixed(n: i32) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::i32_unsuffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn i64_unsuffixed(n: i64) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::i64_unsuffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn i128_unsuffixed(n: i128) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::i128_unsuffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn isize_unsuffixed(n: isize) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::isize_unsuffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn f64_unsuffixed(f: f64) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::f64_unsuffixed(f),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn f64_suffixed(f: f64) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::f64_suffixed(f),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn f32_unsuffixed(f: f32) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::f32_unsuffixed(f),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn f32_suffixed(f: f32) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::f32_suffixed(f),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn string(string: &str) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::string(string),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn character(ch: char) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::character(ch),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn byte_string(s: &[u8]) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::byte_string(s),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn span(&self) -> Span {
|
|
||||||
self.span
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_span(&mut self, span: Span) {
|
|
||||||
self.span = span;
|
|
||||||
self.identity |= Identity::RESPANNED;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn subspan<R: RangeBounds<usize>>(&self, range: R) -> Option<Span> {
|
|
||||||
let _ = range;
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromStr for Literal {
|
|
||||||
type Err = LexError;
|
|
||||||
|
|
||||||
fn from_str(repr: &str) -> Result<Self, Self::Err> {
|
|
||||||
let fallback = match proc_macro2::Literal::from_str(repr) {
|
|
||||||
Ok(literal) => literal,
|
|
||||||
Err(error) => {
|
|
||||||
return Err(LexError {
|
|
||||||
fallback: error,
|
|
||||||
span: Span::call_site(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Ok(Literal {
|
|
||||||
fallback,
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for Literal {
|
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
Display::fmt(&self.fallback, formatter)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for Literal {
|
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
Debug::fmt(&self.fallback, formatter)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct TokenStream {
|
|
||||||
content: Vec<TokenTree>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TokenStream {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
TokenStream {
|
|
||||||
content: Vec::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_empty(&self) -> bool {
|
|
||||||
self.content.is_empty()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl IntoIterator for TokenStream {
|
|
||||||
type Item = TokenTree;
|
|
||||||
type IntoIter = token_stream::IntoIter;
|
|
||||||
|
|
||||||
fn into_iter(self) -> Self::IntoIter {
|
|
||||||
token_stream::IntoIter {
|
|
||||||
iter: self.content.into_iter(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Extend<TokenStream> for TokenStream {
|
|
||||||
fn extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I) {
|
|
||||||
self.content.extend(streams.into_iter().flatten());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Extend<TokenTree> for TokenStream {
|
|
||||||
fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, streams: I) {
|
|
||||||
self.content.extend(streams);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromIterator<TokenStream> for TokenStream {
|
|
||||||
fn from_iter<I: IntoIterator<Item = TokenStream>>(streams: I) -> Self {
|
|
||||||
let content = streams.into_iter().flatten().collect();
|
|
||||||
TokenStream { content }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromIterator<TokenTree> for TokenStream {
|
|
||||||
fn from_iter<I: IntoIterator<Item = TokenTree>>(streams: I) -> Self {
|
|
||||||
let content = streams.into_iter().collect();
|
|
||||||
TokenStream { content }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromStr for TokenStream {
|
|
||||||
type Err = LexError;
|
|
||||||
|
|
||||||
fn from_str(string: &str) -> Result<Self, Self::Err> {
|
|
||||||
let fallback = match proc_macro2::TokenStream::from_str(string) {
|
|
||||||
Ok(token_stream) => token_stream,
|
|
||||||
Err(error) => {
|
|
||||||
return Err(LexError {
|
|
||||||
fallback: error,
|
|
||||||
span: Span::call_site(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
fn convert_token_stream(stream: proc_macro2::TokenStream) -> TokenStream {
|
|
||||||
TokenStream {
|
|
||||||
content: stream.into_iter().map(convert_token_tree).collect(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn convert_token_tree(token: proc_macro2::TokenTree) -> TokenTree {
|
|
||||||
match token {
|
|
||||||
proc_macro2::TokenTree::Group(group) => TokenTree::Group(Group::new(
|
|
||||||
group.delimiter(),
|
|
||||||
convert_token_stream(group.stream()),
|
|
||||||
)),
|
|
||||||
proc_macro2::TokenTree::Ident(ident) => TokenTree::Ident(Ident {
|
|
||||||
fallback: ident,
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}),
|
|
||||||
proc_macro2::TokenTree::Punct(punct) => TokenTree::Punct(Punct {
|
|
||||||
fallback: punct,
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}),
|
|
||||||
proc_macro2::TokenTree::Literal(literal) => TokenTree::Literal(Literal {
|
|
||||||
fallback: literal,
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(convert_token_stream(fallback))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn display_tokens(tokens: &[TokenTree], formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
let mut joint = false;
|
|
||||||
for (i, token) in tokens.iter().enumerate() {
|
|
||||||
if i != 0 && !joint {
|
|
||||||
write!(formatter, " ")?;
|
|
||||||
}
|
|
||||||
joint = false;
|
|
||||||
match token {
|
|
||||||
TokenTree::Group(group) => Display::fmt(group, formatter),
|
|
||||||
TokenTree::Ident(ident) => Display::fmt(ident, formatter),
|
|
||||||
TokenTree::Punct(punct) => {
|
|
||||||
joint = punct.spacing() == Spacing::Joint;
|
|
||||||
Display::fmt(punct, formatter)
|
|
||||||
}
|
|
||||||
TokenTree::Literal(literal) => Display::fmt(literal, formatter),
|
|
||||||
}?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for TokenStream {
|
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
display_tokens(&self.content, formatter)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for TokenStream {
|
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
formatter.write_str("TokenStream ")?;
|
|
||||||
formatter.debug_list().entries(&self.content).finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct LexError {
|
|
||||||
fallback: proc_macro2::LexError,
|
|
||||||
span: Span,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl LexError {
|
|
||||||
pub fn span(&self) -> Span {
|
|
||||||
self.span
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for LexError {
|
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
Debug::fmt(&self.fallback, formatter)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub mod token_stream {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct IntoIter {
|
|
||||||
pub(crate) iter: <Vec<TokenTree> as IntoIterator>::IntoIter,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Iterator for IntoIter {
|
|
||||||
type Item = TokenTree;
|
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
|
||||||
self.iter.next()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
||||||
self.iter.size_hint()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub mod extra {
|
|
||||||
use crate::Span;
|
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
|
||||||
pub struct DelimSpan {
|
|
||||||
pub(crate) join: Span,
|
|
||||||
pub(crate) open: Span,
|
|
||||||
pub(crate) close: Span,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DelimSpan {
|
|
||||||
pub fn join(&self) -> Span {
|
|
||||||
self.join
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn open(&self) -> Span {
|
|
||||||
self.open
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn close(&self) -> Span {
|
|
||||||
self.close
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,69 +0,0 @@
|
|||||||
use std::str;
|
|
||||||
|
|
||||||
pub struct OutputBuffer {
|
|
||||||
bytes: Vec<u8>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl OutputBuffer {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
OutputBuffer { bytes: Vec::new() }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write_u8(&mut self, value: u8) {
|
|
||||||
self.bytes.push(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write_u16(&mut self, value: u16) {
|
|
||||||
self.bytes.extend_from_slice(&value.to_le_bytes());
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write_u32(&mut self, value: u32) {
|
|
||||||
self.bytes.extend_from_slice(&value.to_le_bytes());
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write_str(&mut self, value: &str) {
|
|
||||||
self.bytes.extend_from_slice(value.as_bytes());
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn into_bytes(self) -> Vec<u8> {
|
|
||||||
self.bytes
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct InputBuffer<'a> {
|
|
||||||
bytes: &'a [u8],
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> InputBuffer<'a> {
|
|
||||||
pub fn new(bytes: &'a [u8]) -> Self {
|
|
||||||
InputBuffer { bytes }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_empty(&self) -> bool {
|
|
||||||
self.bytes.is_empty()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read_u8(&mut self) -> u8 {
|
|
||||||
let (first, rest) = self.bytes.split_first().unwrap();
|
|
||||||
self.bytes = rest;
|
|
||||||
*first
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read_u16(&mut self) -> u16 {
|
|
||||||
let (value, rest) = self.bytes.split_at(2);
|
|
||||||
self.bytes = rest;
|
|
||||||
u16::from_le_bytes([value[0], value[1]])
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read_u32(&mut self) -> u32 {
|
|
||||||
let (value, rest) = self.bytes.split_at(4);
|
|
||||||
self.bytes = rest;
|
|
||||||
u32::from_le_bytes([value[0], value[1], value[2], value[3]])
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read_str(&mut self, len: usize) -> &'a str {
|
|
||||||
let (string, rest) = self.bytes.split_at(len);
|
|
||||||
self.bytes = rest;
|
|
||||||
str::from_utf8(string).unwrap()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
pub enum Bytecode {}
|
|
||||||
|
|
||||||
impl Bytecode {
|
|
||||||
pub const GROUP_PARENTHESIS: u8 = 0;
|
|
||||||
pub const GROUP_BRACE: u8 = 1;
|
|
||||||
pub const GROUP_BRACKET: u8 = 2;
|
|
||||||
pub const GROUP_NONE: u8 = 3;
|
|
||||||
pub const IDENT: u8 = 4;
|
|
||||||
pub const PUNCT_ALONE: u8 = 5;
|
|
||||||
pub const PUNCT_JOINT: u8 = 6;
|
|
||||||
pub const LITERAL: u8 = 7;
|
|
||||||
pub const LOAD_GROUP: u8 = 8;
|
|
||||||
pub const LOAD_IDENT: u8 = 9;
|
|
||||||
pub const LOAD_PUNCT: u8 = 10;
|
|
||||||
pub const LOAD_LITERAL: u8 = 11;
|
|
||||||
pub const SET_SPAN: u8 = 12;
|
|
||||||
}
|
|
||||||
@@ -1,205 +0,0 @@
|
|||||||
pub mod buffer;
|
|
||||||
pub mod bytecode;
|
|
||||||
|
|
||||||
use crate::watt::buffer::{InputBuffer, OutputBuffer};
|
|
||||||
use crate::watt::bytecode::Bytecode;
|
|
||||||
use crate::{Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};
|
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
pub enum Kind {
|
|
||||||
Group(Delimiter),
|
|
||||||
Ident,
|
|
||||||
Punct(Spacing),
|
|
||||||
Literal,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub enum Identity {}
|
|
||||||
|
|
||||||
impl Identity {
|
|
||||||
pub const RESPANNED: u32 = 1 << 31;
|
|
||||||
pub const NOVEL: u32 = u32::MAX;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Span {
|
|
||||||
fn is_call_site(&self) -> bool {
|
|
||||||
self.lo == 0 && self.hi == 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn post_increment(counter: &mut u32) -> impl FnMut() -> u32 + '_ {
|
|
||||||
|| {
|
|
||||||
let value = *counter;
|
|
||||||
*counter += 1;
|
|
||||||
value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn load(buf: &mut InputBuffer) -> TokenStream {
|
|
||||||
let mut span_counter = 1;
|
|
||||||
let mut next_span = post_increment(&mut span_counter);
|
|
||||||
let mut next_span = || {
|
|
||||||
let next = next_span();
|
|
||||||
Span { lo: next, hi: next }
|
|
||||||
};
|
|
||||||
|
|
||||||
let [mut group_counter, mut ident_counter, mut punct_counter, mut literal_counter] = [0; 4];
|
|
||||||
let mut next_group = post_increment(&mut group_counter);
|
|
||||||
let mut next_ident = post_increment(&mut ident_counter);
|
|
||||||
let mut next_punct = post_increment(&mut punct_counter);
|
|
||||||
let mut next_literal = post_increment(&mut literal_counter);
|
|
||||||
|
|
||||||
let mut trees = Vec::new();
|
|
||||||
while !buf.is_empty() {
|
|
||||||
match match buf.read_u8() {
|
|
||||||
Bytecode::GROUP_PARENTHESIS => Kind::Group(Delimiter::Parenthesis),
|
|
||||||
Bytecode::GROUP_BRACE => Kind::Group(Delimiter::Brace),
|
|
||||||
Bytecode::GROUP_BRACKET => Kind::Group(Delimiter::Bracket),
|
|
||||||
Bytecode::GROUP_NONE => Kind::Group(Delimiter::None),
|
|
||||||
Bytecode::IDENT => Kind::Ident,
|
|
||||||
Bytecode::PUNCT_ALONE => Kind::Punct(Spacing::Alone),
|
|
||||||
Bytecode::PUNCT_JOINT => Kind::Punct(Spacing::Joint),
|
|
||||||
Bytecode::LITERAL => Kind::Literal,
|
|
||||||
_ => unreachable!(),
|
|
||||||
} {
|
|
||||||
Kind::Group(delimiter) => {
|
|
||||||
let len = buf.read_u32();
|
|
||||||
let stream = trees.drain(trees.len() - len as usize..).collect();
|
|
||||||
trees.push(TokenTree::Group(Group {
|
|
||||||
delimiter,
|
|
||||||
stream,
|
|
||||||
span: next_span(),
|
|
||||||
span_open: next_span(),
|
|
||||||
span_close: next_span(),
|
|
||||||
identity: next_group(),
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
Kind::Ident => {
|
|
||||||
let len = buf.read_u16();
|
|
||||||
let repr = buf.read_str(len as usize);
|
|
||||||
let ident = if let Some(repr) = repr.strip_prefix("r#") {
|
|
||||||
proc_macro2::Ident::new_raw(repr, proc_macro2::Span::call_site())
|
|
||||||
} else if repr == "$crate" {
|
|
||||||
proc_macro2::Ident::new("crate", proc_macro2::Span::call_site())
|
|
||||||
} else {
|
|
||||||
proc_macro2::Ident::new(repr, proc_macro2::Span::call_site())
|
|
||||||
};
|
|
||||||
trees.push(TokenTree::Ident(Ident {
|
|
||||||
fallback: ident,
|
|
||||||
span: next_span(),
|
|
||||||
identity: next_ident(),
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
Kind::Punct(spacing) => {
|
|
||||||
let ch = buf.read_u8();
|
|
||||||
assert!(ch.is_ascii());
|
|
||||||
let punct = proc_macro2::Punct::new(ch as char, spacing);
|
|
||||||
trees.push(TokenTree::Punct(Punct {
|
|
||||||
fallback: punct,
|
|
||||||
span: next_span(),
|
|
||||||
identity: next_punct(),
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
Kind::Literal => {
|
|
||||||
let len = buf.read_u16();
|
|
||||||
let repr = buf.read_str(len as usize);
|
|
||||||
let literal = proc_macro2::Literal::from_str(repr).unwrap();
|
|
||||||
trees.push(TokenTree::Literal(Literal {
|
|
||||||
fallback: literal,
|
|
||||||
span: next_span(),
|
|
||||||
identity: next_literal(),
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TokenStream { content: trees }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn linearize(tokens: TokenStream) -> Vec<u8> {
|
|
||||||
let mut buf = OutputBuffer::new();
|
|
||||||
for token in &tokens.content {
|
|
||||||
linearize_token(token, &mut buf);
|
|
||||||
}
|
|
||||||
buf.into_bytes()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn linearize_token(token: &TokenTree, buf: &mut OutputBuffer) {
|
|
||||||
let needs_span;
|
|
||||||
match token {
|
|
||||||
TokenTree::Group(group) => {
|
|
||||||
if group.identity < Identity::NOVEL {
|
|
||||||
buf.write_u8(Bytecode::LOAD_GROUP);
|
|
||||||
buf.write_u32(group.identity & !Identity::RESPANNED);
|
|
||||||
needs_span = group.identity >= Identity::RESPANNED;
|
|
||||||
} else {
|
|
||||||
let len = group.stream.len();
|
|
||||||
assert!(len <= u32::MAX as usize);
|
|
||||||
for token in &group.stream {
|
|
||||||
linearize_token(token, buf);
|
|
||||||
}
|
|
||||||
buf.write_u8(match group.delimiter {
|
|
||||||
Delimiter::Parenthesis => Bytecode::GROUP_PARENTHESIS,
|
|
||||||
Delimiter::Brace => Bytecode::GROUP_BRACE,
|
|
||||||
Delimiter::Bracket => Bytecode::GROUP_BRACKET,
|
|
||||||
Delimiter::None => Bytecode::GROUP_NONE,
|
|
||||||
});
|
|
||||||
buf.write_u32(len as u32);
|
|
||||||
needs_span = !group.span.is_call_site();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
TokenTree::Ident(ident) => {
|
|
||||||
if ident.identity < Identity::NOVEL {
|
|
||||||
buf.write_u8(Bytecode::LOAD_IDENT);
|
|
||||||
buf.write_u32(ident.identity & !Identity::RESPANNED);
|
|
||||||
needs_span = ident.identity >= Identity::RESPANNED;
|
|
||||||
} else {
|
|
||||||
buf.write_u8(Bytecode::IDENT);
|
|
||||||
let repr = ident.to_string();
|
|
||||||
assert!(repr.len() <= u16::MAX as usize);
|
|
||||||
buf.write_u16(repr.len() as u16);
|
|
||||||
buf.write_str(&repr);
|
|
||||||
linearize_span(ident.span, buf);
|
|
||||||
needs_span = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
TokenTree::Punct(punct) => {
|
|
||||||
if punct.identity < Identity::NOVEL {
|
|
||||||
buf.write_u8(Bytecode::LOAD_PUNCT);
|
|
||||||
buf.write_u32(punct.identity & !Identity::RESPANNED);
|
|
||||||
needs_span = punct.identity >= Identity::RESPANNED;
|
|
||||||
} else {
|
|
||||||
buf.write_u8(match punct.spacing() {
|
|
||||||
Spacing::Alone => Bytecode::PUNCT_ALONE,
|
|
||||||
Spacing::Joint => Bytecode::PUNCT_JOINT,
|
|
||||||
});
|
|
||||||
let ch = punct.as_char();
|
|
||||||
assert!(ch.is_ascii());
|
|
||||||
buf.write_u8(ch as u8);
|
|
||||||
needs_span = !punct.span.is_call_site();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
TokenTree::Literal(literal) => {
|
|
||||||
if literal.identity < Identity::NOVEL {
|
|
||||||
buf.write_u8(Bytecode::LOAD_LITERAL);
|
|
||||||
buf.write_u32(literal.identity & !Identity::RESPANNED);
|
|
||||||
needs_span = literal.identity >= Identity::RESPANNED;
|
|
||||||
} else {
|
|
||||||
buf.write_u8(Bytecode::LITERAL);
|
|
||||||
let repr = literal.to_string();
|
|
||||||
assert!(repr.len() <= u16::MAX as usize);
|
|
||||||
buf.write_u16(repr.len() as u16);
|
|
||||||
buf.write_str(&repr);
|
|
||||||
needs_span = !literal.span.is_call_site();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if needs_span {
|
|
||||||
buf.write_u8(Bytecode::SET_SPAN);
|
|
||||||
linearize_span(token.span(), buf);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn linearize_span(span: Span, buf: &mut OutputBuffer) {
|
|
||||||
buf.write_u32(span.lo);
|
|
||||||
buf.write_u32(span.hi);
|
|
||||||
}
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "serde_derive"
|
|
||||||
version = "1.0.181"
|
|
||||||
authors = ["David Tolnay <dtolnay@gmail.com>"]
|
|
||||||
categories = ["no-std", "no-std::no-alloc"]
|
|
||||||
description = "Implementation of #[derive(Serialize, Deserialize)]"
|
|
||||||
documentation = "https://serde.rs/derive.html"
|
|
||||||
edition = "2015"
|
|
||||||
homepage = "https://serde.rs"
|
|
||||||
include = ["serde_derive-x86_64-unknown-linux-gnu", "src", "LICENSE-APACHE", "LICENSE-MIT"]
|
|
||||||
keywords = ["serde", "serialization", "no_std", "derive"]
|
|
||||||
license = "MIT OR Apache-2.0"
|
|
||||||
readme = "crates-io.md"
|
|
||||||
repository = "https://github.com/serde-rs/serde"
|
|
||||||
rust-version = "1.56"
|
|
||||||
|
|
||||||
[features]
|
|
||||||
default = []
|
|
||||||
deserialize_in_place = []
|
|
||||||
|
|
||||||
[lib]
|
|
||||||
proc-macro = true
|
|
||||||
|
|
||||||
[target.'cfg(not(all(target_arch = "x86_64", target_os = "linux", target_env = "gnu")))'.dependencies]
|
|
||||||
proc-macro2 = "1"
|
|
||||||
quote = "1"
|
|
||||||
syn = "2.0.28"
|
|
||||||
|
|
||||||
[dev-dependencies]
|
|
||||||
serde = { version = "1", path = "../../serde" }
|
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
|
||||||
targets = ["x86_64-unknown-linux-gnu"]
|
|
||||||
rustdoc-args = ["--generate-link-to-definition"]
|
|
||||||
|
|
||||||
[workspace]
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../LICENSE-APACHE
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../LICENSE-MIT
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../README.md
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../crates-io.md
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../serde_derive/src/bound.rs
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../proc-macro2/src/watt/buffer.rs
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../proc-macro2/src/watt/bytecode.rs
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../serde_derive/src/de.rs
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../serde_derive/src/dummy.rs
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../serde_derive/src/fragment.rs
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../serde_derive/src/internals/
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
//! This crate provides Serde's two derive macros.
|
|
||||||
//!
|
|
||||||
//! ```edition2021
|
|
||||||
//! # use serde_derive::{Deserialize, Serialize};
|
|
||||||
//! #
|
|
||||||
//! #[derive(Serialize, Deserialize)]
|
|
||||||
//! # struct S;
|
|
||||||
//! #
|
|
||||||
//! # fn main() {}
|
|
||||||
//! ```
|
|
||||||
//!
|
|
||||||
//! Please refer to [https://serde.rs/derive.html] for how to set this up.
|
|
||||||
//!
|
|
||||||
//! [https://serde.rs/derive.html]: https://serde.rs/derive.html
|
|
||||||
|
|
||||||
#![doc(html_root_url = "https://docs.rs/serde_derive/1.0.181")]
|
|
||||||
|
|
||||||
#[cfg(not(all(target_arch = "x86_64", target_os = "linux", target_env = "gnu")))]
|
|
||||||
include!("lib_from_source.rs");
|
|
||||||
|
|
||||||
#[cfg(all(target_arch = "x86_64", target_os = "linux", target_env = "gnu"))]
|
|
||||||
include!("lib_precompiled.rs");
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
extern crate proc_macro;
|
|
||||||
extern crate proc_macro2;
|
|
||||||
extern crate quote;
|
|
||||||
extern crate syn;
|
|
||||||
|
|
||||||
#[macro_use]
|
|
||||||
mod bound;
|
|
||||||
#[macro_use]
|
|
||||||
mod fragment;
|
|
||||||
|
|
||||||
mod de;
|
|
||||||
mod dummy;
|
|
||||||
mod internals;
|
|
||||||
mod pretend;
|
|
||||||
mod ser;
|
|
||||||
mod this;
|
|
||||||
|
|
||||||
use proc_macro::TokenStream;
|
|
||||||
use syn::{parse_macro_input, DeriveInput};
|
|
||||||
|
|
||||||
#[proc_macro_derive(Serialize, attributes(serde))]
|
|
||||||
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(syn::Error::into_compile_error)
|
|
||||||
.into()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[proc_macro_derive(Deserialize, attributes(serde))]
|
|
||||||
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(syn::Error::into_compile_error)
|
|
||||||
.into()
|
|
||||||
}
|
|
||||||
@@ -1,234 +0,0 @@
|
|||||||
extern crate proc_macro;
|
|
||||||
|
|
||||||
mod buffer;
|
|
||||||
mod bytecode;
|
|
||||||
|
|
||||||
use crate::buffer::{InputBuffer, OutputBuffer};
|
|
||||||
use crate::bytecode::Bytecode;
|
|
||||||
use proc_macro::{Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};
|
|
||||||
use std::io::{ErrorKind, Read, Write};
|
|
||||||
use std::iter::FromIterator;
|
|
||||||
use std::path::Path;
|
|
||||||
use std::process::{Command, ExitStatus, Stdio};
|
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
#[proc_macro_derive(Serialize, attributes(serde))]
|
|
||||||
pub fn derive_serialize(input: TokenStream) -> TokenStream {
|
|
||||||
derive(0, input)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[proc_macro_derive(Deserialize, attributes(serde))]
|
|
||||||
pub fn derive_deserialize(input: TokenStream) -> TokenStream {
|
|
||||||
derive(1 + cfg!(feature = "deserialize_in_place") as u8, input)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn derive(select: u8, input: TokenStream) -> TokenStream {
|
|
||||||
let mut memory = TokenMemory::default();
|
|
||||||
let mut buf = OutputBuffer::new();
|
|
||||||
buf.write_u8(select);
|
|
||||||
|
|
||||||
memory.spans.push(Span::call_site());
|
|
||||||
for token in input {
|
|
||||||
memory.linearize_token(token, &mut buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
let exe_path = Path::new(concat!(
|
|
||||||
env!("CARGO_MANIFEST_DIR"),
|
|
||||||
"/serde_derive-x86_64-unknown-linux-gnu",
|
|
||||||
));
|
|
||||||
let mut child = match Command::new(exe_path)
|
|
||||||
.stdin(Stdio::piped())
|
|
||||||
.stdout(Stdio::piped())
|
|
||||||
.spawn()
|
|
||||||
{
|
|
||||||
Ok(child) => child,
|
|
||||||
Err(io_error) => {
|
|
||||||
if io_error.kind() == ErrorKind::NotFound {
|
|
||||||
panic!(
|
|
||||||
"file missing from serde_derive manifest directory during macro expansion: {}",
|
|
||||||
exe_path.display(),
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
panic!("failed to spawn process: {}", io_error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut stdin = child.stdin.take().unwrap();
|
|
||||||
let mut buf = buf.into_bytes();
|
|
||||||
stdin.write_all(&buf).unwrap();
|
|
||||||
drop(stdin);
|
|
||||||
|
|
||||||
let mut stdout = child.stdout.take().unwrap();
|
|
||||||
buf.clear();
|
|
||||||
stdout.read_to_end(&mut buf).unwrap();
|
|
||||||
|
|
||||||
let success = child.wait().as_ref().map_or(true, ExitStatus::success);
|
|
||||||
if !success || buf.is_empty() {
|
|
||||||
panic!();
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut buf = InputBuffer::new(&buf);
|
|
||||||
memory.receive(&mut buf)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
struct TokenMemory {
|
|
||||||
spans: Vec<Span>,
|
|
||||||
groups: Vec<Group>,
|
|
||||||
idents: Vec<Ident>,
|
|
||||||
puncts: Vec<Punct>,
|
|
||||||
literals: Vec<Literal>,
|
|
||||||
}
|
|
||||||
|
|
||||||
enum Kind {
|
|
||||||
Group(Delimiter),
|
|
||||||
Ident,
|
|
||||||
Punct(Spacing),
|
|
||||||
Literal,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TokenMemory {
|
|
||||||
// Depth-first post-order traversal.
|
|
||||||
fn linearize_token(&mut self, token: TokenTree, buf: &mut OutputBuffer) {
|
|
||||||
match token {
|
|
||||||
TokenTree::Group(group) => {
|
|
||||||
let mut len = 0usize;
|
|
||||||
for token in group.stream() {
|
|
||||||
self.linearize_token(token, buf);
|
|
||||||
len += 1;
|
|
||||||
}
|
|
||||||
assert!(len <= u32::MAX as usize);
|
|
||||||
buf.write_u8(match group.delimiter() {
|
|
||||||
Delimiter::Parenthesis => Bytecode::GROUP_PARENTHESIS,
|
|
||||||
Delimiter::Brace => Bytecode::GROUP_BRACE,
|
|
||||||
Delimiter::Bracket => Bytecode::GROUP_BRACKET,
|
|
||||||
Delimiter::None => Bytecode::GROUP_NONE,
|
|
||||||
});
|
|
||||||
buf.write_u32(len as u32);
|
|
||||||
self.spans
|
|
||||||
.extend([group.span(), group.span_open(), group.span_close()]);
|
|
||||||
self.groups.push(group);
|
|
||||||
}
|
|
||||||
TokenTree::Ident(ident) => {
|
|
||||||
buf.write_u8(Bytecode::IDENT);
|
|
||||||
let repr = ident.to_string();
|
|
||||||
assert!(repr.len() <= u16::MAX as usize);
|
|
||||||
buf.write_u16(repr.len() as u16);
|
|
||||||
buf.write_str(&repr);
|
|
||||||
self.spans.push(ident.span());
|
|
||||||
self.idents.push(ident);
|
|
||||||
}
|
|
||||||
TokenTree::Punct(punct) => {
|
|
||||||
buf.write_u8(match punct.spacing() {
|
|
||||||
Spacing::Alone => Bytecode::PUNCT_ALONE,
|
|
||||||
Spacing::Joint => Bytecode::PUNCT_JOINT,
|
|
||||||
});
|
|
||||||
let ch = punct.as_char();
|
|
||||||
assert!(ch.is_ascii());
|
|
||||||
buf.write_u8(ch as u8);
|
|
||||||
self.spans.push(punct.span());
|
|
||||||
self.puncts.push(punct);
|
|
||||||
}
|
|
||||||
TokenTree::Literal(literal) => {
|
|
||||||
buf.write_u8(Bytecode::LITERAL);
|
|
||||||
let repr = literal.to_string();
|
|
||||||
assert!(repr.len() <= u16::MAX as usize);
|
|
||||||
buf.write_u16(repr.len() as u16);
|
|
||||||
buf.write_str(&repr);
|
|
||||||
self.spans.push(literal.span());
|
|
||||||
self.literals.push(literal);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn receive(&self, buf: &mut InputBuffer) -> TokenStream {
|
|
||||||
let mut trees = Vec::new();
|
|
||||||
while !buf.is_empty() {
|
|
||||||
match match buf.read_u8() {
|
|
||||||
Bytecode::GROUP_PARENTHESIS => Kind::Group(Delimiter::Parenthesis),
|
|
||||||
Bytecode::GROUP_BRACE => Kind::Group(Delimiter::Brace),
|
|
||||||
Bytecode::GROUP_BRACKET => Kind::Group(Delimiter::Bracket),
|
|
||||||
Bytecode::GROUP_NONE => Kind::Group(Delimiter::None),
|
|
||||||
Bytecode::IDENT => Kind::Ident,
|
|
||||||
Bytecode::PUNCT_ALONE => Kind::Punct(Spacing::Alone),
|
|
||||||
Bytecode::PUNCT_JOINT => Kind::Punct(Spacing::Joint),
|
|
||||||
Bytecode::LITERAL => Kind::Literal,
|
|
||||||
Bytecode::LOAD_GROUP => {
|
|
||||||
let identity = buf.read_u32();
|
|
||||||
let group = self.groups[identity as usize].clone();
|
|
||||||
trees.push(TokenTree::Group(group));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
Bytecode::LOAD_IDENT => {
|
|
||||||
let identity = buf.read_u32();
|
|
||||||
let ident = self.idents[identity as usize].clone();
|
|
||||||
trees.push(TokenTree::Ident(ident));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
Bytecode::LOAD_PUNCT => {
|
|
||||||
let identity = buf.read_u32();
|
|
||||||
let punct = self.puncts[identity as usize].clone();
|
|
||||||
trees.push(TokenTree::Punct(punct));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
Bytecode::LOAD_LITERAL => {
|
|
||||||
let identity = buf.read_u32();
|
|
||||||
let literal = self.literals[identity as usize].clone();
|
|
||||||
trees.push(TokenTree::Literal(literal));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
Bytecode::SET_SPAN => {
|
|
||||||
trees.last_mut().unwrap().set_span(self.read_span(buf));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
_ => unreachable!(),
|
|
||||||
} {
|
|
||||||
Kind::Group(delimiter) => {
|
|
||||||
let len = buf.read_u32();
|
|
||||||
let stream = trees.drain(trees.len() - len as usize..).collect();
|
|
||||||
let group = Group::new(delimiter, stream);
|
|
||||||
trees.push(TokenTree::Group(group));
|
|
||||||
}
|
|
||||||
Kind::Ident => {
|
|
||||||
let len = buf.read_u16();
|
|
||||||
let repr = buf.read_str(len as usize);
|
|
||||||
let span = self.read_span(buf);
|
|
||||||
let ident = if let Some(repr) = repr.strip_prefix("r#") {
|
|
||||||
Ident::new_raw(repr, span)
|
|
||||||
} else {
|
|
||||||
Ident::new(repr, span)
|
|
||||||
};
|
|
||||||
trees.push(TokenTree::Ident(ident));
|
|
||||||
}
|
|
||||||
Kind::Punct(spacing) => {
|
|
||||||
let ch = buf.read_u8();
|
|
||||||
assert!(ch.is_ascii());
|
|
||||||
let punct = Punct::new(ch as char, spacing);
|
|
||||||
trees.push(TokenTree::Punct(punct));
|
|
||||||
}
|
|
||||||
Kind::Literal => {
|
|
||||||
let len = buf.read_u16();
|
|
||||||
let repr = buf.read_str(len as usize);
|
|
||||||
let literal = Literal::from_str(repr).unwrap();
|
|
||||||
trees.push(TokenTree::Literal(literal));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TokenStream::from_iter(trees)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_span(&self, buf: &mut InputBuffer) -> Span {
|
|
||||||
let lo = buf.read_u32();
|
|
||||||
let hi = buf.read_u32();
|
|
||||||
let span = self.spans[lo as usize];
|
|
||||||
if lo == hi {
|
|
||||||
span
|
|
||||||
} else {
|
|
||||||
#[cfg(any())] // FIXME
|
|
||||||
return span.join(self.spans[hi as usize]).unwrap_or(span);
|
|
||||||
span
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../serde_derive/src/pretend.rs
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../serde_derive/src/ser.rs
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../serde_derive/src/this.rs
|
|
||||||
+10
-2
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde"
|
name = "serde"
|
||||||
version = "1.0.181" # remember to update html_root_url and serde_derive dependency
|
version = "1.0.186" # remember to update html_root_url and serde_derive dependency
|
||||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
||||||
build = "build.rs"
|
build = "build.rs"
|
||||||
categories = ["encoding", "no-std", "no-std::no-alloc"]
|
categories = ["encoding", "no-std", "no-std::no-alloc"]
|
||||||
@@ -15,7 +15,7 @@ repository = "https://github.com/serde-rs/serde"
|
|||||||
rust-version = "1.31"
|
rust-version = "1.31"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
serde_derive = { version = "=1.0.181", optional = true, path = "../serde_derive" }
|
serde_derive = { version = "1", optional = true, path = "../serde_derive" }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
serde_derive = { version = "1", path = "../serde_derive" }
|
serde_derive = { version = "1", path = "../serde_derive" }
|
||||||
@@ -31,6 +31,14 @@ features = ["derive"]
|
|||||||
targets = ["x86_64-unknown-linux-gnu"]
|
targets = ["x86_64-unknown-linux-gnu"]
|
||||||
rustdoc-args = ["--generate-link-to-definition"]
|
rustdoc-args = ["--generate-link-to-definition"]
|
||||||
|
|
||||||
|
# This cfg cannot be enabled, but it still forces Cargo to keep serde_derive's
|
||||||
|
# version in lockstep with serde's, even if someone depends on the two crates
|
||||||
|
# separately with serde's "derive" feature disabled. Every serde_derive release
|
||||||
|
# is compatible with exactly one serde release because the generated code
|
||||||
|
# involves nonpublic APIs which are not bound by semver.
|
||||||
|
[target.'cfg(any())'.dependencies]
|
||||||
|
serde_derive = { version = "=1.0.186", path = "../serde_derive" }
|
||||||
|
|
||||||
|
|
||||||
### FEATURES #################################################################
|
### FEATURES #################################################################
|
||||||
|
|
||||||
|
|||||||
@@ -1789,6 +1789,9 @@ forwarded_impl!((T), Box<[T]>, Vec::into_boxed_slice);
|
|||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
forwarded_impl!((), Box<str>, String::into_boxed_str);
|
forwarded_impl!((), Box<str>, String::into_boxed_str);
|
||||||
|
|
||||||
|
#[cfg(all(feature = "std", any(unix, windows)))]
|
||||||
|
forwarded_impl!((), Box<OsStr>, OsString::into_boxed_os_str);
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
impl<'de, 'a, T: ?Sized> Deserialize<'de> for Cow<'a, T>
|
impl<'de, 'a, T: ?Sized> Deserialize<'de> for Cow<'a, T>
|
||||||
where
|
where
|
||||||
|
|||||||
+1
-3
@@ -123,7 +123,6 @@ mod format;
|
|||||||
mod ignored_any;
|
mod ignored_any;
|
||||||
mod impls;
|
mod impls;
|
||||||
pub(crate) mod size_hint;
|
pub(crate) mod size_hint;
|
||||||
mod utf8;
|
|
||||||
|
|
||||||
pub use self::ignored_any::IgnoredAny;
|
pub use self::ignored_any::IgnoredAny;
|
||||||
|
|
||||||
@@ -1478,7 +1477,7 @@ pub trait Visitor<'de>: Sized {
|
|||||||
where
|
where
|
||||||
E: Error,
|
E: Error,
|
||||||
{
|
{
|
||||||
self.visit_str(utf8::encode(v).as_str())
|
self.visit_str(v.encode_utf8(&mut [0u8; 4]))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The input contains a string. The lifetime of the string is ephemeral and
|
/// The input contains a string. The lifetime of the string is ephemeral and
|
||||||
@@ -1555,7 +1554,6 @@ pub trait Visitor<'de>: Sized {
|
|||||||
where
|
where
|
||||||
E: Error,
|
E: Error,
|
||||||
{
|
{
|
||||||
let _ = v;
|
|
||||||
Err(Error::invalid_type(Unexpected::Bytes(v), &self))
|
Err(Error::invalid_type(Unexpected::Bytes(v), &self))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,46 +0,0 @@
|
|||||||
use crate::lib::*;
|
|
||||||
|
|
||||||
const TAG_CONT: u8 = 0b1000_0000;
|
|
||||||
const TAG_TWO_B: u8 = 0b1100_0000;
|
|
||||||
const TAG_THREE_B: u8 = 0b1110_0000;
|
|
||||||
const TAG_FOUR_B: u8 = 0b1111_0000;
|
|
||||||
const MAX_ONE_B: u32 = 0x80;
|
|
||||||
const MAX_TWO_B: u32 = 0x800;
|
|
||||||
const MAX_THREE_B: u32 = 0x10000;
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn encode(c: char) -> Encode {
|
|
||||||
let code = c as u32;
|
|
||||||
let mut buf = [0; 4];
|
|
||||||
let pos = if code < MAX_ONE_B {
|
|
||||||
buf[3] = code as u8;
|
|
||||||
3
|
|
||||||
} else if code < MAX_TWO_B {
|
|
||||||
buf[2] = (code >> 6 & 0x1F) as u8 | TAG_TWO_B;
|
|
||||||
buf[3] = (code & 0x3F) as u8 | TAG_CONT;
|
|
||||||
2
|
|
||||||
} else if code < MAX_THREE_B {
|
|
||||||
buf[1] = (code >> 12 & 0x0F) as u8 | TAG_THREE_B;
|
|
||||||
buf[2] = (code >> 6 & 0x3F) as u8 | TAG_CONT;
|
|
||||||
buf[3] = (code & 0x3F) as u8 | TAG_CONT;
|
|
||||||
1
|
|
||||||
} else {
|
|
||||||
buf[0] = (code >> 18 & 0x07) as u8 | TAG_FOUR_B;
|
|
||||||
buf[1] = (code >> 12 & 0x3F) as u8 | TAG_CONT;
|
|
||||||
buf[2] = (code >> 6 & 0x3F) as u8 | TAG_CONT;
|
|
||||||
buf[3] = (code & 0x3F) as u8 | TAG_CONT;
|
|
||||||
0
|
|
||||||
};
|
|
||||||
Encode { buf, pos }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Encode {
|
|
||||||
buf: [u8; 4],
|
|
||||||
pos: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Encode {
|
|
||||||
pub fn as_str(&self) -> &str {
|
|
||||||
str::from_utf8(&self.buf[self.pos..]).unwrap()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+1
-1
@@ -93,7 +93,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.181")]
|
#![doc(html_root_url = "https://docs.rs/serde/1.0.186")]
|
||||||
// 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
|
||||||
|
|||||||
@@ -1370,3 +1370,16 @@ impl Serialize for AdjacentlyTaggedEnumVariant {
|
|||||||
serializer.serialize_unit_variant(self.enum_name, self.variant_index, self.variant_name)
|
serializer.serialize_unit_variant(self.enum_name, self.variant_index, self.variant_name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Error when Serialize for a non_exhaustive remote enum encounters a variant
|
||||||
|
// that is not recognized.
|
||||||
|
pub struct CannotSerializeVariant<T>(pub T);
|
||||||
|
|
||||||
|
impl<T> Display for CannotSerializeVariant<T>
|
||||||
|
where
|
||||||
|
T: Debug,
|
||||||
|
{
|
||||||
|
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(formatter, "enum variant cannot be serialized: {:?}", self.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde_derive"
|
name = "serde_derive"
|
||||||
version = "1.0.181" # remember to update html_root_url
|
version = "1.0.186" # 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>"]
|
||||||
categories = ["no-std", "no-std::no-alloc"]
|
categories = ["no-std", "no-std::no-alloc"]
|
||||||
description = "Macros 1.1 implementation of #[derive(Serialize, Deserialize)]"
|
description = "Macros 1.1 implementation of #[derive(Serialize, Deserialize)]"
|
||||||
|
|||||||
+116
-117
@@ -6,8 +6,6 @@ use proc_macro2::{Literal, Span, TokenStream};
|
|||||||
use quote::{quote, quote_spanned, ToTokens};
|
use quote::{quote, quote_spanned, ToTokens};
|
||||||
use std::collections::BTreeSet;
|
use std::collections::BTreeSet;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
#[cfg(precompiled)]
|
|
||||||
use std::sync::atomic::Ordering;
|
|
||||||
use syn::punctuated::Punctuated;
|
use syn::punctuated::Punctuated;
|
||||||
use syn::spanned::Spanned;
|
use syn::spanned::Spanned;
|
||||||
use syn::{parse_quote, Ident, Index, Member};
|
use syn::{parse_quote, Ident, Index, Member};
|
||||||
@@ -301,11 +299,6 @@ fn deserialize_body(cont: &Container, params: &Parameters) -> Fragment {
|
|||||||
|
|
||||||
#[cfg(feature = "deserialize_in_place")]
|
#[cfg(feature = "deserialize_in_place")]
|
||||||
fn deserialize_in_place_body(cont: &Container, params: &Parameters) -> Option<Stmts> {
|
fn deserialize_in_place_body(cont: &Container, params: &Parameters) -> Option<Stmts> {
|
||||||
#[cfg(precompiled)]
|
|
||||||
if !crate::DESERIALIZE_IN_PLACE.load(Ordering::Relaxed) {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Only remote derives have getters, and we do not generate
|
// Only remote derives have getters, and we do not generate
|
||||||
// deserialize_in_place for remote derives.
|
// deserialize_in_place for remote derives.
|
||||||
assert!(!params.has_getter);
|
assert!(!params.has_getter);
|
||||||
@@ -723,19 +716,11 @@ fn deserialize_seq(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let value_if_none = match field.attrs.default() {
|
let value_if_none = expr_is_missing_seq(None, index_in_seq, field, cattrs, expecting);
|
||||||
attr::Default::Default => quote!(_serde::__private::Default::default()),
|
|
||||||
attr::Default::Path(path) => quote!(#path()),
|
|
||||||
attr::Default::None => quote!(
|
|
||||||
return _serde::__private::Err(_serde::de::Error::invalid_length(#index_in_seq, &#expecting));
|
|
||||||
),
|
|
||||||
};
|
|
||||||
let assign = quote! {
|
let assign = quote! {
|
||||||
let #var = match #visit {
|
let #var = match #visit {
|
||||||
_serde::__private::Some(__value) => __value,
|
_serde::__private::Some(__value) => __value,
|
||||||
_serde::__private::None => {
|
_serde::__private::None => #value_if_none,
|
||||||
#value_if_none
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
index_in_seq += 1;
|
index_in_seq += 1;
|
||||||
@@ -811,24 +796,14 @@ fn deserialize_seq_in_place(
|
|||||||
self.place.#member = #default;
|
self.place.#member = #default;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let value_if_none = match field.attrs.default() {
|
let value_if_none = expr_is_missing_seq(Some(quote!(self.place.#member = )), index_in_seq, field, cattrs, expecting);
|
||||||
attr::Default::Default => quote!(
|
|
||||||
self.place.#member = _serde::__private::Default::default();
|
|
||||||
),
|
|
||||||
attr::Default::Path(path) => quote!(
|
|
||||||
self.place.#member = #path();
|
|
||||||
),
|
|
||||||
attr::Default::None => quote!(
|
|
||||||
return _serde::__private::Err(_serde::de::Error::invalid_length(#index_in_seq, &#expecting));
|
|
||||||
),
|
|
||||||
};
|
|
||||||
let write = match field.attrs.deserialize_with() {
|
let write = match field.attrs.deserialize_with() {
|
||||||
None => {
|
None => {
|
||||||
quote! {
|
quote! {
|
||||||
if let _serde::__private::None = _serde::de::SeqAccess::next_element_seed(&mut __seq,
|
if let _serde::__private::None = _serde::de::SeqAccess::next_element_seed(&mut __seq,
|
||||||
_serde::__private::de::InPlaceSeed(&mut self.place.#member))?
|
_serde::__private::de::InPlaceSeed(&mut self.place.#member))?
|
||||||
{
|
{
|
||||||
#value_if_none
|
#value_if_none;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -841,7 +816,7 @@ fn deserialize_seq_in_place(
|
|||||||
self.place.#member = __wrap.value;
|
self.place.#member = __wrap.value;
|
||||||
}
|
}
|
||||||
_serde::__private::None => {
|
_serde::__private::None => {
|
||||||
#value_if_none
|
#value_if_none;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -983,12 +958,7 @@ fn deserialize_struct(
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
let field_visitor = Stmts(deserialize_generated_identifier(
|
let field_visitor = deserialize_field_identifier(&field_names_idents, cattrs);
|
||||||
&field_names_idents,
|
|
||||||
cattrs,
|
|
||||||
false,
|
|
||||||
None,
|
|
||||||
));
|
|
||||||
|
|
||||||
// untagged struct variants do not get a visit_seq method. The same applies to
|
// untagged struct variants do not get a visit_seq method. The same applies to
|
||||||
// structs that only have a map representation.
|
// structs that only have a map representation.
|
||||||
@@ -1040,7 +1010,7 @@ fn deserialize_struct(
|
|||||||
} else {
|
} else {
|
||||||
let field_names = field_names_idents
|
let field_names = field_names_idents
|
||||||
.iter()
|
.iter()
|
||||||
.flat_map(|(_, _, aliases)| aliases);
|
.flat_map(|&(_, _, aliases)| aliases);
|
||||||
|
|
||||||
Some(quote! {
|
Some(quote! {
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
@@ -1146,12 +1116,7 @@ fn deserialize_struct_in_place(
|
|||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let field_visitor = Stmts(deserialize_generated_identifier(
|
let field_visitor = deserialize_field_identifier(&field_names_idents, cattrs);
|
||||||
&field_names_idents,
|
|
||||||
cattrs,
|
|
||||||
false,
|
|
||||||
None,
|
|
||||||
));
|
|
||||||
|
|
||||||
let mut_seq = if field_names_idents.is_empty() {
|
let mut_seq = if field_names_idents.is_empty() {
|
||||||
quote!(_)
|
quote!(_)
|
||||||
@@ -1162,7 +1127,7 @@ fn deserialize_struct_in_place(
|
|||||||
let visit_map = Stmts(deserialize_map_in_place(params, fields, cattrs));
|
let visit_map = Stmts(deserialize_map_in_place(params, fields, cattrs));
|
||||||
let field_names = field_names_idents
|
let field_names = field_names_idents
|
||||||
.iter()
|
.iter()
|
||||||
.flat_map(|(_, _, aliases)| aliases);
|
.flat_map(|&(_, _, aliases)| aliases);
|
||||||
let type_name = cattrs.name().deserialize_name();
|
let type_name = cattrs.name().deserialize_name();
|
||||||
|
|
||||||
let in_place_impl_generics = de_impl_generics.in_place();
|
let in_place_impl_generics = de_impl_generics.in_place();
|
||||||
@@ -1265,7 +1230,12 @@ fn prepare_enum_variant_enum(
|
|||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let other_idx = deserialized_variants.position(|(_, variant)| variant.attrs.other());
|
let fallthrough = deserialized_variants
|
||||||
|
.position(|(_, variant)| variant.attrs.other())
|
||||||
|
.map(|other_idx| {
|
||||||
|
let ignore_variant = variant_names_idents[other_idx].1.clone();
|
||||||
|
quote!(_serde::__private::Ok(__Field::#ignore_variant))
|
||||||
|
});
|
||||||
|
|
||||||
let variants_stmt = {
|
let variants_stmt = {
|
||||||
let variant_names = variant_names_idents.iter().map(|(name, _, _)| name);
|
let variant_names = variant_names_idents.iter().map(|(name, _, _)| name);
|
||||||
@@ -1279,7 +1249,8 @@ fn prepare_enum_variant_enum(
|
|||||||
&variant_names_idents,
|
&variant_names_idents,
|
||||||
cattrs,
|
cattrs,
|
||||||
true,
|
true,
|
||||||
other_idx,
|
None,
|
||||||
|
fallthrough,
|
||||||
));
|
));
|
||||||
|
|
||||||
(variants_stmt, variant_visitor)
|
(variants_stmt, variant_visitor)
|
||||||
@@ -2001,30 +1972,15 @@ fn deserialize_untagged_newtype_variant(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_generated_identifier(
|
fn deserialize_generated_identifier(
|
||||||
fields: &[(String, Ident, Vec<String>)],
|
fields: &[(&str, Ident, &BTreeSet<String>)],
|
||||||
cattrs: &attr::Container,
|
cattrs: &attr::Container,
|
||||||
is_variant: bool,
|
is_variant: bool,
|
||||||
other_idx: Option<usize>,
|
ignore_variant: Option<TokenStream>,
|
||||||
|
fallthrough: Option<TokenStream>,
|
||||||
) -> Fragment {
|
) -> Fragment {
|
||||||
let this_value = quote!(__Field);
|
let this_value = quote!(__Field);
|
||||||
let field_idents: &Vec<_> = &fields.iter().map(|(_, ident, _)| ident).collect();
|
let field_idents: &Vec<_> = &fields.iter().map(|(_, ident, _)| ident).collect();
|
||||||
|
|
||||||
let (ignore_variant, fallthrough) = if !is_variant && cattrs.has_flatten() {
|
|
||||||
let ignore_variant = quote!(__other(_serde::__private::de::Content<'de>),);
|
|
||||||
let fallthrough = quote!(_serde::__private::Ok(__Field::__other(__value)));
|
|
||||||
(Some(ignore_variant), Some(fallthrough))
|
|
||||||
} else if let Some(other_idx) = other_idx {
|
|
||||||
let ignore_variant = fields[other_idx].1.clone();
|
|
||||||
let fallthrough = quote!(_serde::__private::Ok(__Field::#ignore_variant));
|
|
||||||
(None, Some(fallthrough))
|
|
||||||
} else if is_variant || cattrs.deny_unknown_fields() {
|
|
||||||
(None, None)
|
|
||||||
} else {
|
|
||||||
let ignore_variant = quote!(__ignore,);
|
|
||||||
let fallthrough = quote!(_serde::__private::Ok(__Field::__ignore));
|
|
||||||
(Some(ignore_variant), Some(fallthrough))
|
|
||||||
};
|
|
||||||
|
|
||||||
let visitor_impl = Stmts(deserialize_identifier(
|
let visitor_impl = Stmts(deserialize_identifier(
|
||||||
&this_value,
|
&this_value,
|
||||||
fields,
|
fields,
|
||||||
@@ -2070,6 +2026,33 @@ fn deserialize_generated_identifier(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Generates enum and its `Deserialize` implementation that represents each
|
||||||
|
/// non-skipped field of the struct
|
||||||
|
fn deserialize_field_identifier(
|
||||||
|
fields: &[(&str, Ident, &BTreeSet<String>)],
|
||||||
|
cattrs: &attr::Container,
|
||||||
|
) -> Stmts {
|
||||||
|
let (ignore_variant, fallthrough) = if cattrs.has_flatten() {
|
||||||
|
let ignore_variant = quote!(__other(_serde::__private::de::Content<'de>),);
|
||||||
|
let fallthrough = quote!(_serde::__private::Ok(__Field::__other(__value)));
|
||||||
|
(Some(ignore_variant), Some(fallthrough))
|
||||||
|
} else if cattrs.deny_unknown_fields() {
|
||||||
|
(None, None)
|
||||||
|
} else {
|
||||||
|
let ignore_variant = quote!(__ignore,);
|
||||||
|
let fallthrough = quote!(_serde::__private::Ok(__Field::__ignore));
|
||||||
|
(Some(ignore_variant), Some(fallthrough))
|
||||||
|
};
|
||||||
|
|
||||||
|
Stmts(deserialize_generated_identifier(
|
||||||
|
fields,
|
||||||
|
cattrs,
|
||||||
|
false,
|
||||||
|
ignore_variant,
|
||||||
|
fallthrough,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
// Generates `Deserialize::deserialize` body for an enum with
|
// Generates `Deserialize::deserialize` body for an enum with
|
||||||
// `serde(field_identifier)` or `serde(variant_identifier)` attribute.
|
// `serde(field_identifier)` or `serde(variant_identifier)` attribute.
|
||||||
fn deserialize_custom_identifier(
|
fn deserialize_custom_identifier(
|
||||||
@@ -2131,7 +2114,7 @@ fn deserialize_custom_identifier(
|
|||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let names = names_idents.iter().map(|(name, _, _)| name);
|
let names = names_idents.iter().flat_map(|&(_, _, aliases)| aliases);
|
||||||
|
|
||||||
let names_const = if fallthrough.is_some() {
|
let names_const = if fallthrough.is_some() {
|
||||||
None
|
None
|
||||||
@@ -2187,32 +2170,24 @@ fn deserialize_custom_identifier(
|
|||||||
|
|
||||||
fn deserialize_identifier(
|
fn deserialize_identifier(
|
||||||
this_value: &TokenStream,
|
this_value: &TokenStream,
|
||||||
fields: &[(String, Ident, Vec<String>)],
|
fields: &[(&str, Ident, &BTreeSet<String>)],
|
||||||
is_variant: bool,
|
is_variant: bool,
|
||||||
fallthrough: Option<TokenStream>,
|
fallthrough: Option<TokenStream>,
|
||||||
fallthrough_borrowed: Option<TokenStream>,
|
fallthrough_borrowed: Option<TokenStream>,
|
||||||
collect_other_fields: bool,
|
collect_other_fields: bool,
|
||||||
expecting: Option<&str>,
|
expecting: Option<&str>,
|
||||||
) -> Fragment {
|
) -> Fragment {
|
||||||
let mut flat_fields = Vec::new();
|
let str_mapping = fields.iter().map(|(_, ident, aliases)| {
|
||||||
for (_, ident, aliases) in fields {
|
// `aliases` also contains a main name
|
||||||
flat_fields.extend(aliases.iter().map(|alias| (alias, ident)));
|
quote!(#(#aliases)|* => _serde::__private::Ok(#this_value::#ident))
|
||||||
}
|
});
|
||||||
|
let bytes_mapping = fields.iter().map(|(_, ident, aliases)| {
|
||||||
let field_strs: &Vec<_> = &flat_fields.iter().map(|(name, _)| name).collect();
|
// `aliases` also contains a main name
|
||||||
let field_bytes: &Vec<_> = &flat_fields
|
let aliases = aliases
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(name, _)| Literal::byte_string(name.as_bytes()))
|
.map(|alias| Literal::byte_string(alias.as_bytes()));
|
||||||
.collect();
|
quote!(#(#aliases)|* => _serde::__private::Ok(#this_value::#ident))
|
||||||
|
});
|
||||||
let constructors: &Vec<_> = &flat_fields
|
|
||||||
.iter()
|
|
||||||
.map(|(_, ident)| quote!(#this_value::#ident))
|
|
||||||
.collect();
|
|
||||||
let main_constructors: &Vec<_> = &fields
|
|
||||||
.iter()
|
|
||||||
.map(|(_, ident, _)| quote!(#this_value::#ident))
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
let expecting = expecting.unwrap_or(if is_variant {
|
let expecting = expecting.unwrap_or(if is_variant {
|
||||||
"variant identifier"
|
"variant identifier"
|
||||||
@@ -2220,8 +2195,6 @@ fn deserialize_identifier(
|
|||||||
"field identifier"
|
"field identifier"
|
||||||
});
|
});
|
||||||
|
|
||||||
let index_expecting = if is_variant { "variant" } else { "field" };
|
|
||||||
|
|
||||||
let bytes_to_str = if fallthrough.is_some() || collect_other_fields {
|
let bytes_to_str = if fallthrough.is_some() || collect_other_fields {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
@@ -2269,21 +2242,6 @@ fn deserialize_identifier(
|
|||||||
&fallthrough_arm_tokens
|
&fallthrough_arm_tokens
|
||||||
};
|
};
|
||||||
|
|
||||||
let u64_fallthrough_arm_tokens;
|
|
||||||
let u64_fallthrough_arm = if let Some(fallthrough) = &fallthrough {
|
|
||||||
fallthrough
|
|
||||||
} else {
|
|
||||||
let fallthrough_msg = format!("{} index 0 <= i < {}", index_expecting, fields.len());
|
|
||||||
u64_fallthrough_arm_tokens = quote! {
|
|
||||||
_serde::__private::Err(_serde::de::Error::invalid_value(
|
|
||||||
_serde::de::Unexpected::Unsigned(__value),
|
|
||||||
&#fallthrough_msg,
|
|
||||||
))
|
|
||||||
};
|
|
||||||
&u64_fallthrough_arm_tokens
|
|
||||||
};
|
|
||||||
|
|
||||||
let variant_indices = 0_u64..;
|
|
||||||
let visit_other = if collect_other_fields {
|
let visit_other = if collect_other_fields {
|
||||||
quote! {
|
quote! {
|
||||||
fn visit_bool<__E>(self, __value: bool) -> _serde::__private::Result<Self::Value, __E>
|
fn visit_bool<__E>(self, __value: bool) -> _serde::__private::Result<Self::Value, __E>
|
||||||
@@ -2378,15 +2336,33 @@ fn deserialize_identifier(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
let u64_mapping = fields.iter().enumerate().map(|(i, (_, ident, _))| {
|
||||||
|
let i = i as u64;
|
||||||
|
quote!(#i => _serde::__private::Ok(#this_value::#ident))
|
||||||
|
});
|
||||||
|
|
||||||
|
let u64_fallthrough_arm_tokens;
|
||||||
|
let u64_fallthrough_arm = if let Some(fallthrough) = &fallthrough {
|
||||||
|
fallthrough
|
||||||
|
} else {
|
||||||
|
let index_expecting = if is_variant { "variant" } else { "field" };
|
||||||
|
let fallthrough_msg = format!("{} index 0 <= i < {}", index_expecting, fields.len());
|
||||||
|
u64_fallthrough_arm_tokens = quote! {
|
||||||
|
_serde::__private::Err(_serde::de::Error::invalid_value(
|
||||||
|
_serde::de::Unexpected::Unsigned(__value),
|
||||||
|
&#fallthrough_msg,
|
||||||
|
))
|
||||||
|
};
|
||||||
|
&u64_fallthrough_arm_tokens
|
||||||
|
};
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
fn visit_u64<__E>(self, __value: u64) -> _serde::__private::Result<Self::Value, __E>
|
fn visit_u64<__E>(self, __value: u64) -> _serde::__private::Result<Self::Value, __E>
|
||||||
where
|
where
|
||||||
__E: _serde::de::Error,
|
__E: _serde::de::Error,
|
||||||
{
|
{
|
||||||
match __value {
|
match __value {
|
||||||
#(
|
#(#u64_mapping,)*
|
||||||
#variant_indices => _serde::__private::Ok(#main_constructors),
|
|
||||||
)*
|
|
||||||
_ => #u64_fallthrough_arm,
|
_ => #u64_fallthrough_arm,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2394,6 +2370,8 @@ fn deserialize_identifier(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let visit_borrowed = if fallthrough_borrowed.is_some() || collect_other_fields {
|
let visit_borrowed = if fallthrough_borrowed.is_some() || collect_other_fields {
|
||||||
|
let str_mapping = str_mapping.clone();
|
||||||
|
let bytes_mapping = bytes_mapping.clone();
|
||||||
let fallthrough_borrowed_arm = fallthrough_borrowed.as_ref().unwrap_or(fallthrough_arm);
|
let fallthrough_borrowed_arm = fallthrough_borrowed.as_ref().unwrap_or(fallthrough_arm);
|
||||||
Some(quote! {
|
Some(quote! {
|
||||||
fn visit_borrowed_str<__E>(self, __value: &'de str) -> _serde::__private::Result<Self::Value, __E>
|
fn visit_borrowed_str<__E>(self, __value: &'de str) -> _serde::__private::Result<Self::Value, __E>
|
||||||
@@ -2401,9 +2379,7 @@ fn deserialize_identifier(
|
|||||||
__E: _serde::de::Error,
|
__E: _serde::de::Error,
|
||||||
{
|
{
|
||||||
match __value {
|
match __value {
|
||||||
#(
|
#(#str_mapping,)*
|
||||||
#field_strs => _serde::__private::Ok(#constructors),
|
|
||||||
)*
|
|
||||||
_ => {
|
_ => {
|
||||||
#value_as_borrowed_str_content
|
#value_as_borrowed_str_content
|
||||||
#fallthrough_borrowed_arm
|
#fallthrough_borrowed_arm
|
||||||
@@ -2416,9 +2392,7 @@ fn deserialize_identifier(
|
|||||||
__E: _serde::de::Error,
|
__E: _serde::de::Error,
|
||||||
{
|
{
|
||||||
match __value {
|
match __value {
|
||||||
#(
|
#(#bytes_mapping,)*
|
||||||
#field_bytes => _serde::__private::Ok(#constructors),
|
|
||||||
)*
|
|
||||||
_ => {
|
_ => {
|
||||||
#bytes_to_str
|
#bytes_to_str
|
||||||
#value_as_borrowed_bytes_content
|
#value_as_borrowed_bytes_content
|
||||||
@@ -2443,9 +2417,7 @@ fn deserialize_identifier(
|
|||||||
__E: _serde::de::Error,
|
__E: _serde::de::Error,
|
||||||
{
|
{
|
||||||
match __value {
|
match __value {
|
||||||
#(
|
#(#str_mapping,)*
|
||||||
#field_strs => _serde::__private::Ok(#constructors),
|
|
||||||
)*
|
|
||||||
_ => {
|
_ => {
|
||||||
#value_as_str_content
|
#value_as_str_content
|
||||||
#fallthrough_arm
|
#fallthrough_arm
|
||||||
@@ -2458,9 +2430,7 @@ fn deserialize_identifier(
|
|||||||
__E: _serde::de::Error,
|
__E: _serde::de::Error,
|
||||||
{
|
{
|
||||||
match __value {
|
match __value {
|
||||||
#(
|
#(#bytes_mapping,)*
|
||||||
#field_bytes => _serde::__private::Ok(#constructors),
|
|
||||||
)*
|
|
||||||
_ => {
|
_ => {
|
||||||
#bytes_to_str
|
#bytes_to_str
|
||||||
#value_as_bytes_content
|
#value_as_bytes_content
|
||||||
@@ -2993,6 +2963,35 @@ fn expr_is_missing(field: &Field, cattrs: &attr::Container) -> Fragment {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn expr_is_missing_seq(
|
||||||
|
assign_to: Option<TokenStream>,
|
||||||
|
index: usize,
|
||||||
|
field: &Field,
|
||||||
|
cattrs: &attr::Container,
|
||||||
|
expecting: &str,
|
||||||
|
) -> TokenStream {
|
||||||
|
match field.attrs.default() {
|
||||||
|
attr::Default::Default => {
|
||||||
|
let span = field.original.span();
|
||||||
|
return quote_spanned!(span=> #assign_to _serde::__private::Default::default());
|
||||||
|
}
|
||||||
|
attr::Default::Path(path) => {
|
||||||
|
return quote_spanned!(path.span()=> #assign_to #path());
|
||||||
|
}
|
||||||
|
attr::Default::None => { /* below */ }
|
||||||
|
}
|
||||||
|
|
||||||
|
match *cattrs.default() {
|
||||||
|
attr::Default::Default | attr::Default::Path(_) => {
|
||||||
|
let member = &field.member;
|
||||||
|
quote!(#assign_to __default.#member)
|
||||||
|
}
|
||||||
|
attr::Default::None => quote!(
|
||||||
|
return _serde::__private::Err(_serde::de::Error::invalid_length(#index, &#expecting))
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn effective_style(variant: &Variant) -> Style {
|
fn effective_style(variant: &Variant) -> Style {
|
||||||
match variant.style {
|
match variant.style {
|
||||||
Style::Newtype if variant.fields[0].attrs.skip_deserializing() => Style::Unit,
|
Style::Newtype if variant.fields[0].attrs.skip_deserializing() => Style::Unit,
|
||||||
|
|||||||
@@ -134,7 +134,7 @@ pub struct Name {
|
|||||||
serialize_renamed: bool,
|
serialize_renamed: bool,
|
||||||
deserialize: String,
|
deserialize: String,
|
||||||
deserialize_renamed: bool,
|
deserialize_renamed: bool,
|
||||||
deserialize_aliases: Vec<String>,
|
deserialize_aliases: BTreeSet<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn unraw(ident: &Ident) -> String {
|
fn unraw(ident: &Ident) -> String {
|
||||||
@@ -148,16 +148,12 @@ impl Name {
|
|||||||
de_name: Attr<String>,
|
de_name: Attr<String>,
|
||||||
de_aliases: Option<VecAttr<String>>,
|
de_aliases: Option<VecAttr<String>>,
|
||||||
) -> Name {
|
) -> Name {
|
||||||
let deserialize_aliases = match de_aliases {
|
let mut alias_set = BTreeSet::new();
|
||||||
Some(de_aliases) => {
|
if let Some(de_aliases) = de_aliases {
|
||||||
let mut alias_list = BTreeSet::new();
|
for alias_name in de_aliases.get() {
|
||||||
for alias_name in de_aliases.get() {
|
alias_set.insert(alias_name);
|
||||||
alias_list.insert(alias_name);
|
|
||||||
}
|
|
||||||
alias_list.into_iter().collect()
|
|
||||||
}
|
}
|
||||||
None => Vec::new(),
|
}
|
||||||
};
|
|
||||||
|
|
||||||
let ser_name = ser_name.get();
|
let ser_name = ser_name.get();
|
||||||
let ser_renamed = ser_name.is_some();
|
let ser_renamed = ser_name.is_some();
|
||||||
@@ -168,27 +164,22 @@ impl Name {
|
|||||||
serialize_renamed: ser_renamed,
|
serialize_renamed: ser_renamed,
|
||||||
deserialize: de_name.unwrap_or(source_name),
|
deserialize: de_name.unwrap_or(source_name),
|
||||||
deserialize_renamed: de_renamed,
|
deserialize_renamed: de_renamed,
|
||||||
deserialize_aliases,
|
deserialize_aliases: alias_set,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the container name for the container when serializing.
|
/// Return the container name for the container when serializing.
|
||||||
pub fn serialize_name(&self) -> String {
|
pub fn serialize_name(&self) -> &str {
|
||||||
self.serialize.clone()
|
&self.serialize
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the container name for the container when deserializing.
|
/// Return the container name for the container when deserializing.
|
||||||
pub fn deserialize_name(&self) -> String {
|
pub fn deserialize_name(&self) -> &str {
|
||||||
self.deserialize.clone()
|
&self.deserialize
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_aliases(&self) -> Vec<String> {
|
fn deserialize_aliases(&self) -> &BTreeSet<String> {
|
||||||
let mut aliases = self.deserialize_aliases.clone();
|
&self.deserialize_aliases
|
||||||
let main_name = self.deserialize_name();
|
|
||||||
if !aliases.contains(&main_name) {
|
|
||||||
aliases.push(main_name);
|
|
||||||
}
|
|
||||||
aliases
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -230,6 +221,7 @@ pub struct Container {
|
|||||||
is_packed: bool,
|
is_packed: bool,
|
||||||
/// Error message generated when type can't be deserialized
|
/// Error message generated when type can't be deserialized
|
||||||
expecting: Option<String>,
|
expecting: Option<String>,
|
||||||
|
non_exhaustive: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Styles of representing an enum.
|
/// Styles of representing an enum.
|
||||||
@@ -315,9 +307,12 @@ impl Container {
|
|||||||
let mut variant_identifier = BoolAttr::none(cx, VARIANT_IDENTIFIER);
|
let mut variant_identifier = BoolAttr::none(cx, VARIANT_IDENTIFIER);
|
||||||
let mut serde_path = Attr::none(cx, CRATE);
|
let mut serde_path = Attr::none(cx, CRATE);
|
||||||
let mut expecting = Attr::none(cx, EXPECTING);
|
let mut expecting = Attr::none(cx, EXPECTING);
|
||||||
|
let mut non_exhaustive = false;
|
||||||
|
|
||||||
for attr in &item.attrs {
|
for attr in &item.attrs {
|
||||||
if attr.path() != SERDE {
|
if attr.path() != SERDE {
|
||||||
|
non_exhaustive |=
|
||||||
|
matches!(&attr.meta, syn::Meta::Path(path) if path == NON_EXHAUSTIVE);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -405,20 +400,20 @@ impl Container {
|
|||||||
if let Some(path) = parse_lit_into_expr_path(cx, DEFAULT, &meta)? {
|
if let Some(path) = parse_lit_into_expr_path(cx, DEFAULT, &meta)? {
|
||||||
match &item.data {
|
match &item.data {
|
||||||
syn::Data::Struct(syn::DataStruct { fields, .. }) => match fields {
|
syn::Data::Struct(syn::DataStruct { fields, .. }) => match fields {
|
||||||
syn::Fields::Named(_) => {
|
syn::Fields::Named(_) | syn::Fields::Unnamed(_) => {
|
||||||
default.set(&meta.path, Default::Path(path));
|
default.set(&meta.path, Default::Path(path));
|
||||||
}
|
}
|
||||||
syn::Fields::Unnamed(_) | syn::Fields::Unit => {
|
syn::Fields::Unit => {
|
||||||
let msg = "#[serde(default = \"...\")] can only be used on structs with named fields";
|
let msg = "#[serde(default = \"...\")] can only be used on structs that have fields";
|
||||||
cx.syn_error(meta.error(msg));
|
cx.syn_error(meta.error(msg));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
syn::Data::Enum(_) => {
|
syn::Data::Enum(_) => {
|
||||||
let msg = "#[serde(default = \"...\")] can only be used on structs with named fields";
|
let msg = "#[serde(default = \"...\")] can only be used on structs";
|
||||||
cx.syn_error(meta.error(msg));
|
cx.syn_error(meta.error(msg));
|
||||||
}
|
}
|
||||||
syn::Data::Union(_) => {
|
syn::Data::Union(_) => {
|
||||||
let msg = "#[serde(default = \"...\")] can only be used on structs with named fields";
|
let msg = "#[serde(default = \"...\")] can only be used on structs";
|
||||||
cx.syn_error(meta.error(msg));
|
cx.syn_error(meta.error(msg));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -427,20 +422,20 @@ impl Container {
|
|||||||
// #[serde(default)]
|
// #[serde(default)]
|
||||||
match &item.data {
|
match &item.data {
|
||||||
syn::Data::Struct(syn::DataStruct { fields, .. }) => match fields {
|
syn::Data::Struct(syn::DataStruct { fields, .. }) => match fields {
|
||||||
syn::Fields::Named(_) => {
|
syn::Fields::Named(_) | syn::Fields::Unnamed(_) => {
|
||||||
default.set(meta.path, Default::Default);
|
default.set(meta.path, Default::Default);
|
||||||
}
|
}
|
||||||
syn::Fields::Unnamed(_) | syn::Fields::Unit => {
|
syn::Fields::Unit => {
|
||||||
let msg = "#[serde(default)] can only be used on structs with named fields";
|
let msg = "#[serde(default)] can only be used on structs that have fields";
|
||||||
cx.error_spanned_by(fields, msg);
|
cx.error_spanned_by(fields, msg);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
syn::Data::Enum(_) => {
|
syn::Data::Enum(_) => {
|
||||||
let msg = "#[serde(default)] can only be used on structs with named fields";
|
let msg = "#[serde(default)] can only be used on structs";
|
||||||
cx.syn_error(meta.error(msg));
|
cx.syn_error(meta.error(msg));
|
||||||
}
|
}
|
||||||
syn::Data::Union(_) => {
|
syn::Data::Union(_) => {
|
||||||
let msg = "#[serde(default)] can only be used on structs with named fields";
|
let msg = "#[serde(default)] can only be used on structs";
|
||||||
cx.syn_error(meta.error(msg));
|
cx.syn_error(meta.error(msg));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -596,6 +591,7 @@ impl Container {
|
|||||||
serde_path: serde_path.get(),
|
serde_path: serde_path.get(),
|
||||||
is_packed,
|
is_packed,
|
||||||
expecting: expecting.get(),
|
expecting: expecting.get(),
|
||||||
|
non_exhaustive,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -681,6 +677,10 @@ impl Container {
|
|||||||
pub fn expecting(&self) -> Option<&str> {
|
pub fn expecting(&self) -> Option<&str> {
|
||||||
self.expecting.as_ref().map(String::as_ref)
|
self.expecting.as_ref().map(String::as_ref)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn non_exhaustive(&self) -> bool {
|
||||||
|
self.non_exhaustive
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn decide_tag(
|
fn decide_tag(
|
||||||
@@ -716,7 +716,7 @@ fn decide_tag(
|
|||||||
}
|
}
|
||||||
TagType::Internal { tag }
|
TagType::Internal { tag }
|
||||||
}
|
}
|
||||||
(Some((untagged_tokens, _)), Some((tag_tokens, _)), None) => {
|
(Some((untagged_tokens, ())), Some((tag_tokens, _)), None) => {
|
||||||
let msg = "enum cannot be both untagged and internally tagged";
|
let msg = "enum cannot be both untagged and internally tagged";
|
||||||
cx.error_spanned_by(untagged_tokens, msg);
|
cx.error_spanned_by(untagged_tokens, msg);
|
||||||
cx.error_spanned_by(tag_tokens, msg);
|
cx.error_spanned_by(tag_tokens, msg);
|
||||||
@@ -727,14 +727,14 @@ fn decide_tag(
|
|||||||
cx.error_spanned_by(content_tokens, msg);
|
cx.error_spanned_by(content_tokens, msg);
|
||||||
TagType::External
|
TagType::External
|
||||||
}
|
}
|
||||||
(Some((untagged_tokens, _)), None, Some((content_tokens, _))) => {
|
(Some((untagged_tokens, ())), None, Some((content_tokens, _))) => {
|
||||||
let msg = "untagged enum cannot have #[serde(content = \"...\")]";
|
let msg = "untagged enum cannot have #[serde(content = \"...\")]";
|
||||||
cx.error_spanned_by(untagged_tokens, msg);
|
cx.error_spanned_by(untagged_tokens, msg);
|
||||||
cx.error_spanned_by(content_tokens, msg);
|
cx.error_spanned_by(content_tokens, msg);
|
||||||
TagType::External
|
TagType::External
|
||||||
}
|
}
|
||||||
(None, Some((_, tag)), Some((_, content))) => TagType::Adjacent { tag, content },
|
(None, Some((_, tag)), Some((_, content))) => TagType::Adjacent { tag, content },
|
||||||
(Some((untagged_tokens, _)), Some((tag_tokens, _)), Some((content_tokens, _))) => {
|
(Some((untagged_tokens, ())), Some((tag_tokens, _)), Some((content_tokens, _))) => {
|
||||||
let msg = "untagged enum cannot have #[serde(tag = \"...\", content = \"...\")]";
|
let msg = "untagged enum cannot have #[serde(tag = \"...\", content = \"...\")]";
|
||||||
cx.error_spanned_by(untagged_tokens, msg);
|
cx.error_spanned_by(untagged_tokens, msg);
|
||||||
cx.error_spanned_by(tag_tokens, msg);
|
cx.error_spanned_by(tag_tokens, msg);
|
||||||
@@ -756,7 +756,7 @@ fn decide_identifier(
|
|||||||
variant_identifier.0.get_with_tokens(),
|
variant_identifier.0.get_with_tokens(),
|
||||||
) {
|
) {
|
||||||
(_, None, None) => Identifier::No,
|
(_, None, None) => Identifier::No,
|
||||||
(_, Some((field_identifier_tokens, _)), Some((variant_identifier_tokens, _))) => {
|
(_, Some((field_identifier_tokens, ())), Some((variant_identifier_tokens, ()))) => {
|
||||||
let msg =
|
let msg =
|
||||||
"#[serde(field_identifier)] and #[serde(variant_identifier)] cannot both be set";
|
"#[serde(field_identifier)] and #[serde(variant_identifier)] cannot both be set";
|
||||||
cx.error_spanned_by(field_identifier_tokens, msg);
|
cx.error_spanned_by(field_identifier_tokens, msg);
|
||||||
@@ -977,7 +977,7 @@ impl Variant {
|
|||||||
&self.name
|
&self.name
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn aliases(&self) -> Vec<String> {
|
pub fn aliases(&self) -> &BTreeSet<String> {
|
||||||
self.name.deserialize_aliases()
|
self.name.deserialize_aliases()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -988,6 +988,9 @@ impl Variant {
|
|||||||
if !self.name.deserialize_renamed {
|
if !self.name.deserialize_renamed {
|
||||||
self.name.deserialize = rules.deserialize.apply_to_variant(&self.name.deserialize);
|
self.name.deserialize = rules.deserialize.apply_to_variant(&self.name.deserialize);
|
||||||
}
|
}
|
||||||
|
self.name
|
||||||
|
.deserialize_aliases
|
||||||
|
.insert(self.name.deserialize.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rename_all_rules(&self) -> RenameAllRules {
|
pub fn rename_all_rules(&self) -> RenameAllRules {
|
||||||
@@ -1316,7 +1319,7 @@ impl Field {
|
|||||||
&self.name
|
&self.name
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn aliases(&self) -> Vec<String> {
|
pub fn aliases(&self) -> &BTreeSet<String> {
|
||||||
self.name.deserialize_aliases()
|
self.name.deserialize_aliases()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1327,6 +1330,9 @@ impl Field {
|
|||||||
if !self.name.deserialize_renamed {
|
if !self.name.deserialize_renamed {
|
||||||
self.name.deserialize = rules.deserialize.apply_to_field(&self.name.deserialize);
|
self.name.deserialize = rules.deserialize.apply_to_field(&self.name.deserialize);
|
||||||
}
|
}
|
||||||
|
self.name
|
||||||
|
.deserialize_aliases
|
||||||
|
.insert(self.name.deserialize.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn skip_serializing(&self) -> bool {
|
pub fn skip_serializing(&self) -> bool {
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
use crate::internals::ast::{Container, Data, Field, Style};
|
use crate::internals::ast::{Container, Data, Field, Style};
|
||||||
use crate::internals::attr::{Identifier, TagType};
|
use crate::internals::attr::{Default, Identifier, TagType};
|
||||||
use crate::internals::{ungroup, Ctxt, Derive};
|
use crate::internals::{ungroup, Ctxt, Derive};
|
||||||
use syn::{Member, Type};
|
use syn::{Member, Type};
|
||||||
|
|
||||||
// Cross-cutting checks that require looking at more than a single attrs object.
|
// Cross-cutting checks that require looking at more than a single attrs object.
|
||||||
// Simpler checks should happen when parsing and building the attrs.
|
// Simpler checks should happen when parsing and building the attrs.
|
||||||
pub fn check(cx: &Ctxt, cont: &mut Container, derive: Derive) {
|
pub fn check(cx: &Ctxt, cont: &mut Container, derive: Derive) {
|
||||||
|
check_default_on_tuple(cx, cont);
|
||||||
check_remote_generic(cx, cont);
|
check_remote_generic(cx, cont);
|
||||||
check_getter(cx, cont);
|
check_getter(cx, cont);
|
||||||
check_flatten(cx, cont);
|
check_flatten(cx, cont);
|
||||||
@@ -17,6 +18,39 @@ pub fn check(cx: &Ctxt, cont: &mut Container, derive: Derive) {
|
|||||||
check_from_and_try_from(cx, cont);
|
check_from_and_try_from(cx, cont);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If some field of a tuple struct is marked #[serde(default)] then all fields
|
||||||
|
// after it must also be marked with that attribute, or the struct must have a
|
||||||
|
// container-level serde(default) attribute. A field's default value is only
|
||||||
|
// used for tuple fields if the sequence is exhausted at that point; that means
|
||||||
|
// all subsequent fields will fail to deserialize if they don't have their own
|
||||||
|
// default.
|
||||||
|
fn check_default_on_tuple(cx: &Ctxt, cont: &Container) {
|
||||||
|
if let Default::None = cont.attrs.default() {
|
||||||
|
if let Data::Struct(Style::Tuple, fields) = &cont.data {
|
||||||
|
let mut first_default_index = None;
|
||||||
|
for (i, field) in fields.iter().enumerate() {
|
||||||
|
// Skipped fields automatically get the #[serde(default)]
|
||||||
|
// attribute. We are interested only on non-skipped fields here.
|
||||||
|
if field.attrs.skip_deserializing() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if let Default::None = field.attrs.default() {
|
||||||
|
if let Some(first) = first_default_index {
|
||||||
|
cx.error_spanned_by(
|
||||||
|
field.ty,
|
||||||
|
format!("field must have #[serde(default)] because previous field {} has #[serde(default)]", first),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if first_default_index.is_none() {
|
||||||
|
first_default_index = Some(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Remote derive definition type must have either all of the generics of the
|
// Remote derive definition type must have either all of the generics of the
|
||||||
// remote type:
|
// remote type:
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ pub const FLATTEN: Symbol = Symbol("flatten");
|
|||||||
pub const FROM: Symbol = Symbol("from");
|
pub const FROM: Symbol = Symbol("from");
|
||||||
pub const GETTER: Symbol = Symbol("getter");
|
pub const GETTER: Symbol = Symbol("getter");
|
||||||
pub const INTO: Symbol = Symbol("into");
|
pub const INTO: Symbol = Symbol("into");
|
||||||
|
pub const NON_EXHAUSTIVE: Symbol = Symbol("non_exhaustive");
|
||||||
pub const OTHER: Symbol = Symbol("other");
|
pub const OTHER: Symbol = Symbol("other");
|
||||||
pub const REMOTE: Symbol = Symbol("remote");
|
pub const REMOTE: Symbol = Symbol("remote");
|
||||||
pub const RENAME: Symbol = Symbol("rename");
|
pub const RENAME: Symbol = Symbol("rename");
|
||||||
|
|||||||
+3
-22
@@ -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.181")]
|
#![doc(html_root_url = "https://docs.rs/serde_derive/1.0.186")]
|
||||||
// Ignored clippy lints
|
// Ignored clippy lints
|
||||||
#![allow(
|
#![allow(
|
||||||
// clippy false positive: https://github.com/rust-lang/rust-clippy/issues/7054
|
// clippy false positive: https://github.com/rust-lang/rust-clippy/issues/7054
|
||||||
@@ -66,17 +66,11 @@ extern crate proc_macro2;
|
|||||||
extern crate quote;
|
extern crate quote;
|
||||||
extern crate syn;
|
extern crate syn;
|
||||||
|
|
||||||
#[cfg(not(precompiled))]
|
|
||||||
extern crate proc_macro;
|
extern crate proc_macro;
|
||||||
#[cfg(precompiled)]
|
|
||||||
extern crate proc_macro2 as proc_macro;
|
|
||||||
|
|
||||||
mod internals;
|
mod internals;
|
||||||
|
|
||||||
use proc_macro::TokenStream;
|
use proc_macro::TokenStream;
|
||||||
#[cfg(precompiled)]
|
|
||||||
use std::sync::atomic::AtomicBool;
|
|
||||||
#[cfg(not(precompiled))]
|
|
||||||
use syn::parse_macro_input;
|
use syn::parse_macro_input;
|
||||||
use syn::DeriveInput;
|
use syn::DeriveInput;
|
||||||
|
|
||||||
@@ -91,20 +85,7 @@ mod pretend;
|
|||||||
mod ser;
|
mod ser;
|
||||||
mod this;
|
mod this;
|
||||||
|
|
||||||
#[cfg(precompiled)]
|
#[proc_macro_derive(Serialize, attributes(serde))]
|
||||||
macro_rules! parse_macro_input {
|
|
||||||
($tokenstream:ident as $ty:ty) => {
|
|
||||||
match syn::parse2::<$ty>($tokenstream) {
|
|
||||||
Ok(data) => data,
|
|
||||||
Err(err) => return err.to_compile_error(),
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(precompiled)]
|
|
||||||
pub static DESERIALIZE_IN_PLACE: AtomicBool = AtomicBool::new(false);
|
|
||||||
|
|
||||||
#[cfg_attr(not(precompiled), proc_macro_derive(Serialize, attributes(serde)))]
|
|
||||||
pub fn derive_serialize(input: TokenStream) -> TokenStream {
|
pub fn derive_serialize(input: TokenStream) -> TokenStream {
|
||||||
let mut input = parse_macro_input!(input as DeriveInput);
|
let mut input = parse_macro_input!(input as DeriveInput);
|
||||||
ser::expand_derive_serialize(&mut input)
|
ser::expand_derive_serialize(&mut input)
|
||||||
@@ -112,7 +93,7 @@ pub fn derive_serialize(input: TokenStream) -> TokenStream {
|
|||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg_attr(not(precompiled), proc_macro_derive(Deserialize, attributes(serde)))]
|
#[proc_macro_derive(Deserialize, attributes(serde))]
|
||||||
pub fn derive_deserialize(input: TokenStream) -> TokenStream {
|
pub fn derive_deserialize(input: TokenStream) -> TokenStream {
|
||||||
let mut input = parse_macro_input!(input as DeriveInput);
|
let mut input = parse_macro_input!(input as DeriveInput);
|
||||||
de::expand_derive_deserialize(&mut input)
|
de::expand_derive_deserialize(&mut input)
|
||||||
|
|||||||
+17
-11
@@ -401,7 +401,7 @@ fn serialize_enum(params: &Parameters, variants: &[Variant], cattrs: &attr::Cont
|
|||||||
|
|
||||||
let self_var = ¶ms.self_var;
|
let self_var = ¶ms.self_var;
|
||||||
|
|
||||||
let arms: Vec<_> = variants
|
let mut arms: Vec<_> = variants
|
||||||
.iter()
|
.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.map(|(variant_index, variant)| {
|
.map(|(variant_index, variant)| {
|
||||||
@@ -409,6 +409,12 @@ fn serialize_enum(params: &Parameters, variants: &[Variant], cattrs: &attr::Cont
|
|||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
if cattrs.remote().is_some() && cattrs.non_exhaustive() {
|
||||||
|
arms.push(quote! {
|
||||||
|
ref unrecognized => _serde::__private::Err(_serde::ser::Error::custom(_serde::__private::ser::CannotSerializeVariant(unrecognized))),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
quote_expr! {
|
quote_expr! {
|
||||||
match *#self_var {
|
match *#self_var {
|
||||||
#(#arms)*
|
#(#arms)*
|
||||||
@@ -566,7 +572,7 @@ fn serialize_externally_tagged_variant(
|
|||||||
},
|
},
|
||||||
params,
|
params,
|
||||||
&variant.fields,
|
&variant.fields,
|
||||||
&type_name,
|
type_name,
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -631,7 +637,7 @@ fn serialize_internally_tagged_variant(
|
|||||||
StructVariant::InternallyTagged { tag, variant_name },
|
StructVariant::InternallyTagged { tag, variant_name },
|
||||||
params,
|
params,
|
||||||
&variant.fields,
|
&variant.fields,
|
||||||
&type_name,
|
type_name,
|
||||||
),
|
),
|
||||||
Style::Tuple => unreachable!("checked in serde_derive_internals"),
|
Style::Tuple => unreachable!("checked in serde_derive_internals"),
|
||||||
}
|
}
|
||||||
@@ -698,13 +704,13 @@ fn serialize_adjacently_tagged_variant(
|
|||||||
StructVariant::Untagged,
|
StructVariant::Untagged,
|
||||||
params,
|
params,
|
||||||
&variant.fields,
|
&variant.fields,
|
||||||
&variant_name,
|
variant_name,
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let fields_ty = variant.fields.iter().map(|f| &f.ty);
|
let fields_ty = variant.fields.iter().map(|f| &f.ty);
|
||||||
let fields_ident: &Vec<_> = &match variant.style {
|
let fields_ident: &[_] = &match variant.style {
|
||||||
Style::Unit => {
|
Style::Unit => {
|
||||||
if variant.attrs.serialize_with().is_some() {
|
if variant.attrs.serialize_with().is_some() {
|
||||||
vec![]
|
vec![]
|
||||||
@@ -794,16 +800,16 @@ fn serialize_untagged_variant(
|
|||||||
Style::Tuple => serialize_tuple_variant(TupleVariant::Untagged, params, &variant.fields),
|
Style::Tuple => serialize_tuple_variant(TupleVariant::Untagged, params, &variant.fields),
|
||||||
Style::Struct => {
|
Style::Struct => {
|
||||||
let type_name = cattrs.name().serialize_name();
|
let type_name = cattrs.name().serialize_name();
|
||||||
serialize_struct_variant(StructVariant::Untagged, params, &variant.fields, &type_name)
|
serialize_struct_variant(StructVariant::Untagged, params, &variant.fields, type_name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum TupleVariant {
|
enum TupleVariant<'a> {
|
||||||
ExternallyTagged {
|
ExternallyTagged {
|
||||||
type_name: String,
|
type_name: &'a str,
|
||||||
variant_index: u32,
|
variant_index: u32,
|
||||||
variant_name: String,
|
variant_name: &'a str,
|
||||||
},
|
},
|
||||||
Untagged,
|
Untagged,
|
||||||
}
|
}
|
||||||
@@ -870,11 +876,11 @@ fn serialize_tuple_variant(
|
|||||||
enum StructVariant<'a> {
|
enum StructVariant<'a> {
|
||||||
ExternallyTagged {
|
ExternallyTagged {
|
||||||
variant_index: u32,
|
variant_index: u32,
|
||||||
variant_name: String,
|
variant_name: &'a str,
|
||||||
},
|
},
|
||||||
InternallyTagged {
|
InternallyTagged {
|
||||||
tag: &'a str,
|
tag: &'a str,
|
||||||
variant_name: String,
|
variant_name: &'a str,
|
||||||
},
|
},
|
||||||
Untagged,
|
Untagged,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
#![allow(internal_features)]
|
||||||
#![feature(lang_items, start)]
|
#![feature(lang_items, start)]
|
||||||
#![no_std]
|
#![no_std]
|
||||||
|
|
||||||
|
|||||||
@@ -605,7 +605,7 @@ fn test_unknown_field_rename_struct() {
|
|||||||
Token::Str("a4"),
|
Token::Str("a4"),
|
||||||
Token::I32(3),
|
Token::I32(3),
|
||||||
],
|
],
|
||||||
"unknown field `a4`, expected one of `a1`, `a3`, `a2`, `a5`, `a6`",
|
"unknown field `a4`, expected one of `a1`, `a2`, `a3`, `a5`, `a6`",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -799,7 +799,7 @@ fn test_unknown_field_rename_enum() {
|
|||||||
Token::Str("d"),
|
Token::Str("d"),
|
||||||
Token::I8(2),
|
Token::I8(2),
|
||||||
],
|
],
|
||||||
"unknown field `d`, expected one of `a`, `c`, `b`, `e`, `f`",
|
"unknown field `d`, expected one of `a`, `b`, `c`, `e`, `f`",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -843,7 +843,7 @@ pub fn is_zero(n: &u8) -> bool {
|
|||||||
*n == 0
|
*n == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn vec_first_element<T, S>(vec: &Vec<T>, serializer: S) -> StdResult<S::Ok, S::Error>
|
fn vec_first_element<T, S>(vec: &[T], serializer: S) -> StdResult<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: Serialize,
|
||||||
S: Serializer,
|
S: Serializer,
|
||||||
|
|||||||
@@ -3,86 +3,186 @@
|
|||||||
#![allow(clippy::derive_partial_eq_without_eq)]
|
#![allow(clippy::derive_partial_eq_without_eq)]
|
||||||
|
|
||||||
use serde_derive::Deserialize;
|
use serde_derive::Deserialize;
|
||||||
use serde_test::{assert_de_tokens, Token};
|
use serde_test::{assert_de_tokens, assert_de_tokens_error, Token};
|
||||||
|
|
||||||
|
mod variant_identifier {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_variant_identifier() {
|
|
||||||
#[derive(Deserialize, Debug, PartialEq)]
|
#[derive(Deserialize, Debug, PartialEq)]
|
||||||
#[serde(variant_identifier)]
|
#[serde(variant_identifier)]
|
||||||
enum V {
|
enum V {
|
||||||
Aaa,
|
Aaa,
|
||||||
|
#[serde(alias = "Ccc", alias = "Ddd")]
|
||||||
Bbb,
|
Bbb,
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_de_tokens(&V::Aaa, &[Token::U8(0)]);
|
#[test]
|
||||||
assert_de_tokens(&V::Aaa, &[Token::U16(0)]);
|
fn variant1() {
|
||||||
assert_de_tokens(&V::Aaa, &[Token::U32(0)]);
|
assert_de_tokens(&V::Aaa, &[Token::U8(0)]);
|
||||||
assert_de_tokens(&V::Aaa, &[Token::U64(0)]);
|
assert_de_tokens(&V::Aaa, &[Token::U16(0)]);
|
||||||
assert_de_tokens(&V::Aaa, &[Token::Str("Aaa")]);
|
assert_de_tokens(&V::Aaa, &[Token::U32(0)]);
|
||||||
assert_de_tokens(&V::Aaa, &[Token::Bytes(b"Aaa")]);
|
assert_de_tokens(&V::Aaa, &[Token::U64(0)]);
|
||||||
|
assert_de_tokens(&V::Aaa, &[Token::Str("Aaa")]);
|
||||||
|
assert_de_tokens(&V::Aaa, &[Token::Bytes(b"Aaa")]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn aliases() {
|
||||||
|
assert_de_tokens(&V::Bbb, &[Token::U8(1)]);
|
||||||
|
assert_de_tokens(&V::Bbb, &[Token::U16(1)]);
|
||||||
|
assert_de_tokens(&V::Bbb, &[Token::U32(1)]);
|
||||||
|
assert_de_tokens(&V::Bbb, &[Token::U64(1)]);
|
||||||
|
|
||||||
|
assert_de_tokens(&V::Bbb, &[Token::Str("Bbb")]);
|
||||||
|
assert_de_tokens(&V::Bbb, &[Token::Bytes(b"Bbb")]);
|
||||||
|
|
||||||
|
assert_de_tokens(&V::Bbb, &[Token::Str("Ccc")]);
|
||||||
|
assert_de_tokens(&V::Bbb, &[Token::Bytes(b"Ccc")]);
|
||||||
|
|
||||||
|
assert_de_tokens(&V::Bbb, &[Token::Str("Ddd")]);
|
||||||
|
assert_de_tokens(&V::Bbb, &[Token::Bytes(b"Ddd")]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn unknown() {
|
||||||
|
assert_de_tokens_error::<V>(
|
||||||
|
&[Token::U8(42)],
|
||||||
|
"invalid value: integer `42`, expected variant index 0 <= i < 2",
|
||||||
|
);
|
||||||
|
assert_de_tokens_error::<V>(
|
||||||
|
&[Token::U16(42)],
|
||||||
|
"invalid value: integer `42`, expected variant index 0 <= i < 2",
|
||||||
|
);
|
||||||
|
assert_de_tokens_error::<V>(
|
||||||
|
&[Token::U32(42)],
|
||||||
|
"invalid value: integer `42`, expected variant index 0 <= i < 2",
|
||||||
|
);
|
||||||
|
assert_de_tokens_error::<V>(
|
||||||
|
&[Token::U64(42)],
|
||||||
|
"invalid value: integer `42`, expected variant index 0 <= i < 2",
|
||||||
|
);
|
||||||
|
assert_de_tokens_error::<V>(
|
||||||
|
&[Token::Str("Unknown")],
|
||||||
|
"unknown variant `Unknown`, expected one of `Aaa`, `Bbb`, `Ccc`, `Ddd`",
|
||||||
|
);
|
||||||
|
assert_de_tokens_error::<V>(
|
||||||
|
&[Token::Bytes(b"Unknown")],
|
||||||
|
"unknown variant `Unknown`, expected one of `Aaa`, `Bbb`, `Ccc`, `Ddd`",
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
mod field_identifier {
|
||||||
fn test_field_identifier() {
|
use super::*;
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, PartialEq)]
|
#[derive(Deserialize, Debug, PartialEq)]
|
||||||
#[serde(field_identifier, rename_all = "snake_case")]
|
#[serde(field_identifier, rename_all = "snake_case")]
|
||||||
enum F {
|
enum F {
|
||||||
Aaa,
|
Aaa,
|
||||||
|
#[serde(alias = "ccc", alias = "ddd")]
|
||||||
Bbb,
|
Bbb,
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_de_tokens(&F::Aaa, &[Token::U8(0)]);
|
#[test]
|
||||||
assert_de_tokens(&F::Aaa, &[Token::U16(0)]);
|
fn field1() {
|
||||||
assert_de_tokens(&F::Aaa, &[Token::U32(0)]);
|
assert_de_tokens(&F::Aaa, &[Token::U8(0)]);
|
||||||
assert_de_tokens(&F::Aaa, &[Token::U64(0)]);
|
assert_de_tokens(&F::Aaa, &[Token::U16(0)]);
|
||||||
assert_de_tokens(&F::Aaa, &[Token::Str("aaa")]);
|
assert_de_tokens(&F::Aaa, &[Token::U32(0)]);
|
||||||
assert_de_tokens(&F::Aaa, &[Token::Bytes(b"aaa")]);
|
assert_de_tokens(&F::Aaa, &[Token::U64(0)]);
|
||||||
}
|
assert_de_tokens(&F::Aaa, &[Token::Str("aaa")]);
|
||||||
|
assert_de_tokens(&F::Aaa, &[Token::Bytes(b"aaa")]);
|
||||||
#[test]
|
|
||||||
fn test_unit_fallthrough() {
|
|
||||||
#[derive(Deserialize, Debug, PartialEq)]
|
|
||||||
#[serde(field_identifier, rename_all = "snake_case")]
|
|
||||||
enum F {
|
|
||||||
Aaa,
|
|
||||||
Bbb,
|
|
||||||
#[serde(other)]
|
|
||||||
Other,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_de_tokens(&F::Other, &[Token::U8(42)]);
|
#[test]
|
||||||
assert_de_tokens(&F::Other, &[Token::U16(42)]);
|
fn aliases() {
|
||||||
assert_de_tokens(&F::Other, &[Token::U32(42)]);
|
assert_de_tokens(&F::Bbb, &[Token::U8(1)]);
|
||||||
assert_de_tokens(&F::Other, &[Token::U64(42)]);
|
assert_de_tokens(&F::Bbb, &[Token::U16(1)]);
|
||||||
assert_de_tokens(&F::Other, &[Token::Str("x")]);
|
assert_de_tokens(&F::Bbb, &[Token::U32(1)]);
|
||||||
}
|
assert_de_tokens(&F::Bbb, &[Token::U64(1)]);
|
||||||
|
|
||||||
#[test]
|
assert_de_tokens(&F::Bbb, &[Token::Str("bbb")]);
|
||||||
fn test_newtype_fallthrough() {
|
assert_de_tokens(&F::Bbb, &[Token::Bytes(b"bbb")]);
|
||||||
#[derive(Deserialize, Debug, PartialEq)]
|
|
||||||
#[serde(field_identifier, rename_all = "snake_case")]
|
assert_de_tokens(&F::Bbb, &[Token::Str("ccc")]);
|
||||||
enum F {
|
assert_de_tokens(&F::Bbb, &[Token::Bytes(b"ccc")]);
|
||||||
Aaa,
|
|
||||||
Bbb,
|
assert_de_tokens(&F::Bbb, &[Token::Str("ddd")]);
|
||||||
Other(String),
|
assert_de_tokens(&F::Bbb, &[Token::Bytes(b"ddd")]);
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_de_tokens(&F::Other("x".to_owned()), &[Token::Str("x")]);
|
#[test]
|
||||||
}
|
fn unknown() {
|
||||||
|
assert_de_tokens_error::<F>(
|
||||||
#[test]
|
&[Token::U8(42)],
|
||||||
fn test_newtype_fallthrough_generic() {
|
"invalid value: integer `42`, expected field index 0 <= i < 2",
|
||||||
#[derive(Deserialize, Debug, PartialEq)]
|
);
|
||||||
#[serde(field_identifier, rename_all = "snake_case")]
|
assert_de_tokens_error::<F>(
|
||||||
enum F<T> {
|
&[Token::U16(42)],
|
||||||
Aaa,
|
"invalid value: integer `42`, expected field index 0 <= i < 2",
|
||||||
Bbb,
|
);
|
||||||
Other(T),
|
assert_de_tokens_error::<F>(
|
||||||
|
&[Token::U32(42)],
|
||||||
|
"invalid value: integer `42`, expected field index 0 <= i < 2",
|
||||||
|
);
|
||||||
|
assert_de_tokens_error::<F>(
|
||||||
|
&[Token::U64(42)],
|
||||||
|
"invalid value: integer `42`, expected field index 0 <= i < 2",
|
||||||
|
);
|
||||||
|
assert_de_tokens_error::<F>(
|
||||||
|
&[Token::Str("unknown")],
|
||||||
|
"unknown field `unknown`, expected one of `aaa`, `bbb`, `ccc`, `ddd`",
|
||||||
|
);
|
||||||
|
assert_de_tokens_error::<F>(
|
||||||
|
&[Token::Bytes(b"unknown")],
|
||||||
|
"unknown field `unknown`, expected one of `aaa`, `bbb`, `ccc`, `ddd`",
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_de_tokens(&F::Other(42u8), &[Token::U8(42)]);
|
#[test]
|
||||||
assert_de_tokens(&F::Other(42u16), &[Token::U16(42)]);
|
fn unit_fallthrough() {
|
||||||
assert_de_tokens(&F::Other(42u32), &[Token::U32(42)]);
|
#[derive(Deserialize, Debug, PartialEq)]
|
||||||
assert_de_tokens(&F::Other(42u64), &[Token::U64(42)]);
|
#[serde(field_identifier, rename_all = "snake_case")]
|
||||||
assert_de_tokens(&F::Other("x".to_owned()), &[Token::Str("x")]);
|
enum F {
|
||||||
|
Aaa,
|
||||||
|
Bbb,
|
||||||
|
#[serde(other)]
|
||||||
|
Other,
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_de_tokens(&F::Other, &[Token::U8(42)]);
|
||||||
|
assert_de_tokens(&F::Other, &[Token::U16(42)]);
|
||||||
|
assert_de_tokens(&F::Other, &[Token::U32(42)]);
|
||||||
|
assert_de_tokens(&F::Other, &[Token::U64(42)]);
|
||||||
|
assert_de_tokens(&F::Other, &[Token::Str("x")]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn newtype_fallthrough() {
|
||||||
|
#[derive(Deserialize, Debug, PartialEq)]
|
||||||
|
#[serde(field_identifier, rename_all = "snake_case")]
|
||||||
|
enum F {
|
||||||
|
Aaa,
|
||||||
|
Bbb,
|
||||||
|
Other(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_de_tokens(&F::Other("x".to_owned()), &[Token::Str("x")]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn newtype_fallthrough_generic() {
|
||||||
|
#[derive(Deserialize, Debug, PartialEq)]
|
||||||
|
#[serde(field_identifier, rename_all = "snake_case")]
|
||||||
|
enum F<T> {
|
||||||
|
Aaa,
|
||||||
|
Bbb,
|
||||||
|
Other(T),
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_de_tokens(&F::Other(42u8), &[Token::U8(42)]);
|
||||||
|
assert_de_tokens(&F::Other(42u16), &[Token::U16(42)]);
|
||||||
|
assert_de_tokens(&F::Other(42u32), &[Token::U32(42)]);
|
||||||
|
assert_de_tokens(&F::Other(42u64), &[Token::U64(42)]);
|
||||||
|
assert_de_tokens(&F::Other("x".to_owned()), &[Token::Str("x")]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -125,6 +125,9 @@ struct Test {
|
|||||||
|
|
||||||
#[serde(with = "EnumConcrete")]
|
#[serde(with = "EnumConcrete")]
|
||||||
enum_concrete: remote::EnumGeneric<u8>,
|
enum_concrete: remote::EnumGeneric<u8>,
|
||||||
|
|
||||||
|
#[serde(with = "ErrorKindDef")]
|
||||||
|
io_error_kind: ErrorKind,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
@@ -197,6 +200,23 @@ enum EnumConcrete {
|
|||||||
Variant(u8),
|
Variant(u8),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum ErrorKind {
|
||||||
|
NotFound,
|
||||||
|
PermissionDenied,
|
||||||
|
#[allow(dead_code)]
|
||||||
|
ConnectionRefused,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
#[serde(remote = "ErrorKind")]
|
||||||
|
#[non_exhaustive]
|
||||||
|
enum ErrorKindDef {
|
||||||
|
NotFound,
|
||||||
|
PermissionDenied,
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
|
||||||
impl From<PrimitivePrivDef> for remote::PrimitivePriv {
|
impl From<PrimitivePrivDef> for remote::PrimitivePriv {
|
||||||
fn from(def: PrimitivePrivDef) -> Self {
|
fn from(def: PrimitivePrivDef) -> Self {
|
||||||
remote::PrimitivePriv::new(def.0)
|
remote::PrimitivePriv::new(def.0)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
error: #[serde(default)] can only be used on structs with named fields
|
error: #[serde(default)] can only be used on structs
|
||||||
--> tests/ui/default-attribute/enum.rs:4:9
|
--> tests/ui/default-attribute/enum.rs:4:9
|
||||||
|
|
|
|
||||||
4 | #[serde(default)]
|
4 | #[serde(default)]
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
error: #[serde(default = "...")] can only be used on structs with named fields
|
error: #[serde(default = "...")] can only be used on structs
|
||||||
--> tests/ui/default-attribute/enum_path.rs:4:9
|
--> tests/ui/default-attribute/enum_path.rs:4:9
|
||||||
|
|
|
|
||||||
4 | #[serde(default = "default_e")]
|
4 | #[serde(default = "default_e")]
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
use serde_derive::Deserialize;
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
#[serde(default)]
|
|
||||||
struct T(u8, u8);
|
|
||||||
|
|
||||||
fn main() {}
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
error: #[serde(default)] can only be used on structs with named fields
|
|
||||||
--> tests/ui/default-attribute/nameless_struct_fields.rs:5:9
|
|
||||||
|
|
|
||||||
5 | struct T(u8, u8);
|
|
||||||
| ^^^^^^^^
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
use serde_derive::Deserialize;
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
#[serde(default = "default_t")]
|
|
||||||
struct T(u8, u8);
|
|
||||||
|
|
||||||
fn main() {}
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
error: #[serde(default = "...")] can only be used on structs with named fields
|
|
||||||
--> tests/ui/default-attribute/nameless_struct_fields_path.rs:4:9
|
|
||||||
|
|
|
||||||
4 | #[serde(default = "default_t")]
|
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^
|
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
use serde_derive::Deserialize;
|
||||||
|
|
||||||
|
// No errors expected.
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct T0(u8, u8);
|
||||||
|
|
||||||
|
// No errors expected:
|
||||||
|
// - If both fields are provided, both get value from data.
|
||||||
|
// - If only one field is provided, the second gets default value.
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct T1(u8, #[serde(default)] u8);
|
||||||
|
|
||||||
|
// ERROR: The first field can get default value only if sequence is empty, but
|
||||||
|
// that mean that all other fields cannot be deserialized without errors.
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct T2(#[serde(default)] u8, u8, u8);
|
||||||
|
|
||||||
|
// No errors expected:
|
||||||
|
// - If both fields are provided, both get value from data.
|
||||||
|
// - If only one field is provided, the second gets default value.
|
||||||
|
// - If no fields are provided, both get default value.
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct T3(#[serde(default)] u8, #[serde(default)] u8);
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
// No errors expected -- missing fields get default values.
|
||||||
|
#[derive(Deserialize, Default)]
|
||||||
|
#[serde(default)]
|
||||||
|
struct T4(u8, u8);
|
||||||
|
|
||||||
|
// No errors expected -- missing fields get default values.
|
||||||
|
#[derive(Deserialize, Default)]
|
||||||
|
#[serde(default)]
|
||||||
|
struct T5(#[serde(default)] u8, u8);
|
||||||
|
|
||||||
|
// No errors expected -- missing fields get default values.
|
||||||
|
#[derive(Deserialize, Default)]
|
||||||
|
#[serde(default)]
|
||||||
|
struct T6(u8, #[serde(default)] u8);
|
||||||
|
|
||||||
|
// No errors expected -- missing fields get default values.
|
||||||
|
#[derive(Deserialize, Default)]
|
||||||
|
#[serde(default)]
|
||||||
|
struct T7(#[serde(default)] u8, #[serde(default)] u8);
|
||||||
|
|
||||||
|
fn main() {}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
error: field must have #[serde(default)] because previous field 0 has #[serde(default)]
|
||||||
|
--> tests/ui/default-attribute/tuple_struct.rs:16:33
|
||||||
|
|
|
||||||
|
16 | struct T2(#[serde(default)] u8, u8, u8);
|
||||||
|
| ^^
|
||||||
|
|
||||||
|
error: field must have #[serde(default)] because previous field 0 has #[serde(default)]
|
||||||
|
--> tests/ui/default-attribute/tuple_struct.rs:16:37
|
||||||
|
|
|
||||||
|
16 | struct T2(#[serde(default)] u8, u8, u8);
|
||||||
|
| ^^
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
use serde_derive::Deserialize;
|
||||||
|
|
||||||
|
fn d<T>() -> T {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
|
||||||
|
// No errors expected:
|
||||||
|
// - If both fields are provided, both get value from data.
|
||||||
|
// - If only one field is provided, the second gets default value.
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct T1(u8, #[serde(default = "d")] u8);
|
||||||
|
|
||||||
|
// ERROR: The first field can get default value only if sequence is empty, but
|
||||||
|
// that mean that all other fields cannot be deserialized without errors.
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct T2(#[serde(default = "d")] u8, u8, u8);
|
||||||
|
|
||||||
|
// No errors expected:
|
||||||
|
// - If both fields are provided, both get value from data.
|
||||||
|
// - If only one field is provided, the second gets default value.
|
||||||
|
// - If no fields are provided, both get default value.
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct T3(#[serde(default = "d")] u8, #[serde(default = "d")] u8);
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
// No errors expected -- missing fields get default values.
|
||||||
|
#[derive(Deserialize, Default)]
|
||||||
|
#[serde(default)]
|
||||||
|
struct T1D(#[serde(default = "d")] u8, u8);
|
||||||
|
|
||||||
|
// No errors expected -- missing fields get default values.
|
||||||
|
#[derive(Deserialize, Default)]
|
||||||
|
#[serde(default)]
|
||||||
|
struct T2D(u8, #[serde(default = "d")] u8);
|
||||||
|
|
||||||
|
// No errors expected -- missing fields get default values.
|
||||||
|
#[derive(Deserialize, Default)]
|
||||||
|
#[serde(default)]
|
||||||
|
struct T3D(#[serde(default = "d")] u8, #[serde(default = "d")] u8);
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
// No errors expected -- missing fields get default values.
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
#[serde(default = "d")]
|
||||||
|
struct T1Path(#[serde(default)] u8, u8);
|
||||||
|
|
||||||
|
// No errors expected -- missing fields get default values.
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
#[serde(default = "d")]
|
||||||
|
struct T2Path(u8, #[serde(default)] u8);
|
||||||
|
|
||||||
|
// No errors expected -- missing fields get default values.
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
#[serde(default = "d")]
|
||||||
|
struct T3Path(#[serde(default)] u8, #[serde(default)] u8);
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
// No errors expected -- missing fields get default values.
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
#[serde(default = "d")]
|
||||||
|
struct T1PathD(#[serde(default = "d")] u8, u8);
|
||||||
|
|
||||||
|
// No errors expected -- missing fields get default values.
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
#[serde(default = "d")]
|
||||||
|
struct T2PathD(u8, #[serde(default = "d")] u8);
|
||||||
|
|
||||||
|
// No errors expected -- missing fields get default values.
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
#[serde(default = "d")]
|
||||||
|
struct T3PathD(#[serde(default = "d")] u8, #[serde(default = "d")] u8);
|
||||||
|
|
||||||
|
fn main() {}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
error: field must have #[serde(default)] because previous field 0 has #[serde(default)]
|
||||||
|
--> tests/ui/default-attribute/tuple_struct_path.rs:16:39
|
||||||
|
|
|
||||||
|
16 | struct T2(#[serde(default = "d")] u8, u8, u8);
|
||||||
|
| ^^
|
||||||
|
|
||||||
|
error: field must have #[serde(default)] because previous field 0 has #[serde(default)]
|
||||||
|
--> tests/ui/default-attribute/tuple_struct_path.rs:16:43
|
||||||
|
|
|
||||||
|
16 | struct T2(#[serde(default = "d")] u8, u8, u8);
|
||||||
|
| ^^
|
||||||
Reference in New Issue
Block a user