mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-22 21:48:02 +00:00
Compare commits
347 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0676673ca5 | |||
| 7a4335d664 | |||
| 31a0e73489 | |||
| 74fe70855f | |||
| e74925bc43 | |||
| 56be1c203e | |||
| 2f9bf4d3eb | |||
| ad94aed753 | |||
| 30db83fc44 | |||
| b0f7b00e1f | |||
| 7255e192d8 | |||
| 2102e1aa42 | |||
| 85d5c1fd38 | |||
| b789286bc3 | |||
| a6a8a334f7 | |||
| 78a11a27b6 | |||
| d2d7bad04a | |||
| b978854258 | |||
| 0fb672a1ef | |||
| dd9913675d | |||
| 11677ad926 | |||
| 25a53f10db | |||
| 26e2ef001c | |||
| 30f79b3b2e | |||
| 89f84c2915 | |||
| 6882285be0 | |||
| 3260bc5896 | |||
| 6140b6f527 | |||
| 4cabc9f293 | |||
| aa7c6345a4 | |||
| 27414c90a8 | |||
| 50e2f4b213 | |||
| 22be673beb | |||
| 166c89fabf | |||
| 6e0b13eedb | |||
| 7e8f978ca9 | |||
| 6c0e838a7c | |||
| d3da41927a | |||
| 425a4b7a74 | |||
| 63c65ef742 | |||
| e838b0bd81 | |||
| 041e99c78a | |||
| 07dcc4f7fe | |||
| b88052d875 | |||
| a28292764c | |||
| 2027088741 | |||
| e2d8589976 | |||
| c8a9f99d14 | |||
| 645d04012d | |||
| 100ddada2f | |||
| 2ef1cd4b35 | |||
| be9c3fd69d | |||
| ef522e1d16 | |||
| 1ddb6c2fdb | |||
| eb3f2329af | |||
| 9e8f14816b | |||
| 03da66c805 | |||
| f75426f47e | |||
| 662fc3861c | |||
| 28c10020b9 | |||
| 89c8d85de9 | |||
| 6502838f27 | |||
| c93a0f335a | |||
| 8264e002a7 | |||
| 117ef22142 | |||
| 3fb5e71c33 | |||
| 296db177e2 | |||
| e4a4389177 | |||
| 7aa0453c3b | |||
| 09b78b24e9 | |||
| a622b8a74a | |||
| 399ef081ec | |||
| 3686277e14 | |||
| 807bd20a64 | |||
| ed9a140348 | |||
| 2de7c2bea2 | |||
| e6a4a3772e | |||
| 0fca04e1a6 | |||
| 92bfc8d3af | |||
| fa0312ac45 | |||
| 1920b694aa | |||
| 3bfd41d624 | |||
| 290449f19b | |||
| 3a1f387e69 | |||
| 541603ac94 | |||
| 0666fbfa20 | |||
| ea071ae1d4 | |||
| 992a01bad2 | |||
| d640b5624f | |||
| 48479e4bae | |||
| dfaf48bc09 | |||
| dcbc3e0162 | |||
| 0289d31724 | |||
| 015e39776f | |||
| 6a9a21f178 | |||
| 81ac54b20d | |||
| 6b4e75520a | |||
| b053b4f492 | |||
| 4cf1fec575 | |||
| ee7d77defa | |||
| d0dfc4577e | |||
| bbbd1d24c9 | |||
| fb3a9e0d7c | |||
| 5ffebeb6ef | |||
| 75db73066b | |||
| 2796833c82 | |||
| 95730dc7f7 | |||
| 795261919f | |||
| 3783a30ae7 | |||
| b61ec84886 | |||
| 780a461d92 | |||
| c0ba323166 | |||
| 20a48c9580 | |||
| 09938803af | |||
| 6d0b43a220 | |||
| c604bdbfe4 | |||
| 9fef892f6d | |||
| b1c7db47b8 | |||
| e76e87a430 | |||
| 8a4dfa7231 | |||
| 107018c628 | |||
| a398237930 | |||
| b63c65d7f5 | |||
| f60324e883 | |||
| 361c23a09a | |||
| 43b23c7ea0 | |||
| 6081497506 | |||
| 48e5753e76 | |||
| bbba632ab3 | |||
| e77db40b8d | |||
| 2c1f62d4b4 | |||
| 1aebdc2760 | |||
| 705e58be8c | |||
| 7c2c12aa43 | |||
| a0f850f15b | |||
| fccb9499bc | |||
| a139ab2572 | |||
| 1d910a484c | |||
| ee9166ec97 | |||
| b5a9eff32e | |||
| 9441a29663 | |||
| ab6588ef74 | |||
| 1d11f03449 | |||
| e11d01fe1d | |||
| a901f50850 | |||
| c399e9c368 | |||
| 25381be0c9 | |||
| ef2a7c753f | |||
| 99f165b45a | |||
| 2fb5560746 | |||
| bd653ab30c | |||
| b5d68aedaa | |||
| 624879c4c6 | |||
| bd9e9abf35 | |||
| 3e4a23cbd0 | |||
| 6326ceec3f | |||
| 8f4d37c7ec | |||
| 1b8290b318 | |||
| 48193fbccd | |||
| 51799dd654 | |||
| 732ac49321 | |||
| ac8ea72d88 | |||
| f583401284 | |||
| 2d88228b7d | |||
| 0c6a2bbf79 | |||
| a80d830f27 | |||
| 5f3fd9994e | |||
| d6de911855 | |||
| 04af32230e | |||
| 4cb8d079f8 | |||
| 6ab55a1e52 | |||
| acfd19cb46 | |||
| e3058105f0 | |||
| dc200a6450 | |||
| 2c0999a0b9 | |||
| dd460f82a1 | |||
| c3d637f397 | |||
| 479a00a215 | |||
| c42e7c8012 | |||
| 5b8e0657d4 | |||
| 9fc0d13e2c | |||
| bc22641359 | |||
| 05098105a8 | |||
| 5b23634dc6 | |||
| 32f0d00ff9 | |||
| 9d87851f0c | |||
| c0296ee11b | |||
| 54671259aa | |||
| 994f7c7924 | |||
| 7a8e4977e2 | |||
| fb7b6ea7ea | |||
| 063dd5b93f | |||
| a38aa31ade | |||
| f42b2581da | |||
| 2ba406726f | |||
| 7e9826e17b | |||
| f4dcc5c918 | |||
| 8b1887c440 | |||
| bbfb1d3504 | |||
| e106feb5ec | |||
| 696f6f56db | |||
| b7b636a23f | |||
| 183b91775e | |||
| 0e70f59021 | |||
| 4d9b76db73 | |||
| 9af132f594 | |||
| 6c063569c0 | |||
| 7e9b98401d | |||
| f301e09e02 | |||
| b80e722f81 | |||
| 1714c262c4 | |||
| a42cdafdcd | |||
| eb4c3f16f7 | |||
| ce86f351d6 | |||
| 0b90f6c96a | |||
| 2198463218 | |||
| be57a5e00a | |||
| b1b09eba60 | |||
| eb1e8c140d | |||
| 43da87939d | |||
| 06d99a13a6 | |||
| 49a911d7de | |||
| 27d6628785 | |||
| e4e2956e79 | |||
| ea2f7b81d9 | |||
| f0dfdb5247 | |||
| 6a5da85fcd | |||
| 0750eee4ff | |||
| ef551a517c | |||
| 1c5ea24f76 | |||
| 88d73e5250 | |||
| 1ff2a972c6 | |||
| bb72fe2726 | |||
| e50b14afee | |||
| cbd1cbef07 | |||
| 01da3f79c9 | |||
| f5e0fbcb14 | |||
| 38c130a303 | |||
| c7393614ff | |||
| a13c6382b6 | |||
| a803ec1c1f | |||
| f7636428ed | |||
| f85c4f2fa9 | |||
| a9a9903107 | |||
| bd4a0981ba | |||
| 35e5cf3e15 | |||
| 07fc9f689e | |||
| 14b0e18c57 | |||
| dd27ec8703 | |||
| db3f00c3b3 | |||
| adcb11ca18 | |||
| b7be637e8c | |||
| 30c4aa2cf4 | |||
| a649190a4d | |||
| d81f0ef652 | |||
| 1a3a49ce7c | |||
| 6b948111ca | |||
| 6adfdc56e5 | |||
| 61531ddd9e | |||
| ccf9c6fc07 | |||
| b25d0ea7f9 | |||
| 4f4557fd05 | |||
| bf400d6799 | |||
| 4d2e36d19b | |||
| df6310e5f5 | |||
| 938ab5ddec | |||
| ef5a0de384 | |||
| 5d186c77a6 | |||
| 44bf3633af | |||
| f261184416 | |||
| df40f80fcf | |||
| e7060ba83d | |||
| d98f0eea3d | |||
| 4f157a8b81 | |||
| d493649f52 | |||
| 0e947e6c3b | |||
| 9249dab54c | |||
| 7440e56c53 | |||
| 0d79306285 | |||
| 37faaf295e | |||
| 650358fa00 | |||
| 6159ead404 | |||
| 692ac99c69 | |||
| 86161ce15f | |||
| 5361c790bb | |||
| 126730edc8 | |||
| 3aec2a96a8 | |||
| 227d039b1e | |||
| 0353354d61 | |||
| 34ae0422f4 | |||
| cc128feb4c | |||
| 7766103174 | |||
| 30f7c7110d | |||
| 50354c2d0b | |||
| c4f67e679f | |||
| 0daafe423f | |||
| 37021910c9 | |||
| 7328b34810 | |||
| fabbd2b097 | |||
| 6814f978d7 | |||
| 4ea403c54a | |||
| f4f6b5af3a | |||
| 2062a3c16d | |||
| 9a53bd9125 | |||
| 4873b48b02 | |||
| e19844c659 | |||
| 93bb9e147c | |||
| ab230e6e44 | |||
| 51ea34b217 | |||
| 1050f6b808 | |||
| 15ec95a98d | |||
| 072145e0e9 | |||
| 92957f17f2 | |||
| 667db558b6 | |||
| f41509261e | |||
| 6d009711a2 | |||
| 354b48fd40 | |||
| 3fd8e52f0c | |||
| 142dce0d3d | |||
| 6aed101630 | |||
| e2ccfd9ea7 | |||
| a07d794f74 | |||
| 90d28fc314 | |||
| 55cf0ac51a | |||
| 07696c1674 | |||
| f803b290f3 | |||
| d96e181150 | |||
| 3ffb86fc70 | |||
| 649a72a587 | |||
| b2676348eb | |||
| 8c036ee5a3 | |||
| d99009f3c6 | |||
| be3c37eb8b | |||
| f0346ae054 | |||
| fa6ce42056 | |||
| a9320db6f9 | |||
| d208762c81 | |||
| 5386897d24 | |||
| 68eb59df0c | |||
| a7f4551669 | |||
| 983347484e | |||
| f52d134c14 | |||
| 6660676b0d | |||
| 1d42d3571a | |||
| ebd06eebdb | |||
| f1985823a3 | |||
| 60e4092b8e |
@@ -1 +0,0 @@
|
||||
test_suite/tests/expand/*.expanded.rs linguist-generated
|
||||
+35
-44
@@ -3,8 +3,12 @@ name: CI
|
||||
on:
|
||||
push:
|
||||
pull_request:
|
||||
workflow_dispatch:
|
||||
schedule: [cron: "40 1 * * *"]
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
env:
|
||||
RUSTFLAGS: -Dwarnings
|
||||
|
||||
@@ -12,18 +16,16 @@ jobs:
|
||||
test:
|
||||
name: Test suite
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 45
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: dtolnay/rust-toolchain@nightly
|
||||
- run: cd test_suite && cargo test --features unstable
|
||||
- uses: dtolnay/install@cargo-expand
|
||||
if: github.event_name != 'pull_request'
|
||||
- run: cd test_suite && cargo test --features expandtest --test expandtest -- --include-ignored
|
||||
if: github.event_name != 'pull_request'
|
||||
|
||||
windows:
|
||||
name: Test suite (windows)
|
||||
runs-on: windows-latest
|
||||
timeout-minutes: 45
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: dtolnay/rust-toolchain@nightly
|
||||
@@ -36,6 +38,7 @@ jobs:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
rust: [stable, beta]
|
||||
timeout-minutes: 45
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: dtolnay/rust-toolchain@master
|
||||
@@ -43,8 +46,6 @@ jobs:
|
||||
toolchain: ${{matrix.rust}}
|
||||
- run: cd serde && cargo build --features rc
|
||||
- run: cd serde && cargo build --no-default-features
|
||||
- run: cd serde_test && cargo build
|
||||
- run: cd serde_test && cargo test --features serde/derive,serde/rc
|
||||
|
||||
nightly:
|
||||
name: Rust nightly ${{matrix.os == 'windows' && '(windows)' || ''}}
|
||||
@@ -53,6 +54,7 @@ jobs:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
os: [ubuntu, windows]
|
||||
timeout-minutes: 45
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: dtolnay/rust-toolchain@nightly
|
||||
@@ -68,23 +70,6 @@ jobs:
|
||||
RUSTFLAGS: --cfg exhaustive ${{env.RUSTFLAGS}}
|
||||
if: matrix.os != 'windows'
|
||||
|
||||
msrv:
|
||||
name: Rust 1.13.0
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: dtolnay/rust-toolchain@1.13.0
|
||||
- name: Get timestamp for cache
|
||||
id: date
|
||||
run: echo ::set-output name=yearmo::$(date +%Y%m)
|
||||
- uses: actions/cache@v1
|
||||
with:
|
||||
path: ~/.cargo/registry/index
|
||||
key: cargo-registry-index-${{steps.date.outputs.yearmo}}
|
||||
- run: cd serde && cargo build --features rc
|
||||
- run: cd serde && cargo build --no-default-features
|
||||
- run: cd serde_test && cargo build
|
||||
|
||||
build:
|
||||
name: Rust ${{matrix.rust}}
|
||||
runs-on: ubuntu-latest
|
||||
@@ -92,11 +77,13 @@ jobs:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
rust: [1.19.0, 1.20.0, 1.21.0, 1.25.0, 1.26.0, 1.34.0]
|
||||
timeout-minutes: 45
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: dtolnay/rust-toolchain@master
|
||||
with:
|
||||
toolchain: ${{matrix.rust}}
|
||||
- run: cd serde && cargo build --features rc
|
||||
- run: cd serde && cargo build --no-default-features
|
||||
- run: cd serde && cargo build
|
||||
|
||||
@@ -107,6 +94,7 @@ jobs:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
rust: [1.27.0, 1.28.0]
|
||||
timeout-minutes: 45
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: dtolnay/rust-toolchain@master
|
||||
@@ -118,65 +106,67 @@ jobs:
|
||||
- run: cd serde && cargo build
|
||||
|
||||
derive:
|
||||
name: Rust 1.31.0
|
||||
name: Rust 1.56.0
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 45
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: dtolnay/rust-toolchain@1.31.0
|
||||
- uses: dtolnay/rust-toolchain@1.56.0
|
||||
- run: cd serde && cargo check --no-default-features
|
||||
- run: cd serde && cargo check
|
||||
- run: cd serde_derive && cargo check
|
||||
- run: cd precompiled/serde_derive && cargo check
|
||||
|
||||
alloc:
|
||||
name: Rust 1.36.0
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 45
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: dtolnay/rust-toolchain@1.36.0
|
||||
- run: cd serde && cargo build --no-default-features --features alloc
|
||||
|
||||
emscripten:
|
||||
name: Emscripten
|
||||
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:
|
||||
name: Minimal versions
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 45
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: dtolnay/rust-toolchain@nightly
|
||||
- uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: 9
|
||||
- name: Install cargo-web
|
||||
run: |
|
||||
CARGO_WEB_RELEASE=$(curl -L -s -H Accept:application/json https://github.com/koute/cargo-web/releases/latest)
|
||||
CARGO_WEB_VERSION=$(echo "${CARGO_WEB_RELEASE}" | jq -r .tag_name)
|
||||
CARGO_WEB_URL="https://github.com/koute/cargo-web/releases/download/${CARGO_WEB_VERSION}/cargo-web-x86_64-unknown-linux-gnu.gz"
|
||||
mkdir -p ~/.cargo/bin
|
||||
curl -L "${CARGO_WEB_URL}" | gzip -d > ~/.cargo/bin/cargo-web
|
||||
chmod +x ~/.cargo/bin/cargo-web
|
||||
- run: cd test_suite && cargo web test --target=asmjs-unknown-emscripten --nodejs
|
||||
continue-on-error: true
|
||||
- run: cd test_suite && cargo web test --target=wasm32-unknown-emscripten --nodejs
|
||||
continue-on-error: true
|
||||
- run: cargo generate-lockfile -Z minimal-versions
|
||||
- run: cargo check --locked --workspace
|
||||
|
||||
clippy:
|
||||
name: Clippy
|
||||
runs-on: ubuntu-latest
|
||||
if: github.event_name != 'pull_request'
|
||||
timeout-minutes: 45
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: dtolnay/rust-toolchain@clippy
|
||||
- run: cd serde && cargo clippy --features rc,unstable -- -Dclippy::all -Dclippy::pedantic
|
||||
- run: cd serde_derive && cargo clippy -- -Dclippy::all -Dclippy::pedantic
|
||||
- run: cd serde_derive_internals && cargo clippy -- -Dclippy::all -Dclippy::pedantic
|
||||
- run: cd serde_test && cargo clippy -- -Dclippy::all -Dclippy::pedantic
|
||||
- run: cd test_suite && cargo clippy --tests --features unstable -- -Dclippy::all -Dclippy::pedantic
|
||||
- run: cd test_suite/no_std && cargo clippy -- -Dclippy::all -Dclippy::pedantic
|
||||
|
||||
miri:
|
||||
name: Miri
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 45
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: dtolnay/rust-toolchain@miri
|
||||
- run: cargo miri setup
|
||||
- run: cd serde && cargo miri test --features derive,rc,unstable
|
||||
env:
|
||||
MIRIFLAGS: -Zmiri-strict-provenance
|
||||
@@ -188,7 +178,8 @@ jobs:
|
||||
name: Outdated
|
||||
runs-on: ubuntu-latest
|
||||
if: github.event_name != 'pull_request'
|
||||
timeout-minutes: 45
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: dtolnay/install@cargo-outdated
|
||||
- run: cargo outdated --workspace --exclude macrotest,prettyplease --exit-code 1
|
||||
- run: cargo outdated --workspace --exit-code 1
|
||||
|
||||
+3
-1
@@ -3,6 +3,8 @@ members = [
|
||||
"serde",
|
||||
"serde_derive",
|
||||
"serde_derive_internals",
|
||||
"serde_test",
|
||||
"test_suite",
|
||||
]
|
||||
|
||||
[patch.crates-io]
|
||||
serde = { path = "serde" }
|
||||
|
||||
@@ -174,28 +174,3 @@ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
# Serde   [![Build Status]][actions] [![Latest Version]][crates.io] [![serde: rustc 1.13+]][Rust 1.13] [![serde_derive: rustc 1.31+]][Rust 1.31]
|
||||
# Serde   [![Build Status]][actions] [![Latest Version]][crates.io] [![serde: rustc 1.19+]][Rust 1.19] [![serde_derive: rustc 1.56+]][Rust 1.56]
|
||||
|
||||
[Build Status]: https://img.shields.io/github/workflow/status/serde-rs/serde/CI/master
|
||||
[Build Status]: https://img.shields.io/github/actions/workflow/status/serde-rs/serde/ci.yml?branch=master
|
||||
[actions]: https://github.com/serde-rs/serde/actions?query=branch%3Amaster
|
||||
[Latest Version]: https://img.shields.io/crates/v/serde.svg
|
||||
[crates.io]: https://crates.io/crates/serde
|
||||
[serde: rustc 1.13+]: https://img.shields.io/badge/serde-rustc_1.13+-lightgray.svg
|
||||
[serde_derive: rustc 1.31+]: https://img.shields.io/badge/serde_derive-rustc_1.31+-lightgray.svg
|
||||
[Rust 1.13]: https://blog.rust-lang.org/2016/11/10/Rust-1.13.html
|
||||
[Rust 1.31]: https://blog.rust-lang.org/2018/12/06/Rust-1.31-and-rust-2018.html
|
||||
[serde: rustc 1.19+]: https://img.shields.io/badge/serde-rustc_1.19+-lightgray.svg
|
||||
[serde_derive: rustc 1.56+]: https://img.shields.io/badge/serde_derive-rustc_1.56+-lightgray.svg
|
||||
[Rust 1.19]: https://blog.rust-lang.org/2017/07/20/Rust-1.19.html
|
||||
[Rust 1.56]: https://blog.rust-lang.org/2021/10/21/Rust-1.56.0.html
|
||||
|
||||
**Serde is a framework for *ser*ializing and *de*serializing Rust data structures efficiently and generically.**
|
||||
|
||||
@@ -19,7 +19,7 @@ You may be looking for:
|
||||
- [Data formats supported by Serde](https://serde.rs/#data-formats)
|
||||
- [Setting up `#[derive(Serialize, Deserialize)]`](https://serde.rs/derive.html)
|
||||
- [Examples](https://serde.rs/examples.html)
|
||||
- [API documentation](https://docs.serde.rs/serde/)
|
||||
- [API documentation](https://docs.rs/serde)
|
||||
- [Release notes](https://github.com/serde-rs/serde/releases)
|
||||
|
||||
## Serde in action
|
||||
@@ -48,7 +48,7 @@ serde_json = "1.0"
|
||||
<p></p>
|
||||
|
||||
```rust
|
||||
use serde::{Serialize, Deserialize};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct Point {
|
||||
|
||||
+2
-2
@@ -10,13 +10,13 @@ You may be looking for:
|
||||
- [Data formats supported by Serde](https://serde.rs/#data-formats)
|
||||
- [Setting up `#[derive(Serialize, Deserialize)]`](https://serde.rs/derive.html)
|
||||
- [Examples](https://serde.rs/examples.html)
|
||||
- [API documentation](https://docs.serde.rs/serde/)
|
||||
- [API documentation](https://docs.rs/serde)
|
||||
- [Release notes](https://github.com/serde-rs/serde/releases)
|
||||
|
||||
## Serde in action
|
||||
|
||||
```rust
|
||||
use serde::{Serialize, Deserialize};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct Point {
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
/serde_derive/serde_derive-x86_64-unknown-linux-gnu
|
||||
@@ -0,0 +1,14 @@
|
||||
[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
|
||||
@@ -0,0 +1,17 @@
|
||||
[package]
|
||||
name = "serde_derive"
|
||||
version = "1.0.177"
|
||||
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.25", default-features = false, features = ["clone-impls", "derive", "full", "parsing", "printing"] }
|
||||
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
println!("cargo:rustc-cfg=precompiled");
|
||||
println!("cargo:rustc-cfg=feature=\"deserialize_in_place\"");
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
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();
|
||||
}
|
||||
Symlink
+1
@@ -0,0 +1 @@
|
||||
../../serde_derive/src
|
||||
Executable
+21
@@ -0,0 +1,21 @@
|
||||
#!/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
|
||||
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.66"
|
||||
edition = "2021"
|
||||
publish = false
|
||||
|
||||
[dependencies]
|
||||
proc-macro2 = { package = "proc-macro2-fallback", version = "1" }
|
||||
@@ -0,0 +1,815 @@
|
||||
#[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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
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()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
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;
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
[package]
|
||||
name = "serde_derive"
|
||||
version = "1.0.177"
|
||||
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.25"
|
||||
|
||||
[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 +1 @@
|
||||
../../LICENSE-APACHE
|
||||
+1
@@ -0,0 +1 @@
|
||||
../../LICENSE-MIT
|
||||
+1
@@ -0,0 +1 @@
|
||||
../../README.md
|
||||
+1
@@ -0,0 +1 @@
|
||||
../../crates-io.md
|
||||
+1
@@ -0,0 +1 @@
|
||||
../../../serde_derive/src/bound.rs
|
||||
+1
@@ -0,0 +1 @@
|
||||
../../proc-macro2/src/watt/buffer.rs
|
||||
@@ -0,0 +1 @@
|
||||
../../proc-macro2/src/watt/bytecode.rs
|
||||
+1
@@ -0,0 +1 @@
|
||||
../../../serde_derive/src/de.rs
|
||||
+1
@@ -0,0 +1 @@
|
||||
../../../serde_derive/src/dummy.rs
|
||||
@@ -0,0 +1 @@
|
||||
../../../serde_derive/src/fragment.rs
|
||||
+1
@@ -0,0 +1 @@
|
||||
../../../serde_derive/src/internals/
|
||||
@@ -0,0 +1,22 @@
|
||||
//! 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.177")]
|
||||
|
||||
#[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");
|
||||
@@ -0,0 +1,36 @@
|
||||
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;
|
||||
mod try;
|
||||
|
||||
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()
|
||||
}
|
||||
@@ -0,0 +1,234 @@
|
||||
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 +1 @@
|
||||
../../../serde_derive/src/pretend.rs
|
||||
+1
@@ -0,0 +1 @@
|
||||
../../../serde_derive/src/ser.rs
|
||||
+1
@@ -0,0 +1 @@
|
||||
../../../serde_derive/src/this.rs
|
||||
+1
@@ -0,0 +1 @@
|
||||
../../../serde_derive/src/try.rs
|
||||
@@ -1 +0,0 @@
|
||||
msrv = "1.13.0"
|
||||
+11
-7
@@ -1,30 +1,34 @@
|
||||
[package]
|
||||
name = "serde"
|
||||
version = "1.0.143" # remember to update html_root_url and serde_derive dependency
|
||||
version = "1.0.177" # remember to update html_root_url and serde_derive dependency
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
||||
build = "build.rs"
|
||||
categories = ["encoding", "no-std"]
|
||||
categories = ["encoding", "no-std", "no-std::no-alloc"]
|
||||
description = "A generic serialization/deserialization framework"
|
||||
documentation = "https://docs.serde.rs/serde/"
|
||||
documentation = "https://docs.rs/serde"
|
||||
homepage = "https://serde.rs"
|
||||
include = ["build.rs", "src/**/*.rs", "crates-io.md", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
|
||||
keywords = ["serde", "serialization", "no_std"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
readme = "crates-io.md"
|
||||
repository = "https://github.com/serde-rs/serde"
|
||||
rust-version = "1.13"
|
||||
rust-version = "1.19"
|
||||
|
||||
[dependencies]
|
||||
serde_derive = { version = "=1.0.143", optional = true, path = "../serde_derive" }
|
||||
serde_derive = { version = "=1.0.177", optional = true, path = "../serde_derive" }
|
||||
|
||||
[dev-dependencies]
|
||||
serde_derive = { version = "1.0", path = "../serde_derive" }
|
||||
serde_derive = { version = "1", path = "../serde_derive" }
|
||||
|
||||
[lib]
|
||||
doc-scrape-examples = false
|
||||
|
||||
[package.metadata.playground]
|
||||
features = ["derive", "rc"]
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
features = ["derive"]
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
||||
rustdoc-args = ["--generate-link-to-definition"]
|
||||
|
||||
|
||||
### FEATURES #################################################################
|
||||
|
||||
+32
-20
@@ -6,6 +6,8 @@ use std::str::{self, FromStr};
|
||||
// opening a GitHub issue if your build environment requires some way to enable
|
||||
// these cfgs other than by executing our build script.
|
||||
fn main() {
|
||||
println!("cargo:rerun-if-changed=build.rs");
|
||||
|
||||
let minor = match rustc_minor_version() {
|
||||
Some(minor) => minor,
|
||||
None => return,
|
||||
@@ -76,11 +78,6 @@ fn main() {
|
||||
println!("cargo:rustc-cfg=no_num_nonzero");
|
||||
}
|
||||
|
||||
// Current minimum supported version of serde_derive crate is Rust 1.31.
|
||||
if minor < 31 {
|
||||
println!("cargo:rustc-cfg=no_serde_derive");
|
||||
}
|
||||
|
||||
// TryFrom, Atomic types, non-zero signed integers, and SystemTime::checked_add
|
||||
// stabilized in Rust 1.34:
|
||||
// https://blog.rust-lang.org/2019/04/11/Rust-1.34.0.html#tryfrom-and-tryinto
|
||||
@@ -89,24 +86,39 @@ fn main() {
|
||||
println!("cargo:rustc-cfg=no_core_try_from");
|
||||
println!("cargo:rustc-cfg=no_num_nonzero_signed");
|
||||
println!("cargo:rustc-cfg=no_systemtime_checked_add");
|
||||
println!("cargo:rustc-cfg=no_relaxed_trait_bounds");
|
||||
}
|
||||
|
||||
// Whitelist of archs that support std::sync::atomic module. Ideally we
|
||||
// would use #[cfg(target_has_atomic = "...")] but it is not stable yet.
|
||||
// Instead this is based on rustc's compiler/rustc_target/src/spec/*.rs.
|
||||
let has_atomic64 = target.starts_with("x86_64")
|
||||
|| target.starts_with("i686")
|
||||
|| target.starts_with("aarch64")
|
||||
|| target.starts_with("powerpc64")
|
||||
|| target.starts_with("sparc64")
|
||||
|| target.starts_with("mips64el")
|
||||
|| target.starts_with("riscv64");
|
||||
let has_atomic32 = has_atomic64 || emscripten;
|
||||
if minor < 34 || !has_atomic64 {
|
||||
println!("cargo:rustc-cfg=no_std_atomic64");
|
||||
// Current minimum supported version of serde_derive crate is Rust 1.56.
|
||||
if minor < 56 {
|
||||
println!("cargo:rustc-cfg=no_serde_derive");
|
||||
}
|
||||
if minor < 34 || !has_atomic32 {
|
||||
println!("cargo:rustc-cfg=no_std_atomic");
|
||||
|
||||
// Support for #[cfg(target_has_atomic = "...")] stabilized in Rust 1.60.
|
||||
if minor < 60 {
|
||||
println!("cargo:rustc-cfg=no_target_has_atomic");
|
||||
// Allowlist of archs that support std::sync::atomic module. This is
|
||||
// based on rustc's compiler/rustc_target/src/spec/*.rs.
|
||||
let has_atomic64 = target.starts_with("x86_64")
|
||||
|| target.starts_with("i686")
|
||||
|| target.starts_with("aarch64")
|
||||
|| target.starts_with("powerpc64")
|
||||
|| target.starts_with("sparc64")
|
||||
|| target.starts_with("mips64el")
|
||||
|| target.starts_with("riscv64");
|
||||
let has_atomic32 = has_atomic64 || emscripten;
|
||||
if minor < 34 || !has_atomic64 {
|
||||
println!("cargo:rustc-cfg=no_std_atomic64");
|
||||
}
|
||||
if minor < 34 || !has_atomic32 {
|
||||
println!("cargo:rustc-cfg=no_std_atomic");
|
||||
}
|
||||
}
|
||||
|
||||
// Support for core::ffi::CStr and alloc::ffi::CString stabilized in Rust 1.64.
|
||||
// https://blog.rust-lang.org/2022/09/22/Rust-1.64.0.html#c-compatible-ffi-types-in-core-and-alloc
|
||||
if minor < 64 {
|
||||
println!("cargo:rustc-cfg=no_core_cstr");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use lib::fmt::{self, Write};
|
||||
use lib::str;
|
||||
|
||||
pub struct Buf<'a> {
|
||||
pub(super) struct Buf<'a> {
|
||||
bytes: &'a mut [u8],
|
||||
offset: usize,
|
||||
}
|
||||
|
||||
@@ -10,13 +10,12 @@ use de::{
|
||||
/// any type, except that it does not store any information about the data that
|
||||
/// gets deserialized.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// use std::fmt;
|
||||
/// use std::marker::PhantomData;
|
||||
///
|
||||
/// ```edition2021
|
||||
/// use serde::de::{
|
||||
/// self, Deserialize, DeserializeSeed, Deserializer, IgnoredAny, SeqAccess, Visitor,
|
||||
/// };
|
||||
/// use std::fmt;
|
||||
/// use std::marker::PhantomData;
|
||||
///
|
||||
/// /// A seed that can be used to deserialize only the `n`th element of a sequence
|
||||
/// /// while efficiently discarding elements of any type before or after index `n`.
|
||||
@@ -108,7 +107,7 @@ use de::{
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
#[derive(Copy, Clone, Debug, Default)]
|
||||
#[derive(Copy, Clone, Debug, Default, PartialEq)]
|
||||
pub struct IgnoredAny;
|
||||
|
||||
impl<'de> Visitor<'de> for IgnoredAny {
|
||||
@@ -228,7 +227,7 @@ impl<'de> Visitor<'de> for IgnoredAny {
|
||||
where
|
||||
A: EnumAccess<'de>,
|
||||
{
|
||||
data.variant::<IgnoredAny>()?.1.newtype_variant()
|
||||
try!(data.variant::<IgnoredAny>()).1.newtype_variant()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+339
-54
@@ -666,10 +666,10 @@ impl<'de: 'a, 'a> Deserialize<'de> for &'a [u8] {
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))]
|
||||
struct CStringVisitor;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))]
|
||||
impl<'de> Visitor<'de> for CStringVisitor {
|
||||
type Value = CString;
|
||||
|
||||
@@ -681,8 +681,8 @@ impl<'de> Visitor<'de> for CStringVisitor {
|
||||
where
|
||||
A: SeqAccess<'de>,
|
||||
{
|
||||
let len = size_hint::cautious(seq.size_hint());
|
||||
let mut values = Vec::with_capacity(len);
|
||||
let capacity = size_hint::cautious::<u8>(seq.size_hint());
|
||||
let mut values = Vec::<u8>::with_capacity(capacity);
|
||||
|
||||
while let Some(value) = try!(seq.next_element()) {
|
||||
values.push(value);
|
||||
@@ -720,7 +720,7 @@ impl<'de> Visitor<'de> for CStringVisitor {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))]
|
||||
impl<'de> Deserialize<'de> for CString {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
@@ -733,7 +733,7 @@ impl<'de> Deserialize<'de> for CString {
|
||||
macro_rules! forwarded_impl {
|
||||
(
|
||||
$(#[doc = $doc:tt])*
|
||||
( $($id: ident),* ), $ty: ty, $func: expr
|
||||
($($id:ident),*), $ty:ty, $func:expr
|
||||
) => {
|
||||
$(#[doc = $doc])*
|
||||
impl<'de $(, $id : Deserialize<'de>,)*> Deserialize<'de> for $ty {
|
||||
@@ -747,7 +747,10 @@ macro_rules! forwarded_impl {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "std", not(no_de_boxed_c_str)))]
|
||||
#[cfg(all(
|
||||
any(feature = "std", all(not(no_core_cstr), feature = "alloc")),
|
||||
not(no_de_boxed_c_str)
|
||||
))]
|
||||
forwarded_impl!((), Box<CStr>, CString::into_boxed_c_str);
|
||||
|
||||
#[cfg(not(no_core_reverse))]
|
||||
@@ -860,7 +863,7 @@ impl<'de, T: ?Sized> Deserialize<'de> for PhantomData<T> {
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
macro_rules! seq_impl {
|
||||
(
|
||||
$ty:ident < T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)*)* >,
|
||||
$ty:ident <T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)*)*>,
|
||||
$access:ident,
|
||||
$clear:expr,
|
||||
$with_capacity:expr,
|
||||
@@ -933,7 +936,7 @@ macro_rules! seq_impl {
|
||||
A: SeqAccess<'de>,
|
||||
{
|
||||
$clear(&mut self.0);
|
||||
$reserve(&mut self.0, size_hint::cautious($access.size_hint()));
|
||||
$reserve(&mut self.0, size_hint::cautious::<T>($access.size_hint()));
|
||||
|
||||
// FIXME: try to overwrite old values here? (Vec, VecDeque, LinkedList)
|
||||
while let Some(value) = try!($access.next_element()) {
|
||||
@@ -959,7 +962,7 @@ seq_impl!(
|
||||
BinaryHeap<T: Ord>,
|
||||
seq,
|
||||
BinaryHeap::clear,
|
||||
BinaryHeap::with_capacity(size_hint::cautious(seq.size_hint())),
|
||||
BinaryHeap::with_capacity(size_hint::cautious::<T>(seq.size_hint())),
|
||||
BinaryHeap::reserve,
|
||||
BinaryHeap::push
|
||||
);
|
||||
@@ -989,16 +992,17 @@ seq_impl!(
|
||||
HashSet<T: Eq + Hash, S: BuildHasher + Default>,
|
||||
seq,
|
||||
HashSet::clear,
|
||||
HashSet::with_capacity_and_hasher(size_hint::cautious(seq.size_hint()), S::default()),
|
||||
HashSet::with_capacity_and_hasher(size_hint::cautious::<T>(seq.size_hint()), S::default()),
|
||||
HashSet::reserve,
|
||||
HashSet::insert);
|
||||
HashSet::insert
|
||||
);
|
||||
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
seq_impl!(
|
||||
VecDeque<T>,
|
||||
seq,
|
||||
VecDeque::clear,
|
||||
VecDeque::with_capacity(size_hint::cautious(seq.size_hint())),
|
||||
VecDeque::with_capacity(size_hint::cautious::<T>(seq.size_hint())),
|
||||
VecDeque::reserve,
|
||||
VecDeque::push_back
|
||||
);
|
||||
@@ -1032,7 +1036,8 @@ where
|
||||
where
|
||||
A: SeqAccess<'de>,
|
||||
{
|
||||
let mut values = Vec::with_capacity(size_hint::cautious(seq.size_hint()));
|
||||
let capacity = size_hint::cautious::<T>(seq.size_hint());
|
||||
let mut values = Vec::<T>::with_capacity(capacity);
|
||||
|
||||
while let Some(value) = try!(seq.next_element()) {
|
||||
values.push(value);
|
||||
@@ -1068,7 +1073,7 @@ where
|
||||
where
|
||||
A: SeqAccess<'de>,
|
||||
{
|
||||
let hint = size_hint::cautious(seq.size_hint());
|
||||
let hint = size_hint::cautious::<T>(seq.size_hint());
|
||||
if let Some(additional) = hint.checked_sub(self.0.len()) {
|
||||
self.0.reserve(additional);
|
||||
}
|
||||
@@ -1353,7 +1358,7 @@ tuple_impls! {
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
macro_rules! map_impl {
|
||||
(
|
||||
$ty:ident < K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)*)* >,
|
||||
$ty:ident <K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)*)*>,
|
||||
$access:ident,
|
||||
$with_capacity:expr
|
||||
) => {
|
||||
@@ -1406,16 +1411,14 @@ macro_rules! map_impl {
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
map_impl!(
|
||||
BTreeMap<K: Ord, V>,
|
||||
map,
|
||||
BTreeMap::new());
|
||||
map_impl!(BTreeMap<K: Ord, V>, map, BTreeMap::new());
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
map_impl!(
|
||||
HashMap<K: Eq + Hash, V, S: BuildHasher + Default>,
|
||||
map,
|
||||
HashMap::with_capacity_and_hasher(size_hint::cautious(map.size_hint()), S::default()));
|
||||
HashMap::with_capacity_and_hasher(size_hint::cautious::<(K, V)>(map.size_hint()), S::default())
|
||||
);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -1440,15 +1443,15 @@ macro_rules! parse_ip_impl {
|
||||
#[cfg(feature = "std")]
|
||||
macro_rules! variant_identifier {
|
||||
(
|
||||
$name_kind: ident ( $($variant: ident; $bytes: expr; $index: expr),* )
|
||||
$expecting_message: expr,
|
||||
$variants_name: ident
|
||||
$name_kind:ident ($($variant:ident; $bytes:expr; $index:expr),*)
|
||||
$expecting_message:expr,
|
||||
$variants_name:ident
|
||||
) => {
|
||||
enum $name_kind {
|
||||
$( $variant ),*
|
||||
$($variant),*
|
||||
}
|
||||
|
||||
static $variants_name: &'static [&'static str] = &[ $( stringify!($variant) ),*];
|
||||
static $variants_name: &[&str] = &[$(stringify!($variant)),*];
|
||||
|
||||
impl<'de> Deserialize<'de> for $name_kind {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
@@ -1515,12 +1518,12 @@ macro_rules! variant_identifier {
|
||||
#[cfg(feature = "std")]
|
||||
macro_rules! deserialize_enum {
|
||||
(
|
||||
$name: ident $name_kind: ident ( $($variant: ident; $bytes: expr; $index: expr),* )
|
||||
$expecting_message: expr,
|
||||
$deserializer: expr
|
||||
$name:ident $name_kind:ident ($($variant:ident; $bytes:expr; $index:expr),*)
|
||||
$expecting_message:expr,
|
||||
$deserializer:expr
|
||||
) => {
|
||||
variant_identifier!{
|
||||
$name_kind ( $($variant; $bytes; $index),* )
|
||||
variant_identifier! {
|
||||
$name_kind ($($variant; $bytes; $index),*)
|
||||
$expecting_message,
|
||||
VARIANTS
|
||||
}
|
||||
@@ -1585,7 +1588,7 @@ macro_rules! parse_socket_impl {
|
||||
if deserializer.is_human_readable() {
|
||||
deserializer.deserialize_str(FromStrVisitor::new($expecting))
|
||||
} else {
|
||||
<(_, u16)>::deserialize(deserializer).map(|(ip, port)| $new(ip, port))
|
||||
<(_, u16)>::deserialize(deserializer).map($new)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1612,12 +1615,10 @@ impl<'de> Deserialize<'de> for net::SocketAddr {
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
parse_socket_impl!("IPv4 socket address" net::SocketAddrV4, net::SocketAddrV4::new);
|
||||
parse_socket_impl!("IPv4 socket address" net::SocketAddrV4, |(ip, port)| net::SocketAddrV4::new(ip, port));
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
parse_socket_impl!("IPv6 socket address" net::SocketAddrV6, |ip, port| net::SocketAddrV6::new(
|
||||
ip, port, 0, 0
|
||||
));
|
||||
parse_socket_impl!("IPv6 socket address" net::SocketAddrV6, |(ip, port)| net::SocketAddrV6::new(ip, port, 0, 0));
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -2096,7 +2097,7 @@ impl<'de> Deserialize<'de> for Duration {
|
||||
}
|
||||
}
|
||||
|
||||
const FIELDS: &'static [&'static str] = &["secs", "nanos"];
|
||||
const FIELDS: &[&str] = &["secs", "nanos"];
|
||||
deserializer.deserialize_struct("Duration", FIELDS, DurationVisitor)
|
||||
}
|
||||
}
|
||||
@@ -2238,7 +2239,7 @@ impl<'de> Deserialize<'de> for SystemTime {
|
||||
}
|
||||
}
|
||||
|
||||
const FIELDS: &'static [&'static str] = &["secs_since_epoch", "nanos_since_epoch"];
|
||||
const FIELDS: &[&str] = &["secs_since_epoch", "nanos_since_epoch"];
|
||||
let duration = try!(deserializer.deserialize_struct("SystemTime", FIELDS, DurationVisitor));
|
||||
#[cfg(not(no_systemtime_checked_add))]
|
||||
let ret = UNIX_EPOCH
|
||||
@@ -2256,9 +2257,9 @@ impl<'de> Deserialize<'de> for SystemTime {
|
||||
//
|
||||
// #[derive(Deserialize)]
|
||||
// #[serde(deny_unknown_fields)]
|
||||
// struct Range {
|
||||
// start: u64,
|
||||
// end: u32,
|
||||
// struct Range<Idx> {
|
||||
// start: Idx,
|
||||
// end: Idx,
|
||||
// }
|
||||
impl<'de, Idx> Deserialize<'de> for Range<Idx>
|
||||
where
|
||||
@@ -2268,14 +2269,14 @@ where
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let (start, end) = deserializer.deserialize_struct(
|
||||
let (start, end) = try!(deserializer.deserialize_struct(
|
||||
"Range",
|
||||
range::FIELDS,
|
||||
range::RangeVisitor {
|
||||
expecting: "struct Range",
|
||||
phantom: PhantomData,
|
||||
},
|
||||
)?;
|
||||
));
|
||||
Ok(start..end)
|
||||
}
|
||||
}
|
||||
@@ -2289,14 +2290,14 @@ where
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let (start, end) = deserializer.deserialize_struct(
|
||||
let (start, end) = try!(deserializer.deserialize_struct(
|
||||
"RangeInclusive",
|
||||
range::FIELDS,
|
||||
range::RangeVisitor {
|
||||
expecting: "struct RangeInclusive",
|
||||
phantom: PhantomData,
|
||||
},
|
||||
)?;
|
||||
));
|
||||
Ok(RangeInclusive::new(start, end))
|
||||
}
|
||||
}
|
||||
@@ -2306,7 +2307,7 @@ mod range {
|
||||
|
||||
use de::{Deserialize, Deserializer, Error, MapAccess, SeqAccess, Visitor};
|
||||
|
||||
pub const FIELDS: &'static [&'static str] = &["start", "end"];
|
||||
pub const FIELDS: &[&str] = &["start", "end"];
|
||||
|
||||
// If this were outside of the serde crate, it would just use:
|
||||
//
|
||||
@@ -2432,6 +2433,282 @@ mod range {
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Similar to:
|
||||
//
|
||||
// #[derive(Deserialize)]
|
||||
// #[serde(deny_unknown_fields)]
|
||||
// struct RangeFrom<Idx> {
|
||||
// start: Idx,
|
||||
// }
|
||||
impl<'de, Idx> Deserialize<'de> for RangeFrom<Idx>
|
||||
where
|
||||
Idx: Deserialize<'de>,
|
||||
{
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let start = try!(deserializer.deserialize_struct(
|
||||
"RangeFrom",
|
||||
range_from::FIELDS,
|
||||
range_from::RangeFromVisitor {
|
||||
expecting: "struct RangeFrom",
|
||||
phantom: PhantomData,
|
||||
},
|
||||
));
|
||||
Ok(start..)
|
||||
}
|
||||
}
|
||||
|
||||
mod range_from {
|
||||
use lib::*;
|
||||
|
||||
use de::{Deserialize, Deserializer, Error, MapAccess, SeqAccess, Visitor};
|
||||
|
||||
pub const FIELDS: &[&str] = &["end"];
|
||||
|
||||
// If this were outside of the serde crate, it would just use:
|
||||
//
|
||||
// #[derive(Deserialize)]
|
||||
// #[serde(field_identifier, rename_all = "lowercase")]
|
||||
enum Field {
|
||||
End,
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for Field {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
struct FieldVisitor;
|
||||
|
||||
impl<'de> Visitor<'de> for FieldVisitor {
|
||||
type Value = Field;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
formatter.write_str("`end`")
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
match value {
|
||||
"end" => Ok(Field::End),
|
||||
_ => Err(Error::unknown_field(value, FIELDS)),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
match value {
|
||||
b"end" => Ok(Field::End),
|
||||
_ => {
|
||||
let value = ::__private::from_utf8_lossy(value);
|
||||
Err(Error::unknown_field(&*value, FIELDS))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.deserialize_identifier(FieldVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct RangeFromVisitor<Idx> {
|
||||
pub expecting: &'static str,
|
||||
pub phantom: PhantomData<Idx>,
|
||||
}
|
||||
|
||||
impl<'de, Idx> Visitor<'de> for RangeFromVisitor<Idx>
|
||||
where
|
||||
Idx: Deserialize<'de>,
|
||||
{
|
||||
type Value = Idx;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
formatter.write_str(self.expecting)
|
||||
}
|
||||
|
||||
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
|
||||
where
|
||||
A: SeqAccess<'de>,
|
||||
{
|
||||
let end: Idx = match try!(seq.next_element()) {
|
||||
Some(value) => value,
|
||||
None => {
|
||||
return Err(Error::invalid_length(0, &self));
|
||||
}
|
||||
};
|
||||
Ok(end)
|
||||
}
|
||||
|
||||
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
|
||||
where
|
||||
A: MapAccess<'de>,
|
||||
{
|
||||
let mut end: Option<Idx> = None;
|
||||
while let Some(key) = try!(map.next_key()) {
|
||||
match key {
|
||||
Field::End => {
|
||||
if end.is_some() {
|
||||
return Err(<A::Error as Error>::duplicate_field("end"));
|
||||
}
|
||||
end = Some(try!(map.next_value()));
|
||||
}
|
||||
}
|
||||
}
|
||||
let end = match end {
|
||||
Some(end) => end,
|
||||
None => return Err(<A::Error as Error>::missing_field("end")),
|
||||
};
|
||||
Ok(end)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Similar to:
|
||||
//
|
||||
// #[derive(Deserialize)]
|
||||
// #[serde(deny_unknown_fields)]
|
||||
// struct RangeTo<Idx> {
|
||||
// start: Idx,
|
||||
// }
|
||||
impl<'de, Idx> Deserialize<'de> for RangeTo<Idx>
|
||||
where
|
||||
Idx: Deserialize<'de>,
|
||||
{
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let end = try!(deserializer.deserialize_struct(
|
||||
"RangeTo",
|
||||
range_to::FIELDS,
|
||||
range_to::RangeToVisitor {
|
||||
expecting: "struct RangeTo",
|
||||
phantom: PhantomData,
|
||||
},
|
||||
));
|
||||
Ok(..end)
|
||||
}
|
||||
}
|
||||
|
||||
mod range_to {
|
||||
use lib::*;
|
||||
|
||||
use de::{Deserialize, Deserializer, Error, MapAccess, SeqAccess, Visitor};
|
||||
|
||||
pub const FIELDS: &[&str] = &["start"];
|
||||
|
||||
// If this were outside of the serde crate, it would just use:
|
||||
//
|
||||
// #[derive(Deserialize)]
|
||||
// #[serde(field_identifier, rename_all = "lowercase")]
|
||||
enum Field {
|
||||
Start,
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for Field {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
struct FieldVisitor;
|
||||
|
||||
impl<'de> Visitor<'de> for FieldVisitor {
|
||||
type Value = Field;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
formatter.write_str("`start`")
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
match value {
|
||||
"start" => Ok(Field::Start),
|
||||
_ => Err(Error::unknown_field(value, FIELDS)),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
match value {
|
||||
b"start" => Ok(Field::Start),
|
||||
_ => {
|
||||
let value = ::__private::from_utf8_lossy(value);
|
||||
Err(Error::unknown_field(&*value, FIELDS))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.deserialize_identifier(FieldVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct RangeToVisitor<Idx> {
|
||||
pub expecting: &'static str,
|
||||
pub phantom: PhantomData<Idx>,
|
||||
}
|
||||
|
||||
impl<'de, Idx> Visitor<'de> for RangeToVisitor<Idx>
|
||||
where
|
||||
Idx: Deserialize<'de>,
|
||||
{
|
||||
type Value = Idx;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
formatter.write_str(self.expecting)
|
||||
}
|
||||
|
||||
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
|
||||
where
|
||||
A: SeqAccess<'de>,
|
||||
{
|
||||
let start: Idx = match try!(seq.next_element()) {
|
||||
Some(value) => value,
|
||||
None => {
|
||||
return Err(Error::invalid_length(0, &self));
|
||||
}
|
||||
};
|
||||
Ok(start)
|
||||
}
|
||||
|
||||
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
|
||||
where
|
||||
A: MapAccess<'de>,
|
||||
{
|
||||
let mut start: Option<Idx> = None;
|
||||
while let Some(key) = try!(map.next_key()) {
|
||||
match key {
|
||||
Field::Start => {
|
||||
if start.is_some() {
|
||||
return Err(<A::Error as Error>::duplicate_field("start"));
|
||||
}
|
||||
start = Some(try!(map.next_value()));
|
||||
}
|
||||
}
|
||||
}
|
||||
let start = match start {
|
||||
Some(start) => start,
|
||||
None => return Err(<A::Error as Error>::missing_field("start")),
|
||||
};
|
||||
Ok(start)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[cfg(any(not(no_ops_bound), all(feature = "std", not(no_collections_bound))))]
|
||||
impl<'de, T> Deserialize<'de> for Bound<T>
|
||||
where
|
||||
@@ -2532,7 +2809,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
const VARIANTS: &'static [&'static str] = &["Unbounded", "Included", "Excluded"];
|
||||
const VARIANTS: &[&str] = &["Unbounded", "Included", "Excluded"];
|
||||
|
||||
deserializer.deserialize_enum("Bound", VARIANTS, BoundVisitor(PhantomData))
|
||||
}
|
||||
@@ -2640,7 +2917,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
const VARIANTS: &'static [&'static str] = &["Ok", "Err"];
|
||||
const VARIANTS: &[&str] = &["Ok", "Err"];
|
||||
|
||||
deserializer.deserialize_enum("Result", VARIANTS, ResultVisitor(PhantomData))
|
||||
}
|
||||
@@ -2662,8 +2939,9 @@ where
|
||||
|
||||
#[cfg(all(feature = "std", not(no_std_atomic)))]
|
||||
macro_rules! atomic_impl {
|
||||
($($ty:ident)*) => {
|
||||
($($ty:ident $size:expr)*) => {
|
||||
$(
|
||||
#[cfg(any(no_target_has_atomic, target_has_atomic = $size))]
|
||||
impl<'de> Deserialize<'de> for $ty {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
@@ -2678,14 +2956,21 @@ macro_rules! atomic_impl {
|
||||
|
||||
#[cfg(all(feature = "std", not(no_std_atomic)))]
|
||||
atomic_impl! {
|
||||
AtomicBool
|
||||
AtomicI8 AtomicI16 AtomicI32 AtomicIsize
|
||||
AtomicU8 AtomicU16 AtomicU32 AtomicUsize
|
||||
AtomicBool "8"
|
||||
AtomicI8 "8"
|
||||
AtomicI16 "16"
|
||||
AtomicI32 "32"
|
||||
AtomicIsize "ptr"
|
||||
AtomicU8 "8"
|
||||
AtomicU16 "16"
|
||||
AtomicU32 "32"
|
||||
AtomicUsize "ptr"
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "std", not(no_std_atomic64)))]
|
||||
atomic_impl! {
|
||||
AtomicI64 AtomicU64
|
||||
AtomicI64 "64"
|
||||
AtomicU64 "64"
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
@@ -2698,7 +2983,7 @@ struct FromStrVisitor<T> {
|
||||
impl<T> FromStrVisitor<T> {
|
||||
fn new(expecting: &'static str) -> Self {
|
||||
FromStrVisitor {
|
||||
expecting: expecting,
|
||||
expecting,
|
||||
ty: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
+34
-39
@@ -162,7 +162,7 @@ macro_rules! declare_error_trait {
|
||||
///
|
||||
/// The message should not be capitalized and should not end with a period.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use std::str::FromStr;
|
||||
/// #
|
||||
/// # struct IpAddr;
|
||||
@@ -307,7 +307,7 @@ declare_error_trait!(Error: Sized + Debug + Display);
|
||||
/// This is used as an argument to the `invalid_type`, `invalid_value`, and
|
||||
/// `invalid_length` methods of the `Error` trait to build error messages.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use std::fmt;
|
||||
/// #
|
||||
/// # use serde::de::{self, Unexpected, Visitor};
|
||||
@@ -432,10 +432,9 @@ impl<'a> fmt::Display for Unexpected<'a> {
|
||||
/// Within the context of a `Visitor` implementation, the `Visitor` itself
|
||||
/// (`&self`) is an implementation of this trait.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use std::fmt;
|
||||
/// #
|
||||
/// ```edition2021
|
||||
/// # use serde::de::{self, Unexpected, Visitor};
|
||||
/// # use std::fmt;
|
||||
/// #
|
||||
/// # struct Example;
|
||||
/// #
|
||||
@@ -457,7 +456,7 @@ impl<'a> fmt::Display for Unexpected<'a> {
|
||||
///
|
||||
/// Outside of a `Visitor`, `&"..."` can be used.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use serde::de::{self, Unexpected};
|
||||
/// #
|
||||
/// # fn example<E>() -> Result<(), E>
|
||||
@@ -465,7 +464,10 @@ impl<'a> fmt::Display for Unexpected<'a> {
|
||||
/// # E: de::Error,
|
||||
/// # {
|
||||
/// # let v = true;
|
||||
/// return Err(de::Error::invalid_type(Unexpected::Bool(v), &"a negative integer"));
|
||||
/// return Err(de::Error::invalid_type(
|
||||
/// Unexpected::Bool(v),
|
||||
/// &"a negative integer",
|
||||
/// ));
|
||||
/// # }
|
||||
/// ```
|
||||
pub trait Expected {
|
||||
@@ -501,8 +503,8 @@ impl<'a> Display for Expected + 'a {
|
||||
/// by Serde.
|
||||
///
|
||||
/// Serde provides `Deserialize` implementations for many Rust primitive and
|
||||
/// standard library types. The complete list is [here][de]. All of these can
|
||||
/// be deserialized using Serde out of the box.
|
||||
/// standard library types. The complete list is [here][crate::de]. All of these
|
||||
/// can be deserialized using Serde out of the box.
|
||||
///
|
||||
/// Additionally, Serde provides a procedural macro called `serde_derive` to
|
||||
/// automatically generate `Deserialize` implementations for structs and enums
|
||||
@@ -518,7 +520,6 @@ impl<'a> Display for Expected + 'a {
|
||||
/// `LinkedHashMap<K, V>` type that is deserializable by Serde because the crate
|
||||
/// provides an implementation of `Deserialize` for it.
|
||||
///
|
||||
/// [de]: https://docs.serde.rs/serde/de/index.html
|
||||
/// [derive]: https://serde.rs/derive.html
|
||||
/// [impl-deserialize]: https://serde.rs/impl-deserialize.html
|
||||
///
|
||||
@@ -565,7 +566,7 @@ pub trait Deserialize<'de>: Sized {
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
// Default implementation just delegates to `deserialize` impl.
|
||||
*place = Deserialize::deserialize(deserializer)?;
|
||||
*place = try!(Deserialize::deserialize(deserializer));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -578,7 +579,7 @@ pub trait Deserialize<'de>: Sized {
|
||||
/// from the input string, but a `from_reader` function may only deserialize
|
||||
/// owned data.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use serde::de::{Deserialize, DeserializeOwned};
|
||||
/// # use std::io::{Read, Result};
|
||||
/// #
|
||||
@@ -617,7 +618,7 @@ impl<T> DeserializeOwned for T where T: for<'de> Deserialize<'de> {}
|
||||
///
|
||||
/// The canonical API for stateless deserialization looks like this:
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use serde::Deserialize;
|
||||
/// #
|
||||
/// # enum Error {}
|
||||
@@ -631,7 +632,7 @@ impl<T> DeserializeOwned for T where T: for<'de> Deserialize<'de> {}
|
||||
/// Adjusting an API like this to support stateful deserialization is a matter
|
||||
/// of accepting a seed as input:
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use serde::de::DeserializeSeed;
|
||||
/// #
|
||||
/// # enum Error {}
|
||||
@@ -664,12 +665,11 @@ impl<T> DeserializeOwned for T where T: for<'de> Deserialize<'de> {}
|
||||
/// into it. This requires stateful deserialization using the `DeserializeSeed`
|
||||
/// trait.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// use serde::de::{Deserialize, DeserializeSeed, Deserializer, SeqAccess, Visitor};
|
||||
/// use std::fmt;
|
||||
/// use std::marker::PhantomData;
|
||||
///
|
||||
/// use serde::de::{Deserialize, DeserializeSeed, Deserializer, SeqAccess, Visitor};
|
||||
///
|
||||
/// // A DeserializeSeed implementation that uses stateful deserialization to
|
||||
/// // append array elements onto the end of an existing vector. The preexisting
|
||||
/// // state ("seed") in this case is the Vec<T>. The `deserialize` method of
|
||||
@@ -710,7 +710,7 @@ impl<T> DeserializeOwned for T where T: for<'de> Deserialize<'de> {}
|
||||
/// {
|
||||
/// // Decrease the number of reallocations if there are many elements
|
||||
/// if let Some(size_hint) = seq.size_hint() {
|
||||
/// self.0.reserve(size_hint);
|
||||
/// self.0.reserve(size_hint);
|
||||
/// }
|
||||
///
|
||||
/// // Visit each element in the inner array and push it onto
|
||||
@@ -862,10 +862,10 @@ where
|
||||
/// The `Deserializer` trait supports two entry point styles which enables
|
||||
/// different kinds of deserialization.
|
||||
///
|
||||
/// 1. The `deserialize` method. Self-describing data formats like JSON are able
|
||||
/// to look at the serialized data and tell what it represents. For example
|
||||
/// the JSON deserializer may see an opening curly brace (`{`) and know that
|
||||
/// it is seeing a map. If the data format supports
|
||||
/// 1. The `deserialize_any` method. Self-describing data formats like JSON are
|
||||
/// able to look at the serialized data and tell what it represents. For
|
||||
/// example the JSON deserializer may see an opening curly brace (`{`) and
|
||||
/// know that it is seeing a map. If the data format supports
|
||||
/// `Deserializer::deserialize_any`, it will drive the Visitor using whatever
|
||||
/// type it sees in the input. JSON uses this approach when deserializing
|
||||
/// `serde_json::Value` which is an enum that can represent any JSON
|
||||
@@ -1159,7 +1159,7 @@ pub trait Deserializer<'de>: Sized {
|
||||
/// human-readable one and binary formats like Postcard will prefer the
|
||||
/// compact one.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use std::ops::Add;
|
||||
/// # use std::str::FromStr;
|
||||
/// #
|
||||
@@ -1250,10 +1250,9 @@ pub trait Deserializer<'de>: Sized {
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use std::fmt;
|
||||
/// #
|
||||
/// ```edition2021
|
||||
/// # use serde::de::{self, Unexpected, Visitor};
|
||||
/// # use std::fmt;
|
||||
/// #
|
||||
/// /// A visitor that deserializes a long string - a string containing at least
|
||||
/// /// some minimum number of bytes.
|
||||
@@ -1291,7 +1290,7 @@ pub trait Visitor<'de>: Sized {
|
||||
/// "an integer between 0 and 64". The message should not be capitalized and
|
||||
/// should not end with a period.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use std::fmt;
|
||||
/// #
|
||||
/// # struct S {
|
||||
@@ -2036,7 +2035,7 @@ pub trait VariantAccess<'de>: Sized {
|
||||
/// If the data contains a different type of variant, the following
|
||||
/// `invalid_type` error should be constructed:
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use serde::de::{self, value, DeserializeSeed, Visitor, VariantAccess, Unexpected};
|
||||
/// #
|
||||
/// # struct X;
|
||||
@@ -2076,7 +2075,7 @@ pub trait VariantAccess<'de>: Sized {
|
||||
/// If the data contains a different type of variant, the following
|
||||
/// `invalid_type` error should be constructed:
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use serde::de::{self, value, DeserializeSeed, Visitor, VariantAccess, Unexpected};
|
||||
/// #
|
||||
/// # struct X;
|
||||
@@ -2132,7 +2131,7 @@ pub trait VariantAccess<'de>: Sized {
|
||||
/// If the data contains a different type of variant, the following
|
||||
/// `invalid_type` error should be constructed:
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use serde::de::{self, value, DeserializeSeed, Visitor, VariantAccess, Unexpected};
|
||||
/// #
|
||||
/// # struct X;
|
||||
@@ -2149,11 +2148,7 @@ pub trait VariantAccess<'de>: Sized {
|
||||
/// # T: DeserializeSeed<'de>,
|
||||
/// # { unimplemented!() }
|
||||
/// #
|
||||
/// fn tuple_variant<V>(
|
||||
/// self,
|
||||
/// _len: usize,
|
||||
/// _visitor: V,
|
||||
/// ) -> Result<V::Value, Self::Error>
|
||||
/// fn tuple_variant<V>(self, _len: usize, _visitor: V) -> Result<V::Value, Self::Error>
|
||||
/// where
|
||||
/// V: Visitor<'de>,
|
||||
/// {
|
||||
@@ -2179,7 +2174,7 @@ pub trait VariantAccess<'de>: Sized {
|
||||
/// If the data contains a different type of variant, the following
|
||||
/// `invalid_type` error should be constructed:
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use serde::de::{self, value, DeserializeSeed, Visitor, VariantAccess, Unexpected};
|
||||
/// #
|
||||
/// # struct X;
|
||||
@@ -2239,10 +2234,10 @@ pub trait VariantAccess<'de>: Sized {
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// use serde::de::{value, Deserialize, IntoDeserializer};
|
||||
/// use serde_derive::Deserialize;
|
||||
/// use std::str::FromStr;
|
||||
/// use serde::Deserialize;
|
||||
/// use serde::de::{value, IntoDeserializer};
|
||||
///
|
||||
/// #[derive(Deserialize)]
|
||||
/// enum Setting {
|
||||
|
||||
@@ -31,7 +31,7 @@ pub fn encode(c: char) -> Encode {
|
||||
buf[3] = (code & 0x3F) as u8 | TAG_CONT;
|
||||
0
|
||||
};
|
||||
Encode { buf: buf, pos: pos }
|
||||
Encode { buf, pos }
|
||||
}
|
||||
|
||||
pub struct Encode {
|
||||
|
||||
+52
-21
@@ -1,10 +1,10 @@
|
||||
//! Building blocks for deserializing basic values using the `IntoDeserializer`
|
||||
//! trait.
|
||||
//!
|
||||
//! ```edition2018
|
||||
//! ```edition2021
|
||||
//! use serde::de::{value, Deserialize, IntoDeserializer};
|
||||
//! use serde_derive::Deserialize;
|
||||
//! use std::str::FromStr;
|
||||
//! use serde::Deserialize;
|
||||
//! use serde::de::{value, IntoDeserializer};
|
||||
//!
|
||||
//! #[derive(Deserialize)]
|
||||
//! enum Setting {
|
||||
@@ -251,7 +251,7 @@ macro_rules! primitive_deserializer {
|
||||
#[allow(missing_docs)]
|
||||
pub fn new(value: $ty) -> Self {
|
||||
$name {
|
||||
value: value,
|
||||
value,
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
@@ -330,7 +330,7 @@ impl<E> U32Deserializer<E> {
|
||||
#[allow(missing_docs)]
|
||||
pub fn new(value: u32) -> Self {
|
||||
U32Deserializer {
|
||||
value: value,
|
||||
value,
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
@@ -419,7 +419,7 @@ impl<'a, E> StrDeserializer<'a, E> {
|
||||
#[allow(missing_docs)]
|
||||
pub fn new(value: &'a str) -> Self {
|
||||
StrDeserializer {
|
||||
value: value,
|
||||
value,
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
@@ -498,7 +498,7 @@ impl<'de, E> BorrowedStrDeserializer<'de, E> {
|
||||
/// Create a new borrowed deserializer from the given string.
|
||||
pub fn new(value: &'de str) -> BorrowedStrDeserializer<'de, E> {
|
||||
BorrowedStrDeserializer {
|
||||
value: value,
|
||||
value,
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
@@ -598,7 +598,7 @@ impl<E> StringDeserializer<E> {
|
||||
#[allow(missing_docs)]
|
||||
pub fn new(value: String) -> Self {
|
||||
StringDeserializer {
|
||||
value: value,
|
||||
value,
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
@@ -701,7 +701,7 @@ impl<'a, E> CowStrDeserializer<'a, E> {
|
||||
#[allow(missing_docs)]
|
||||
pub fn new(value: Cow<'a, str>) -> Self {
|
||||
CowStrDeserializer {
|
||||
value: value,
|
||||
value,
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
@@ -783,7 +783,7 @@ impl<'a, E> BytesDeserializer<'a, E> {
|
||||
/// Create a new deserializer from the given bytes.
|
||||
pub fn new(value: &'a [u8]) -> Self {
|
||||
BytesDeserializer {
|
||||
value: value,
|
||||
value,
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
@@ -842,7 +842,7 @@ impl<'de, E> BorrowedBytesDeserializer<'de, E> {
|
||||
/// Create a new borrowed deserializer from the given borrowed bytes.
|
||||
pub fn new(value: &'de [u8]) -> Self {
|
||||
BorrowedBytesDeserializer {
|
||||
value: value,
|
||||
value,
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
@@ -1053,7 +1053,7 @@ pub struct SeqAccessDeserializer<A> {
|
||||
impl<A> SeqAccessDeserializer<A> {
|
||||
/// Construct a new `SeqAccessDeserializer<A>`.
|
||||
pub fn new(seq: A) -> Self {
|
||||
SeqAccessDeserializer { seq: seq }
|
||||
SeqAccessDeserializer { seq }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1454,7 +1454,7 @@ pub struct MapAccessDeserializer<A> {
|
||||
impl<A> MapAccessDeserializer<A> {
|
||||
/// Construct a new `MapAccessDeserializer<A>`.
|
||||
pub fn new(map: A) -> Self {
|
||||
MapAccessDeserializer { map: map }
|
||||
MapAccessDeserializer { map }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1501,7 +1501,7 @@ where
|
||||
where
|
||||
T: de::DeserializeSeed<'de>,
|
||||
{
|
||||
match self.map.next_key_seed(seed)? {
|
||||
match try!(self.map.next_key_seed(seed)) {
|
||||
Some(key) => Ok((key, private::map_as_enum(self.map))),
|
||||
None => Err(de::Error::invalid_type(de::Unexpected::Map, &"enum")),
|
||||
}
|
||||
@@ -1510,6 +1510,41 @@ where
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// A deserializer holding an `EnumAccess`.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct EnumAccessDeserializer<A> {
|
||||
access: A,
|
||||
}
|
||||
|
||||
impl<A> EnumAccessDeserializer<A> {
|
||||
/// Construct a new `EnumAccessDeserializer<A>`.
|
||||
pub fn new(access: A) -> Self {
|
||||
EnumAccessDeserializer { access }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, A> de::Deserializer<'de> for EnumAccessDeserializer<A>
|
||||
where
|
||||
A: de::EnumAccess<'de>,
|
||||
{
|
||||
type Error = A::Error;
|
||||
|
||||
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
visitor.visit_enum(self.access)
|
||||
}
|
||||
|
||||
forward_to_deserialize_any! {
|
||||
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
|
||||
bytes byte_buf option unit unit_struct newtype_struct seq tuple
|
||||
tuple_struct map struct enum identifier ignored_any
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
mod private {
|
||||
use lib::*;
|
||||
|
||||
@@ -1578,7 +1613,7 @@ mod private {
|
||||
}
|
||||
|
||||
pub fn map_as_enum<A>(map: A) -> MapAsEnum<A> {
|
||||
MapAsEnum { map: map }
|
||||
MapAsEnum { map }
|
||||
}
|
||||
|
||||
impl<'de, A> VariantAccess<'de> for MapAsEnum<A>
|
||||
@@ -1602,10 +1637,7 @@ mod private {
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
self.map.next_value_seed(SeedTupleVariant {
|
||||
len: len,
|
||||
visitor: visitor,
|
||||
})
|
||||
self.map.next_value_seed(SeedTupleVariant { len, visitor })
|
||||
}
|
||||
|
||||
fn struct_variant<V>(
|
||||
@@ -1616,8 +1648,7 @@ mod private {
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
self.map
|
||||
.next_value_seed(SeedStructVariant { visitor: visitor })
|
||||
self.map.next_value_seed(SeedStructVariant { visitor })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
/// or do not target platforms that lack 128-bit integers, do not need to
|
||||
/// bother with this macro and may assume support for 128-bit integers.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use serde::__private::doc::Error;
|
||||
/// #
|
||||
/// # struct MySerializer;
|
||||
@@ -50,7 +50,7 @@
|
||||
/// When Serde is built with support for 128-bit integers, this macro expands
|
||||
/// transparently into just the input tokens.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// macro_rules! serde_if_integer128 {
|
||||
/// ($($tt:tt)*) => {
|
||||
/// $($tt)*
|
||||
@@ -61,7 +61,7 @@
|
||||
/// When built without support for 128-bit integers, this macro expands to
|
||||
/// nothing.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// macro_rules! serde_if_integer128 {
|
||||
/// ($($tt:tt)*) => {};
|
||||
/// }
|
||||
|
||||
+62
-15
@@ -45,6 +45,8 @@
|
||||
//! definition.
|
||||
//! - [JSON5], a superset of JSON including some productions from ES5.
|
||||
//! - [URL] query strings, in the x-www-form-urlencoded format.
|
||||
//! - [Starlark], the format used for describing build targets by the Bazel and
|
||||
//! Buck build systems. *(serialization only)*
|
||||
//! - [Envy], a way to deserialize environment variables into Rust structs.
|
||||
//! *(deserialization only)*
|
||||
//! - [Envy Store], a way to deserialize [AWS Parameter Store] parameters into
|
||||
@@ -52,22 +54,29 @@
|
||||
//! - [S-expressions], the textual representation of code and data used by the
|
||||
//! Lisp language family.
|
||||
//! - [D-Bus]'s binary wire format.
|
||||
//! - [FlexBuffers], the schemaless cousin of Google's FlatBuffers zero-copy serialization format.
|
||||
//! - [FlexBuffers], the schemaless cousin of Google's FlatBuffers zero-copy
|
||||
//! serialization format.
|
||||
//! - [Bencode], a simple binary format used in the BitTorrent protocol.
|
||||
//! - [Token streams], for processing Rust procedural macro input.
|
||||
//! *(deserialization only)*
|
||||
//! - [DynamoDB Items], the format used by [rusoto_dynamodb] to transfer data to
|
||||
//! and from DynamoDB.
|
||||
//! - [Hjson], a syntax extension to JSON designed around human reading and
|
||||
//! editing. *(deserialization only)*
|
||||
//!
|
||||
//! [JSON]: https://github.com/serde-rs/json
|
||||
//! [Postcard]: https://github.com/jamesmunns/postcard
|
||||
//! [CBOR]: https://github.com/enarx/ciborium
|
||||
//! [YAML]: https://github.com/dtolnay/serde-yaml
|
||||
//! [MessagePack]: https://github.com/3Hren/msgpack-rust
|
||||
//! [TOML]: https://github.com/alexcrichton/toml-rs
|
||||
//! [TOML]: https://docs.rs/toml
|
||||
//! [Pickle]: https://github.com/birkenfeld/serde-pickle
|
||||
//! [RON]: https://github.com/ron-rs/ron
|
||||
//! [BSON]: https://github.com/mongodb/bson-rust
|
||||
//! [Avro]: https://github.com/flavray/avro-rs
|
||||
//! [Avro]: https://docs.rs/apache-avro
|
||||
//! [JSON5]: https://github.com/callum-oakley/json5-rs
|
||||
//! [URL]: https://docs.rs/serde_qs
|
||||
//! [Starlark]: https://github.com/dtolnay/serde-starlark
|
||||
//! [Envy]: https://github.com/softprops/envy
|
||||
//! [Envy Store]: https://github.com/softprops/envy-store
|
||||
//! [Cargo]: https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
@@ -75,20 +84,23 @@
|
||||
//! [S-expressions]: https://github.com/rotty/lexpr-rs
|
||||
//! [D-Bus]: https://docs.rs/zvariant
|
||||
//! [FlexBuffers]: https://github.com/google/flatbuffers/tree/master/rust/flexbuffers
|
||||
//! [Bencode]: https://github.com/P3KI/bendy
|
||||
//! [Token streams]: https://github.com/oxidecomputer/serde_tokenstream
|
||||
//! [DynamoDB Items]: https://docs.rs/serde_dynamo
|
||||
//! [rusoto_dynamodb]: https://docs.rs/rusoto_dynamodb
|
||||
//! [Hjson]: https://github.com/Canop/deser-hjson
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Serde types in rustdoc of other crates get linked to here.
|
||||
#![doc(html_root_url = "https://docs.rs/serde/1.0.143")]
|
||||
#![doc(html_root_url = "https://docs.rs/serde/1.0.177")]
|
||||
// Support using Serde without the standard library!
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
// Unstable functionality only if the user asks for it. For tracking and
|
||||
// discussion of these features please refer to this issue:
|
||||
//
|
||||
// https://github.com/serde-rs/serde/issues/812
|
||||
#![cfg_attr(feature = "unstable", feature(never_type))]
|
||||
#![cfg_attr(feature = "unstable", feature(error_in_core, never_type))]
|
||||
#![allow(unknown_lints, bare_trait_objects, deprecated)]
|
||||
#![cfg_attr(feature = "cargo-clippy", allow(renamed_and_removed_lints))]
|
||||
// Ignored clippy and clippy_pedantic lints
|
||||
@@ -118,7 +130,7 @@
|
||||
derive_partial_eq_without_eq,
|
||||
enum_glob_use,
|
||||
explicit_auto_deref,
|
||||
let_underscore_drop,
|
||||
let_underscore_untyped,
|
||||
map_err_ignore,
|
||||
new_without_default,
|
||||
result_unit_err,
|
||||
@@ -168,7 +180,7 @@ mod lib {
|
||||
pub use self::core::fmt::{self, Debug, Display};
|
||||
pub use self::core::marker::{self, PhantomData};
|
||||
pub use self::core::num::Wrapping;
|
||||
pub use self::core::ops::Range;
|
||||
pub use self::core::ops::{Range, RangeFrom, RangeTo};
|
||||
pub use self::core::option::{self, Option};
|
||||
pub use self::core::result::{self, Result};
|
||||
|
||||
@@ -207,13 +219,23 @@ mod lib {
|
||||
#[cfg(feature = "std")]
|
||||
pub use std::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque};
|
||||
|
||||
#[cfg(all(not(no_core_cstr), not(feature = "std")))]
|
||||
pub use core::ffi::CStr;
|
||||
#[cfg(feature = "std")]
|
||||
pub use std::ffi::CStr;
|
||||
|
||||
#[cfg(all(not(no_core_cstr), feature = "alloc", not(feature = "std")))]
|
||||
pub use alloc::ffi::CString;
|
||||
#[cfg(feature = "std")]
|
||||
pub use std::ffi::CString;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
pub use std::{error, net};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
pub use std::collections::{HashMap, HashSet};
|
||||
#[cfg(feature = "std")]
|
||||
pub use std::ffi::{CStr, CString, OsStr, OsString};
|
||||
pub use std::ffi::{OsStr, OsString};
|
||||
#[cfg(feature = "std")]
|
||||
pub use std::hash::{BuildHasher, Hash};
|
||||
#[cfg(feature = "std")]
|
||||
@@ -237,18 +259,44 @@ mod lib {
|
||||
#[cfg(not(no_range_inclusive))]
|
||||
pub use self::core::ops::RangeInclusive;
|
||||
|
||||
#[cfg(all(feature = "std", not(no_std_atomic)))]
|
||||
#[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic)))]
|
||||
pub use std::sync::atomic::{
|
||||
AtomicBool, AtomicI16, AtomicI32, AtomicI8, AtomicIsize, AtomicU16, AtomicU32, AtomicU8,
|
||||
AtomicUsize, Ordering,
|
||||
};
|
||||
#[cfg(all(feature = "std", not(no_std_atomic64)))]
|
||||
#[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic64)))]
|
||||
pub use std::sync::atomic::{AtomicI64, AtomicU64};
|
||||
|
||||
#[cfg(all(feature = "std", not(no_target_has_atomic)))]
|
||||
pub use std::sync::atomic::Ordering;
|
||||
#[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "8"))]
|
||||
pub use std::sync::atomic::{AtomicBool, AtomicI8, AtomicU8};
|
||||
#[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "16"))]
|
||||
pub use std::sync::atomic::{AtomicI16, AtomicU16};
|
||||
#[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "32"))]
|
||||
pub use std::sync::atomic::{AtomicI32, AtomicU32};
|
||||
#[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "64"))]
|
||||
pub use std::sync::atomic::{AtomicI64, AtomicU64};
|
||||
#[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "ptr"))]
|
||||
pub use std::sync::atomic::{AtomicIsize, AtomicUsize};
|
||||
|
||||
#[cfg(any(feature = "std", not(no_core_duration)))]
|
||||
pub use self::core::time::Duration;
|
||||
}
|
||||
|
||||
// None of this crate's error handling needs the `From::from` error conversion
|
||||
// performed implicitly by the `?` operator or the standard library's `try!`
|
||||
// macro. This simplified macro gives a 5.5% improvement in compile time
|
||||
// compared to standard `try!`, and 9% improvement compared to `?`.
|
||||
macro_rules! try {
|
||||
($expr:expr) => {
|
||||
match $expr {
|
||||
Ok(val) => val,
|
||||
Err(err) => return Err(err),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[macro_use]
|
||||
@@ -278,7 +326,7 @@ use self::__private as private;
|
||||
#[path = "de/seed.rs"]
|
||||
mod seed;
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
#[cfg(not(any(feature = "std", feature = "unstable")))]
|
||||
mod std_error;
|
||||
|
||||
// Re-export #[derive(Serialize, Deserialize)].
|
||||
@@ -287,12 +335,11 @@ mod std_error;
|
||||
// be annoying for crates that provide handwritten impls or data formats. They
|
||||
// would need to disable default features and then explicitly re-enable std.
|
||||
#[cfg(feature = "serde_derive")]
|
||||
#[allow(unused_imports)]
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
|
||||
/// Derive macro available if serde is built with `features = ["derive"]`.
|
||||
#[cfg(feature = "serde_derive")]
|
||||
#[doc(hidden)]
|
||||
pub use serde_derive::*;
|
||||
pub use serde_derive::{Deserialize, Serialize};
|
||||
|
||||
#[cfg(all(not(no_serde_derive), any(feature = "std", feature = "alloc")))]
|
||||
mod actually_private {
|
||||
|
||||
+4
-5
@@ -11,7 +11,7 @@
|
||||
/// input. This requires repetitive implementations of all the [`Deserializer`]
|
||||
/// trait methods.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use serde::forward_to_deserialize_any;
|
||||
/// # use serde::de::{value, Deserializer, Visitor};
|
||||
/// #
|
||||
@@ -47,7 +47,7 @@
|
||||
/// methods so that they forward directly to [`Deserializer::deserialize_any`].
|
||||
/// You can choose which methods to forward.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use serde::forward_to_deserialize_any;
|
||||
/// # use serde::de::{value, Deserializer, Visitor};
|
||||
/// #
|
||||
@@ -78,11 +78,10 @@
|
||||
/// called `V`. A different type parameter and a different lifetime can be
|
||||
/// specified explicitly if necessary.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use std::marker::PhantomData;
|
||||
/// #
|
||||
/// ```edition2021
|
||||
/// # use serde::forward_to_deserialize_any;
|
||||
/// # use serde::de::{value, Deserializer, Visitor};
|
||||
/// # use std::marker::PhantomData;
|
||||
/// #
|
||||
/// # struct MyDeserializer<V>(PhantomData<V>);
|
||||
/// #
|
||||
|
||||
+150
-158
@@ -474,7 +474,8 @@ mod content {
|
||||
where
|
||||
V: SeqAccess<'de>,
|
||||
{
|
||||
let mut vec = Vec::with_capacity(size_hint::cautious(visitor.size_hint()));
|
||||
let mut vec =
|
||||
Vec::<Content>::with_capacity(size_hint::cautious::<Content>(visitor.size_hint()));
|
||||
while let Some(e) = try!(visitor.next_element()) {
|
||||
vec.push(e);
|
||||
}
|
||||
@@ -485,7 +486,10 @@ mod content {
|
||||
where
|
||||
V: MapAccess<'de>,
|
||||
{
|
||||
let mut vec = Vec::with_capacity(size_hint::cautious(visitor.size_hint()));
|
||||
let mut vec =
|
||||
Vec::<(Content, Content)>::with_capacity(
|
||||
size_hint::cautious::<(Content, Content)>(visitor.size_hint()),
|
||||
);
|
||||
while let Some(kv) = try!(visitor.next_entry()) {
|
||||
vec.push(kv);
|
||||
}
|
||||
@@ -518,7 +522,7 @@ mod content {
|
||||
impl<'de> TagOrContentVisitor<'de> {
|
||||
fn new(name: &'static str) -> Self {
|
||||
TagOrContentVisitor {
|
||||
name: name,
|
||||
name,
|
||||
value: PhantomData,
|
||||
}
|
||||
}
|
||||
@@ -797,51 +801,29 @@ mod content {
|
||||
/// Used by generated code to deserialize an internally tagged enum.
|
||||
///
|
||||
/// Not public API.
|
||||
pub struct TaggedContent<'de, T> {
|
||||
pub tag: T,
|
||||
pub content: Content<'de>,
|
||||
}
|
||||
|
||||
/// Not public API.
|
||||
pub struct TaggedContentVisitor<'de, T> {
|
||||
pub struct TaggedContentVisitor<T> {
|
||||
tag_name: &'static str,
|
||||
expecting: &'static str,
|
||||
value: PhantomData<TaggedContent<'de, T>>,
|
||||
value: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<'de, T> TaggedContentVisitor<'de, T> {
|
||||
impl<T> TaggedContentVisitor<T> {
|
||||
/// Visitor for the content of an internally tagged enum with the given
|
||||
/// tag name.
|
||||
pub fn new(name: &'static str, expecting: &'static str) -> Self {
|
||||
TaggedContentVisitor {
|
||||
tag_name: name,
|
||||
expecting: expecting,
|
||||
expecting,
|
||||
value: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, T> DeserializeSeed<'de> for TaggedContentVisitor<'de, T>
|
||||
impl<'de, T> Visitor<'de> for TaggedContentVisitor<T>
|
||||
where
|
||||
T: Deserialize<'de>,
|
||||
{
|
||||
type Value = TaggedContent<'de, T>;
|
||||
|
||||
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
// Internally tagged enums are only supported in self-describing
|
||||
// formats.
|
||||
deserializer.deserialize_any(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, T> Visitor<'de> for TaggedContentVisitor<'de, T>
|
||||
where
|
||||
T: Deserialize<'de>,
|
||||
{
|
||||
type Value = TaggedContent<'de, T>;
|
||||
type Value = (T, Content<'de>);
|
||||
|
||||
fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt.write_str(self.expecting)
|
||||
@@ -858,10 +840,7 @@ mod content {
|
||||
}
|
||||
};
|
||||
let rest = de::value::SeqAccessDeserializer::new(seq);
|
||||
Ok(TaggedContent {
|
||||
tag: tag,
|
||||
content: try!(Content::deserialize(rest)),
|
||||
})
|
||||
Ok((tag, try!(Content::deserialize(rest))))
|
||||
}
|
||||
|
||||
fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
|
||||
@@ -869,7 +848,10 @@ mod content {
|
||||
M: MapAccess<'de>,
|
||||
{
|
||||
let mut tag = None;
|
||||
let mut vec = Vec::with_capacity(size_hint::cautious(map.size_hint()));
|
||||
let mut vec = Vec::<(Content, Content)>::with_capacity(size_hint::cautious::<(
|
||||
Content,
|
||||
Content,
|
||||
)>(map.size_hint()));
|
||||
while let Some(k) = try!(map.next_key_seed(TagOrContentVisitor::new(self.tag_name))) {
|
||||
match k {
|
||||
TagOrContent::Tag => {
|
||||
@@ -886,10 +868,7 @@ mod content {
|
||||
}
|
||||
match tag {
|
||||
None => Err(de::Error::missing_field(self.tag_name)),
|
||||
Some(tag) => Ok(TaggedContent {
|
||||
tag: tag,
|
||||
content: Content::Map(vec),
|
||||
}),
|
||||
Some(tag) => Ok((tag, Content::Map(vec))),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -915,7 +894,7 @@ mod content {
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_str(self)
|
||||
deserializer.deserialize_identifier(self)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -926,6 +905,20 @@ mod content {
|
||||
write!(formatter, "{:?} or {:?}", self.tag, self.content)
|
||||
}
|
||||
|
||||
fn visit_u64<E>(self, field_index: u64) -> Result<Self::Value, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
match field_index {
|
||||
0 => Ok(TagOrContentField::Tag),
|
||||
1 => Ok(TagOrContentField::Content),
|
||||
_ => Err(de::Error::invalid_value(
|
||||
Unexpected::Unsigned(field_index),
|
||||
&self,
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, field: &str) -> Result<Self::Value, E>
|
||||
where
|
||||
E: de::Error,
|
||||
@@ -938,6 +931,19 @@ mod content {
|
||||
Err(de::Error::invalid_value(Unexpected::Str(field), &self))
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_bytes<E>(self, field: &[u8]) -> Result<Self::Value, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
if field == self.tag.as_bytes() {
|
||||
Ok(TagOrContentField::Tag)
|
||||
} else if field == self.content.as_bytes() {
|
||||
Ok(TagOrContentField::Content)
|
||||
} else {
|
||||
Err(de::Error::invalid_value(Unexpected::Bytes(field), &self))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Used by generated code to deserialize an adjacently tagged enum when
|
||||
@@ -963,7 +969,7 @@ mod content {
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_str(self)
|
||||
deserializer.deserialize_identifier(self)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -978,13 +984,31 @@ mod content {
|
||||
)
|
||||
}
|
||||
|
||||
fn visit_u64<E>(self, field_index: u64) -> Result<Self::Value, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
match field_index {
|
||||
0 => Ok(TagContentOtherField::Tag),
|
||||
1 => Ok(TagContentOtherField::Content),
|
||||
_ => Ok(TagContentOtherField::Other),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, field: &str) -> Result<Self::Value, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
if field == self.tag {
|
||||
self.visit_bytes(field.as_bytes())
|
||||
}
|
||||
|
||||
fn visit_bytes<E>(self, field: &[u8]) -> Result<Self::Value, E>
|
||||
where
|
||||
E: de::Error,
|
||||
{
|
||||
if field == self.tag.as_bytes() {
|
||||
Ok(TagContentOtherField::Tag)
|
||||
} else if field == self.content {
|
||||
} else if field == self.content.as_bytes() {
|
||||
Ok(TagContentOtherField::Content)
|
||||
} else {
|
||||
Ok(TagContentOtherField::Other)
|
||||
@@ -1262,6 +1286,17 @@ mod content {
|
||||
{
|
||||
match self.content {
|
||||
Content::Unit => visitor.visit_unit(),
|
||||
|
||||
// Allow deserializing newtype variant containing unit.
|
||||
//
|
||||
// #[derive(Deserialize)]
|
||||
// #[serde(tag = "result")]
|
||||
// enum Response<T> {
|
||||
// Success(T),
|
||||
// }
|
||||
//
|
||||
// We want {"result":"Success"} to deserialize into Response<()>.
|
||||
Content::Map(ref v) if v.is_empty() => visitor.visit_unit(),
|
||||
_ => Err(self.invalid_type(&visitor)),
|
||||
}
|
||||
}
|
||||
@@ -1446,7 +1481,7 @@ mod content {
|
||||
/// private API, don't use
|
||||
pub fn new(content: Content<'de>) -> Self {
|
||||
ContentDeserializer {
|
||||
content: content,
|
||||
content,
|
||||
err: PhantomData,
|
||||
}
|
||||
}
|
||||
@@ -1467,8 +1502,8 @@ mod content {
|
||||
{
|
||||
pub fn new(variant: Content<'de>, value: Option<Content<'de>>) -> EnumDeserializer<'de, E> {
|
||||
EnumDeserializer {
|
||||
variant: variant,
|
||||
value: value,
|
||||
variant,
|
||||
value,
|
||||
err: PhantomData,
|
||||
}
|
||||
}
|
||||
@@ -1799,7 +1834,7 @@ mod content {
|
||||
V: Visitor<'de>,
|
||||
E: de::Error,
|
||||
{
|
||||
let map = content.iter().map(|&(ref k, ref v)| {
|
||||
let map = content.iter().map(|(k, v)| {
|
||||
(
|
||||
ContentRefDeserializer::new(k),
|
||||
ContentRefDeserializer::new(v),
|
||||
@@ -2096,7 +2131,7 @@ mod content {
|
||||
let (variant, value) = match *self.content {
|
||||
Content::Map(ref value) => {
|
||||
let mut iter = value.iter();
|
||||
let &(ref variant, ref value) = match iter.next() {
|
||||
let (variant, value) = match iter.next() {
|
||||
Some(v) => v,
|
||||
None => {
|
||||
return Err(de::Error::invalid_value(
|
||||
@@ -2124,8 +2159,8 @@ mod content {
|
||||
};
|
||||
|
||||
visitor.visit_enum(EnumRefDeserializer {
|
||||
variant: variant,
|
||||
value: value,
|
||||
variant,
|
||||
value,
|
||||
err: PhantomData,
|
||||
})
|
||||
}
|
||||
@@ -2169,12 +2204,20 @@ mod content {
|
||||
/// private API, don't use
|
||||
pub fn new(content: &'a Content<'de>) -> Self {
|
||||
ContentRefDeserializer {
|
||||
content: content,
|
||||
content,
|
||||
err: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'de: 'a, E> Copy for ContentRefDeserializer<'a, 'de, E> {}
|
||||
|
||||
impl<'a, 'de: 'a, E> Clone for ContentRefDeserializer<'a, 'de, E> {
|
||||
fn clone(&self) -> Self {
|
||||
*self
|
||||
}
|
||||
}
|
||||
|
||||
struct EnumRefDeserializer<'a, 'de: 'a, E>
|
||||
where
|
||||
E: de::Error,
|
||||
@@ -2243,7 +2286,7 @@ mod content {
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
match self.value {
|
||||
Some(&Content::Seq(ref v)) => {
|
||||
Some(Content::Seq(v)) => {
|
||||
de::Deserializer::deserialize_any(SeqRefDeserializer::new(v), visitor)
|
||||
}
|
||||
Some(other) => Err(de::Error::invalid_type(
|
||||
@@ -2266,10 +2309,10 @@ mod content {
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
match self.value {
|
||||
Some(&Content::Map(ref v)) => {
|
||||
Some(Content::Map(v)) => {
|
||||
de::Deserializer::deserialize_any(MapRefDeserializer::new(v), visitor)
|
||||
}
|
||||
Some(&Content::Seq(ref v)) => {
|
||||
Some(Content::Seq(v)) => {
|
||||
de::Deserializer::deserialize_any(SeqRefDeserializer::new(v), visitor)
|
||||
}
|
||||
Some(other) => Err(de::Error::invalid_type(
|
||||
@@ -2392,7 +2435,7 @@ mod content {
|
||||
T: de::DeserializeSeed<'de>,
|
||||
{
|
||||
match self.iter.next() {
|
||||
Some(&(ref key, ref value)) => {
|
||||
Some((key, value)) => {
|
||||
self.value = Some(value);
|
||||
seed.deserialize(ContentRefDeserializer::new(key)).map(Some)
|
||||
}
|
||||
@@ -2470,8 +2513,8 @@ mod content {
|
||||
/// Not public API.
|
||||
pub fn new(type_name: &'a str, variant_name: &'a str) -> Self {
|
||||
InternallyTaggedUnitVisitor {
|
||||
type_name: type_name,
|
||||
variant_name: variant_name,
|
||||
type_name,
|
||||
variant_name,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2515,8 +2558,8 @@ mod content {
|
||||
/// Not public API.
|
||||
pub fn new(type_name: &'a str, variant_name: &'a str) -> Self {
|
||||
UntaggedUnitVisitor {
|
||||
type_name: type_name,
|
||||
variant_name: variant_name,
|
||||
type_name,
|
||||
variant_name,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2697,7 +2740,7 @@ where
|
||||
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
macro_rules! forward_to_deserialize_other {
|
||||
($($func:ident ( $($arg:ty),* ))*) => {
|
||||
($($func:ident ($($arg:ty),*))*) => {
|
||||
$(
|
||||
fn $func<V>(self, $(_: $arg,)* _visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
@@ -2720,11 +2763,7 @@ where
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
visitor.visit_map(FlatInternallyTaggedAccess {
|
||||
iter: self.0.iter_mut(),
|
||||
pending: None,
|
||||
_marker: PhantomData,
|
||||
})
|
||||
self.deserialize_map(visitor)
|
||||
}
|
||||
|
||||
fn deserialize_enum<V>(
|
||||
@@ -2736,17 +2775,8 @@ where
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
for item in self.0.iter_mut() {
|
||||
// items in the vector are nulled out when used. So we can only use
|
||||
// an item if it's still filled in and if the field is one we care
|
||||
// about.
|
||||
let use_item = match *item {
|
||||
None => false,
|
||||
Some((ref c, _)) => c.as_str().map_or(false, |x| variants.contains(&x)),
|
||||
};
|
||||
|
||||
if use_item {
|
||||
let (key, value) = item.take().unwrap();
|
||||
for entry in self.0 {
|
||||
if let Some((key, value)) = flat_map_take_entry(entry, variants) {
|
||||
return visitor.visit_enum(EnumDeserializer::new(key, Some(value)));
|
||||
}
|
||||
}
|
||||
@@ -2761,7 +2791,11 @@ where
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
visitor.visit_map(FlatMapAccess::new(self.0.iter()))
|
||||
visitor.visit_map(FlatMapAccess {
|
||||
iter: self.0.iter(),
|
||||
pending_content: None,
|
||||
_marker: PhantomData,
|
||||
})
|
||||
}
|
||||
|
||||
fn deserialize_struct<V>(
|
||||
@@ -2773,7 +2807,12 @@ where
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
visitor.visit_map(FlatStructAccess::new(self.0.iter_mut(), fields))
|
||||
visitor.visit_map(FlatStructAccess {
|
||||
iter: self.0.iter_mut(),
|
||||
pending_content: None,
|
||||
fields,
|
||||
_marker: PhantomData,
|
||||
})
|
||||
}
|
||||
|
||||
fn deserialize_newtype_struct<V>(self, _name: &str, visitor: V) -> Result<V::Value, Self::Error>
|
||||
@@ -2800,6 +2839,13 @@ where
|
||||
visitor.visit_unit()
|
||||
}
|
||||
|
||||
fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
visitor.visit_unit()
|
||||
}
|
||||
|
||||
forward_to_deserialize_other! {
|
||||
deserialize_bool()
|
||||
deserialize_i8()
|
||||
@@ -2822,30 +2868,16 @@ where
|
||||
deserialize_tuple(usize)
|
||||
deserialize_tuple_struct(&'static str, usize)
|
||||
deserialize_identifier()
|
||||
deserialize_ignored_any()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
pub struct FlatMapAccess<'a, 'de: 'a, E> {
|
||||
struct FlatMapAccess<'a, 'de: 'a, E> {
|
||||
iter: slice::Iter<'a, Option<(Content<'de>, Content<'de>)>>,
|
||||
pending_content: Option<&'a Content<'de>>,
|
||||
_marker: PhantomData<E>,
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
impl<'a, 'de, E> FlatMapAccess<'a, 'de, E> {
|
||||
fn new(
|
||||
iter: slice::Iter<'a, Option<(Content<'de>, Content<'de>)>>,
|
||||
) -> FlatMapAccess<'a, 'de, E> {
|
||||
FlatMapAccess {
|
||||
iter: iter,
|
||||
pending_content: None,
|
||||
_marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
impl<'a, 'de, E> MapAccess<'de> for FlatMapAccess<'a, 'de, E>
|
||||
where
|
||||
@@ -2860,6 +2892,10 @@ where
|
||||
for item in &mut self.iter {
|
||||
// Items in the vector are nulled out when used by a struct.
|
||||
if let Some((ref key, ref content)) = *item {
|
||||
// Do not take(), instead borrow this entry. The internally tagged
|
||||
// enum does its own buffering so we can't tell whether this entry
|
||||
// is going to be consumed. Borrowing here leaves the entry
|
||||
// available for later flattened fields.
|
||||
self.pending_content = Some(content);
|
||||
return seed.deserialize(ContentRefDeserializer::new(key)).map(Some);
|
||||
}
|
||||
@@ -2879,28 +2915,13 @@ where
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
pub struct FlatStructAccess<'a, 'de: 'a, E> {
|
||||
struct FlatStructAccess<'a, 'de: 'a, E> {
|
||||
iter: slice::IterMut<'a, Option<(Content<'de>, Content<'de>)>>,
|
||||
pending_content: Option<Content<'de>>,
|
||||
fields: &'static [&'static str],
|
||||
_marker: PhantomData<E>,
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
impl<'a, 'de, E> FlatStructAccess<'a, 'de, E> {
|
||||
fn new(
|
||||
iter: slice::IterMut<'a, Option<(Content<'de>, Content<'de>)>>,
|
||||
fields: &'static [&'static str],
|
||||
) -> FlatStructAccess<'a, 'de, E> {
|
||||
FlatStructAccess {
|
||||
iter: iter,
|
||||
pending_content: None,
|
||||
fields: fields,
|
||||
_marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
impl<'a, 'de, E> MapAccess<'de> for FlatStructAccess<'a, 'de, E>
|
||||
where
|
||||
@@ -2912,17 +2933,8 @@ where
|
||||
where
|
||||
T: DeserializeSeed<'de>,
|
||||
{
|
||||
while let Some(item) = self.iter.next() {
|
||||
// items in the vector are nulled out when used. So we can only use
|
||||
// an item if it's still filled in and if the field is one we care
|
||||
// about. In case we do not know which fields we want, we take them all.
|
||||
let use_item = match *item {
|
||||
None => false,
|
||||
Some((ref c, _)) => c.as_str().map_or(false, |key| self.fields.contains(&key)),
|
||||
};
|
||||
|
||||
if use_item {
|
||||
let (key, content) = item.take().unwrap();
|
||||
for entry in self.iter.by_ref() {
|
||||
if let Some((key, content)) = flat_map_take_entry(entry, self.fields) {
|
||||
self.pending_content = Some(content);
|
||||
return seed.deserialize(ContentDeserializer::new(key)).map(Some);
|
||||
}
|
||||
@@ -2941,44 +2953,24 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
/// Claims one key-value pair from a FlatMapDeserializer's field buffer if the
|
||||
/// field name matches any of the recognized ones.
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
pub struct FlatInternallyTaggedAccess<'a, 'de: 'a, E> {
|
||||
iter: slice::IterMut<'a, Option<(Content<'de>, Content<'de>)>>,
|
||||
pending: Option<&'a Content<'de>>,
|
||||
_marker: PhantomData<E>,
|
||||
}
|
||||
fn flat_map_take_entry<'de>(
|
||||
entry: &mut Option<(Content<'de>, Content<'de>)>,
|
||||
recognized: &[&str],
|
||||
) -> Option<(Content<'de>, Content<'de>)> {
|
||||
// Entries in the FlatMapDeserializer buffer are nulled out as they get
|
||||
// claimed for deserialization. We only use an entry if it is still present
|
||||
// and if the field is one recognized by the current data structure.
|
||||
let is_recognized = match entry {
|
||||
None => false,
|
||||
Some((k, _v)) => k.as_str().map_or(false, |name| recognized.contains(&name)),
|
||||
};
|
||||
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
impl<'a, 'de, E> MapAccess<'de> for FlatInternallyTaggedAccess<'a, 'de, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
type Error = E;
|
||||
|
||||
fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
|
||||
where
|
||||
T: DeserializeSeed<'de>,
|
||||
{
|
||||
for item in &mut self.iter {
|
||||
if let Some((ref key, ref content)) = *item {
|
||||
// Do not take(), instead borrow this entry. The internally tagged
|
||||
// enum does its own buffering so we can't tell whether this entry
|
||||
// is going to be consumed. Borrowing here leaves the entry
|
||||
// available for later flattened fields.
|
||||
self.pending = Some(content);
|
||||
return seed.deserialize(ContentRefDeserializer::new(key)).map(Some);
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error>
|
||||
where
|
||||
T: DeserializeSeed<'de>,
|
||||
{
|
||||
match self.pending.take() {
|
||||
Some(value) => seed.deserialize(ContentRefDeserializer::new(value)),
|
||||
None => panic!("value is missing"),
|
||||
}
|
||||
if is_recognized {
|
||||
entry.take()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
+22
-22
@@ -27,10 +27,10 @@ where
|
||||
T: Serialize,
|
||||
{
|
||||
value.serialize(TaggedSerializer {
|
||||
type_ident: type_ident,
|
||||
variant_ident: variant_ident,
|
||||
tag: tag,
|
||||
variant_name: variant_name,
|
||||
type_ident,
|
||||
variant_ident,
|
||||
tag,
|
||||
variant_name,
|
||||
delegate: serializer,
|
||||
})
|
||||
}
|
||||
@@ -51,7 +51,6 @@ enum Unsupported {
|
||||
String,
|
||||
ByteArray,
|
||||
Optional,
|
||||
Unit,
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
UnitStruct,
|
||||
Sequence,
|
||||
@@ -70,7 +69,6 @@ impl Display for Unsupported {
|
||||
Unsupported::String => formatter.write_str("a string"),
|
||||
Unsupported::ByteArray => formatter.write_str("a byte array"),
|
||||
Unsupported::Optional => formatter.write_str("an optional"),
|
||||
Unsupported::Unit => formatter.write_str("unit"),
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
Unsupported::UnitStruct => formatter.write_str("unit struct"),
|
||||
Unsupported::Sequence => formatter.write_str("a sequence"),
|
||||
@@ -184,7 +182,9 @@ where
|
||||
}
|
||||
|
||||
fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
|
||||
Err(self.bad_type(Unsupported::Unit))
|
||||
let mut map = try!(self.delegate.serialize_map(Some(1)));
|
||||
try!(map.serialize_entry(self.tag, self.variant_name));
|
||||
map.end()
|
||||
}
|
||||
|
||||
fn serialize_unit_struct(self, _: &'static str) -> Result<Self::Ok, Self::Error> {
|
||||
@@ -350,8 +350,8 @@ mod content {
|
||||
impl<M> SerializeTupleVariantAsMapValue<M> {
|
||||
pub fn new(map: M, name: &'static str, len: usize) -> Self {
|
||||
SerializeTupleVariantAsMapValue {
|
||||
map: map,
|
||||
name: name,
|
||||
map,
|
||||
name,
|
||||
fields: Vec::with_capacity(len),
|
||||
}
|
||||
}
|
||||
@@ -390,8 +390,8 @@ mod content {
|
||||
impl<M> SerializeStructVariantAsMapValue<M> {
|
||||
pub fn new(map: M, name: &'static str, len: usize) -> Self {
|
||||
SerializeStructVariantAsMapValue {
|
||||
map: map,
|
||||
name: name,
|
||||
map,
|
||||
name,
|
||||
fields: Vec::with_capacity(len),
|
||||
}
|
||||
}
|
||||
@@ -525,7 +525,7 @@ mod content {
|
||||
Content::Map(ref entries) => {
|
||||
use ser::SerializeMap;
|
||||
let mut map = try!(serializer.serialize_map(Some(entries.len())));
|
||||
for &(ref k, ref v) in entries {
|
||||
for (k, v) in entries {
|
||||
try!(map.serialize_entry(k, v));
|
||||
}
|
||||
map.end()
|
||||
@@ -711,7 +711,7 @@ mod content {
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeTupleStruct, E> {
|
||||
Ok(SerializeTupleStruct {
|
||||
name: name,
|
||||
name,
|
||||
fields: Vec::with_capacity(len),
|
||||
error: PhantomData,
|
||||
})
|
||||
@@ -725,9 +725,9 @@ mod content {
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeTupleVariant, E> {
|
||||
Ok(SerializeTupleVariant {
|
||||
name: name,
|
||||
variant_index: variant_index,
|
||||
variant: variant,
|
||||
name,
|
||||
variant_index,
|
||||
variant,
|
||||
fields: Vec::with_capacity(len),
|
||||
error: PhantomData,
|
||||
})
|
||||
@@ -747,7 +747,7 @@ mod content {
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeStruct, E> {
|
||||
Ok(SerializeStruct {
|
||||
name: name,
|
||||
name,
|
||||
fields: Vec::with_capacity(len),
|
||||
error: PhantomData,
|
||||
})
|
||||
@@ -761,9 +761,9 @@ mod content {
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeStructVariant, E> {
|
||||
Ok(SerializeStructVariant {
|
||||
name: name,
|
||||
variant_index: variant_index,
|
||||
variant: variant,
|
||||
name,
|
||||
variant_index,
|
||||
variant,
|
||||
fields: Vec::with_capacity(len),
|
||||
error: PhantomData,
|
||||
})
|
||||
@@ -1273,8 +1273,8 @@ where
|
||||
{
|
||||
fn new(map: &'a mut M, name: &'static str) -> FlatMapSerializeStructVariantAsMapValue<'a, M> {
|
||||
FlatMapSerializeStructVariantAsMapValue {
|
||||
map: map,
|
||||
name: name,
|
||||
map,
|
||||
name,
|
||||
fields: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,9 +8,17 @@ where
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
#[inline]
|
||||
pub fn cautious(hint: Option<usize>) -> usize {
|
||||
cmp::min(hint.unwrap_or(0), 4096)
|
||||
pub fn cautious<Element>(hint: Option<usize>) -> usize {
|
||||
const MAX_PREALLOC_BYTES: usize = 1024 * 1024;
|
||||
|
||||
if mem::size_of::<Element>() == 0 {
|
||||
0
|
||||
} else {
|
||||
cmp::min(
|
||||
hint.unwrap_or(0),
|
||||
MAX_PREALLOC_BYTES / mem::size_of::<Element>(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn helper(bounds: (usize, Option<usize>)) -> Option<usize> {
|
||||
|
||||
@@ -17,8 +17,9 @@ macro_rules! fmt_primitives {
|
||||
};
|
||||
}
|
||||
|
||||
/// ```edition2018
|
||||
/// use serde::Serialize;
|
||||
/// ```edition2021
|
||||
/// use serde::ser::Serialize;
|
||||
/// use serde_derive::Serialize;
|
||||
/// use std::fmt::{self, Display};
|
||||
///
|
||||
/// #[derive(Serialize)]
|
||||
|
||||
+103
-22
@@ -72,7 +72,7 @@ impl<'a> Serialize for fmt::Arguments<'a> {
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg(any(feature = "std", not(no_core_cstr)))]
|
||||
impl Serialize for CStr {
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
@@ -83,7 +83,7 @@ impl Serialize for CStr {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))]
|
||||
impl Serialize for CString {
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
@@ -182,9 +182,27 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
#[cfg(all(any(feature = "std", feature = "alloc"), not(no_relaxed_trait_bounds)))]
|
||||
macro_rules! seq_impl {
|
||||
($ty:ident < T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound:ident)* >) => {
|
||||
($ty:ident <T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound:ident)*>) => {
|
||||
impl<T $(, $typaram)*> Serialize for $ty<T $(, $typaram)*>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.collect_seq(self)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(any(feature = "std", feature = "alloc"), no_relaxed_trait_bounds))]
|
||||
macro_rules! seq_impl {
|
||||
($ty:ident <T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound:ident)*>) => {
|
||||
impl<T $(, $typaram)*> Serialize for $ty<T $(, $typaram)*>
|
||||
where
|
||||
T: Serialize $(+ $tbound1 $(+ $tbound2)*)*,
|
||||
@@ -239,6 +257,23 @@ where
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<Idx> Serialize for RangeFrom<Idx>
|
||||
where
|
||||
Idx: Serialize,
|
||||
{
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
use super::SerializeStruct;
|
||||
let mut state = try!(serializer.serialize_struct("RangeFrom", 1));
|
||||
try!(state.serialize_field("start", &self.start));
|
||||
state.end()
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[cfg(not(no_range_inclusive))]
|
||||
impl<Idx> Serialize for RangeInclusive<Idx>
|
||||
where
|
||||
@@ -258,6 +293,23 @@ where
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<Idx> Serialize for RangeTo<Idx>
|
||||
where
|
||||
Idx: Serialize,
|
||||
{
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
use super::SerializeStruct;
|
||||
let mut state = try!(serializer.serialize_struct("RangeTo", 1));
|
||||
try!(state.serialize_field("end", &self.end));
|
||||
state.end()
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[cfg(any(not(no_ops_bound), all(feature = "std", not(no_collections_bound))))]
|
||||
impl<T> Serialize for Bound<T>
|
||||
where
|
||||
@@ -347,9 +399,28 @@ tuple_impls! {
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
#[cfg(all(any(feature = "std", feature = "alloc"), not(no_relaxed_trait_bounds)))]
|
||||
macro_rules! map_impl {
|
||||
($ty:ident < K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound:ident)* >) => {
|
||||
($ty:ident <K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound:ident)*>) => {
|
||||
impl<K, V $(, $typaram)*> Serialize for $ty<K, V $(, $typaram)*>
|
||||
where
|
||||
K: Serialize,
|
||||
V: Serialize,
|
||||
{
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.collect_map(self)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(any(feature = "std", feature = "alloc"), no_relaxed_trait_bounds))]
|
||||
macro_rules! map_impl {
|
||||
($ty:ident <K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound:ident)*>) => {
|
||||
impl<K, V $(, $typaram)*> Serialize for $ty<K, V $(, $typaram)*>
|
||||
where
|
||||
K: Serialize $(+ $kbound1 $(+ $kbound2)*)*,
|
||||
@@ -465,7 +536,7 @@ where
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
macro_rules! nonzero_integers {
|
||||
( $( $T: ident, )+ ) => {
|
||||
($($T:ident,)+) => {
|
||||
$(
|
||||
#[cfg(not(no_num_nonzero))]
|
||||
impl Serialize for num::$T {
|
||||
@@ -522,7 +593,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Serialize for RefCell<T>
|
||||
impl<T: ?Sized> Serialize for RefCell<T>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
@@ -538,7 +609,7 @@ where
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<T> Serialize for Mutex<T>
|
||||
impl<T: ?Sized> Serialize for Mutex<T>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
@@ -554,7 +625,7 @@ where
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<T> Serialize for RwLock<T>
|
||||
impl<T: ?Sized> Serialize for RwLock<T>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
@@ -614,9 +685,10 @@ impl Serialize for SystemTime {
|
||||
S: Serializer,
|
||||
{
|
||||
use super::SerializeStruct;
|
||||
let duration_since_epoch = self
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map_err(|_| S::Error::custom("SystemTime must be later than UNIX_EPOCH"))?;
|
||||
let duration_since_epoch = match self.duration_since(UNIX_EPOCH) {
|
||||
Ok(duration_since_epoch) => duration_since_epoch,
|
||||
Err(_) => return Err(S::Error::custom("SystemTime must be later than UNIX_EPOCH")),
|
||||
};
|
||||
let mut state = try!(serializer.serialize_struct("SystemTime", 2));
|
||||
try!(state.serialize_field("secs_since_epoch", &duration_since_epoch.as_secs()));
|
||||
try!(state.serialize_field("nanos_since_epoch", &duration_since_epoch.subsec_nanos()));
|
||||
@@ -675,7 +747,7 @@ impl Serialize for net::IpAddr {
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
const DEC_DIGITS_LUT: &'static [u8] = b"\
|
||||
const DEC_DIGITS_LUT: &[u8] = b"\
|
||||
0001020304050607080910111213141516171819\
|
||||
2021222324252627282930313233343536373839\
|
||||
4041424344454647484950515253545556575859\
|
||||
@@ -735,8 +807,9 @@ impl Serialize for net::Ipv4Addr {
|
||||
// Skip over delimiters that we initialized buf with
|
||||
written += format_u8(*oct, &mut buf[written + 1..]) + 1;
|
||||
}
|
||||
// We've only written ASCII bytes to the buffer, so it is valid UTF-8
|
||||
serializer.serialize_str(unsafe { str::from_utf8_unchecked(&buf[..written]) })
|
||||
// Safety: We've only written ASCII bytes to the buffer, so it is valid UTF-8
|
||||
let buf = unsafe { str::from_utf8_unchecked(&buf[..written]) };
|
||||
serializer.serialize_str(buf)
|
||||
} else {
|
||||
self.octets().serialize(serializer)
|
||||
}
|
||||
@@ -908,15 +981,16 @@ where
|
||||
|
||||
#[cfg(all(feature = "std", not(no_std_atomic)))]
|
||||
macro_rules! atomic_impl {
|
||||
($($ty:ident)*) => {
|
||||
($($ty:ident $size:expr)*) => {
|
||||
$(
|
||||
#[cfg(any(no_target_has_atomic, target_has_atomic = $size))]
|
||||
impl Serialize for $ty {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
// Matches the atomic ordering used in libcore for the Debug impl
|
||||
self.load(Ordering::SeqCst).serialize(serializer)
|
||||
self.load(Ordering::Relaxed).serialize(serializer)
|
||||
}
|
||||
}
|
||||
)*
|
||||
@@ -925,12 +999,19 @@ macro_rules! atomic_impl {
|
||||
|
||||
#[cfg(all(feature = "std", not(no_std_atomic)))]
|
||||
atomic_impl! {
|
||||
AtomicBool
|
||||
AtomicI8 AtomicI16 AtomicI32 AtomicIsize
|
||||
AtomicU8 AtomicU16 AtomicU32 AtomicUsize
|
||||
AtomicBool "8"
|
||||
AtomicI8 "8"
|
||||
AtomicI16 "16"
|
||||
AtomicI32 "32"
|
||||
AtomicIsize "ptr"
|
||||
AtomicU8 "8"
|
||||
AtomicU16 "16"
|
||||
AtomicU32 "32"
|
||||
AtomicUsize "ptr"
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "std", not(no_std_atomic64)))]
|
||||
atomic_impl! {
|
||||
AtomicI64 AtomicU64
|
||||
AtomicI64 "64"
|
||||
AtomicU64 "64"
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ use ser::{
|
||||
/// [`SerializeTuple`], [`SerializeTupleStruct`], [`SerializeTupleVariant`],
|
||||
/// [`SerializeMap`], [`SerializeStruct`], and [`SerializeStructVariant`].
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use serde::ser::{Serializer, Impossible};
|
||||
/// # use serde::__private::doc::Error;
|
||||
/// #
|
||||
|
||||
+66
-68
@@ -115,10 +115,13 @@ mod impossible;
|
||||
|
||||
pub use self::impossible::Impossible;
|
||||
|
||||
#[cfg(all(feature = "unstable", not(feature = "std")))]
|
||||
#[doc(inline)]
|
||||
pub use core::error::Error as StdError;
|
||||
#[cfg(feature = "std")]
|
||||
#[doc(no_inline)]
|
||||
pub use std::error::Error as StdError;
|
||||
#[cfg(not(feature = "std"))]
|
||||
#[cfg(not(any(feature = "std", feature = "unstable")))]
|
||||
#[doc(no_inline)]
|
||||
pub use std_error::Error as StdError;
|
||||
|
||||
@@ -146,7 +149,7 @@ macro_rules! declare_error_trait {
|
||||
/// For example, a filesystem [`Path`] may refuse to serialize
|
||||
/// itself if it contains invalid UTF-8 data.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # struct Path;
|
||||
/// #
|
||||
/// # impl Path {
|
||||
@@ -191,8 +194,8 @@ declare_error_trait!(Error: Sized + Debug + Display);
|
||||
/// by Serde.
|
||||
///
|
||||
/// Serde provides `Serialize` implementations for many Rust primitive and
|
||||
/// standard library types. The complete list is [here][ser]. All of these can
|
||||
/// be serialized using Serde out of the box.
|
||||
/// standard library types. The complete list is [here][crate::ser]. All of
|
||||
/// these can be serialized using Serde out of the box.
|
||||
///
|
||||
/// Additionally, Serde provides a procedural macro called [`serde_derive`] to
|
||||
/// automatically generate `Serialize` implementations for structs and enums in
|
||||
@@ -212,14 +215,13 @@ declare_error_trait!(Error: Sized + Debug + Display);
|
||||
/// [`linked-hash-map`]: https://crates.io/crates/linked-hash-map
|
||||
/// [`serde_derive`]: https://crates.io/crates/serde_derive
|
||||
/// [derive section of the manual]: https://serde.rs/derive.html
|
||||
/// [ser]: https://docs.serde.rs/serde/ser/index.html
|
||||
pub trait Serialize {
|
||||
/// Serialize this value into the given Serde serializer.
|
||||
///
|
||||
/// See the [Implementing `Serialize`] section of the manual for more
|
||||
/// information about how to implement this method.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// use serde::ser::{Serialize, SerializeStruct, Serializer};
|
||||
///
|
||||
/// struct Person {
|
||||
@@ -386,7 +388,7 @@ pub trait Serializer: Sized {
|
||||
|
||||
/// Serialize a `bool` value.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use serde::Serializer;
|
||||
/// #
|
||||
/// # serde::__private_serialize!();
|
||||
@@ -408,7 +410,7 @@ pub trait Serializer: Sized {
|
||||
/// reasonable implementation would be to cast the value to `i64` and
|
||||
/// forward to `serialize_i64`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use serde::Serializer;
|
||||
/// #
|
||||
/// # serde::__private_serialize!();
|
||||
@@ -430,7 +432,7 @@ pub trait Serializer: Sized {
|
||||
/// reasonable implementation would be to cast the value to `i64` and
|
||||
/// forward to `serialize_i64`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use serde::Serializer;
|
||||
/// #
|
||||
/// # serde::__private_serialize!();
|
||||
@@ -452,7 +454,7 @@ pub trait Serializer: Sized {
|
||||
/// reasonable implementation would be to cast the value to `i64` and
|
||||
/// forward to `serialize_i64`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use serde::Serializer;
|
||||
/// #
|
||||
/// # serde::__private_serialize!();
|
||||
@@ -470,7 +472,7 @@ pub trait Serializer: Sized {
|
||||
|
||||
/// Serialize an `i64` value.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use serde::Serializer;
|
||||
/// #
|
||||
/// # serde::__private_serialize!();
|
||||
@@ -489,7 +491,7 @@ pub trait Serializer: Sized {
|
||||
serde_if_integer128! {
|
||||
/// Serialize an `i128` value.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use serde::Serializer;
|
||||
/// #
|
||||
/// # serde::__private_serialize!();
|
||||
@@ -518,7 +520,7 @@ pub trait Serializer: Sized {
|
||||
/// reasonable implementation would be to cast the value to `u64` and
|
||||
/// forward to `serialize_u64`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use serde::Serializer;
|
||||
/// #
|
||||
/// # serde::__private_serialize!();
|
||||
@@ -540,7 +542,7 @@ pub trait Serializer: Sized {
|
||||
/// reasonable implementation would be to cast the value to `u64` and
|
||||
/// forward to `serialize_u64`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use serde::Serializer;
|
||||
/// #
|
||||
/// # serde::__private_serialize!();
|
||||
@@ -562,7 +564,7 @@ pub trait Serializer: Sized {
|
||||
/// reasonable implementation would be to cast the value to `u64` and
|
||||
/// forward to `serialize_u64`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use serde::Serializer;
|
||||
/// #
|
||||
/// # serde::__private_serialize!();
|
||||
@@ -580,7 +582,7 @@ pub trait Serializer: Sized {
|
||||
|
||||
/// Serialize a `u64` value.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use serde::Serializer;
|
||||
/// #
|
||||
/// # serde::__private_serialize!();
|
||||
@@ -599,7 +601,7 @@ pub trait Serializer: Sized {
|
||||
serde_if_integer128! {
|
||||
/// Serialize a `u128` value.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use serde::Serializer;
|
||||
/// #
|
||||
/// # serde::__private_serialize!();
|
||||
@@ -628,7 +630,7 @@ pub trait Serializer: Sized {
|
||||
/// reasonable implementation would be to cast the value to `f64` and
|
||||
/// forward to `serialize_f64`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use serde::Serializer;
|
||||
/// #
|
||||
/// # serde::__private_serialize!();
|
||||
@@ -646,7 +648,7 @@ pub trait Serializer: Sized {
|
||||
|
||||
/// Serialize an `f64` value.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use serde::Serializer;
|
||||
/// #
|
||||
/// # serde::__private_serialize!();
|
||||
@@ -667,7 +669,7 @@ pub trait Serializer: Sized {
|
||||
/// If the format does not support characters, it is reasonable to serialize
|
||||
/// it as a single element `str` or a `u32`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use serde::Serializer;
|
||||
/// #
|
||||
/// # serde::__private_serialize!();
|
||||
@@ -685,7 +687,7 @@ pub trait Serializer: Sized {
|
||||
|
||||
/// Serialize a `&str`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use serde::Serializer;
|
||||
/// #
|
||||
/// # serde::__private_serialize!();
|
||||
@@ -709,7 +711,7 @@ pub trait Serializer: Sized {
|
||||
/// `serialize_seq`. If forwarded, the implementation looks usually just
|
||||
/// like this:
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use serde::ser::{Serializer, SerializeSeq};
|
||||
/// # use serde::__private::doc::Error;
|
||||
/// #
|
||||
@@ -738,7 +740,7 @@ pub trait Serializer: Sized {
|
||||
|
||||
/// Serialize a [`None`] value.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use serde::{Serialize, Serializer};
|
||||
/// #
|
||||
/// # enum Option<T> {
|
||||
@@ -771,7 +773,7 @@ pub trait Serializer: Sized {
|
||||
|
||||
/// Serialize a [`Some(T)`] value.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use serde::{Serialize, Serializer};
|
||||
/// #
|
||||
/// # enum Option<T> {
|
||||
@@ -806,7 +808,7 @@ pub trait Serializer: Sized {
|
||||
|
||||
/// Serialize a `()` value.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use serde::Serializer;
|
||||
/// #
|
||||
/// # serde::__private_serialize!();
|
||||
@@ -826,7 +828,7 @@ pub trait Serializer: Sized {
|
||||
///
|
||||
/// A reasonable implementation would be to forward to `serialize_unit`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// use serde::{Serialize, Serializer};
|
||||
///
|
||||
/// struct Nothing;
|
||||
@@ -848,7 +850,7 @@ pub trait Serializer: Sized {
|
||||
/// this variant within the enum, and the `variant` is the name of the
|
||||
/// variant.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// use serde::{Serialize, Serializer};
|
||||
///
|
||||
/// enum E {
|
||||
@@ -881,7 +883,7 @@ pub trait Serializer: Sized {
|
||||
/// wrappers around the data they contain. A reasonable implementation would
|
||||
/// be to forward to `value.serialize(self)`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// use serde::{Serialize, Serializer};
|
||||
///
|
||||
/// struct Millimeters(u8);
|
||||
@@ -909,7 +911,7 @@ pub trait Serializer: Sized {
|
||||
/// this variant within the enum, and the `variant` is the name of the
|
||||
/// variant. The `value` is the data contained within this newtype variant.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// use serde::{Serialize, Serializer};
|
||||
///
|
||||
/// enum E {
|
||||
@@ -947,7 +949,7 @@ pub trait Serializer: Sized {
|
||||
/// not be computable before the sequence is iterated. Some serializers only
|
||||
/// support sequences whose length is known up front.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use std::marker::PhantomData;
|
||||
/// #
|
||||
/// # struct Vec<T>(PhantomData<T>);
|
||||
@@ -960,14 +962,14 @@ pub trait Serializer: Sized {
|
||||
/// #
|
||||
/// # impl<'a, T> IntoIterator for &'a Vec<T> {
|
||||
/// # type Item = &'a T;
|
||||
/// # type IntoIter = Box<Iterator<Item = &'a T>>;
|
||||
/// # type IntoIter = Box<dyn Iterator<Item = &'a T>>;
|
||||
/// #
|
||||
/// # fn into_iter(self) -> Self::IntoIter {
|
||||
/// # unimplemented!()
|
||||
/// # }
|
||||
/// # }
|
||||
/// #
|
||||
/// use serde::ser::{Serialize, Serializer, SerializeSeq};
|
||||
/// use serde::ser::{Serialize, SerializeSeq, Serializer};
|
||||
///
|
||||
/// impl<T> Serialize for Vec<T>
|
||||
/// where
|
||||
@@ -992,8 +994,8 @@ pub trait Serializer: Sized {
|
||||
/// This call must be followed by zero or more calls to `serialize_element`,
|
||||
/// then a call to `end`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// use serde::ser::{Serialize, Serializer, SerializeTuple};
|
||||
/// ```edition2021
|
||||
/// use serde::ser::{Serialize, SerializeTuple, Serializer};
|
||||
///
|
||||
/// # mod fool {
|
||||
/// # trait Serialize {}
|
||||
@@ -1022,7 +1024,7 @@ pub trait Serializer: Sized {
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// use serde::ser::{Serialize, SerializeTuple, Serializer};
|
||||
///
|
||||
/// const VRAM_SIZE: usize = 386;
|
||||
@@ -1050,7 +1052,7 @@ pub trait Serializer: Sized {
|
||||
/// The `name` is the name of the tuple struct and the `len` is the number
|
||||
/// of data fields that will be serialized.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// use serde::ser::{Serialize, SerializeTupleStruct, Serializer};
|
||||
///
|
||||
/// struct Rgb(u8, u8, u8);
|
||||
@@ -1082,7 +1084,7 @@ pub trait Serializer: Sized {
|
||||
/// this variant within the enum, the `variant` is the name of the variant,
|
||||
/// and the `len` is the number of data fields that will be serialized.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// use serde::ser::{Serialize, SerializeTupleVariant, Serializer};
|
||||
///
|
||||
/// enum E {
|
||||
@@ -1128,7 +1130,7 @@ pub trait Serializer: Sized {
|
||||
/// be computable before the map is iterated. Some serializers only support
|
||||
/// maps whose length is known up front.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use std::marker::PhantomData;
|
||||
/// #
|
||||
/// # struct HashMap<K, V>(PhantomData<K>, PhantomData<V>);
|
||||
@@ -1141,14 +1143,14 @@ pub trait Serializer: Sized {
|
||||
/// #
|
||||
/// # impl<'a, K, V> IntoIterator for &'a HashMap<K, V> {
|
||||
/// # type Item = (&'a K, &'a V);
|
||||
/// # type IntoIter = Box<Iterator<Item = (&'a K, &'a V)>>;
|
||||
/// # type IntoIter = Box<dyn Iterator<Item = (&'a K, &'a V)>>;
|
||||
/// #
|
||||
/// # fn into_iter(self) -> Self::IntoIter {
|
||||
/// # unimplemented!()
|
||||
/// # }
|
||||
/// # }
|
||||
/// #
|
||||
/// use serde::ser::{Serialize, Serializer, SerializeMap};
|
||||
/// use serde::ser::{Serialize, SerializeMap, Serializer};
|
||||
///
|
||||
/// impl<K, V> Serialize for HashMap<K, V>
|
||||
/// where
|
||||
@@ -1176,7 +1178,7 @@ pub trait Serializer: Sized {
|
||||
/// The `name` is the name of the struct and the `len` is the number of
|
||||
/// data fields that will be serialized.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// use serde::ser::{Serialize, SerializeStruct, Serializer};
|
||||
///
|
||||
/// struct Rgb {
|
||||
@@ -1212,7 +1214,7 @@ pub trait Serializer: Sized {
|
||||
/// this variant within the enum, the `variant` is the name of the variant,
|
||||
/// and the `len` is the number of data fields that will be serialized.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// use serde::ser::{Serialize, SerializeStructVariant, Serializer};
|
||||
///
|
||||
/// enum E {
|
||||
@@ -1254,7 +1256,7 @@ pub trait Serializer: Sized {
|
||||
/// using [`serialize_seq`]. Implementors should not need to override this
|
||||
/// method.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// use serde::{Serialize, Serializer};
|
||||
///
|
||||
/// struct SecretlyOneHigher {
|
||||
@@ -1302,7 +1304,7 @@ pub trait Serializer: Sized {
|
||||
/// using [`serialize_map`]. Implementors should not need to override this
|
||||
/// method.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// use serde::{Serialize, Serializer};
|
||||
/// use std::collections::BTreeSet;
|
||||
///
|
||||
@@ -1353,7 +1355,7 @@ pub trait Serializer: Sized {
|
||||
/// delegates to [`serialize_str`]. Serializers are encouraged to provide a
|
||||
/// more efficient implementation if possible.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # struct DateTime;
|
||||
/// #
|
||||
/// # impl DateTime {
|
||||
@@ -1368,9 +1370,7 @@ pub trait Serializer: Sized {
|
||||
/// where
|
||||
/// S: Serializer,
|
||||
/// {
|
||||
/// serializer.collect_str(&format_args!("{:?}{:?}",
|
||||
/// self.naive_local(),
|
||||
/// self.offset()))
|
||||
/// serializer.collect_str(&format_args!("{:?}{:?}", self.naive_local(), self.offset()))
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
@@ -1391,7 +1391,7 @@ pub trait Serializer: Sized {
|
||||
/// of this method. If no more sensible behavior is possible, the
|
||||
/// implementation is expected to return an error.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # struct DateTime;
|
||||
/// #
|
||||
/// # impl DateTime {
|
||||
@@ -1406,9 +1406,7 @@ pub trait Serializer: Sized {
|
||||
/// where
|
||||
/// S: Serializer,
|
||||
/// {
|
||||
/// serializer.collect_str(&format_args!("{:?}{:?}",
|
||||
/// self.naive_local(),
|
||||
/// self.offset()))
|
||||
/// serializer.collect_str(&format_args!("{:?}{:?}", self.naive_local(), self.offset()))
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
@@ -1426,7 +1424,7 @@ pub trait Serializer: Sized {
|
||||
/// human-readable one and binary formats like Postcard will prefer the
|
||||
/// compact one.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use std::fmt::{self, Display};
|
||||
/// #
|
||||
/// # struct Timestamp;
|
||||
@@ -1475,7 +1473,7 @@ pub trait Serializer: Sized {
|
||||
///
|
||||
/// # Example use
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use std::marker::PhantomData;
|
||||
/// #
|
||||
/// # struct Vec<T>(PhantomData<T>);
|
||||
@@ -1488,13 +1486,13 @@ pub trait Serializer: Sized {
|
||||
/// #
|
||||
/// # impl<'a, T> IntoIterator for &'a Vec<T> {
|
||||
/// # type Item = &'a T;
|
||||
/// # type IntoIter = Box<Iterator<Item = &'a T>>;
|
||||
/// # type IntoIter = Box<dyn Iterator<Item = &'a T>>;
|
||||
/// # fn into_iter(self) -> Self::IntoIter {
|
||||
/// # unimplemented!()
|
||||
/// # }
|
||||
/// # }
|
||||
/// #
|
||||
/// use serde::ser::{Serialize, Serializer, SerializeSeq};
|
||||
/// use serde::ser::{Serialize, SerializeSeq, Serializer};
|
||||
///
|
||||
/// impl<T> Serialize for Vec<T>
|
||||
/// where
|
||||
@@ -1539,8 +1537,8 @@ pub trait SerializeSeq {
|
||||
///
|
||||
/// # Example use
|
||||
///
|
||||
/// ```edition2018
|
||||
/// use serde::ser::{Serialize, Serializer, SerializeTuple};
|
||||
/// ```edition2021
|
||||
/// use serde::ser::{Serialize, SerializeTuple, Serializer};
|
||||
///
|
||||
/// # mod fool {
|
||||
/// # trait Serialize {}
|
||||
@@ -1569,7 +1567,7 @@ pub trait SerializeSeq {
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use std::marker::PhantomData;
|
||||
/// #
|
||||
/// # struct Array<T>(PhantomData<T>);
|
||||
@@ -1582,13 +1580,13 @@ pub trait SerializeSeq {
|
||||
/// #
|
||||
/// # impl<'a, T> IntoIterator for &'a Array<T> {
|
||||
/// # type Item = &'a T;
|
||||
/// # type IntoIter = Box<Iterator<Item = &'a T>>;
|
||||
/// # type IntoIter = Box<dyn Iterator<Item = &'a T>>;
|
||||
/// # fn into_iter(self) -> Self::IntoIter {
|
||||
/// # unimplemented!()
|
||||
/// # }
|
||||
/// # }
|
||||
/// #
|
||||
/// use serde::ser::{Serialize, Serializer, SerializeTuple};
|
||||
/// use serde::ser::{Serialize, SerializeTuple, Serializer};
|
||||
///
|
||||
/// # mod fool {
|
||||
/// # trait Serialize {}
|
||||
@@ -1639,7 +1637,7 @@ pub trait SerializeTuple {
|
||||
///
|
||||
/// # Example use
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// use serde::ser::{Serialize, SerializeTupleStruct, Serializer};
|
||||
///
|
||||
/// struct Rgb(u8, u8, u8);
|
||||
@@ -1684,7 +1682,7 @@ pub trait SerializeTupleStruct {
|
||||
///
|
||||
/// # Example use
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// use serde::ser::{Serialize, SerializeTupleVariant, Serializer};
|
||||
///
|
||||
/// enum E {
|
||||
@@ -1742,7 +1740,7 @@ pub trait SerializeTupleVariant {
|
||||
///
|
||||
/// # Example use
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// # use std::marker::PhantomData;
|
||||
/// #
|
||||
/// # struct HashMap<K, V>(PhantomData<K>, PhantomData<V>);
|
||||
@@ -1755,14 +1753,14 @@ pub trait SerializeTupleVariant {
|
||||
/// #
|
||||
/// # impl<'a, K, V> IntoIterator for &'a HashMap<K, V> {
|
||||
/// # type Item = (&'a K, &'a V);
|
||||
/// # type IntoIter = Box<Iterator<Item = (&'a K, &'a V)>>;
|
||||
/// # type IntoIter = Box<dyn Iterator<Item = (&'a K, &'a V)>>;
|
||||
/// #
|
||||
/// # fn into_iter(self) -> Self::IntoIter {
|
||||
/// # unimplemented!()
|
||||
/// # }
|
||||
/// # }
|
||||
/// #
|
||||
/// use serde::ser::{Serialize, Serializer, SerializeMap};
|
||||
/// use serde::ser::{Serialize, SerializeMap, Serializer};
|
||||
///
|
||||
/// impl<K, V> Serialize for HashMap<K, V>
|
||||
/// where
|
||||
@@ -1853,7 +1851,7 @@ pub trait SerializeMap {
|
||||
///
|
||||
/// # Example use
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// use serde::ser::{Serialize, SerializeStruct, Serializer};
|
||||
///
|
||||
/// struct Rgb {
|
||||
@@ -1913,7 +1911,7 @@ pub trait SerializeStruct {
|
||||
///
|
||||
/// # Example use
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// use serde::ser::{Serialize, SerializeStructVariant, Serializer};
|
||||
///
|
||||
/// enum E {
|
||||
|
||||
@@ -9,7 +9,7 @@ use lib::{Debug, Display};
|
||||
/// generally provide their error types with a `std::error::Error` impl
|
||||
/// directly:
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// #[derive(Debug)]
|
||||
/// struct MySerError {...}
|
||||
///
|
||||
@@ -29,7 +29,7 @@ use lib::{Debug, Display};
|
||||
/// std = ["serde/std"]
|
||||
/// ```
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// #[cfg(feature = "std")]
|
||||
/// impl std::error::Error for MySerError {}
|
||||
/// ```
|
||||
@@ -37,7 +37,7 @@ use lib::{Debug, Display};
|
||||
/// ... or else provide the std Error impl unconditionally via Serde's
|
||||
/// re-export:
|
||||
///
|
||||
/// ```edition2018
|
||||
/// ```edition2021
|
||||
/// impl serde::ser::StdError for MySerError {}
|
||||
/// ```
|
||||
pub trait Error: Debug + Display {
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
msrv = "1.31.0"
|
||||
@@ -1,17 +1,16 @@
|
||||
[package]
|
||||
name = "serde_derive"
|
||||
version = "1.0.143" # remember to update html_root_url
|
||||
version = "1.0.177" # remember to update html_root_url
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
||||
categories = ["no-std"]
|
||||
categories = ["no-std", "no-std::no-alloc"]
|
||||
description = "Macros 1.1 implementation of #[derive(Serialize, Deserialize)]"
|
||||
documentation = "https://serde.rs/derive.html"
|
||||
homepage = "https://serde.rs"
|
||||
include = ["build.rs", "src/**/*.rs", "crates-io.md", "README.md", "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.31"
|
||||
rust-version = "1.56"
|
||||
|
||||
[features]
|
||||
default = []
|
||||
@@ -24,10 +23,11 @@ proc-macro = true
|
||||
[dependencies]
|
||||
proc-macro2 = "1.0"
|
||||
quote = "1.0"
|
||||
syn = "1.0.90"
|
||||
syn = "2.0.25"
|
||||
|
||||
[dev-dependencies]
|
||||
serde = { version = "1.0", path = "../serde" }
|
||||
serde = { version = "1", path = "../serde" }
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
||||
rustdoc-args = ["--generate-link-to-definition"]
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
use std::env;
|
||||
use std::process::Command;
|
||||
use std::str;
|
||||
|
||||
// The rustc-cfg strings below are *not* public API. Please let us know by
|
||||
// opening a GitHub issue if your build environment requires some way to enable
|
||||
// these cfgs other than by executing our build script.
|
||||
fn main() {
|
||||
let minor = match rustc_minor_version() {
|
||||
Some(minor) => minor,
|
||||
None => return,
|
||||
};
|
||||
|
||||
// Underscore const names stabilized in Rust 1.37:
|
||||
// https://blog.rust-lang.org/2019/08/15/Rust-1.37.0.html#using-unnamed-const-items-for-macros
|
||||
if minor < 37 {
|
||||
println!("cargo:rustc-cfg=no_underscore_consts");
|
||||
}
|
||||
|
||||
// The ptr::addr_of! macro stabilized in Rust 1.51:
|
||||
// https://blog.rust-lang.org/2021/03/25/Rust-1.51.0.html#stabilized-apis
|
||||
if minor < 51 {
|
||||
println!("cargo:rustc-cfg=no_ptr_addr_of");
|
||||
}
|
||||
}
|
||||
|
||||
fn rustc_minor_version() -> Option<u32> {
|
||||
let rustc = env::var_os("RUSTC")?;
|
||||
let output = Command::new(rustc).arg("--version").output().ok()?;
|
||||
let version = str::from_utf8(&output.stdout).ok()?;
|
||||
let mut pieces = version.split('.');
|
||||
if pieces.next() != Some("rustc 1") {
|
||||
return None;
|
||||
}
|
||||
pieces.next()?.parse().ok()
|
||||
}
|
||||
+19
-14
@@ -1,12 +1,9 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
use syn;
|
||||
use syn::punctuated::{Pair, Punctuated};
|
||||
|
||||
use internals::ast::{Container, Data};
|
||||
use internals::{attr, ungroup};
|
||||
|
||||
use crate::internals::ast::{Container, Data};
|
||||
use crate::internals::{attr, ungroup};
|
||||
use proc_macro2::Span;
|
||||
use std::collections::HashSet;
|
||||
use syn::punctuated::{Pair, Punctuated};
|
||||
use syn::Token;
|
||||
|
||||
// Remove the default from every type parameter because in the generated impls
|
||||
// they look like associated types: "error: associated type bindings are not
|
||||
@@ -200,10 +197,16 @@ pub fn with_bound(
|
||||
for arg in &arguments.args {
|
||||
match arg {
|
||||
syn::GenericArgument::Type(arg) => self.visit_type(arg),
|
||||
syn::GenericArgument::Binding(arg) => self.visit_type(&arg.ty),
|
||||
syn::GenericArgument::AssocType(arg) => self.visit_type(&arg.ty),
|
||||
syn::GenericArgument::Lifetime(_)
|
||||
| syn::GenericArgument::Constraint(_)
|
||||
| syn::GenericArgument::Const(_) => {}
|
||||
| syn::GenericArgument::Const(_)
|
||||
| syn::GenericArgument::AssocConst(_)
|
||||
| syn::GenericArgument::Constraint(_) => {}
|
||||
#[cfg_attr(
|
||||
all(test, exhaustive),
|
||||
deny(non_exhaustive_omitted_patterns)
|
||||
)]
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -226,7 +229,9 @@ pub fn with_bound(
|
||||
fn visit_type_param_bound(&mut self, bound: &'ast syn::TypeParamBound) {
|
||||
match bound {
|
||||
syn::TypeParamBound::Trait(bound) => self.visit_path(&bound.path),
|
||||
syn::TypeParamBound::Lifetime(_) => {}
|
||||
syn::TypeParamBound::Lifetime(_) | syn::TypeParamBound::Verbatim(_) => {}
|
||||
#[cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -251,7 +256,7 @@ pub fn with_bound(
|
||||
};
|
||||
match &cont.data {
|
||||
Data::Enum(variants) => {
|
||||
for variant in variants.iter() {
|
||||
for variant in variants {
|
||||
let relevant_fields = variant
|
||||
.fields
|
||||
.iter()
|
||||
@@ -334,7 +339,7 @@ pub fn with_self_bound(
|
||||
|
||||
pub fn with_lifetime_bound(generics: &syn::Generics, lifetime: &str) -> syn::Generics {
|
||||
let bound = syn::Lifetime::new(lifetime, Span::call_site());
|
||||
let def = syn::LifetimeDef {
|
||||
let def = syn::LifetimeParam {
|
||||
attrs: Vec::new(),
|
||||
lifetime: bound.clone(),
|
||||
colon_token: None,
|
||||
|
||||
+406
-348
File diff suppressed because it is too large
Load Diff
@@ -1,23 +1,10 @@
|
||||
use proc_macro2::{Ident, TokenStream};
|
||||
use quote::format_ident;
|
||||
use crate::try;
|
||||
use proc_macro2::TokenStream;
|
||||
use quote::quote;
|
||||
|
||||
use syn;
|
||||
use try;
|
||||
|
||||
pub fn wrap_in_const(
|
||||
serde_path: Option<&syn::Path>,
|
||||
trait_: &str,
|
||||
ty: &Ident,
|
||||
code: TokenStream,
|
||||
) -> TokenStream {
|
||||
pub fn wrap_in_const(serde_path: Option<&syn::Path>, code: TokenStream) -> TokenStream {
|
||||
let try_replacement = try::replacement();
|
||||
|
||||
let dummy_const = if cfg!(no_underscore_consts) {
|
||||
format_ident!("_IMPL_{}_FOR_{}", trait_, unraw(ty))
|
||||
} else {
|
||||
format_ident!("_")
|
||||
};
|
||||
|
||||
let use_serde = match serde_path {
|
||||
Some(path) => quote! {
|
||||
use #path as _serde;
|
||||
@@ -31,18 +18,10 @@ pub fn wrap_in_const(
|
||||
quote! {
|
||||
#[doc(hidden)]
|
||||
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
|
||||
const #dummy_const: () = {
|
||||
const _: () = {
|
||||
#use_serde
|
||||
#try_replacement
|
||||
#code
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(deprecated)]
|
||||
fn unraw(ident: &Ident) -> String {
|
||||
// str::trim_start_matches was added in 1.30, trim_left_matches deprecated
|
||||
// in 1.33. We currently support rustc back to 1.15 so we need to continue
|
||||
// to use the deprecated one.
|
||||
ident.to_string().trim_left_matches("r#").to_owned()
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use proc_macro2::TokenStream;
|
||||
use quote::ToTokens;
|
||||
use syn::token;
|
||||
use syn::{token, Token};
|
||||
|
||||
pub enum Fragment {
|
||||
/// Tokens that can be used as an expression.
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
//! A Serde ast, parsed from the Syn ast and ready to generate Rust code.
|
||||
|
||||
use internals::attr;
|
||||
use internals::check;
|
||||
use internals::{Ctxt, Derive};
|
||||
use syn;
|
||||
use crate::internals::{attr, check, Ctxt, Derive};
|
||||
use syn::punctuated::Punctuated;
|
||||
use syn::Token;
|
||||
|
||||
/// A source data structure annotated with `#[derive(Serialize)]` and/or `#[derive(Deserialize)]`,
|
||||
/// parsed into an internal representation.
|
||||
@@ -88,9 +86,12 @@ impl<'a> Container<'a> {
|
||||
if field.attrs.flatten() {
|
||||
has_flatten = true;
|
||||
}
|
||||
field
|
||||
.attrs
|
||||
.rename_by_rules(variant.attrs.rename_all_rules());
|
||||
field.attrs.rename_by_rules(
|
||||
variant
|
||||
.attrs
|
||||
.rename_all_rules()
|
||||
.or(attrs.rename_all_fields_rules()),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -121,7 +122,7 @@ impl<'a> Container<'a> {
|
||||
}
|
||||
|
||||
impl<'a> Data<'a> {
|
||||
pub fn all_fields(&'a self) -> Box<Iterator<Item = &'a Field<'a>> + 'a> {
|
||||
pub fn all_fields(&'a self) -> Box<dyn Iterator<Item = &'a Field<'a>> + 'a> {
|
||||
match self {
|
||||
Data::Enum(variants) => {
|
||||
Box::new(variants.iter().flat_map(|variant| variant.fields.iter()))
|
||||
@@ -140,7 +141,7 @@ fn enum_from_ast<'a>(
|
||||
variants: &'a Punctuated<syn::Variant, Token![,]>,
|
||||
container_default: &attr::Default,
|
||||
) -> Vec<Variant<'a>> {
|
||||
variants
|
||||
let variants: Vec<Variant> = variants
|
||||
.iter()
|
||||
.map(|variant| {
|
||||
let attrs = attr::Variant::from_ast(cx, variant);
|
||||
@@ -154,7 +155,20 @@ fn enum_from_ast<'a>(
|
||||
original: variant,
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
.collect();
|
||||
|
||||
let index_of_last_tagged_variant = variants
|
||||
.iter()
|
||||
.rposition(|variant| !variant.attrs.untagged());
|
||||
if let Some(index_of_last_tagged_variant) = index_of_last_tagged_variant {
|
||||
for variant in &variants[..index_of_last_tagged_variant] {
|
||||
if variant.attrs.untagged() {
|
||||
cx.error_spanned_by(&variant.ident, "all variants with the #[serde(untagged)] attribute must be placed at the end of the enum");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
variants
|
||||
}
|
||||
|
||||
fn struct_from_ast<'a>(
|
||||
|
||||
+661
-741
File diff suppressed because it is too large
Load Diff
@@ -1,13 +1,8 @@
|
||||
//! Code to convert the Rust-styled field/variant (e.g. `my_field`, `MyType`) to the
|
||||
//! case of the source (e.g. `my-field`, `MY_FIELD`).
|
||||
|
||||
// See https://users.rust-lang.org/t/psa-dealing-with-warning-unused-import-std-ascii-asciiext-in-today-s-nightly/13726
|
||||
#[allow(deprecated, unused_imports)]
|
||||
use std::ascii::AsciiExt;
|
||||
|
||||
use std::fmt::{self, Debug, Display};
|
||||
|
||||
use self::RenameRule::*;
|
||||
use std::fmt::{self, Debug, Display};
|
||||
|
||||
/// The different possible ways to change case of fields in a struct, or variants in an enum.
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
@@ -59,8 +54,8 @@ impl RenameRule {
|
||||
}
|
||||
|
||||
/// Apply a renaming rule to an enum variant, returning the version expected in the source.
|
||||
pub fn apply_to_variant(&self, variant: &str) -> String {
|
||||
match *self {
|
||||
pub fn apply_to_variant(self, variant: &str) -> String {
|
||||
match self {
|
||||
None | PascalCase => variant.to_owned(),
|
||||
LowerCase => variant.to_ascii_lowercase(),
|
||||
UpperCase => variant.to_ascii_uppercase(),
|
||||
@@ -84,8 +79,8 @@ impl RenameRule {
|
||||
}
|
||||
|
||||
/// Apply a renaming rule to a struct field, returning the version expected in the source.
|
||||
pub fn apply_to_field(&self, field: &str) -> String {
|
||||
match *self {
|
||||
pub fn apply_to_field(self, field: &str) -> String {
|
||||
match self {
|
||||
None | LowerCase | SnakeCase => field.to_owned(),
|
||||
UpperCase => field.to_ascii_uppercase(),
|
||||
PascalCase => {
|
||||
@@ -112,6 +107,14 @@ impl RenameRule {
|
||||
ScreamingKebabCase => ScreamingSnakeCase.apply_to_field(field).replace('_', "-"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the `RenameRule` if it is not `None`, `rule_b` otherwise.
|
||||
pub fn or(self, rule_b: Self) -> Self {
|
||||
match self {
|
||||
None => rule_b,
|
||||
_ => self,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ParseError<'a> {
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
use internals::ast::{Container, Data, Field, Style};
|
||||
use internals::attr::{Identifier, TagType};
|
||||
use internals::{ungroup, Ctxt, Derive};
|
||||
use crate::internals::ast::{Container, Data, Field, Style};
|
||||
use crate::internals::attr::{Identifier, TagType};
|
||||
use crate::internals::{ungroup, Ctxt, Derive};
|
||||
use syn::{Member, Type};
|
||||
|
||||
/// Cross-cutting checks that require looking at more than a single attrs
|
||||
/// object. Simpler checks should happen when parsing and building the attrs.
|
||||
// Cross-cutting checks that require looking at more than a single attrs object.
|
||||
// Simpler checks should happen when parsing and building the attrs.
|
||||
pub fn check(cx: &Ctxt, cont: &mut Container, derive: Derive) {
|
||||
check_remote_generic(cx, cont);
|
||||
check_getter(cx, cont);
|
||||
check_flatten(cx, cont);
|
||||
check_identifier(cx, cont);
|
||||
@@ -16,8 +17,30 @@ pub fn check(cx: &Ctxt, cont: &mut Container, derive: Derive) {
|
||||
check_from_and_try_from(cx, cont);
|
||||
}
|
||||
|
||||
/// Getters are only allowed inside structs (not enums) with the `remote`
|
||||
/// attribute.
|
||||
// Remote derive definition type must have either all of the generics of the
|
||||
// remote type:
|
||||
//
|
||||
// #[serde(remote = "Generic")]
|
||||
// struct Generic<T> {…}
|
||||
//
|
||||
// or none of them, i.e. defining impls for one concrete instantiation of the
|
||||
// remote type only:
|
||||
//
|
||||
// #[serde(remote = "Generic<T>")]
|
||||
// struct ConcreteDef {…}
|
||||
//
|
||||
fn check_remote_generic(cx: &Ctxt, cont: &Container) {
|
||||
if let Some(remote) = cont.attrs.remote() {
|
||||
let local_has_generic = !cont.generics.params.is_empty();
|
||||
let remote_has_generic = !remote.segments.last().unwrap().arguments.is_none();
|
||||
if local_has_generic && remote_has_generic {
|
||||
cx.error_spanned_by(remote, "remove generic parameters from this path");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Getters are only allowed inside structs (not enums) with the `remote`
|
||||
// attribute.
|
||||
fn check_getter(cx: &Ctxt, cont: &Container) {
|
||||
match cont.data {
|
||||
Data::Enum(_) => {
|
||||
@@ -39,7 +62,7 @@ fn check_getter(cx: &Ctxt, cont: &Container) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Flattening has some restrictions we can test.
|
||||
// Flattening has some restrictions we can test.
|
||||
fn check_flatten(cx: &Ctxt, cont: &Container) {
|
||||
match &cont.data {
|
||||
Data::Enum(variants) => {
|
||||
@@ -78,18 +101,16 @@ fn check_flatten_field(cx: &Ctxt, style: Style, field: &Field) {
|
||||
}
|
||||
}
|
||||
|
||||
/// The `other` attribute must be used at most once and it must be the last
|
||||
/// variant of an enum.
|
||||
///
|
||||
/// Inside a `variant_identifier` all variants must be unit variants. Inside a
|
||||
/// `field_identifier` all but possibly one variant must be unit variants. The
|
||||
/// last variant may be a newtype variant which is an implicit "other" case.
|
||||
// The `other` attribute must be used at most once and it must be the last
|
||||
// variant of an enum.
|
||||
//
|
||||
// Inside a `variant_identifier` all variants must be unit variants. Inside a
|
||||
// `field_identifier` all but possibly one variant must be unit variants. The
|
||||
// last variant may be a newtype variant which is an implicit "other" case.
|
||||
fn check_identifier(cx: &Ctxt, cont: &Container) {
|
||||
let variants = match &cont.data {
|
||||
Data::Enum(variants) => variants,
|
||||
Data::Struct(_, _) => {
|
||||
return;
|
||||
}
|
||||
Data::Struct(_, _) => return,
|
||||
};
|
||||
|
||||
for (i, variant) in variants.iter().enumerate() {
|
||||
@@ -166,17 +187,15 @@ fn check_identifier(cx: &Ctxt, cont: &Container) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Skip-(de)serializing attributes are not allowed on variants marked
|
||||
/// (de)serialize_with.
|
||||
// Skip-(de)serializing attributes are not allowed on variants marked
|
||||
// (de)serialize_with.
|
||||
fn check_variant_skip_attrs(cx: &Ctxt, cont: &Container) {
|
||||
let variants = match &cont.data {
|
||||
Data::Enum(variants) => variants,
|
||||
Data::Struct(_, _) => {
|
||||
return;
|
||||
}
|
||||
Data::Struct(_, _) => return,
|
||||
};
|
||||
|
||||
for variant in variants.iter() {
|
||||
for variant in variants {
|
||||
if variant.attrs.serialize_with().is_some() {
|
||||
if variant.attrs.skip_serializing() {
|
||||
cx.error_spanned_by(
|
||||
@@ -241,10 +260,9 @@ fn check_variant_skip_attrs(cx: &Ctxt, cont: &Container) {
|
||||
}
|
||||
}
|
||||
|
||||
/// The tag of an internally-tagged struct variant must not be
|
||||
/// the same as either one of its fields, as this would result in
|
||||
/// duplicate keys in the serialized output and/or ambiguity in
|
||||
/// the to-be-deserialized input.
|
||||
// The tag of an internally-tagged struct variant must not be the same as either
|
||||
// one of its fields, as this would result in duplicate keys in the serialized
|
||||
// output and/or ambiguity in the to-be-deserialized input.
|
||||
fn check_internal_tag_field_name_conflict(cx: &Ctxt, cont: &Container) {
|
||||
let variants = match &cont.data {
|
||||
Data::Enum(variants) => variants,
|
||||
@@ -267,8 +285,10 @@ fn check_internal_tag_field_name_conflict(cx: &Ctxt, cont: &Container) {
|
||||
match variant.style {
|
||||
Style::Struct => {
|
||||
for field in &variant.fields {
|
||||
let check_ser = !field.attrs.skip_serializing();
|
||||
let check_de = !field.attrs.skip_deserializing();
|
||||
let check_ser =
|
||||
!(field.attrs.skip_serializing() || variant.attrs.skip_serializing());
|
||||
let check_de =
|
||||
!(field.attrs.skip_deserializing() || variant.attrs.skip_deserializing());
|
||||
let name = field.attrs.name();
|
||||
let ser_name = name.serialize_name();
|
||||
|
||||
@@ -290,8 +310,8 @@ fn check_internal_tag_field_name_conflict(cx: &Ctxt, cont: &Container) {
|
||||
}
|
||||
}
|
||||
|
||||
/// In the case of adjacently-tagged enums, the type and the
|
||||
/// contents tag must differ, for the same reason.
|
||||
// In the case of adjacently-tagged enums, the type and the contents tag must
|
||||
// differ, for the same reason.
|
||||
fn check_adjacent_tag_conflict(cx: &Ctxt, cont: &Container) {
|
||||
let (type_tag, content_tag) = match cont.attrs.tag() {
|
||||
TagType::Adjacent { tag, content } => (tag, content),
|
||||
@@ -309,7 +329,7 @@ fn check_adjacent_tag_conflict(cx: &Ctxt, cont: &Container) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Enums and unit structs cannot be transparent.
|
||||
// Enums and unit structs cannot be transparent.
|
||||
fn check_transparent(cx: &Ctxt, cont: &mut Container, derive: Derive) {
|
||||
if !cont.attrs.transparent() {
|
||||
return;
|
||||
|
||||
@@ -2,7 +2,6 @@ use quote::ToTokens;
|
||||
use std::cell::RefCell;
|
||||
use std::fmt::Display;
|
||||
use std::thread;
|
||||
use syn;
|
||||
|
||||
/// A type to collect errors together and format them.
|
||||
///
|
||||
@@ -44,12 +43,19 @@ impl Ctxt {
|
||||
}
|
||||
|
||||
/// Consume this object, producing a formatted error string if there are errors.
|
||||
pub fn check(self) -> Result<(), Vec<syn::Error>> {
|
||||
let errors = self.errors.borrow_mut().take().unwrap();
|
||||
match errors.len() {
|
||||
0 => Ok(()),
|
||||
_ => Err(errors),
|
||||
pub fn check(self) -> syn::Result<()> {
|
||||
let mut errors = self.errors.borrow_mut().take().unwrap().into_iter();
|
||||
|
||||
let mut combined = match errors.next() {
|
||||
Some(first) => first,
|
||||
None => return Ok(()),
|
||||
};
|
||||
|
||||
for rest in errors {
|
||||
combined.combine(rest);
|
||||
}
|
||||
|
||||
Err(combined)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,19 +1,18 @@
|
||||
pub mod ast;
|
||||
pub mod attr;
|
||||
|
||||
mod ctxt;
|
||||
pub use self::ctxt::Ctxt;
|
||||
|
||||
mod receiver;
|
||||
pub use self::receiver::replace_receiver;
|
||||
|
||||
mod case;
|
||||
mod check;
|
||||
mod ctxt;
|
||||
mod receiver;
|
||||
mod respan;
|
||||
mod symbol;
|
||||
|
||||
use syn::Type;
|
||||
|
||||
pub use self::ctxt::Ctxt;
|
||||
pub use self::receiver::replace_receiver;
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub enum Derive {
|
||||
Serialize,
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use internals::respan::respan;
|
||||
use crate::internals::respan::respan;
|
||||
use proc_macro2::Span;
|
||||
use quote::ToTokens;
|
||||
use std::mem;
|
||||
use syn::punctuated::Punctuated;
|
||||
use syn::{
|
||||
parse_quote, Data, DeriveInput, Expr, ExprPath, GenericArgument, GenericParam, Generics, Macro,
|
||||
Path, PathArguments, QSelf, ReturnType, Type, TypeParamBound, TypePath, WherePredicate,
|
||||
Path, PathArguments, QSelf, ReturnType, Token, Type, TypeParamBound, TypePath, WherePredicate,
|
||||
};
|
||||
|
||||
pub fn replace_receiver(input: &mut DeriveInput) {
|
||||
@@ -179,10 +179,13 @@ impl ReplaceReceiver<'_> {
|
||||
for arg in &mut arguments.args {
|
||||
match arg {
|
||||
GenericArgument::Type(arg) => self.visit_type_mut(arg),
|
||||
GenericArgument::Binding(arg) => self.visit_type_mut(&mut arg.ty),
|
||||
GenericArgument::AssocType(arg) => self.visit_type_mut(&mut arg.ty),
|
||||
GenericArgument::Lifetime(_)
|
||||
| GenericArgument::Constraint(_)
|
||||
| GenericArgument::Const(_) => {}
|
||||
| GenericArgument::Const(_)
|
||||
| GenericArgument::AssocConst(_)
|
||||
| GenericArgument::Constraint(_) => {}
|
||||
#[cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -205,7 +208,9 @@ impl ReplaceReceiver<'_> {
|
||||
fn visit_type_param_bound_mut(&mut self, bound: &mut TypeParamBound) {
|
||||
match bound {
|
||||
TypeParamBound::Trait(bound) => self.visit_path_mut(&mut bound.path),
|
||||
TypeParamBound::Lifetime(_) => {}
|
||||
TypeParamBound::Lifetime(_) | TypeParamBound::Verbatim(_) => {}
|
||||
#[cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -229,7 +234,9 @@ impl ReplaceReceiver<'_> {
|
||||
self.visit_type_param_bound_mut(bound);
|
||||
}
|
||||
}
|
||||
WherePredicate::Lifetime(_) | WherePredicate::Eq(_) => {}
|
||||
WherePredicate::Lifetime(_) => {}
|
||||
#[cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ pub const DEFAULT: Symbol = Symbol("default");
|
||||
pub const DENY_UNKNOWN_FIELDS: Symbol = Symbol("deny_unknown_fields");
|
||||
pub const DESERIALIZE: Symbol = Symbol("deserialize");
|
||||
pub const DESERIALIZE_WITH: Symbol = Symbol("deserialize_with");
|
||||
pub const EXPECTING: Symbol = Symbol("expecting");
|
||||
pub const FIELD_IDENTIFIER: Symbol = Symbol("field_identifier");
|
||||
pub const FLATTEN: Symbol = Symbol("flatten");
|
||||
pub const FROM: Symbol = Symbol("from");
|
||||
@@ -22,6 +23,8 @@ pub const OTHER: Symbol = Symbol("other");
|
||||
pub const REMOTE: Symbol = Symbol("remote");
|
||||
pub const RENAME: Symbol = Symbol("rename");
|
||||
pub const RENAME_ALL: Symbol = Symbol("rename_all");
|
||||
pub const RENAME_ALL_FIELDS: Symbol = Symbol("rename_all_fields");
|
||||
pub const REPR: Symbol = Symbol("repr");
|
||||
pub const SERDE: Symbol = Symbol("serde");
|
||||
pub const SERIALIZE: Symbol = Symbol("serialize");
|
||||
pub const SERIALIZE_WITH: Symbol = Symbol("serialize_with");
|
||||
@@ -35,7 +38,6 @@ pub const TRY_FROM: Symbol = Symbol("try_from");
|
||||
pub const UNTAGGED: Symbol = Symbol("untagged");
|
||||
pub const VARIANT_IDENTIFIER: Symbol = Symbol("variant_identifier");
|
||||
pub const WITH: Symbol = Symbol("with");
|
||||
pub const EXPECTING: Symbol = Symbol("expecting");
|
||||
|
||||
impl PartialEq<Symbol> for Ident {
|
||||
fn eq(&self, word: &Symbol) -> bool {
|
||||
|
||||
+30
-17
@@ -1,7 +1,7 @@
|
||||
//! This crate provides Serde's two derive macros.
|
||||
//!
|
||||
//! ```edition2018
|
||||
//! # use serde_derive::{Serialize, Deserialize};
|
||||
//! ```edition2021
|
||||
//! # use serde_derive::{Deserialize, Serialize};
|
||||
//! #
|
||||
//! #[derive(Serialize, Deserialize)]
|
||||
//! # struct S;
|
||||
@@ -13,8 +13,7 @@
|
||||
//!
|
||||
//! [https://serde.rs/derive.html]: https://serde.rs/derive.html
|
||||
|
||||
#![doc(html_root_url = "https://docs.rs/serde_derive/1.0.143")]
|
||||
#![allow(unknown_lints, bare_trait_objects)]
|
||||
#![doc(html_root_url = "https://docs.rs/serde_derive/1.0.177")]
|
||||
// Ignored clippy lints
|
||||
#![allow(
|
||||
// clippy false positive: https://github.com/rust-lang/rust-clippy/issues/7054
|
||||
@@ -43,7 +42,7 @@
|
||||
clippy::enum_glob_use,
|
||||
clippy::indexing_slicing,
|
||||
clippy::items_after_statements,
|
||||
clippy::let_underscore_drop,
|
||||
clippy::let_underscore_untyped,
|
||||
clippy::manual_assert,
|
||||
clippy::map_err_ignore,
|
||||
clippy::match_same_arms,
|
||||
@@ -63,17 +62,22 @@
|
||||
)]
|
||||
#![cfg_attr(all(test, exhaustive), feature(non_exhaustive_omitted_patterns_lint))]
|
||||
|
||||
#[macro_use]
|
||||
extern crate proc_macro2;
|
||||
extern crate quote;
|
||||
#[macro_use]
|
||||
extern crate syn;
|
||||
|
||||
#[cfg(not(precompiled))]
|
||||
extern crate proc_macro;
|
||||
extern crate proc_macro2;
|
||||
#[cfg(precompiled)]
|
||||
extern crate proc_macro2 as proc_macro;
|
||||
|
||||
mod internals;
|
||||
|
||||
use proc_macro::TokenStream;
|
||||
#[cfg(precompiled)]
|
||||
use std::sync::atomic::AtomicBool;
|
||||
#[cfg(not(precompiled))]
|
||||
use syn::parse_macro_input;
|
||||
use syn::DeriveInput;
|
||||
|
||||
#[macro_use]
|
||||
@@ -85,25 +89,34 @@ mod de;
|
||||
mod dummy;
|
||||
mod pretend;
|
||||
mod ser;
|
||||
mod this;
|
||||
mod try;
|
||||
|
||||
#[proc_macro_derive(Serialize, attributes(serde))]
|
||||
#[cfg(precompiled)]
|
||||
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 {
|
||||
let mut input = parse_macro_input!(input as DeriveInput);
|
||||
ser::expand_derive_serialize(&mut input)
|
||||
.unwrap_or_else(to_compile_errors)
|
||||
.unwrap_or_else(syn::Error::into_compile_error)
|
||||
.into()
|
||||
}
|
||||
|
||||
#[proc_macro_derive(Deserialize, attributes(serde))]
|
||||
#[cfg_attr(not(precompiled), 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(to_compile_errors)
|
||||
.unwrap_or_else(syn::Error::into_compile_error)
|
||||
.into()
|
||||
}
|
||||
|
||||
fn to_compile_errors(errors: Vec<syn::Error>) -> proc_macro2::TokenStream {
|
||||
let compile_errors = errors.iter().map(syn::Error::to_compile_error);
|
||||
quote!(#(#compile_errors)*)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use crate::internals::ast::{Container, Data, Field, Style, Variant};
|
||||
use proc_macro2::TokenStream;
|
||||
use quote::format_ident;
|
||||
|
||||
use internals::ast::{Container, Data, Field, Style, Variant};
|
||||
use quote::{format_ident, quote};
|
||||
|
||||
// Suppress dead_code warnings that would otherwise appear when using a remote
|
||||
// derive. Other than this pretend code, a struct annotated with remote derive
|
||||
@@ -97,29 +96,14 @@ fn pretend_fields_used_struct_packed(cont: &Container, fields: &[Field]) -> Toke
|
||||
|
||||
let members = fields.iter().map(|field| &field.member).collect::<Vec<_>>();
|
||||
|
||||
#[cfg(not(no_ptr_addr_of))]
|
||||
{
|
||||
quote! {
|
||||
match _serde::__private::None::<&#type_ident #ty_generics> {
|
||||
_serde::__private::Some(__v @ #type_ident { #(#members: _),* }) => {
|
||||
#(
|
||||
let _ = _serde::__private::ptr::addr_of!(__v.#members);
|
||||
)*
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(no_ptr_addr_of)]
|
||||
{
|
||||
let placeholders = (0usize..).map(|i| format_ident!("__v{}", i));
|
||||
|
||||
quote! {
|
||||
match _serde::__private::None::<#type_ident #ty_generics> {
|
||||
_serde::__private::Some(#type_ident { #(#members: #placeholders),* }) => {}
|
||||
_ => {}
|
||||
quote! {
|
||||
match _serde::__private::None::<&#type_ident #ty_generics> {
|
||||
_serde::__private::Some(__v @ #type_ident { #(#members: _),* }) => {
|
||||
#(
|
||||
let _ = _serde::__private::ptr::addr_of!(__v.#members);
|
||||
)*
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+48
-48
@@ -1,17 +1,13 @@
|
||||
use crate::fragment::{Fragment, Match, Stmts};
|
||||
use crate::internals::ast::{Container, Data, Field, Style, Variant};
|
||||
use crate::internals::{attr, replace_receiver, Ctxt, Derive};
|
||||
use crate::{bound, dummy, pretend, this};
|
||||
use proc_macro2::{Span, TokenStream};
|
||||
use quote::{quote, quote_spanned};
|
||||
use syn::spanned::Spanned;
|
||||
use syn::{self, Ident, Index, Member};
|
||||
use syn::{parse_quote, Ident, Index, Member};
|
||||
|
||||
use bound;
|
||||
use dummy;
|
||||
use fragment::{Fragment, Match, Stmts};
|
||||
use internals::ast::{Container, Data, Field, Style, Variant};
|
||||
use internals::{attr, replace_receiver, Ctxt, Derive};
|
||||
use pretend;
|
||||
|
||||
pub fn expand_derive_serialize(
|
||||
input: &mut syn::DeriveInput,
|
||||
) -> Result<TokenStream, Vec<syn::Error>> {
|
||||
pub fn expand_derive_serialize(input: &mut syn::DeriveInput) -> syn::Result<TokenStream> {
|
||||
replace_receiver(input);
|
||||
|
||||
let ctxt = Ctxt::new();
|
||||
@@ -58,8 +54,6 @@ pub fn expand_derive_serialize(
|
||||
|
||||
Ok(dummy::wrap_in_const(
|
||||
cont.attrs.custom_serde_path(),
|
||||
"SERIALIZE",
|
||||
ident,
|
||||
impl_block,
|
||||
))
|
||||
}
|
||||
@@ -82,9 +76,13 @@ struct Parameters {
|
||||
self_var: Ident,
|
||||
|
||||
/// Path to the type the impl is for. Either a single `Ident` for local
|
||||
/// types or `some::remote::Ident` for remote types. Does not include
|
||||
/// generic parameters.
|
||||
this: syn::Path,
|
||||
/// types (does not include generic parameters) or `some::remote::Path` for
|
||||
/// remote types.
|
||||
this_type: syn::Path,
|
||||
|
||||
/// Same as `this_type` but using `::<T>` for generic parameters for use in
|
||||
/// expression position.
|
||||
this_value: syn::Path,
|
||||
|
||||
/// Generics including any explicit and inferred bounds for the impl.
|
||||
generics: syn::Generics,
|
||||
@@ -105,18 +103,15 @@ impl Parameters {
|
||||
Ident::new("self", Span::call_site())
|
||||
};
|
||||
|
||||
let this = match cont.attrs.remote() {
|
||||
Some(remote) => remote.clone(),
|
||||
None => cont.ident.clone().into(),
|
||||
};
|
||||
|
||||
let this_type = this::this_type(cont);
|
||||
let this_value = this::this_value(cont);
|
||||
let is_packed = cont.attrs.is_packed();
|
||||
|
||||
let generics = build_generics(cont);
|
||||
|
||||
Parameters {
|
||||
self_var,
|
||||
this,
|
||||
this_type,
|
||||
this_value,
|
||||
generics,
|
||||
is_remote,
|
||||
is_packed,
|
||||
@@ -126,7 +121,7 @@ impl Parameters {
|
||||
/// Type name to use in error messages and `&'static str` arguments to
|
||||
/// various Serializer methods.
|
||||
fn type_name(&self) -> String {
|
||||
self.this.segments.last().unwrap().ident.to_string()
|
||||
self.this_type.segments.last().unwrap().ident.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -427,7 +422,7 @@ fn serialize_variant(
|
||||
variant_index: u32,
|
||||
cattrs: &attr::Container,
|
||||
) -> TokenStream {
|
||||
let this = ¶ms.this;
|
||||
let this_value = ¶ms.this_value;
|
||||
let variant_ident = &variant.ident;
|
||||
|
||||
if variant.attrs.skip_serializing() {
|
||||
@@ -445,47 +440,49 @@ fn serialize_variant(
|
||||
Style::Struct => quote!({ .. }),
|
||||
};
|
||||
quote! {
|
||||
#this::#variant_ident #fields_pat => #skipped_err,
|
||||
#this_value::#variant_ident #fields_pat => #skipped_err,
|
||||
}
|
||||
} else {
|
||||
// variant wasn't skipped
|
||||
let case = match variant.style {
|
||||
Style::Unit => {
|
||||
quote! {
|
||||
#this::#variant_ident
|
||||
#this_value::#variant_ident
|
||||
}
|
||||
}
|
||||
Style::Newtype => {
|
||||
quote! {
|
||||
#this::#variant_ident(ref __field0)
|
||||
#this_value::#variant_ident(ref __field0)
|
||||
}
|
||||
}
|
||||
Style::Tuple => {
|
||||
let field_names = (0..variant.fields.len())
|
||||
.map(|i| Ident::new(&format!("__field{}", i), Span::call_site()));
|
||||
quote! {
|
||||
#this::#variant_ident(#(ref #field_names),*)
|
||||
#this_value::#variant_ident(#(ref #field_names),*)
|
||||
}
|
||||
}
|
||||
Style::Struct => {
|
||||
let members = variant.fields.iter().map(|f| &f.member);
|
||||
quote! {
|
||||
#this::#variant_ident { #(ref #members),* }
|
||||
#this_value::#variant_ident { #(ref #members),* }
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let body = Match(match cattrs.tag() {
|
||||
attr::TagType::External => {
|
||||
let body = Match(match (cattrs.tag(), variant.attrs.untagged()) {
|
||||
(attr::TagType::External, false) => {
|
||||
serialize_externally_tagged_variant(params, variant, variant_index, cattrs)
|
||||
}
|
||||
attr::TagType::Internal { tag } => {
|
||||
(attr::TagType::Internal { tag }, false) => {
|
||||
serialize_internally_tagged_variant(params, variant, cattrs, tag)
|
||||
}
|
||||
attr::TagType::Adjacent { tag, content } => {
|
||||
(attr::TagType::Adjacent { tag, content }, false) => {
|
||||
serialize_adjacently_tagged_variant(params, variant, cattrs, tag, content)
|
||||
}
|
||||
attr::TagType::None => serialize_untagged_variant(params, variant, cattrs),
|
||||
(attr::TagType::None, _) | (_, true) => {
|
||||
serialize_untagged_variant(params, variant, cattrs)
|
||||
}
|
||||
});
|
||||
|
||||
quote! {
|
||||
@@ -640,7 +637,7 @@ fn serialize_adjacently_tagged_variant(
|
||||
tag: &str,
|
||||
content: &str,
|
||||
) -> Fragment {
|
||||
let this = ¶ms.this;
|
||||
let this_type = ¶ms.this_type;
|
||||
let type_name = cattrs.name().serialize_name();
|
||||
let variant_name = variant.attrs.name().serialize_name();
|
||||
|
||||
@@ -717,9 +714,10 @@ fn serialize_adjacently_tagged_variant(
|
||||
let (wrapper_impl_generics, wrapper_ty_generics, _) = wrapper_generics.split_for_impl();
|
||||
|
||||
quote_block! {
|
||||
#[doc(hidden)]
|
||||
struct __AdjacentlyTagged #wrapper_generics #where_clause {
|
||||
data: (#(&'__a #fields_ty,)*),
|
||||
phantom: _serde::__private::PhantomData<#this #ty_generics>,
|
||||
phantom: _serde::__private::PhantomData<#this_type #ty_generics>,
|
||||
}
|
||||
|
||||
impl #wrapper_impl_generics _serde::Serialize for __AdjacentlyTagged #wrapper_ty_generics #where_clause {
|
||||
@@ -741,7 +739,7 @@ fn serialize_adjacently_tagged_variant(
|
||||
try!(_serde::ser::SerializeStruct::serialize_field(
|
||||
&mut __struct, #content, &__AdjacentlyTagged {
|
||||
data: (#(#fields_ident,)*),
|
||||
phantom: _serde::__private::PhantomData::<#this #ty_generics>,
|
||||
phantom: _serde::__private::PhantomData::<#this_type #ty_generics>,
|
||||
}));
|
||||
_serde::ser::SerializeStruct::end(__struct)
|
||||
}
|
||||
@@ -866,8 +864,8 @@ enum StructVariant<'a> {
|
||||
Untagged,
|
||||
}
|
||||
|
||||
fn serialize_struct_variant<'a>(
|
||||
context: StructVariant<'a>,
|
||||
fn serialize_struct_variant(
|
||||
context: StructVariant,
|
||||
params: &Parameters,
|
||||
fields: &[Field],
|
||||
name: &str,
|
||||
@@ -950,8 +948,8 @@ fn serialize_struct_variant<'a>(
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize_struct_variant_with_flatten<'a>(
|
||||
context: StructVariant<'a>,
|
||||
fn serialize_struct_variant_with_flatten(
|
||||
context: StructVariant,
|
||||
params: &Parameters,
|
||||
fields: &[Field],
|
||||
name: &str,
|
||||
@@ -971,7 +969,7 @@ fn serialize_struct_variant_with_flatten<'a>(
|
||||
variant_index,
|
||||
variant_name,
|
||||
} => {
|
||||
let this = ¶ms.this;
|
||||
let this_type = ¶ms.this_type;
|
||||
let fields_ty = fields.iter().map(|f| &f.ty);
|
||||
let members = &fields.iter().map(|f| &f.member).collect::<Vec<_>>();
|
||||
|
||||
@@ -980,9 +978,10 @@ fn serialize_struct_variant_with_flatten<'a>(
|
||||
let (wrapper_impl_generics, wrapper_ty_generics, _) = wrapper_generics.split_for_impl();
|
||||
|
||||
quote_block! {
|
||||
#[doc(hidden)]
|
||||
struct __EnumFlatten #wrapper_generics #where_clause {
|
||||
data: (#(&'__a #fields_ty,)*),
|
||||
phantom: _serde::__private::PhantomData<#this #ty_generics>,
|
||||
phantom: _serde::__private::PhantomData<#this_type #ty_generics>,
|
||||
}
|
||||
|
||||
impl #wrapper_impl_generics _serde::Serialize for __EnumFlatten #wrapper_ty_generics #where_clause {
|
||||
@@ -1006,7 +1005,7 @@ fn serialize_struct_variant_with_flatten<'a>(
|
||||
#variant_name,
|
||||
&__EnumFlatten {
|
||||
data: (#(#members,)*),
|
||||
phantom: _serde::__private::PhantomData::<#this #ty_generics>,
|
||||
phantom: _serde::__private::PhantomData::<#this_type #ty_generics>,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1192,7 +1191,7 @@ fn wrap_serialize_with(
|
||||
field_tys: &[&syn::Type],
|
||||
field_exprs: &[TokenStream],
|
||||
) -> TokenStream {
|
||||
let this = ¶ms.this;
|
||||
let this_type = ¶ms.this_type;
|
||||
let (_, ty_generics, where_clause) = params.generics.split_for_impl();
|
||||
|
||||
let wrapper_generics = if field_exprs.is_empty() {
|
||||
@@ -1210,9 +1209,10 @@ fn wrap_serialize_with(
|
||||
});
|
||||
|
||||
quote!({
|
||||
#[doc(hidden)]
|
||||
struct __SerializeWith #wrapper_impl_generics #where_clause {
|
||||
values: (#(&'__a #field_tys, )*),
|
||||
phantom: _serde::__private::PhantomData<#this #ty_generics>,
|
||||
phantom: _serde::__private::PhantomData<#this_type #ty_generics>,
|
||||
}
|
||||
|
||||
impl #wrapper_impl_generics _serde::Serialize for __SerializeWith #wrapper_ty_generics #where_clause {
|
||||
@@ -1226,7 +1226,7 @@ fn wrap_serialize_with(
|
||||
|
||||
&__SerializeWith {
|
||||
values: (#(#field_exprs, )*),
|
||||
phantom: _serde::__private::PhantomData::<#this #ty_generics>,
|
||||
phantom: _serde::__private::PhantomData::<#this_type #ty_generics>,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
use crate::internals::ast::Container;
|
||||
use syn::{Path, PathArguments, Token};
|
||||
|
||||
pub fn this_type(cont: &Container) -> Path {
|
||||
if let Some(remote) = cont.attrs.remote() {
|
||||
let mut this = remote.clone();
|
||||
for segment in &mut this.segments {
|
||||
if let PathArguments::AngleBracketed(arguments) = &mut segment.arguments {
|
||||
arguments.colon2_token = None;
|
||||
}
|
||||
}
|
||||
this
|
||||
} else {
|
||||
Path::from(cont.ident.clone())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn this_value(cont: &Container) -> Path {
|
||||
if let Some(remote) = cont.attrs.remote() {
|
||||
let mut this = remote.clone();
|
||||
for segment in &mut this.segments {
|
||||
if let PathArguments::AngleBracketed(arguments) = &mut segment.arguments {
|
||||
if arguments.colon2_token.is_none() {
|
||||
arguments.colon2_token = Some(Token);
|
||||
}
|
||||
}
|
||||
}
|
||||
this
|
||||
} else {
|
||||
Path::from(cont.ident.clone())
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
use proc_macro2::{Punct, Spacing, TokenStream};
|
||||
use quote::quote;
|
||||
|
||||
// None of our generated code requires the `From::from` error conversion
|
||||
// performed by the standard library's `try!` macro. With this simplified macro
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
msrv = "1.31.0"
|
||||
@@ -1,15 +1,15 @@
|
||||
[package]
|
||||
name = "serde_derive_internals"
|
||||
version = "0.26.0" # remember to update html_root_url
|
||||
version = "0.28.0" # remember to update html_root_url
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
||||
description = "AST representation used by Serde derive macros. Unstable."
|
||||
documentation = "https://docs.rs/serde_derive_internals"
|
||||
exclude = ["build.rs"]
|
||||
homepage = "https://serde.rs"
|
||||
include = ["lib.rs", "src/**/*.rs", "LICENSE-APACHE", "LICENSE-MIT"]
|
||||
keywords = ["serde", "serialization"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
repository = "https://github.com/serde-rs/serde"
|
||||
rust-version = "1.31"
|
||||
rust-version = "1.56"
|
||||
|
||||
[lib]
|
||||
path = "lib.rs"
|
||||
@@ -17,7 +17,8 @@ path = "lib.rs"
|
||||
[dependencies]
|
||||
proc-macro2 = "1.0"
|
||||
quote = "1.0"
|
||||
syn = { version = "1.0.90", default-features = false, features = ["derive", "parsing", "printing", "clone-impls"] }
|
||||
syn = { version = "2.0.25", default-features = false, features = ["clone-impls", "derive", "parsing", "printing"] }
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
||||
rustdoc-args = ["--generate-link-to-definition"]
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
use std::path::Path;
|
||||
|
||||
fn main() {
|
||||
println!("cargo:rerun-if-changed=build.rs");
|
||||
println!("cargo:rerun-if-changed=src/mod.rs");
|
||||
|
||||
// Sometimes on Windows the git checkout does not correctly wire up the
|
||||
// symlink from serde_derive_internals/src to serde_derive/src/internals.
|
||||
// When this happens we'll just build based on relative paths within the git
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#![doc(html_root_url = "https://docs.rs/serde_derive_internals/0.26.0")]
|
||||
#![allow(unknown_lints, bare_trait_objects)]
|
||||
#![doc(html_root_url = "https://docs.rs/serde_derive_internals/0.28.0")]
|
||||
// Ignored clippy lints
|
||||
#![allow(
|
||||
clippy::cognitive_complexity,
|
||||
@@ -22,7 +21,7 @@
|
||||
clippy::doc_markdown,
|
||||
clippy::enum_glob_use,
|
||||
clippy::items_after_statements,
|
||||
clippy::let_underscore_drop,
|
||||
clippy::let_underscore_untyped,
|
||||
clippy::manual_assert,
|
||||
clippy::match_same_arms,
|
||||
// clippy bug: https://github.com/rust-lang/rust-clippy/issues/6984
|
||||
@@ -30,18 +29,18 @@
|
||||
clippy::missing_errors_doc,
|
||||
clippy::module_name_repetitions,
|
||||
clippy::must_use_candidate,
|
||||
clippy::return_self_not_must_use,
|
||||
clippy::similar_names,
|
||||
clippy::single_match_else,
|
||||
clippy::struct_excessive_bools,
|
||||
clippy::too_many_lines,
|
||||
clippy::unused_self,
|
||||
clippy::wildcard_imports
|
||||
)]
|
||||
|
||||
#[macro_use]
|
||||
extern crate syn;
|
||||
|
||||
extern crate proc_macro2;
|
||||
extern crate quote;
|
||||
extern crate syn;
|
||||
|
||||
#[cfg_attr(serde_build_from_git, path = "../serde_derive/src/internals/mod.rs")]
|
||||
#[cfg_attr(not(serde_build_from_git), path = "src/mod.rs")]
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
msrv = "1.13.0"
|
||||
@@ -1,25 +0,0 @@
|
||||
[package]
|
||||
name = "serde_test"
|
||||
version = "1.0.143" # remember to update html_root_url
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
||||
build = "build.rs"
|
||||
categories = ["development-tools::testing"]
|
||||
description = "Token De/Serializer for testing De/Serialize implementations"
|
||||
documentation = "https://docs.serde.rs/serde_test/"
|
||||
homepage = "https://serde.rs"
|
||||
include = ["build.rs", "src/**/*.rs", "crates-io.md", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
|
||||
keywords = ["serde", "serialization", "testing"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
readme = "crates-io.md"
|
||||
repository = "https://github.com/serde-rs/serde"
|
||||
rust-version = "1.13"
|
||||
|
||||
[dependencies]
|
||||
serde = { version = "1.0.60", path = "../serde" }
|
||||
|
||||
[dev-dependencies]
|
||||
serde = { version = "1.0", path = "../serde" }
|
||||
serde_derive = { version = "1.0", path = "../serde_derive" }
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
||||
@@ -1 +0,0 @@
|
||||
../LICENSE-APACHE
|
||||
@@ -1 +0,0 @@
|
||||
../LICENSE-MIT
|
||||
@@ -1 +0,0 @@
|
||||
../README.md
|
||||
@@ -1,48 +0,0 @@
|
||||
use std::env;
|
||||
use std::process::Command;
|
||||
use std::str::{self, FromStr};
|
||||
|
||||
// The rustc-cfg strings below are *not* public API. Please let us know by
|
||||
// opening a GitHub issue if your build environment requires some way to enable
|
||||
// these cfgs other than by executing our build script.
|
||||
fn main() {
|
||||
let minor = match rustc_minor_version() {
|
||||
Some(minor) => minor,
|
||||
None => return,
|
||||
};
|
||||
|
||||
// #[track_caller] stabilized in Rust 1.46:
|
||||
// https://blog.rust-lang.org/2020/08/27/Rust-1.46.0.html#track_caller
|
||||
if minor < 46 {
|
||||
println!("cargo:rustc-cfg=no_track_caller");
|
||||
}
|
||||
}
|
||||
|
||||
fn rustc_minor_version() -> Option<u32> {
|
||||
let rustc = match env::var_os("RUSTC") {
|
||||
Some(rustc) => rustc,
|
||||
None => return None,
|
||||
};
|
||||
|
||||
let output = match Command::new(rustc).arg("--version").output() {
|
||||
Ok(output) => output,
|
||||
Err(_) => return None,
|
||||
};
|
||||
|
||||
let version = match str::from_utf8(&output.stdout) {
|
||||
Ok(version) => version,
|
||||
Err(_) => return None,
|
||||
};
|
||||
|
||||
let mut pieces = version.split('.');
|
||||
if pieces.next() != Some("rustc 1") {
|
||||
return None;
|
||||
}
|
||||
|
||||
let next = match pieces.next() {
|
||||
Some(next) => next,
|
||||
None => return None,
|
||||
};
|
||||
|
||||
u32::from_str(next).ok()
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
../crates-io.md
|
||||
@@ -1,223 +0,0 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use de::Deserializer;
|
||||
use ser::Serializer;
|
||||
use token::Token;
|
||||
|
||||
use std::fmt::Debug;
|
||||
|
||||
/// Runs both `assert_ser_tokens` and `assert_de_tokens`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde::{Serialize, Deserialize};
|
||||
/// # use serde_test::{assert_tokens, Token};
|
||||
/// #
|
||||
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
/// struct S {
|
||||
/// a: u8,
|
||||
/// b: u8,
|
||||
/// }
|
||||
///
|
||||
/// let s = S { a: 0, b: 0 };
|
||||
/// assert_tokens(&s, &[
|
||||
/// Token::Struct { name: "S", len: 2 },
|
||||
/// Token::Str("a"),
|
||||
/// Token::U8(0),
|
||||
/// Token::Str("b"),
|
||||
/// Token::U8(0),
|
||||
/// Token::StructEnd,
|
||||
/// ]);
|
||||
/// ```
|
||||
#[cfg_attr(not(no_track_caller), track_caller)]
|
||||
pub fn assert_tokens<'de, T>(value: &T, tokens: &'de [Token])
|
||||
where
|
||||
T: Serialize + Deserialize<'de> + PartialEq + Debug,
|
||||
{
|
||||
assert_ser_tokens(value, tokens);
|
||||
assert_de_tokens(value, tokens);
|
||||
}
|
||||
|
||||
/// Asserts that `value` serializes to the given `tokens`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde::{Serialize, Deserialize};
|
||||
/// # use serde_test::{assert_ser_tokens, Token};
|
||||
/// #
|
||||
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
/// struct S {
|
||||
/// a: u8,
|
||||
/// b: u8,
|
||||
/// }
|
||||
///
|
||||
/// let s = S { a: 0, b: 0 };
|
||||
/// assert_ser_tokens(&s, &[
|
||||
/// Token::Struct { name: "S", len: 2 },
|
||||
/// Token::Str("a"),
|
||||
/// Token::U8(0),
|
||||
/// Token::Str("b"),
|
||||
/// Token::U8(0),
|
||||
/// Token::StructEnd,
|
||||
/// ]);
|
||||
/// ```
|
||||
#[cfg_attr(not(no_track_caller), track_caller)]
|
||||
pub fn assert_ser_tokens<T: ?Sized>(value: &T, tokens: &[Token])
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
let mut ser = Serializer::new(tokens);
|
||||
match value.serialize(&mut ser) {
|
||||
Ok(_) => {}
|
||||
Err(err) => panic!("value failed to serialize: {}", err),
|
||||
}
|
||||
|
||||
if ser.remaining() > 0 {
|
||||
panic!("{} remaining tokens", ser.remaining());
|
||||
}
|
||||
}
|
||||
|
||||
/// Asserts that `value` serializes to the given `tokens`, and then yields
|
||||
/// `error`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// use std::sync::{Arc, Mutex};
|
||||
/// use std::thread;
|
||||
///
|
||||
/// use serde::Serialize;
|
||||
/// use serde_test::{assert_ser_tokens_error, Token};
|
||||
///
|
||||
/// #[derive(Serialize)]
|
||||
/// struct Example {
|
||||
/// lock: Arc<Mutex<u32>>,
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// let example = Example { lock: Arc::new(Mutex::new(0)) };
|
||||
/// let lock = example.lock.clone();
|
||||
///
|
||||
/// let _ = thread::spawn(move || {
|
||||
/// // This thread will acquire the mutex first, unwrapping the result
|
||||
/// // of `lock` because the lock has not been poisoned.
|
||||
/// let _guard = lock.lock().unwrap();
|
||||
///
|
||||
/// // This panic while holding the lock (`_guard` is in scope) will
|
||||
/// // poison the mutex.
|
||||
/// panic!()
|
||||
/// }).join();
|
||||
///
|
||||
/// let expected = &[
|
||||
/// Token::Struct { name: "Example", len: 1 },
|
||||
/// Token::Str("lock"),
|
||||
/// ];
|
||||
/// let error = "lock poison error while serializing";
|
||||
/// assert_ser_tokens_error(&example, expected, error);
|
||||
/// }
|
||||
/// ```
|
||||
#[cfg_attr(not(no_track_caller), track_caller)]
|
||||
pub fn assert_ser_tokens_error<T: ?Sized>(value: &T, tokens: &[Token], error: &str)
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
let mut ser = Serializer::new(tokens);
|
||||
match value.serialize(&mut ser) {
|
||||
Ok(_) => panic!("value serialized successfully"),
|
||||
Err(e) => assert_eq!(e, *error),
|
||||
}
|
||||
|
||||
if ser.remaining() > 0 {
|
||||
panic!("{} remaining tokens", ser.remaining());
|
||||
}
|
||||
}
|
||||
|
||||
/// Asserts that the given `tokens` deserialize into `value`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde::{Serialize, Deserialize};
|
||||
/// # use serde_test::{assert_de_tokens, Token};
|
||||
/// #
|
||||
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
/// struct S {
|
||||
/// a: u8,
|
||||
/// b: u8,
|
||||
/// }
|
||||
///
|
||||
/// let s = S { a: 0, b: 0 };
|
||||
/// assert_de_tokens(&s, &[
|
||||
/// Token::Struct { name: "S", len: 2 },
|
||||
/// Token::Str("a"),
|
||||
/// Token::U8(0),
|
||||
/// Token::Str("b"),
|
||||
/// Token::U8(0),
|
||||
/// Token::StructEnd,
|
||||
/// ]);
|
||||
/// ```
|
||||
#[cfg_attr(not(no_track_caller), track_caller)]
|
||||
pub fn assert_de_tokens<'de, T>(value: &T, tokens: &'de [Token])
|
||||
where
|
||||
T: Deserialize<'de> + PartialEq + Debug,
|
||||
{
|
||||
let mut de = Deserializer::new(tokens);
|
||||
let mut deserialized_val = match T::deserialize(&mut de) {
|
||||
Ok(v) => {
|
||||
assert_eq!(v, *value);
|
||||
v
|
||||
}
|
||||
Err(e) => panic!("tokens failed to deserialize: {}", e),
|
||||
};
|
||||
if de.remaining() > 0 {
|
||||
panic!("{} remaining tokens", de.remaining());
|
||||
}
|
||||
|
||||
// Do the same thing for deserialize_in_place. This isn't *great* because a
|
||||
// no-op impl of deserialize_in_place can technically succeed here. Still,
|
||||
// this should catch a lot of junk.
|
||||
let mut de = Deserializer::new(tokens);
|
||||
match T::deserialize_in_place(&mut de, &mut deserialized_val) {
|
||||
Ok(()) => {
|
||||
assert_eq!(deserialized_val, *value);
|
||||
}
|
||||
Err(e) => panic!("tokens failed to deserialize_in_place: {}", e),
|
||||
}
|
||||
if de.remaining() > 0 {
|
||||
panic!("{} remaining tokens", de.remaining());
|
||||
}
|
||||
}
|
||||
|
||||
/// Asserts that the given `tokens` yield `error` when deserializing.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde::{Serialize, Deserialize};
|
||||
/// # use serde_test::{assert_de_tokens_error, Token};
|
||||
/// #
|
||||
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
/// #[serde(deny_unknown_fields)]
|
||||
/// struct S {
|
||||
/// a: u8,
|
||||
/// b: u8,
|
||||
/// }
|
||||
///
|
||||
/// assert_de_tokens_error::<S>(
|
||||
/// &[
|
||||
/// Token::Struct { name: "S", len: 2 },
|
||||
/// Token::Str("x"),
|
||||
/// ],
|
||||
/// "unknown field `x`, expected `a` or `b`",
|
||||
/// );
|
||||
/// ```
|
||||
#[cfg_attr(not(no_track_caller), track_caller)]
|
||||
pub fn assert_de_tokens_error<'de, T>(tokens: &'de [Token], error: &str)
|
||||
where
|
||||
T: Deserialize<'de>,
|
||||
{
|
||||
let mut de = Deserializer::new(tokens);
|
||||
match T::deserialize(&mut de) {
|
||||
Ok(_) => panic!("tokens deserialized successfully"),
|
||||
Err(e) => assert_eq!(e, *error),
|
||||
}
|
||||
|
||||
// There may be one token left if a peek caused the error
|
||||
de.next_token_opt();
|
||||
|
||||
if de.remaining() > 0 {
|
||||
panic!("{} remaining tokens", de.remaining());
|
||||
}
|
||||
}
|
||||
@@ -1,847 +0,0 @@
|
||||
use std::fmt;
|
||||
|
||||
use serde::ser::{
|
||||
SerializeMap, SerializeSeq, SerializeStruct, SerializeStructVariant, SerializeTuple,
|
||||
SerializeTupleStruct, SerializeTupleVariant,
|
||||
};
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct Readable<T: ?Sized>(T);
|
||||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct Compact<T: ?Sized>(T);
|
||||
|
||||
/// Trait to determine whether a value is represented in human-readable or
|
||||
/// compact form.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
/// use serde_test::{assert_tokens, Configure, Token};
|
||||
///
|
||||
/// #[derive(Debug, PartialEq)]
|
||||
/// struct Example(u8, u8);
|
||||
///
|
||||
/// impl Serialize for Example {
|
||||
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
/// where
|
||||
/// S: Serializer,
|
||||
/// {
|
||||
/// if serializer.is_human_readable() {
|
||||
/// format!("{}.{}", self.0, self.1).serialize(serializer)
|
||||
/// } else {
|
||||
/// (self.0, self.1).serialize(serializer)
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// impl<'de> Deserialize<'de> for Example {
|
||||
/// fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
/// where
|
||||
/// D: Deserializer<'de>,
|
||||
/// {
|
||||
/// use serde::de::Error;
|
||||
/// if deserializer.is_human_readable() {
|
||||
/// let s = String::deserialize(deserializer)?;
|
||||
/// let parts: Vec<_> = s.split('.').collect();
|
||||
/// Ok(Example(
|
||||
/// parts[0].parse().map_err(D::Error::custom)?,
|
||||
/// parts[1].parse().map_err(D::Error::custom)?,
|
||||
/// ))
|
||||
/// } else {
|
||||
/// let (x, y) = Deserialize::deserialize(deserializer)?;
|
||||
/// Ok(Example(x, y))
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// assert_tokens(
|
||||
/// &Example(1, 0).compact(),
|
||||
/// &[
|
||||
/// Token::Tuple { len: 2 },
|
||||
/// Token::U8(1),
|
||||
/// Token::U8(0),
|
||||
/// Token::TupleEnd,
|
||||
/// ],
|
||||
/// );
|
||||
/// assert_tokens(&Example(1, 0).readable(), &[Token::Str("1.0")]);
|
||||
/// }
|
||||
/// ```
|
||||
pub trait Configure {
|
||||
/// Marks `self` as using `is_human_readable == true`
|
||||
fn readable(self) -> Readable<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
Readable(self)
|
||||
}
|
||||
/// Marks `self` as using `is_human_readable == false`
|
||||
fn compact(self) -> Compact<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
Compact(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> Configure for T {}
|
||||
|
||||
impl<T: ?Sized> Serialize for Readable<T>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
self.0.serialize(Readable(serializer))
|
||||
}
|
||||
}
|
||||
impl<T: ?Sized> Serialize for Compact<T>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
self.0.serialize(Compact(serializer))
|
||||
}
|
||||
}
|
||||
impl<'de, T> Deserialize<'de> for Readable<T>
|
||||
where
|
||||
T: Deserialize<'de>,
|
||||
{
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
T::deserialize(Readable(deserializer)).map(Readable)
|
||||
}
|
||||
}
|
||||
impl<'de, T> Deserialize<'de> for Compact<T>
|
||||
where
|
||||
T: Deserialize<'de>,
|
||||
{
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
T::deserialize(Compact(deserializer)).map(Compact)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, T> DeserializeSeed<'de> for Readable<T>
|
||||
where
|
||||
T: DeserializeSeed<'de>,
|
||||
{
|
||||
type Value = T::Value;
|
||||
|
||||
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
self.0.deserialize(Readable(deserializer))
|
||||
}
|
||||
}
|
||||
impl<'de, T> DeserializeSeed<'de> for Compact<T>
|
||||
where
|
||||
T: DeserializeSeed<'de>,
|
||||
{
|
||||
type Value = T::Value;
|
||||
|
||||
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
self.0.deserialize(Compact(deserializer))
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! forward_method {
|
||||
($name: ident (self $(, $arg: ident : $arg_type: ty)* ) -> $return_type: ty) => {
|
||||
fn $name (self $(, $arg : $arg_type)* ) -> $return_type {
|
||||
(self.0).$name( $($arg),* )
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! forward_serialize_methods {
|
||||
( $( $name: ident $arg_type: ty ),* ) => {
|
||||
$(
|
||||
forward_method!($name(self, v : $arg_type) -> Result<Self::Ok, Self::Error>);
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! impl_serializer {
|
||||
($wrapper:ident, $is_human_readable:expr) => {
|
||||
impl<S> Serializer for $wrapper<S>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
type Ok = S::Ok;
|
||||
type Error = S::Error;
|
||||
|
||||
type SerializeSeq = $wrapper<S::SerializeSeq>;
|
||||
type SerializeTuple = $wrapper<S::SerializeTuple>;
|
||||
type SerializeTupleStruct = $wrapper<S::SerializeTupleStruct>;
|
||||
type SerializeTupleVariant = $wrapper<S::SerializeTupleVariant>;
|
||||
type SerializeMap = $wrapper<S::SerializeMap>;
|
||||
type SerializeStruct = $wrapper<S::SerializeStruct>;
|
||||
type SerializeStructVariant = $wrapper<S::SerializeStructVariant>;
|
||||
|
||||
fn is_human_readable(&self) -> bool {
|
||||
$is_human_readable
|
||||
}
|
||||
|
||||
forward_serialize_methods! {
|
||||
serialize_bool bool,
|
||||
serialize_i8 i8,
|
||||
serialize_i16 i16,
|
||||
serialize_i32 i32,
|
||||
serialize_i64 i64,
|
||||
serialize_u8 u8,
|
||||
serialize_u16 u16,
|
||||
serialize_u32 u32,
|
||||
serialize_u64 u64,
|
||||
serialize_f32 f32,
|
||||
serialize_f64 f64,
|
||||
serialize_char char,
|
||||
serialize_str &str,
|
||||
serialize_bytes &[u8],
|
||||
serialize_unit_struct &'static str
|
||||
|
||||
}
|
||||
|
||||
fn serialize_unit(self) -> Result<S::Ok, S::Error> {
|
||||
self.0.serialize_unit()
|
||||
}
|
||||
|
||||
fn serialize_unit_variant(
|
||||
self,
|
||||
name: &'static str,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
) -> Result<S::Ok, S::Error> {
|
||||
self.0.serialize_unit_variant(name, variant_index, variant)
|
||||
}
|
||||
|
||||
fn serialize_newtype_struct<T: ?Sized>(
|
||||
self,
|
||||
name: &'static str,
|
||||
value: &T,
|
||||
) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
self.0.serialize_newtype_struct(name, &$wrapper(value))
|
||||
}
|
||||
|
||||
fn serialize_newtype_variant<T: ?Sized>(
|
||||
self,
|
||||
name: &'static str,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
value: &T,
|
||||
) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
self.0
|
||||
.serialize_newtype_variant(name, variant_index, variant, &$wrapper(value))
|
||||
}
|
||||
|
||||
fn serialize_none(self) -> Result<S::Ok, Self::Error> {
|
||||
self.0.serialize_none()
|
||||
}
|
||||
|
||||
fn serialize_some<T: ?Sized>(self, value: &T) -> Result<S::Ok, Self::Error>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
self.0.serialize_some(&$wrapper(value))
|
||||
}
|
||||
|
||||
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
|
||||
self.0.serialize_seq(len).map($wrapper)
|
||||
}
|
||||
|
||||
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> {
|
||||
self.0.serialize_tuple(len).map($wrapper)
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct(
|
||||
self,
|
||||
name: &'static str,
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeTupleStruct, Self::Error> {
|
||||
self.0.serialize_tuple_struct(name, len).map($wrapper)
|
||||
}
|
||||
|
||||
fn serialize_tuple_variant(
|
||||
self,
|
||||
name: &'static str,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeTupleVariant, Self::Error> {
|
||||
self.0
|
||||
.serialize_tuple_variant(name, variant_index, variant, len)
|
||||
.map($wrapper)
|
||||
}
|
||||
|
||||
fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
|
||||
self.0.serialize_map(len).map($wrapper)
|
||||
}
|
||||
|
||||
fn serialize_struct(
|
||||
self,
|
||||
name: &'static str,
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeStruct, Self::Error> {
|
||||
self.0.serialize_struct(name, len).map($wrapper)
|
||||
}
|
||||
|
||||
fn serialize_struct_variant(
|
||||
self,
|
||||
name: &'static str,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeStructVariant, Self::Error> {
|
||||
self.0
|
||||
.serialize_struct_variant(name, variant_index, variant, len)
|
||||
.map($wrapper)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> SerializeSeq for $wrapper<S>
|
||||
where
|
||||
S: SerializeSeq,
|
||||
{
|
||||
type Ok = S::Ok;
|
||||
type Error = S::Error;
|
||||
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), S::Error>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
self.0.serialize_element(&$wrapper(value))
|
||||
}
|
||||
fn end(self) -> Result<S::Ok, S::Error> {
|
||||
self.0.end()
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> SerializeTuple for $wrapper<S>
|
||||
where
|
||||
S: SerializeTuple,
|
||||
{
|
||||
type Ok = S::Ok;
|
||||
type Error = S::Error;
|
||||
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), S::Error>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
self.0.serialize_element(&$wrapper(value))
|
||||
}
|
||||
fn end(self) -> Result<S::Ok, S::Error> {
|
||||
self.0.end()
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> SerializeTupleStruct for $wrapper<S>
|
||||
where
|
||||
S: SerializeTupleStruct,
|
||||
{
|
||||
type Ok = S::Ok;
|
||||
type Error = S::Error;
|
||||
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), S::Error>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
self.0.serialize_field(&$wrapper(value))
|
||||
}
|
||||
fn end(self) -> Result<S::Ok, S::Error> {
|
||||
self.0.end()
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> SerializeTupleVariant for $wrapper<S>
|
||||
where
|
||||
S: SerializeTupleVariant,
|
||||
{
|
||||
type Ok = S::Ok;
|
||||
type Error = S::Error;
|
||||
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), S::Error>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
self.0.serialize_field(&$wrapper(value))
|
||||
}
|
||||
fn end(self) -> Result<S::Ok, S::Error> {
|
||||
self.0.end()
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> SerializeMap for $wrapper<S>
|
||||
where
|
||||
S: SerializeMap,
|
||||
{
|
||||
type Ok = S::Ok;
|
||||
type Error = S::Error;
|
||||
fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), S::Error>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
self.0.serialize_key(&$wrapper(key))
|
||||
}
|
||||
fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), S::Error>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
self.0.serialize_value(&$wrapper(value))
|
||||
}
|
||||
fn serialize_entry<K: ?Sized, V: ?Sized>(
|
||||
&mut self,
|
||||
key: &K,
|
||||
value: &V,
|
||||
) -> Result<(), S::Error>
|
||||
where
|
||||
K: Serialize,
|
||||
V: Serialize,
|
||||
{
|
||||
self.0.serialize_entry(key, &$wrapper(value))
|
||||
}
|
||||
fn end(self) -> Result<S::Ok, S::Error> {
|
||||
self.0.end()
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> SerializeStruct for $wrapper<S>
|
||||
where
|
||||
S: SerializeStruct,
|
||||
{
|
||||
type Ok = S::Ok;
|
||||
type Error = S::Error;
|
||||
fn serialize_field<T: ?Sized>(
|
||||
&mut self,
|
||||
name: &'static str,
|
||||
field: &T,
|
||||
) -> Result<(), S::Error>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
self.0.serialize_field(name, &$wrapper(field))
|
||||
}
|
||||
fn end(self) -> Result<S::Ok, S::Error> {
|
||||
self.0.end()
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> SerializeStructVariant for $wrapper<S>
|
||||
where
|
||||
S: SerializeStructVariant,
|
||||
{
|
||||
type Ok = S::Ok;
|
||||
type Error = S::Error;
|
||||
fn serialize_field<T: ?Sized>(
|
||||
&mut self,
|
||||
name: &'static str,
|
||||
field: &T,
|
||||
) -> Result<(), S::Error>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
self.0.serialize_field(name, &$wrapper(field))
|
||||
}
|
||||
fn end(self) -> Result<S::Ok, S::Error> {
|
||||
self.0.end()
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl_serializer!(Readable, true);
|
||||
impl_serializer!(Compact, false);
|
||||
|
||||
use serde::de::{DeserializeSeed, EnumAccess, Error, MapAccess, SeqAccess, VariantAccess, Visitor};
|
||||
|
||||
macro_rules! forward_deserialize_methods {
|
||||
( $wrapper : ident ( $( $name: ident ),* ) ) => {
|
||||
$(
|
||||
fn $name<V>(self, visitor: V) -> Result<V::Value, D::Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
(self.0).$name($wrapper(visitor))
|
||||
}
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! impl_deserializer {
|
||||
($wrapper:ident, $is_human_readable:expr) => {
|
||||
impl<'de, D> Deserializer<'de> for $wrapper<D>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
type Error = D::Error;
|
||||
|
||||
forward_deserialize_methods! {
|
||||
$wrapper (
|
||||
deserialize_any,
|
||||
deserialize_bool,
|
||||
deserialize_u8,
|
||||
deserialize_u16,
|
||||
deserialize_u32,
|
||||
deserialize_u64,
|
||||
deserialize_i8,
|
||||
deserialize_i16,
|
||||
deserialize_i32,
|
||||
deserialize_i64,
|
||||
deserialize_f32,
|
||||
deserialize_f64,
|
||||
deserialize_char,
|
||||
deserialize_str,
|
||||
deserialize_string,
|
||||
deserialize_bytes,
|
||||
deserialize_byte_buf,
|
||||
deserialize_option,
|
||||
deserialize_unit,
|
||||
deserialize_seq,
|
||||
deserialize_map,
|
||||
deserialize_identifier,
|
||||
deserialize_ignored_any
|
||||
)
|
||||
}
|
||||
|
||||
fn deserialize_unit_struct<V>(
|
||||
self,
|
||||
name: &'static str,
|
||||
visitor: V,
|
||||
) -> Result<V::Value, D::Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
self.0.deserialize_unit_struct(name, $wrapper(visitor))
|
||||
}
|
||||
fn deserialize_newtype_struct<V>(
|
||||
self,
|
||||
name: &'static str,
|
||||
visitor: V,
|
||||
) -> Result<V::Value, D::Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
self.0.deserialize_newtype_struct(name, $wrapper(visitor))
|
||||
}
|
||||
fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, D::Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
self.0.deserialize_tuple(len, $wrapper(visitor))
|
||||
}
|
||||
fn deserialize_tuple_struct<V>(
|
||||
self,
|
||||
name: &'static str,
|
||||
len: usize,
|
||||
visitor: V,
|
||||
) -> Result<V::Value, D::Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
self.0
|
||||
.deserialize_tuple_struct(name, len, $wrapper(visitor))
|
||||
}
|
||||
fn deserialize_struct<V>(
|
||||
self,
|
||||
name: &'static str,
|
||||
fields: &'static [&'static str],
|
||||
visitor: V,
|
||||
) -> Result<V::Value, D::Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
self.0.deserialize_struct(name, fields, $wrapper(visitor))
|
||||
}
|
||||
fn deserialize_enum<V>(
|
||||
self,
|
||||
name: &'static str,
|
||||
variants: &'static [&'static str],
|
||||
visitor: V,
|
||||
) -> Result<V::Value, D::Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
self.0.deserialize_enum(name, variants, $wrapper(visitor))
|
||||
}
|
||||
|
||||
fn is_human_readable(&self) -> bool {
|
||||
$is_human_readable
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, D> Visitor<'de> for $wrapper<D>
|
||||
where
|
||||
D: Visitor<'de>,
|
||||
{
|
||||
type Value = D::Value;
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
self.0.expecting(formatter)
|
||||
}
|
||||
fn visit_bool<E>(self, v: bool) -> Result<D::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
self.0.visit_bool(v)
|
||||
}
|
||||
fn visit_i8<E>(self, v: i8) -> Result<D::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
self.0.visit_i8(v)
|
||||
}
|
||||
fn visit_i16<E>(self, v: i16) -> Result<D::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
self.0.visit_i16(v)
|
||||
}
|
||||
fn visit_i32<E>(self, v: i32) -> Result<D::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
self.0.visit_i32(v)
|
||||
}
|
||||
fn visit_i64<E>(self, v: i64) -> Result<D::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
self.0.visit_i64(v)
|
||||
}
|
||||
fn visit_u8<E>(self, v: u8) -> Result<D::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
self.0.visit_u8(v)
|
||||
}
|
||||
fn visit_u16<E>(self, v: u16) -> Result<D::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
self.0.visit_u16(v)
|
||||
}
|
||||
fn visit_u32<E>(self, v: u32) -> Result<D::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
self.0.visit_u32(v)
|
||||
}
|
||||
fn visit_u64<E>(self, v: u64) -> Result<D::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
self.0.visit_u64(v)
|
||||
}
|
||||
fn visit_f32<E>(self, v: f32) -> Result<D::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
self.0.visit_f32(v)
|
||||
}
|
||||
fn visit_f64<E>(self, v: f64) -> Result<D::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
self.0.visit_f64(v)
|
||||
}
|
||||
fn visit_char<E>(self, v: char) -> Result<D::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
self.0.visit_char(v)
|
||||
}
|
||||
fn visit_str<E>(self, v: &str) -> Result<D::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
self.0.visit_str(v)
|
||||
}
|
||||
fn visit_borrowed_str<E>(self, v: &'de str) -> Result<D::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
self.0.visit_borrowed_str(v)
|
||||
}
|
||||
fn visit_string<E>(self, v: String) -> Result<D::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
self.0.visit_string(v)
|
||||
}
|
||||
fn visit_bytes<E>(self, v: &[u8]) -> Result<D::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
self.0.visit_bytes(v)
|
||||
}
|
||||
fn visit_borrowed_bytes<E>(self, v: &'de [u8]) -> Result<D::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
self.0.visit_borrowed_bytes(v)
|
||||
}
|
||||
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<D::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
self.0.visit_byte_buf(v)
|
||||
}
|
||||
fn visit_none<E>(self) -> Result<D::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
self.0.visit_none()
|
||||
}
|
||||
fn visit_some<D2>(self, deserializer: D2) -> Result<Self::Value, D2::Error>
|
||||
where
|
||||
D2: Deserializer<'de>,
|
||||
{
|
||||
self.0.visit_some($wrapper(deserializer))
|
||||
}
|
||||
fn visit_unit<E>(self) -> Result<D::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
self.0.visit_unit()
|
||||
}
|
||||
fn visit_newtype_struct<D2>(self, deserializer: D2) -> Result<Self::Value, D2::Error>
|
||||
where
|
||||
D2: Deserializer<'de>,
|
||||
{
|
||||
self.0.visit_newtype_struct($wrapper(deserializer))
|
||||
}
|
||||
fn visit_seq<V>(self, seq: V) -> Result<D::Value, V::Error>
|
||||
where
|
||||
V: SeqAccess<'de>,
|
||||
{
|
||||
self.0.visit_seq($wrapper(seq))
|
||||
}
|
||||
fn visit_map<V>(self, map: V) -> Result<D::Value, V::Error>
|
||||
where
|
||||
V: MapAccess<'de>,
|
||||
{
|
||||
self.0.visit_map($wrapper(map))
|
||||
}
|
||||
fn visit_enum<V>(self, data: V) -> Result<D::Value, V::Error>
|
||||
where
|
||||
V: EnumAccess<'de>,
|
||||
{
|
||||
self.0.visit_enum($wrapper(data))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, D> SeqAccess<'de> for $wrapper<D>
|
||||
where
|
||||
D: SeqAccess<'de>,
|
||||
{
|
||||
type Error = D::Error;
|
||||
fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, D::Error>
|
||||
where
|
||||
T: DeserializeSeed<'de>,
|
||||
{
|
||||
self.0.next_element_seed($wrapper(seed))
|
||||
}
|
||||
fn size_hint(&self) -> Option<usize> {
|
||||
self.0.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, D> MapAccess<'de> for $wrapper<D>
|
||||
where
|
||||
D: MapAccess<'de>,
|
||||
{
|
||||
type Error = D::Error;
|
||||
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, D::Error>
|
||||
where
|
||||
K: DeserializeSeed<'de>,
|
||||
{
|
||||
self.0.next_key_seed($wrapper(seed))
|
||||
}
|
||||
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, D::Error>
|
||||
where
|
||||
V: DeserializeSeed<'de>,
|
||||
{
|
||||
self.0.next_value_seed($wrapper(seed))
|
||||
}
|
||||
fn next_entry_seed<K, V>(
|
||||
&mut self,
|
||||
kseed: K,
|
||||
vseed: V,
|
||||
) -> Result<Option<(K::Value, V::Value)>, D::Error>
|
||||
where
|
||||
K: DeserializeSeed<'de>,
|
||||
V: DeserializeSeed<'de>,
|
||||
{
|
||||
self.0.next_entry_seed($wrapper(kseed), $wrapper(vseed))
|
||||
}
|
||||
fn size_hint(&self) -> Option<usize> {
|
||||
self.0.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, D> EnumAccess<'de> for $wrapper<D>
|
||||
where
|
||||
D: EnumAccess<'de>,
|
||||
{
|
||||
type Error = D::Error;
|
||||
type Variant = $wrapper<D::Variant>;
|
||||
fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error>
|
||||
where
|
||||
V: DeserializeSeed<'de>,
|
||||
{
|
||||
self.0
|
||||
.variant_seed($wrapper(seed))
|
||||
.map(|(value, variant)| (value, $wrapper(variant)))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, D> VariantAccess<'de> for $wrapper<D>
|
||||
where
|
||||
D: VariantAccess<'de>,
|
||||
{
|
||||
type Error = D::Error;
|
||||
fn unit_variant(self) -> Result<(), D::Error> {
|
||||
self.0.unit_variant()
|
||||
}
|
||||
fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, D::Error>
|
||||
where
|
||||
T: DeserializeSeed<'de>,
|
||||
{
|
||||
self.0.newtype_variant_seed($wrapper(seed))
|
||||
}
|
||||
fn tuple_variant<V>(self, len: usize, visitor: V) -> Result<V::Value, D::Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
self.0.tuple_variant(len, $wrapper(visitor))
|
||||
}
|
||||
fn struct_variant<V>(
|
||||
self,
|
||||
fields: &'static [&'static str],
|
||||
visitor: V,
|
||||
) -> Result<V::Value, D::Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
self.0.struct_variant(fields, $wrapper(visitor))
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl_deserializer!(Readable, true);
|
||||
impl_deserializer!(Compact, false);
|
||||
@@ -1,683 +0,0 @@
|
||||
use serde::de::value::{MapAccessDeserializer, SeqAccessDeserializer};
|
||||
use serde::de::{
|
||||
self, Deserialize, DeserializeSeed, EnumAccess, IntoDeserializer, MapAccess, SeqAccess,
|
||||
VariantAccess, Visitor,
|
||||
};
|
||||
|
||||
use error::Error;
|
||||
use token::Token;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Deserializer<'de> {
|
||||
tokens: &'de [Token],
|
||||
}
|
||||
|
||||
macro_rules! assert_next_token {
|
||||
($de:expr, $expected:expr) => {
|
||||
match $de.next_token_opt() {
|
||||
Some(token) if token == $expected => {}
|
||||
Some(other) => panic!(
|
||||
"expected Token::{} but deserialization wants Token::{}",
|
||||
other, $expected
|
||||
),
|
||||
None => panic!(
|
||||
"end of tokens but deserialization wants Token::{}",
|
||||
$expected
|
||||
),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! unexpected {
|
||||
($token:expr) => {
|
||||
panic!("deserialization did not expect this token: {}", $token)
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! end_of_tokens {
|
||||
() => {
|
||||
panic!("ran out of tokens to deserialize")
|
||||
};
|
||||
}
|
||||
|
||||
impl<'de> Deserializer<'de> {
|
||||
pub fn new(tokens: &'de [Token]) -> Self {
|
||||
Deserializer { tokens: tokens }
|
||||
}
|
||||
|
||||
fn peek_token_opt(&self) -> Option<Token> {
|
||||
self.tokens.first().cloned()
|
||||
}
|
||||
|
||||
fn peek_token(&self) -> Token {
|
||||
match self.peek_token_opt() {
|
||||
Some(token) => token,
|
||||
None => end_of_tokens!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next_token_opt(&mut self) -> Option<Token> {
|
||||
match self.tokens.split_first() {
|
||||
Some((&first, rest)) => {
|
||||
self.tokens = rest;
|
||||
Some(first)
|
||||
}
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn next_token(&mut self) -> Token {
|
||||
match self.tokens.split_first() {
|
||||
Some((&first, rest)) => {
|
||||
self.tokens = rest;
|
||||
first
|
||||
}
|
||||
None => end_of_tokens!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn remaining(&self) -> usize {
|
||||
self.tokens.len()
|
||||
}
|
||||
|
||||
fn visit_seq<V>(
|
||||
&mut self,
|
||||
len: Option<usize>,
|
||||
end: Token,
|
||||
visitor: V,
|
||||
) -> Result<V::Value, Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
let value = visitor.visit_seq(DeserializerSeqVisitor {
|
||||
de: self,
|
||||
len: len,
|
||||
end: end,
|
||||
})?;
|
||||
assert_next_token!(self, end);
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
fn visit_map<V>(
|
||||
&mut self,
|
||||
len: Option<usize>,
|
||||
end: Token,
|
||||
visitor: V,
|
||||
) -> Result<V::Value, Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
let value = visitor.visit_map(DeserializerMapVisitor {
|
||||
de: self,
|
||||
len: len,
|
||||
end: end,
|
||||
})?;
|
||||
assert_next_token!(self, end);
|
||||
Ok(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
||||
type Error = Error;
|
||||
|
||||
forward_to_deserialize_any! {
|
||||
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
|
||||
bytes byte_buf unit seq map identifier ignored_any
|
||||
}
|
||||
|
||||
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
let token = self.next_token();
|
||||
match token {
|
||||
Token::Bool(v) => visitor.visit_bool(v),
|
||||
Token::I8(v) => visitor.visit_i8(v),
|
||||
Token::I16(v) => visitor.visit_i16(v),
|
||||
Token::I32(v) => visitor.visit_i32(v),
|
||||
Token::I64(v) => visitor.visit_i64(v),
|
||||
Token::U8(v) => visitor.visit_u8(v),
|
||||
Token::U16(v) => visitor.visit_u16(v),
|
||||
Token::U32(v) => visitor.visit_u32(v),
|
||||
Token::U64(v) => visitor.visit_u64(v),
|
||||
Token::F32(v) => visitor.visit_f32(v),
|
||||
Token::F64(v) => visitor.visit_f64(v),
|
||||
Token::Char(v) => visitor.visit_char(v),
|
||||
Token::Str(v) => visitor.visit_str(v),
|
||||
Token::BorrowedStr(v) => visitor.visit_borrowed_str(v),
|
||||
Token::String(v) => visitor.visit_string(v.to_owned()),
|
||||
Token::Bytes(v) => visitor.visit_bytes(v),
|
||||
Token::BorrowedBytes(v) => visitor.visit_borrowed_bytes(v),
|
||||
Token::ByteBuf(v) => visitor.visit_byte_buf(v.to_vec()),
|
||||
Token::None => visitor.visit_none(),
|
||||
Token::Some => visitor.visit_some(self),
|
||||
Token::Unit | Token::UnitStruct { .. } => visitor.visit_unit(),
|
||||
Token::NewtypeStruct { .. } => visitor.visit_newtype_struct(self),
|
||||
Token::Seq { len } => self.visit_seq(len, Token::SeqEnd, visitor),
|
||||
Token::Tuple { len } => self.visit_seq(Some(len), Token::TupleEnd, visitor),
|
||||
Token::TupleStruct { len, .. } => {
|
||||
self.visit_seq(Some(len), Token::TupleStructEnd, visitor)
|
||||
}
|
||||
Token::Map { len } => self.visit_map(len, Token::MapEnd, visitor),
|
||||
Token::Struct { len, .. } => self.visit_map(Some(len), Token::StructEnd, visitor),
|
||||
Token::Enum { .. } => {
|
||||
let variant = self.next_token();
|
||||
let next = self.peek_token();
|
||||
match (variant, next) {
|
||||
(Token::Str(variant), Token::Unit) => {
|
||||
self.next_token();
|
||||
visitor.visit_str(variant)
|
||||
}
|
||||
(Token::BorrowedStr(variant), Token::Unit) => {
|
||||
self.next_token();
|
||||
visitor.visit_borrowed_str(variant)
|
||||
}
|
||||
(Token::String(variant), Token::Unit) => {
|
||||
self.next_token();
|
||||
visitor.visit_string(variant.to_string())
|
||||
}
|
||||
(Token::Bytes(variant), Token::Unit) => {
|
||||
self.next_token();
|
||||
visitor.visit_bytes(variant)
|
||||
}
|
||||
(Token::BorrowedBytes(variant), Token::Unit) => {
|
||||
self.next_token();
|
||||
visitor.visit_borrowed_bytes(variant)
|
||||
}
|
||||
(Token::ByteBuf(variant), Token::Unit) => {
|
||||
self.next_token();
|
||||
visitor.visit_byte_buf(variant.to_vec())
|
||||
}
|
||||
(Token::U8(variant), Token::Unit) => {
|
||||
self.next_token();
|
||||
visitor.visit_u8(variant)
|
||||
}
|
||||
(Token::U16(variant), Token::Unit) => {
|
||||
self.next_token();
|
||||
visitor.visit_u16(variant)
|
||||
}
|
||||
(Token::U32(variant), Token::Unit) => {
|
||||
self.next_token();
|
||||
visitor.visit_u32(variant)
|
||||
}
|
||||
(Token::U64(variant), Token::Unit) => {
|
||||
self.next_token();
|
||||
visitor.visit_u64(variant)
|
||||
}
|
||||
(variant, Token::Unit) => unexpected!(variant),
|
||||
(variant, _) => {
|
||||
visitor.visit_map(EnumMapVisitor::new(self, variant, EnumFormat::Any))
|
||||
}
|
||||
}
|
||||
}
|
||||
Token::UnitVariant { variant, .. } => visitor.visit_str(variant),
|
||||
Token::NewtypeVariant { variant, .. } => visitor.visit_map(EnumMapVisitor::new(
|
||||
self,
|
||||
Token::Str(variant),
|
||||
EnumFormat::Any,
|
||||
)),
|
||||
Token::TupleVariant { variant, .. } => visitor.visit_map(EnumMapVisitor::new(
|
||||
self,
|
||||
Token::Str(variant),
|
||||
EnumFormat::Seq,
|
||||
)),
|
||||
Token::StructVariant { variant, .. } => visitor.visit_map(EnumMapVisitor::new(
|
||||
self,
|
||||
Token::Str(variant),
|
||||
EnumFormat::Map,
|
||||
)),
|
||||
Token::SeqEnd
|
||||
| Token::TupleEnd
|
||||
| Token::TupleStructEnd
|
||||
| Token::MapEnd
|
||||
| Token::StructEnd
|
||||
| Token::TupleVariantEnd
|
||||
| Token::StructVariantEnd => {
|
||||
unexpected!(token);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.peek_token() {
|
||||
Token::Unit | Token::None => {
|
||||
self.next_token();
|
||||
visitor.visit_none()
|
||||
}
|
||||
Token::Some => {
|
||||
self.next_token();
|
||||
visitor.visit_some(self)
|
||||
}
|
||||
_ => self.deserialize_any(visitor),
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_enum<V>(
|
||||
self,
|
||||
name: &'static str,
|
||||
_variants: &'static [&'static str],
|
||||
visitor: V,
|
||||
) -> Result<V::Value, Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.peek_token() {
|
||||
Token::Enum { name: n } if name == n => {
|
||||
self.next_token();
|
||||
|
||||
visitor.visit_enum(DeserializerEnumVisitor { de: self })
|
||||
}
|
||||
Token::UnitVariant { name: n, .. }
|
||||
| Token::NewtypeVariant { name: n, .. }
|
||||
| Token::TupleVariant { name: n, .. }
|
||||
| Token::StructVariant { name: n, .. }
|
||||
if name == n =>
|
||||
{
|
||||
visitor.visit_enum(DeserializerEnumVisitor { de: self })
|
||||
}
|
||||
_ => self.deserialize_any(visitor),
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_unit_struct<V>(self, name: &'static str, visitor: V) -> Result<V::Value, Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.peek_token() {
|
||||
Token::UnitStruct { .. } => {
|
||||
assert_next_token!(self, Token::UnitStruct { name: name });
|
||||
visitor.visit_unit()
|
||||
}
|
||||
_ => self.deserialize_any(visitor),
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_newtype_struct<V>(
|
||||
self,
|
||||
name: &'static str,
|
||||
visitor: V,
|
||||
) -> Result<V::Value, Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.peek_token() {
|
||||
Token::NewtypeStruct { .. } => {
|
||||
assert_next_token!(self, Token::NewtypeStruct { name: name });
|
||||
visitor.visit_newtype_struct(self)
|
||||
}
|
||||
_ => self.deserialize_any(visitor),
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.peek_token() {
|
||||
Token::Unit | Token::UnitStruct { .. } => {
|
||||
self.next_token();
|
||||
visitor.visit_unit()
|
||||
}
|
||||
Token::Seq { .. } => {
|
||||
self.next_token();
|
||||
self.visit_seq(Some(len), Token::SeqEnd, visitor)
|
||||
}
|
||||
Token::Tuple { .. } => {
|
||||
self.next_token();
|
||||
self.visit_seq(Some(len), Token::TupleEnd, visitor)
|
||||
}
|
||||
Token::TupleStruct { .. } => {
|
||||
self.next_token();
|
||||
self.visit_seq(Some(len), Token::TupleStructEnd, visitor)
|
||||
}
|
||||
_ => self.deserialize_any(visitor),
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_tuple_struct<V>(
|
||||
self,
|
||||
name: &'static str,
|
||||
len: usize,
|
||||
visitor: V,
|
||||
) -> Result<V::Value, Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.peek_token() {
|
||||
Token::Unit => {
|
||||
self.next_token();
|
||||
visitor.visit_unit()
|
||||
}
|
||||
Token::UnitStruct { .. } => {
|
||||
assert_next_token!(self, Token::UnitStruct { name: name });
|
||||
visitor.visit_unit()
|
||||
}
|
||||
Token::Seq { .. } => {
|
||||
self.next_token();
|
||||
self.visit_seq(Some(len), Token::SeqEnd, visitor)
|
||||
}
|
||||
Token::Tuple { .. } => {
|
||||
self.next_token();
|
||||
self.visit_seq(Some(len), Token::TupleEnd, visitor)
|
||||
}
|
||||
Token::TupleStruct { len: n, .. } => {
|
||||
assert_next_token!(self, Token::TupleStruct { name: name, len: n });
|
||||
self.visit_seq(Some(len), Token::TupleStructEnd, visitor)
|
||||
}
|
||||
_ => self.deserialize_any(visitor),
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_struct<V>(
|
||||
self,
|
||||
name: &'static str,
|
||||
fields: &'static [&'static str],
|
||||
visitor: V,
|
||||
) -> Result<V::Value, Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.peek_token() {
|
||||
Token::Struct { len: n, .. } => {
|
||||
assert_next_token!(self, Token::Struct { name: name, len: n });
|
||||
self.visit_map(Some(fields.len()), Token::StructEnd, visitor)
|
||||
}
|
||||
Token::Map { .. } => {
|
||||
self.next_token();
|
||||
self.visit_map(Some(fields.len()), Token::MapEnd, visitor)
|
||||
}
|
||||
_ => self.deserialize_any(visitor),
|
||||
}
|
||||
}
|
||||
|
||||
fn is_human_readable(&self) -> bool {
|
||||
panic!(
|
||||
"Types which have different human-readable and compact representations \
|
||||
must explicitly mark their test cases with `serde_test::Configure`"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct DeserializerSeqVisitor<'a, 'de: 'a> {
|
||||
de: &'a mut Deserializer<'de>,
|
||||
len: Option<usize>,
|
||||
end: Token,
|
||||
}
|
||||
|
||||
impl<'de, 'a> SeqAccess<'de> for DeserializerSeqVisitor<'a, 'de> {
|
||||
type Error = Error;
|
||||
|
||||
fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Error>
|
||||
where
|
||||
T: DeserializeSeed<'de>,
|
||||
{
|
||||
if self.de.peek_token_opt() == Some(self.end) {
|
||||
return Ok(None);
|
||||
}
|
||||
self.len = self.len.map(|len| len.saturating_sub(1));
|
||||
seed.deserialize(&mut *self.de).map(Some)
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> Option<usize> {
|
||||
self.len
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct DeserializerMapVisitor<'a, 'de: 'a> {
|
||||
de: &'a mut Deserializer<'de>,
|
||||
len: Option<usize>,
|
||||
end: Token,
|
||||
}
|
||||
|
||||
impl<'de, 'a> MapAccess<'de> for DeserializerMapVisitor<'a, 'de> {
|
||||
type Error = Error;
|
||||
|
||||
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error>
|
||||
where
|
||||
K: DeserializeSeed<'de>,
|
||||
{
|
||||
if self.de.peek_token_opt() == Some(self.end) {
|
||||
return Ok(None);
|
||||
}
|
||||
self.len = self.len.map(|len| len.saturating_sub(1));
|
||||
seed.deserialize(&mut *self.de).map(Some)
|
||||
}
|
||||
|
||||
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error>
|
||||
where
|
||||
V: DeserializeSeed<'de>,
|
||||
{
|
||||
seed.deserialize(&mut *self.de)
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> Option<usize> {
|
||||
self.len
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct DeserializerEnumVisitor<'a, 'de: 'a> {
|
||||
de: &'a mut Deserializer<'de>,
|
||||
}
|
||||
|
||||
impl<'de, 'a> EnumAccess<'de> for DeserializerEnumVisitor<'a, 'de> {
|
||||
type Error = Error;
|
||||
type Variant = Self;
|
||||
|
||||
fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self), Error>
|
||||
where
|
||||
V: DeserializeSeed<'de>,
|
||||
{
|
||||
match self.de.peek_token() {
|
||||
Token::UnitVariant { variant: v, .. }
|
||||
| Token::NewtypeVariant { variant: v, .. }
|
||||
| Token::TupleVariant { variant: v, .. }
|
||||
| Token::StructVariant { variant: v, .. } => {
|
||||
let de = v.into_deserializer();
|
||||
let value = seed.deserialize(de)?;
|
||||
Ok((value, self))
|
||||
}
|
||||
_ => {
|
||||
let value = seed.deserialize(&mut *self.de)?;
|
||||
Ok((value, self))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, 'a> VariantAccess<'de> for DeserializerEnumVisitor<'a, 'de> {
|
||||
type Error = Error;
|
||||
|
||||
fn unit_variant(self) -> Result<(), Error> {
|
||||
match self.de.peek_token() {
|
||||
Token::UnitVariant { .. } => {
|
||||
self.de.next_token();
|
||||
Ok(())
|
||||
}
|
||||
_ => Deserialize::deserialize(self.de),
|
||||
}
|
||||
}
|
||||
|
||||
fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Self::Error>
|
||||
where
|
||||
T: DeserializeSeed<'de>,
|
||||
{
|
||||
match self.de.peek_token() {
|
||||
Token::NewtypeVariant { .. } => {
|
||||
self.de.next_token();
|
||||
seed.deserialize(self.de)
|
||||
}
|
||||
_ => seed.deserialize(self.de),
|
||||
}
|
||||
}
|
||||
|
||||
fn tuple_variant<V>(self, len: usize, visitor: V) -> Result<V::Value, Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.de.peek_token() {
|
||||
Token::TupleVariant { len: enum_len, .. } => {
|
||||
let token = self.de.next_token();
|
||||
|
||||
if len == enum_len {
|
||||
self.de
|
||||
.visit_seq(Some(len), Token::TupleVariantEnd, visitor)
|
||||
} else {
|
||||
unexpected!(token);
|
||||
}
|
||||
}
|
||||
Token::Seq {
|
||||
len: Some(enum_len),
|
||||
} => {
|
||||
let token = self.de.next_token();
|
||||
|
||||
if len == enum_len {
|
||||
self.de.visit_seq(Some(len), Token::SeqEnd, visitor)
|
||||
} else {
|
||||
unexpected!(token);
|
||||
}
|
||||
}
|
||||
_ => de::Deserializer::deserialize_any(self.de, visitor),
|
||||
}
|
||||
}
|
||||
|
||||
fn struct_variant<V>(
|
||||
self,
|
||||
fields: &'static [&'static str],
|
||||
visitor: V,
|
||||
) -> Result<V::Value, Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.de.peek_token() {
|
||||
Token::StructVariant { len: enum_len, .. } => {
|
||||
let token = self.de.next_token();
|
||||
|
||||
if fields.len() == enum_len {
|
||||
self.de
|
||||
.visit_map(Some(fields.len()), Token::StructVariantEnd, visitor)
|
||||
} else {
|
||||
unexpected!(token);
|
||||
}
|
||||
}
|
||||
Token::Map {
|
||||
len: Some(enum_len),
|
||||
} => {
|
||||
let token = self.de.next_token();
|
||||
|
||||
if fields.len() == enum_len {
|
||||
self.de
|
||||
.visit_map(Some(fields.len()), Token::MapEnd, visitor)
|
||||
} else {
|
||||
unexpected!(token);
|
||||
}
|
||||
}
|
||||
_ => de::Deserializer::deserialize_any(self.de, visitor),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct EnumMapVisitor<'a, 'de: 'a> {
|
||||
de: &'a mut Deserializer<'de>,
|
||||
variant: Option<Token>,
|
||||
format: EnumFormat,
|
||||
}
|
||||
|
||||
enum EnumFormat {
|
||||
Seq,
|
||||
Map,
|
||||
Any,
|
||||
}
|
||||
|
||||
impl<'a, 'de> EnumMapVisitor<'a, 'de> {
|
||||
fn new(de: &'a mut Deserializer<'de>, variant: Token, format: EnumFormat) -> Self {
|
||||
EnumMapVisitor {
|
||||
de: de,
|
||||
variant: Some(variant),
|
||||
format: format,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, 'a> MapAccess<'de> for EnumMapVisitor<'a, 'de> {
|
||||
type Error = Error;
|
||||
|
||||
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error>
|
||||
where
|
||||
K: DeserializeSeed<'de>,
|
||||
{
|
||||
match self.variant.take() {
|
||||
Some(Token::Str(variant)) => seed.deserialize(variant.into_deserializer()).map(Some),
|
||||
Some(Token::Bytes(variant)) => seed
|
||||
.deserialize(BytesDeserializer { value: variant })
|
||||
.map(Some),
|
||||
Some(Token::U32(variant)) => seed.deserialize(variant.into_deserializer()).map(Some),
|
||||
Some(other) => unexpected!(other),
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error>
|
||||
where
|
||||
V: DeserializeSeed<'de>,
|
||||
{
|
||||
match self.format {
|
||||
EnumFormat::Seq => {
|
||||
let value = {
|
||||
let visitor = DeserializerSeqVisitor {
|
||||
de: self.de,
|
||||
len: None,
|
||||
end: Token::TupleVariantEnd,
|
||||
};
|
||||
seed.deserialize(SeqAccessDeserializer::new(visitor))?
|
||||
};
|
||||
assert_next_token!(self.de, Token::TupleVariantEnd);
|
||||
Ok(value)
|
||||
}
|
||||
EnumFormat::Map => {
|
||||
let value = {
|
||||
let visitor = DeserializerMapVisitor {
|
||||
de: self.de,
|
||||
len: None,
|
||||
end: Token::StructVariantEnd,
|
||||
};
|
||||
seed.deserialize(MapAccessDeserializer::new(visitor))?
|
||||
};
|
||||
assert_next_token!(self.de, Token::StructVariantEnd);
|
||||
Ok(value)
|
||||
}
|
||||
EnumFormat::Any => seed.deserialize(&mut *self.de),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct BytesDeserializer {
|
||||
value: &'static [u8],
|
||||
}
|
||||
|
||||
impl<'de> de::Deserializer<'de> for BytesDeserializer {
|
||||
type Error = Error;
|
||||
|
||||
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
visitor.visit_bytes(self.value)
|
||||
}
|
||||
|
||||
forward_to_deserialize_any! {
|
||||
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
|
||||
bytes byte_buf option unit unit_struct newtype_struct seq tuple
|
||||
tuple_struct map struct enum identifier ignored_any
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
use std::error;
|
||||
use std::fmt::{self, Display};
|
||||
|
||||
use serde::{de, ser};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Error {
|
||||
msg: String,
|
||||
}
|
||||
|
||||
impl ser::Error for Error {
|
||||
fn custom<T: Display>(msg: T) -> Self {
|
||||
Error {
|
||||
msg: msg.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl de::Error for Error {
|
||||
fn custom<T: Display>(msg: T) -> Self {
|
||||
Error {
|
||||
msg: msg.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
formatter.write_str(&self.msg)
|
||||
}
|
||||
}
|
||||
|
||||
impl error::Error for Error {
|
||||
fn description(&self) -> &str {
|
||||
&self.msg
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<str> for Error {
|
||||
fn eq(&self, other: &str) -> bool {
|
||||
self.msg == other
|
||||
}
|
||||
}
|
||||
@@ -1,192 +0,0 @@
|
||||
//! This crate provides a convenient concise way to write unit tests for
|
||||
//! implementations of [`Serialize`] and [`Deserialize`].
|
||||
//!
|
||||
//! [`Serialize`]: https://docs.serde.rs/serde/ser/trait.Serialize.html
|
||||
//! [`Deserialize`]: https://docs.serde.rs/serde/de/trait.Deserialize.html
|
||||
//!
|
||||
//! The `Serialize` impl for a value can be characterized by the sequence of
|
||||
//! [`Serializer`] calls that are made in the course of serializing the value,
|
||||
//! so `serde_test` provides a [`Token`] abstraction which corresponds roughly
|
||||
//! to `Serializer` method calls. There is an [`assert_ser_tokens`] function to
|
||||
//! test that a value serializes to a particular sequence of method calls, an
|
||||
//! [`assert_de_tokens`] function to test that a value can be deserialized from
|
||||
//! a particular sequence of method calls, and an [`assert_tokens`] function to
|
||||
//! test both directions. There are also functions to test expected failure
|
||||
//! conditions.
|
||||
//!
|
||||
//! [`Serializer`]: https://docs.serde.rs/serde/ser/trait.Serializer.html
|
||||
//! [`Token`]: https://docs.serde.rs/serde_test/enum.Token.html
|
||||
//! [`assert_ser_tokens`]: https://docs.serde.rs/serde_test/fn.assert_ser_tokens.html
|
||||
//! [`assert_de_tokens`]: https://docs.serde.rs/serde_test/fn.assert_de_tokens.html
|
||||
//! [`assert_tokens`]: https://docs.serde.rs/serde_test/fn.assert_tokens.html
|
||||
//!
|
||||
//! Here is an example from the [`linked-hash-map`] crate.
|
||||
//!
|
||||
//! [`linked-hash-map`]: https://github.com/contain-rs/linked-hash-map
|
||||
//!
|
||||
//! ```edition2018
|
||||
//! # const IGNORE: &str = stringify! {
|
||||
//! use linked_hash_map::LinkedHashMap;
|
||||
//! # };
|
||||
//! use serde_test::{Token, assert_tokens};
|
||||
//!
|
||||
//! # use std::fmt;
|
||||
//! # use std::marker::PhantomData;
|
||||
//! #
|
||||
//! # use serde::ser::{Serialize, Serializer, SerializeMap};
|
||||
//! # use serde::de::{Deserialize, Deserializer, Visitor, MapAccess};
|
||||
//! #
|
||||
//! # // Dumb imitation of LinkedHashMap.
|
||||
//! # #[derive(PartialEq, Debug)]
|
||||
//! # struct LinkedHashMap<K, V>(Vec<(K, V)>);
|
||||
//! #
|
||||
//! # impl<K, V> LinkedHashMap<K, V> {
|
||||
//! # fn new() -> Self {
|
||||
//! # LinkedHashMap(Vec::new())
|
||||
//! # }
|
||||
//! #
|
||||
//! # fn insert(&mut self, k: K, v: V) {
|
||||
//! # self.0.push((k, v));
|
||||
//! # }
|
||||
//! # }
|
||||
//! #
|
||||
//! # impl<K, V> Serialize for LinkedHashMap<K, V>
|
||||
//! # where
|
||||
//! # K: Serialize,
|
||||
//! # V: Serialize,
|
||||
//! # {
|
||||
//! # fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
//! # where
|
||||
//! # S: Serializer,
|
||||
//! # {
|
||||
//! # let mut map = serializer.serialize_map(Some(self.0.len()))?;
|
||||
//! # for &(ref k, ref v) in &self.0 {
|
||||
//! # map.serialize_entry(k, v)?;
|
||||
//! # }
|
||||
//! # map.end()
|
||||
//! # }
|
||||
//! # }
|
||||
//! #
|
||||
//! # struct LinkedHashMapVisitor<K, V>(PhantomData<(K, V)>);
|
||||
//! #
|
||||
//! # impl<'de, K, V> Visitor<'de> for LinkedHashMapVisitor<K, V>
|
||||
//! # where
|
||||
//! # K: Deserialize<'de>,
|
||||
//! # V: Deserialize<'de>,
|
||||
//! # {
|
||||
//! # type Value = LinkedHashMap<K, V>;
|
||||
//! #
|
||||
//! # fn expecting(&self, _: &mut fmt::Formatter) -> fmt::Result {
|
||||
//! # unimplemented!()
|
||||
//! # }
|
||||
//! #
|
||||
//! # fn visit_map<M>(self, mut access: M) -> Result<Self::Value, M::Error>
|
||||
//! # where
|
||||
//! # M: MapAccess<'de>,
|
||||
//! # {
|
||||
//! # let mut map = LinkedHashMap::new();
|
||||
//! # while let Some((key, value)) = access.next_entry()? {
|
||||
//! # map.insert(key, value);
|
||||
//! # }
|
||||
//! # Ok(map)
|
||||
//! # }
|
||||
//! # }
|
||||
//! #
|
||||
//! # impl<'de, K, V> Deserialize<'de> for LinkedHashMap<K, V>
|
||||
//! # where
|
||||
//! # K: Deserialize<'de>,
|
||||
//! # V: Deserialize<'de>,
|
||||
//! # {
|
||||
//! # fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
//! # where
|
||||
//! # D: Deserializer<'de>,
|
||||
//! # {
|
||||
//! # deserializer.deserialize_map(LinkedHashMapVisitor(PhantomData))
|
||||
//! # }
|
||||
//! # }
|
||||
//! #
|
||||
//! #[test]
|
||||
//! # fn not_a_test_ser_de_empty() {}
|
||||
//! fn test_ser_de_empty() {
|
||||
//! let map = LinkedHashMap::<char, u32>::new();
|
||||
//!
|
||||
//! assert_tokens(&map, &[
|
||||
//! Token::Map { len: Some(0) },
|
||||
//! Token::MapEnd,
|
||||
//! ]);
|
||||
//! }
|
||||
//!
|
||||
//! #[test]
|
||||
//! # fn not_a_test_ser_de() {}
|
||||
//! fn test_ser_de() {
|
||||
//! let mut map = LinkedHashMap::new();
|
||||
//! map.insert('b', 20);
|
||||
//! map.insert('a', 10);
|
||||
//! map.insert('c', 30);
|
||||
//!
|
||||
//! assert_tokens(&map, &[
|
||||
//! Token::Map { len: Some(3) },
|
||||
//! Token::Char('b'),
|
||||
//! Token::I32(20),
|
||||
//!
|
||||
//! Token::Char('a'),
|
||||
//! Token::I32(10),
|
||||
//!
|
||||
//! Token::Char('c'),
|
||||
//! Token::I32(30),
|
||||
//! Token::MapEnd,
|
||||
//! ]);
|
||||
//! }
|
||||
//! #
|
||||
//! # fn main() {
|
||||
//! # test_ser_de_empty();
|
||||
//! # test_ser_de();
|
||||
//! # }
|
||||
//! ```
|
||||
|
||||
#![doc(html_root_url = "https://docs.rs/serde_test/1.0.143")]
|
||||
#![cfg_attr(feature = "cargo-clippy", allow(renamed_and_removed_lints))]
|
||||
// Ignored clippy lints
|
||||
#![cfg_attr(feature = "cargo-clippy", allow(float_cmp, needless_doctest_main))]
|
||||
// Ignored clippy_pedantic lints
|
||||
#![cfg_attr(
|
||||
feature = "cargo-clippy",
|
||||
allow(
|
||||
cloned_instead_of_copied,
|
||||
doc_link_with_quotes, // https://github.com/rust-lang/rust-clippy/issues/8961
|
||||
empty_line_after_outer_attr,
|
||||
manual_assert,
|
||||
missing_docs_in_private_items,
|
||||
missing_panics_doc,
|
||||
module_name_repetitions,
|
||||
must_use_candidate,
|
||||
redundant_field_names,
|
||||
too_many_lines,
|
||||
type_repetition_in_bounds, // https://github.com/rust-lang/rust-clippy/issues/8772
|
||||
use_debug,
|
||||
use_self
|
||||
)
|
||||
)]
|
||||
|
||||
#[macro_use]
|
||||
extern crate serde;
|
||||
|
||||
mod de;
|
||||
mod error;
|
||||
mod ser;
|
||||
|
||||
mod assert;
|
||||
mod configure;
|
||||
mod token;
|
||||
|
||||
pub use assert::{
|
||||
assert_de_tokens, assert_de_tokens_error, assert_ser_tokens, assert_ser_tokens_error,
|
||||
assert_tokens,
|
||||
};
|
||||
pub use token::Token;
|
||||
|
||||
pub use configure::{Compact, Configure, Readable};
|
||||
|
||||
// Not public API.
|
||||
#[doc(hidden)]
|
||||
pub use de::Deserializer;
|
||||
@@ -1,464 +0,0 @@
|
||||
use serde::{ser, Serialize};
|
||||
|
||||
use error::Error;
|
||||
use token::Token;
|
||||
|
||||
/// A `Serializer` that ensures that a value serializes to a given list of
|
||||
/// tokens.
|
||||
#[derive(Debug)]
|
||||
pub struct Serializer<'a> {
|
||||
tokens: &'a [Token],
|
||||
}
|
||||
|
||||
impl<'a> Serializer<'a> {
|
||||
/// Creates the serializer.
|
||||
pub fn new(tokens: &'a [Token]) -> Self {
|
||||
Serializer { tokens: tokens }
|
||||
}
|
||||
|
||||
/// Pulls the next token off of the serializer, ignoring it.
|
||||
fn next_token(&mut self) -> Option<Token> {
|
||||
if let Some((&first, rest)) = self.tokens.split_first() {
|
||||
self.tokens = rest;
|
||||
Some(first)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn remaining(&self) -> usize {
|
||||
self.tokens.len()
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! assert_next_token {
|
||||
($ser:expr, $actual:ident) => {{
|
||||
assert_next_token!($ser, stringify!($actual), Token::$actual, true);
|
||||
}};
|
||||
($ser:expr, $actual:ident($v:expr)) => {{
|
||||
assert_next_token!(
|
||||
$ser,
|
||||
format_args!(concat!(stringify!($actual), "({:?})"), $v),
|
||||
Token::$actual(v),
|
||||
v == $v
|
||||
);
|
||||
}};
|
||||
($ser:expr, $actual:ident { $($k:ident),* }) => {{
|
||||
let compare = ($($k,)*);
|
||||
let field_format = || {
|
||||
use std::fmt::Write;
|
||||
let mut buffer = String::new();
|
||||
$(
|
||||
write!(&mut buffer, concat!(stringify!($k), ": {:?}, "), $k).unwrap();
|
||||
)*
|
||||
buffer
|
||||
};
|
||||
assert_next_token!(
|
||||
$ser,
|
||||
format_args!(concat!(stringify!($actual), " {{ {}}}"), field_format()),
|
||||
Token::$actual { $($k),* },
|
||||
($($k,)*) == compare
|
||||
);
|
||||
}};
|
||||
($ser:expr, $actual:expr, $pat:pat, $guard:expr) => {
|
||||
match $ser.next_token() {
|
||||
Some($pat) if $guard => {}
|
||||
Some(expected) => {
|
||||
panic!("expected Token::{} but serialized as {}",
|
||||
expected, $actual);
|
||||
}
|
||||
None => {
|
||||
panic!("expected end of tokens, but {} was serialized",
|
||||
$actual);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl<'s, 'a> ser::Serializer for &'s mut Serializer<'a> {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
type SerializeSeq = Self;
|
||||
type SerializeTuple = Self;
|
||||
type SerializeTupleStruct = Self;
|
||||
type SerializeTupleVariant = Variant<'s, 'a>;
|
||||
type SerializeMap = Self;
|
||||
type SerializeStruct = Self;
|
||||
type SerializeStructVariant = Variant<'s, 'a>;
|
||||
|
||||
fn serialize_bool(self, v: bool) -> Result<(), Error> {
|
||||
assert_next_token!(self, Bool(v));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_i8(self, v: i8) -> Result<(), Error> {
|
||||
assert_next_token!(self, I8(v));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_i16(self, v: i16) -> Result<(), Error> {
|
||||
assert_next_token!(self, I16(v));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_i32(self, v: i32) -> Result<(), Error> {
|
||||
assert_next_token!(self, I32(v));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_i64(self, v: i64) -> Result<(), Error> {
|
||||
assert_next_token!(self, I64(v));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_u8(self, v: u8) -> Result<(), Error> {
|
||||
assert_next_token!(self, U8(v));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_u16(self, v: u16) -> Result<(), Error> {
|
||||
assert_next_token!(self, U16(v));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_u32(self, v: u32) -> Result<(), Error> {
|
||||
assert_next_token!(self, U32(v));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_u64(self, v: u64) -> Result<(), Error> {
|
||||
assert_next_token!(self, U64(v));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_f32(self, v: f32) -> Result<(), Error> {
|
||||
assert_next_token!(self, F32(v));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_f64(self, v: f64) -> Result<(), Error> {
|
||||
assert_next_token!(self, F64(v));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_char(self, v: char) -> Result<(), Error> {
|
||||
assert_next_token!(self, Char(v));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_str(self, v: &str) -> Result<(), Error> {
|
||||
match self.tokens.first() {
|
||||
Some(&Token::BorrowedStr(_)) => assert_next_token!(self, BorrowedStr(v)),
|
||||
Some(&Token::String(_)) => assert_next_token!(self, String(v)),
|
||||
_ => assert_next_token!(self, Str(v)),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_bytes(self, v: &[u8]) -> Result<(), Self::Error> {
|
||||
match self.tokens.first() {
|
||||
Some(&Token::BorrowedBytes(_)) => assert_next_token!(self, BorrowedBytes(v)),
|
||||
Some(&Token::ByteBuf(_)) => assert_next_token!(self, ByteBuf(v)),
|
||||
_ => assert_next_token!(self, Bytes(v)),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_unit(self) -> Result<(), Error> {
|
||||
assert_next_token!(self, Unit);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_unit_struct(self, name: &'static str) -> Result<(), Error> {
|
||||
assert_next_token!(self, UnitStruct { name });
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_unit_variant(
|
||||
self,
|
||||
name: &'static str,
|
||||
_variant_index: u32,
|
||||
variant: &'static str,
|
||||
) -> Result<(), Error> {
|
||||
if self.tokens.first() == Some(&Token::Enum { name: name }) {
|
||||
self.next_token();
|
||||
assert_next_token!(self, Str(variant));
|
||||
assert_next_token!(self, Unit);
|
||||
} else {
|
||||
assert_next_token!(self, UnitVariant { name, variant });
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_newtype_struct<T: ?Sized>(self, name: &'static str, value: &T) -> Result<(), Error>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
assert_next_token!(self, NewtypeStruct { name });
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_newtype_variant<T: ?Sized>(
|
||||
self,
|
||||
name: &'static str,
|
||||
_variant_index: u32,
|
||||
variant: &'static str,
|
||||
value: &T,
|
||||
) -> Result<(), Error>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
if self.tokens.first() == Some(&Token::Enum { name: name }) {
|
||||
self.next_token();
|
||||
assert_next_token!(self, Str(variant));
|
||||
} else {
|
||||
assert_next_token!(self, NewtypeVariant { name, variant });
|
||||
}
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_none(self) -> Result<(), Error> {
|
||||
assert_next_token!(self, None);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_some<T: ?Sized>(self, value: &T) -> Result<(), Error>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
assert_next_token!(self, Some);
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_seq(self, len: Option<usize>) -> Result<Self, Error> {
|
||||
assert_next_token!(self, Seq { len });
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn serialize_tuple(self, len: usize) -> Result<Self, Error> {
|
||||
assert_next_token!(self, Tuple { len });
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct(self, name: &'static str, len: usize) -> Result<Self, Error> {
|
||||
assert_next_token!(self, TupleStruct { name, len });
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn serialize_tuple_variant(
|
||||
self,
|
||||
name: &'static str,
|
||||
_variant_index: u32,
|
||||
variant: &'static str,
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeTupleVariant, Error> {
|
||||
if self.tokens.first() == Some(&Token::Enum { name: name }) {
|
||||
self.next_token();
|
||||
assert_next_token!(self, Str(variant));
|
||||
let len = Some(len);
|
||||
assert_next_token!(self, Seq { len });
|
||||
Ok(Variant {
|
||||
ser: self,
|
||||
end: Token::SeqEnd,
|
||||
})
|
||||
} else {
|
||||
assert_next_token!(self, TupleVariant { name, variant, len });
|
||||
Ok(Variant {
|
||||
ser: self,
|
||||
end: Token::TupleVariantEnd,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize_map(self, len: Option<usize>) -> Result<Self, Error> {
|
||||
assert_next_token!(self, Map { len });
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn serialize_struct(self, name: &'static str, len: usize) -> Result<Self, Error> {
|
||||
assert_next_token!(self, Struct { name, len });
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn serialize_struct_variant(
|
||||
self,
|
||||
name: &'static str,
|
||||
_variant_index: u32,
|
||||
variant: &'static str,
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeStructVariant, Error> {
|
||||
if self.tokens.first() == Some(&Token::Enum { name: name }) {
|
||||
self.next_token();
|
||||
assert_next_token!(self, Str(variant));
|
||||
let len = Some(len);
|
||||
assert_next_token!(self, Map { len });
|
||||
Ok(Variant {
|
||||
ser: self,
|
||||
end: Token::MapEnd,
|
||||
})
|
||||
} else {
|
||||
assert_next_token!(self, StructVariant { name, variant, len });
|
||||
Ok(Variant {
|
||||
ser: self,
|
||||
end: Token::StructVariantEnd,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn is_human_readable(&self) -> bool {
|
||||
panic!(
|
||||
"Types which have different human-readable and compact representations \
|
||||
must explicitly mark their test cases with `serde_test::Configure`"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Variant<'s, 'a: 's> {
|
||||
ser: &'s mut Serializer<'a>,
|
||||
end: Token,
|
||||
}
|
||||
|
||||
impl<'s, 'a> ser::SerializeSeq for &'s mut Serializer<'a> {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
value.serialize(&mut **self)
|
||||
}
|
||||
|
||||
fn end(self) -> Result<(), Error> {
|
||||
assert_next_token!(self, SeqEnd);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s, 'a> ser::SerializeTuple for &'s mut Serializer<'a> {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
value.serialize(&mut **self)
|
||||
}
|
||||
|
||||
fn end(self) -> Result<(), Error> {
|
||||
assert_next_token!(self, TupleEnd);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s, 'a> ser::SerializeTupleStruct for &'s mut Serializer<'a> {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
value.serialize(&mut **self)
|
||||
}
|
||||
|
||||
fn end(self) -> Result<(), Error> {
|
||||
assert_next_token!(self, TupleStructEnd);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s, 'a> ser::SerializeTupleVariant for Variant<'s, 'a> {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
value.serialize(&mut *self.ser)
|
||||
}
|
||||
|
||||
fn end(self) -> Result<(), Error> {
|
||||
match self.end {
|
||||
Token::TupleVariantEnd => assert_next_token!(self.ser, TupleVariantEnd),
|
||||
Token::SeqEnd => assert_next_token!(self.ser, SeqEnd),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s, 'a> ser::SerializeMap for &'s mut Serializer<'a> {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), Self::Error>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
key.serialize(&mut **self)
|
||||
}
|
||||
|
||||
fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
value.serialize(&mut **self)
|
||||
}
|
||||
|
||||
fn end(self) -> Result<(), Self::Error> {
|
||||
assert_next_token!(self, MapEnd);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s, 'a> ser::SerializeStruct for &'s mut Serializer<'a> {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_field<T: ?Sized>(
|
||||
&mut self,
|
||||
key: &'static str,
|
||||
value: &T,
|
||||
) -> Result<(), Self::Error>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
key.serialize(&mut **self)?;
|
||||
value.serialize(&mut **self)
|
||||
}
|
||||
|
||||
fn end(self) -> Result<(), Self::Error> {
|
||||
assert_next_token!(self, StructEnd);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s, 'a> ser::SerializeStructVariant for Variant<'s, 'a> {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_field<T: ?Sized>(
|
||||
&mut self,
|
||||
key: &'static str,
|
||||
value: &T,
|
||||
) -> Result<(), Self::Error>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
key.serialize(&mut *self.ser)?;
|
||||
value.serialize(&mut *self.ser)
|
||||
}
|
||||
|
||||
fn end(self) -> Result<(), Self::Error> {
|
||||
match self.end {
|
||||
Token::StructVariantEnd => assert_next_token!(self.ser, StructVariantEnd),
|
||||
Token::MapEnd => assert_next_token!(self.ser, MapEnd),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1,519 +0,0 @@
|
||||
use std::fmt::{self, Debug, Display};
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||
pub enum Token {
|
||||
/// A serialized `bool`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde_test::{assert_tokens, Token};
|
||||
/// #
|
||||
/// assert_tokens(&true, &[Token::Bool(true)]);
|
||||
/// ```
|
||||
Bool(bool),
|
||||
|
||||
/// A serialized `i8`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde_test::{assert_tokens, Token};
|
||||
/// #
|
||||
/// assert_tokens(&0i8, &[Token::I8(0)]);
|
||||
/// ```
|
||||
I8(i8),
|
||||
|
||||
/// A serialized `i16`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde_test::{assert_tokens, Token};
|
||||
/// #
|
||||
/// assert_tokens(&0i16, &[Token::I16(0)]);
|
||||
/// ```
|
||||
I16(i16),
|
||||
|
||||
/// A serialized `i32`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde_test::{assert_tokens, Token};
|
||||
/// #
|
||||
/// assert_tokens(&0i32, &[Token::I32(0)]);
|
||||
/// ```
|
||||
I32(i32),
|
||||
|
||||
/// A serialized `i64`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde_test::{assert_tokens, Token};
|
||||
/// #
|
||||
/// assert_tokens(&0i64, &[Token::I64(0)]);
|
||||
/// ```
|
||||
I64(i64),
|
||||
|
||||
/// A serialized `u8`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde_test::{assert_tokens, Token};
|
||||
/// #
|
||||
/// assert_tokens(&0u8, &[Token::U8(0)]);
|
||||
/// ```
|
||||
U8(u8),
|
||||
|
||||
/// A serialized `u16`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde_test::{assert_tokens, Token};
|
||||
/// #
|
||||
/// assert_tokens(&0u16, &[Token::U16(0)]);
|
||||
/// ```
|
||||
U16(u16),
|
||||
|
||||
/// A serialized `u32`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde_test::{assert_tokens, Token};
|
||||
/// #
|
||||
/// assert_tokens(&0u32, &[Token::U32(0)]);
|
||||
/// ```
|
||||
U32(u32),
|
||||
|
||||
/// A serialized `u64`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde_test::{assert_tokens, Token};
|
||||
/// #
|
||||
/// assert_tokens(&0u64, &[Token::U64(0)]);
|
||||
/// ```
|
||||
U64(u64),
|
||||
|
||||
/// A serialized `f32`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde_test::{assert_tokens, Token};
|
||||
/// #
|
||||
/// assert_tokens(&0f32, &[Token::F32(0.0)]);
|
||||
/// ```
|
||||
F32(f32),
|
||||
|
||||
/// A serialized `f64`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde_test::{assert_tokens, Token};
|
||||
/// #
|
||||
/// assert_tokens(&0f64, &[Token::F64(0.0)]);
|
||||
/// ```
|
||||
F64(f64),
|
||||
|
||||
/// A serialized `char`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde_test::{assert_tokens, Token};
|
||||
/// #
|
||||
/// assert_tokens(&'\n', &[Token::Char('\n')]);
|
||||
/// ```
|
||||
Char(char),
|
||||
|
||||
/// A serialized `str`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde_test::{assert_tokens, Token};
|
||||
/// #
|
||||
/// let s = String::from("transient");
|
||||
/// assert_tokens(&s, &[Token::Str("transient")]);
|
||||
/// ```
|
||||
Str(&'static str),
|
||||
|
||||
/// A borrowed `str`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde_test::{assert_tokens, Token};
|
||||
/// #
|
||||
/// let s: &str = "borrowed";
|
||||
/// assert_tokens(&s, &[Token::BorrowedStr("borrowed")]);
|
||||
/// ```
|
||||
BorrowedStr(&'static str),
|
||||
|
||||
/// A serialized `String`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde_test::{assert_tokens, Token};
|
||||
/// #
|
||||
/// let s = String::from("owned");
|
||||
/// assert_tokens(&s, &[Token::String("owned")]);
|
||||
/// ```
|
||||
String(&'static str),
|
||||
|
||||
/// A serialized `[u8]`
|
||||
Bytes(&'static [u8]),
|
||||
|
||||
/// A borrowed `[u8]`.
|
||||
BorrowedBytes(&'static [u8]),
|
||||
|
||||
/// A serialized `ByteBuf`
|
||||
ByteBuf(&'static [u8]),
|
||||
|
||||
/// A serialized `Option<T>` containing none.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde_test::{assert_tokens, Token};
|
||||
/// #
|
||||
/// let opt = None::<char>;
|
||||
/// assert_tokens(&opt, &[Token::None]);
|
||||
/// ```
|
||||
None,
|
||||
|
||||
/// The header to a serialized `Option<T>` containing some value.
|
||||
///
|
||||
/// The tokens of the value follow after this header.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde_test::{assert_tokens, Token};
|
||||
/// #
|
||||
/// let opt = Some('c');
|
||||
/// assert_tokens(&opt, &[
|
||||
/// Token::Some,
|
||||
/// Token::Char('c'),
|
||||
/// ]);
|
||||
/// ```
|
||||
Some,
|
||||
|
||||
/// A serialized `()`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde_test::{assert_tokens, Token};
|
||||
/// #
|
||||
/// assert_tokens(&(), &[Token::Unit]);
|
||||
/// ```
|
||||
Unit,
|
||||
|
||||
/// A serialized unit struct of the given name.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde::{Serialize, Deserialize};
|
||||
/// # use serde_test::{assert_tokens, Token};
|
||||
/// #
|
||||
/// # fn main() {
|
||||
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
/// struct X;
|
||||
///
|
||||
/// assert_tokens(&X, &[Token::UnitStruct { name: "X" }]);
|
||||
/// # }
|
||||
/// ```
|
||||
UnitStruct { name: &'static str },
|
||||
|
||||
/// A unit variant of an enum.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde::{Serialize, Deserialize};
|
||||
/// # use serde_test::{assert_tokens, Token};
|
||||
/// #
|
||||
/// # fn main() {
|
||||
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
/// enum E {
|
||||
/// A,
|
||||
/// }
|
||||
///
|
||||
/// let a = E::A;
|
||||
/// assert_tokens(&a, &[Token::UnitVariant { name: "E", variant: "A" }]);
|
||||
/// # }
|
||||
/// ```
|
||||
UnitVariant {
|
||||
name: &'static str,
|
||||
variant: &'static str,
|
||||
},
|
||||
|
||||
/// The header to a serialized newtype struct of the given name.
|
||||
///
|
||||
/// After this header is the value contained in the newtype struct.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde::{Serialize, Deserialize};
|
||||
/// # use serde_test::{assert_tokens, Token};
|
||||
/// #
|
||||
/// # fn main() {
|
||||
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
/// struct N(String);
|
||||
///
|
||||
/// let n = N("newtype".to_owned());
|
||||
/// assert_tokens(&n, &[
|
||||
/// Token::NewtypeStruct { name: "N" },
|
||||
/// Token::String("newtype"),
|
||||
/// ]);
|
||||
/// # }
|
||||
/// ```
|
||||
NewtypeStruct { name: &'static str },
|
||||
|
||||
/// The header to a newtype variant of an enum.
|
||||
///
|
||||
/// After this header is the value contained in the newtype variant.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde::{Serialize, Deserialize};
|
||||
/// # use serde_test::{assert_tokens, Token};
|
||||
/// #
|
||||
/// # fn main() {
|
||||
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
/// enum E {
|
||||
/// B(u8),
|
||||
/// }
|
||||
///
|
||||
/// let b = E::B(0);
|
||||
/// assert_tokens(&b, &[
|
||||
/// Token::NewtypeVariant { name: "E", variant: "B" },
|
||||
/// Token::U8(0),
|
||||
/// ]);
|
||||
/// # }
|
||||
/// ```
|
||||
NewtypeVariant {
|
||||
name: &'static str,
|
||||
variant: &'static str,
|
||||
},
|
||||
|
||||
/// The header to a sequence.
|
||||
///
|
||||
/// After this header are the elements of the sequence, followed by
|
||||
/// `SeqEnd`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde_test::{assert_tokens, Token};
|
||||
/// #
|
||||
/// let vec = vec!['a', 'b', 'c'];
|
||||
/// assert_tokens(&vec, &[
|
||||
/// Token::Seq { len: Some(3) },
|
||||
/// Token::Char('a'),
|
||||
/// Token::Char('b'),
|
||||
/// Token::Char('c'),
|
||||
/// Token::SeqEnd,
|
||||
/// ]);
|
||||
/// ```
|
||||
Seq { len: Option<usize> },
|
||||
|
||||
/// An indicator of the end of a sequence.
|
||||
SeqEnd,
|
||||
|
||||
/// The header to a tuple.
|
||||
///
|
||||
/// After this header are the elements of the tuple, followed by `TupleEnd`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde_test::{assert_tokens, Token};
|
||||
/// #
|
||||
/// let tuple = ('a', 100);
|
||||
/// assert_tokens(&tuple, &[
|
||||
/// Token::Tuple { len: 2 },
|
||||
/// Token::Char('a'),
|
||||
/// Token::I32(100),
|
||||
/// Token::TupleEnd,
|
||||
/// ]);
|
||||
/// ```
|
||||
Tuple { len: usize },
|
||||
|
||||
/// An indicator of the end of a tuple.
|
||||
TupleEnd,
|
||||
|
||||
/// The header to a tuple struct.
|
||||
///
|
||||
/// After this header are the fields of the tuple struct, followed by
|
||||
/// `TupleStructEnd`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde::{Serialize, Deserialize};
|
||||
/// # use serde_test::{assert_tokens, Token};
|
||||
/// #
|
||||
/// # fn main() {
|
||||
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
/// struct T(u8, u8);
|
||||
///
|
||||
/// let t = T(0, 0);
|
||||
/// assert_tokens(&t, &[
|
||||
/// Token::TupleStruct { name: "T", len: 2 },
|
||||
/// Token::U8(0),
|
||||
/// Token::U8(0),
|
||||
/// Token::TupleStructEnd,
|
||||
/// ]);
|
||||
/// # }
|
||||
/// ```
|
||||
TupleStruct { name: &'static str, len: usize },
|
||||
|
||||
/// An indicator of the end of a tuple struct.
|
||||
TupleStructEnd,
|
||||
|
||||
/// The header to a tuple variant of an enum.
|
||||
///
|
||||
/// After this header are the fields of the tuple variant, followed by
|
||||
/// `TupleVariantEnd`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde::{Serialize, Deserialize};
|
||||
/// # use serde_test::{assert_tokens, Token};
|
||||
/// #
|
||||
/// # fn main() {
|
||||
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
/// enum E {
|
||||
/// C(u8, u8),
|
||||
/// }
|
||||
///
|
||||
/// let c = E::C(0, 0);
|
||||
/// assert_tokens(&c, &[
|
||||
/// Token::TupleVariant { name: "E", variant: "C", len: 2 },
|
||||
/// Token::U8(0),
|
||||
/// Token::U8(0),
|
||||
/// Token::TupleVariantEnd,
|
||||
/// ]);
|
||||
/// # }
|
||||
/// ```
|
||||
TupleVariant {
|
||||
name: &'static str,
|
||||
variant: &'static str,
|
||||
len: usize,
|
||||
},
|
||||
|
||||
/// An indicator of the end of a tuple variant.
|
||||
TupleVariantEnd,
|
||||
|
||||
/// The header to a map.
|
||||
///
|
||||
/// After this header are the entries of the map, followed by `MapEnd`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde_test::{assert_tokens, Token};
|
||||
/// #
|
||||
/// use std::collections::BTreeMap;
|
||||
///
|
||||
/// let mut map = BTreeMap::new();
|
||||
/// map.insert('A', 65);
|
||||
/// map.insert('Z', 90);
|
||||
///
|
||||
/// assert_tokens(&map, &[
|
||||
/// Token::Map { len: Some(2) },
|
||||
/// Token::Char('A'),
|
||||
/// Token::I32(65),
|
||||
/// Token::Char('Z'),
|
||||
/// Token::I32(90),
|
||||
/// Token::MapEnd,
|
||||
/// ]);
|
||||
/// ```
|
||||
Map { len: Option<usize> },
|
||||
|
||||
/// An indicator of the end of a map.
|
||||
MapEnd,
|
||||
|
||||
/// The header of a struct.
|
||||
///
|
||||
/// After this header are the fields of the struct, followed by `StructEnd`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde::{Serialize, Deserialize};
|
||||
/// # use serde_test::{assert_tokens, Token};
|
||||
/// #
|
||||
/// # fn main() {
|
||||
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
/// struct S {
|
||||
/// a: u8,
|
||||
/// b: u8,
|
||||
/// }
|
||||
///
|
||||
/// let s = S { a: 0, b: 0 };
|
||||
/// assert_tokens(&s, &[
|
||||
/// Token::Struct { name: "S", len: 2 },
|
||||
/// Token::Str("a"),
|
||||
/// Token::U8(0),
|
||||
/// Token::Str("b"),
|
||||
/// Token::U8(0),
|
||||
/// Token::StructEnd,
|
||||
/// ]);
|
||||
/// # }
|
||||
/// ```
|
||||
Struct { name: &'static str, len: usize },
|
||||
|
||||
/// An indicator of the end of a struct.
|
||||
StructEnd,
|
||||
|
||||
/// The header of a struct variant of an enum.
|
||||
///
|
||||
/// After this header are the fields of the struct variant, followed by
|
||||
/// `StructVariantEnd`.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde::{Serialize, Deserialize};
|
||||
/// # use serde_test::{assert_tokens, Token};
|
||||
/// #
|
||||
/// # fn main() {
|
||||
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
/// enum E {
|
||||
/// D { d: u8 },
|
||||
/// }
|
||||
///
|
||||
/// let d = E::D { d: 0 };
|
||||
/// assert_tokens(&d, &[
|
||||
/// Token::StructVariant { name: "E", variant: "D", len: 1 },
|
||||
/// Token::Str("d"),
|
||||
/// Token::U8(0),
|
||||
/// Token::StructVariantEnd,
|
||||
/// ]);
|
||||
/// # }
|
||||
/// ```
|
||||
StructVariant {
|
||||
name: &'static str,
|
||||
variant: &'static str,
|
||||
len: usize,
|
||||
},
|
||||
|
||||
/// An indicator of the end of a struct variant.
|
||||
StructVariantEnd,
|
||||
|
||||
/// The header to an enum of the given name.
|
||||
///
|
||||
/// ```edition2018
|
||||
/// # use serde::{Serialize, Deserialize};
|
||||
/// # use serde_test::{assert_tokens, Token};
|
||||
/// #
|
||||
/// # fn main() {
|
||||
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
/// enum E {
|
||||
/// A,
|
||||
/// B(u8),
|
||||
/// C(u8, u8),
|
||||
/// D { d: u8 },
|
||||
/// }
|
||||
///
|
||||
/// let a = E::A;
|
||||
/// assert_tokens(&a, &[
|
||||
/// Token::Enum { name: "E" },
|
||||
/// Token::Str("A"),
|
||||
/// Token::Unit,
|
||||
/// ]);
|
||||
///
|
||||
/// let b = E::B(0);
|
||||
/// assert_tokens(&b, &[
|
||||
/// Token::Enum { name: "E" },
|
||||
/// Token::Str("B"),
|
||||
/// Token::U8(0),
|
||||
/// ]);
|
||||
///
|
||||
/// let c = E::C(0, 0);
|
||||
/// assert_tokens(&c, &[
|
||||
/// Token::Enum { name: "E" },
|
||||
/// Token::Str("C"),
|
||||
/// Token::Seq { len: Some(2) },
|
||||
/// Token::U8(0),
|
||||
/// Token::U8(0),
|
||||
/// Token::SeqEnd,
|
||||
/// ]);
|
||||
///
|
||||
/// let d = E::D { d: 0 };
|
||||
/// assert_tokens(&d, &[
|
||||
/// Token::Enum { name: "E" },
|
||||
/// Token::Str("D"),
|
||||
/// Token::Map { len: Some(1) },
|
||||
/// Token::Str("d"),
|
||||
/// Token::U8(0),
|
||||
/// Token::MapEnd,
|
||||
/// ]);
|
||||
/// # }
|
||||
/// ```
|
||||
Enum { name: &'static str },
|
||||
}
|
||||
|
||||
impl Display for Token {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
Debug::fmt(self, formatter)
|
||||
}
|
||||
}
|
||||
+4
-10
@@ -2,26 +2,20 @@
|
||||
name = "serde_test_suite"
|
||||
version = "0.0.0"
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
||||
build = "build.rs"
|
||||
edition = "2018"
|
||||
publish = false
|
||||
|
||||
[features]
|
||||
expandtest = []
|
||||
unstable = ["serde/unstable"]
|
||||
|
||||
[dependencies]
|
||||
serde = { path = "../serde" }
|
||||
|
||||
[build-dependencies]
|
||||
toolchain_find = "0.2"
|
||||
|
||||
[dev-dependencies]
|
||||
automod = "1.0.1"
|
||||
fnv = "1.0"
|
||||
macrotest = "=1.0.9"
|
||||
prettyplease = "=0.1.14"
|
||||
rustversion = "1.0"
|
||||
serde = { path = "../serde", features = ["rc", "derive"] }
|
||||
serde = { path = "../serde", features = ["rc"] }
|
||||
serde_derive = { path = "../serde_derive", features = ["deserialize_in_place"] }
|
||||
serde_test = { path = "../serde_test" }
|
||||
trybuild = { version = "1.0.49", features = ["diff"] }
|
||||
serde_test = "1.0.176"
|
||||
trybuild = { version = "1.0.66", features = ["diff"] }
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
use std::process::{Command, ExitStatus, Stdio};
|
||||
|
||||
fn has_cargo_expand() -> bool {
|
||||
let cargo_expand = if cfg!(windows) {
|
||||
"cargo-expand.exe"
|
||||
} else {
|
||||
"cargo-expand"
|
||||
};
|
||||
|
||||
Command::new(cargo_expand)
|
||||
.arg("--version")
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.status()
|
||||
.as_ref()
|
||||
.map(ExitStatus::success)
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
if cfg!(feature = "expandtest") && has_cargo_expand() {
|
||||
println!("cargo:rustc-cfg=expandtest");
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ publish = false
|
||||
|
||||
[dependencies]
|
||||
libc = { version = "0.2", default-features = false }
|
||||
serde = { path = "../../serde", default-features = false, features = ["derive"] }
|
||||
serde = { path = "../../serde", default-features = false }
|
||||
serde_derive = { path = "../../serde_derive" }
|
||||
|
||||
[workspace]
|
||||
|
||||
@@ -19,7 +19,7 @@ fn panic(_info: &core::panic::PanicInfo) -> ! {
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
use serde::{Serialize, Deserialize};
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct Unit;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user