mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-27 18:07:57 +00:00
Compare commits
529 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ff3aee490a | |||
| 10bcfc0047 | |||
| 2f03899197 | |||
| 3bea3b6989 | |||
| 85bf29c446 | |||
| 900c922f41 | |||
| 9cf3f2ed7d | |||
| 1ffbeb9516 | |||
| 1051c5e6b7 | |||
| be255d62c8 | |||
| 263a1dbdd7 | |||
| 23dea60e1a | |||
| 690785f129 | |||
| 69aa109fd3 | |||
| 6bea2e04e4 | |||
| a1a82c3c55 | |||
| 7987ad700e | |||
| 0990d97a77 | |||
| a9150aad74 | |||
| 5ac3d84d99 | |||
| f916ec6baa | |||
| 7f831225a9 | |||
| a16893429b | |||
| 260511d149 | |||
| 7659fb686d | |||
| f669f16127 | |||
| 93f43cedaa | |||
| b6c7ce8ec3 | |||
| 35c963101b | |||
| 8909fc0c60 | |||
| 3c747e4585 | |||
| 1b89ff50a9 | |||
| c069b5c640 | |||
| 79f3484ab8 | |||
| ac8b7ea052 | |||
| c1b2f43917 | |||
| 51f8b0c52d | |||
| 6c4cae6b09 | |||
| 4da055a286 | |||
| 66c5d2b153 | |||
| 4bddf1b953 | |||
| 908f32175a | |||
| 3e1cf11060 | |||
| 723fcacad7 | |||
| 94acfe19e6 | |||
| b4677fde9d | |||
| c85e4240be | |||
| 363deb84cc | |||
| f9baf39dc3 | |||
| 3deb08946e | |||
| 43f5eb5c69 | |||
| 5fcfbed3ea | |||
| f3869307cc | |||
| babafa54d2 | |||
| ad6c548573 | |||
| 80b2f5f9e1 | |||
| 2130ba5788 | |||
| a1ddb18c92 | |||
| da3998acfb | |||
| b9de3658ad | |||
| 16af2d9ce7 | |||
| b426ff81e3 | |||
| b0e87aeecd | |||
| a685dcf680 | |||
| 88da17ca21 | |||
| d91f8ba950 | |||
| aa5aa611d4 | |||
| da0b473d60 | |||
| 49d098debd | |||
| 40f1d19dbe | |||
| 514848b584 | |||
| 168b6cf789 | |||
| 8b0e95b6de | |||
| e3a4165363 | |||
| 6ac8049b92 | |||
| 13a33b3c33 | |||
| a8bdd17333 | |||
| 1c9601358b | |||
| f0d1ae08f3 | |||
| e3eaa6a3dd | |||
| 6a630cf283 | |||
| 7bfd518dd4 | |||
| 723a9491e2 | |||
| 2b44efb085 | |||
| 03dc0fc137 | |||
| 85cb0c478e | |||
| abe7194480 | |||
| aaccac7413 | |||
| 7cd4d84cac | |||
| 04ff3e8f95 | |||
| dc3031b614 | |||
| 1e2f931d0c | |||
| 930401b0dd | |||
| cb6eaea151 | |||
| b6f339ca36 | |||
| 2a5caea1a8 | |||
| b9f93f99aa | |||
| eb5cd476ba | |||
| 8478a3b7dd | |||
| dbb909136e | |||
| ad8dd4148b | |||
| f91d2ed9ae | |||
| 9497463718 | |||
| 46e9ecfcdd | |||
| e9c399c822 | |||
| b9dbfcb4ac | |||
| c270e27a4d | |||
| 0307f604ea | |||
| 8939af48fe | |||
| fa5d58cd00 | |||
| 1a3cf4b3c1 | |||
| 7d96352e96 | |||
| 111ecc5d8c | |||
| edd6fe954b | |||
| a20e9249c5 | |||
| b1353a99cd | |||
| c59e876bb3 | |||
| 7f1e697c0d | |||
| 373edcd055 | |||
| f0b5c4f857 | |||
| 3035d4fa34 | |||
| 60ac737439 | |||
| a95b0d301e | |||
| 951ca5ace0 | |||
| adf05a5bf6 | |||
| 418062165f | |||
| 210373b3b6 | |||
| 5f9fffa53e | |||
| 9cda015733 | |||
| 58a8d22931 | |||
| ef0ed22593 | |||
| 79925ac394 | |||
| b60e4092ec | |||
| fdc36e5c06 | |||
| 49e11ce1ba | |||
| 7ae1b5f8f3 | |||
| 1ac054b34a | |||
| 1e36ef551d | |||
| 0058c7226e | |||
| 29d4f3e887 | |||
| 1b8310d98a | |||
| af4c388dff | |||
| 09f6d9361d | |||
| 1f9eb8300f | |||
| c6a5be7f6a | |||
| 0a06af8d21 | |||
| 3393ad6760 | |||
| 830309fcb5 | |||
| ab4f3f3111 | |||
| 00460b8dee | |||
| d4486be2b9 | |||
| 991e344804 | |||
| 6a7de26e5a | |||
| 5382ef3b0b | |||
| a8cbc9184e | |||
| 966e9ccf0c | |||
| 53ade10137 | |||
| 422c719352 | |||
| 3415619bfd | |||
| 04bb76bc00 | |||
| 8b0f482666 | |||
| 4b3178b053 | |||
| 8e1ae68569 | |||
| 31000e1874 | |||
| 4487cb26c7 | |||
| aba4e18553 | |||
| c82f2580b4 | |||
| 1d7b009aec | |||
| 89c4b02bf3 | |||
| eeb8e44cda | |||
| 785c2d9605 | |||
| d549f048e1 | |||
| 4c0dd63011 | |||
| 26fb134165 | |||
| 07e614b52b | |||
| b1f899fbe8 | |||
| b4f860e627 | |||
| d940fe1b49 | |||
| f2899a9e06 | |||
| 3aca38d2d3 | |||
| 30752ac4ff | |||
| b84e6ca4f5 | |||
| 87a2fb0f1a | |||
| 9eaf7b9824 | |||
| 7bde100237 | |||
| da7fc795ee | |||
| 4c5fec1363 | |||
| 6588b0ad37 | |||
| 0093f74cfe | |||
| 171c6da57a | |||
| 2dddc7796d | |||
| 8514f4119a | |||
| 98fb7d94aa | |||
| 31ca16d9bc | |||
| 9f72ce695e | |||
| c383e4f953 | |||
| a94d8750fb | |||
| b0d651be40 | |||
| d5a9c11b5c | |||
| 6bfe1c435a | |||
| a02da49b87 | |||
| 29dc6c3367 | |||
| a7f0bab078 | |||
| 3dc6829303 | |||
| 42e63ff942 | |||
| df07751e6f | |||
| 5445f1741b | |||
| 9d0f811221 | |||
| 36b9a859c4 | |||
| 59628d1712 | |||
| bee7470715 | |||
| 5a359e10f4 | |||
| 5e37ade519 | |||
| 1a9ffdbd0c | |||
| 2adb0e99b0 | |||
| 71ed1f2f12 | |||
| 47954502af | |||
| 4987fd15f7 | |||
| 8bfe0d0ac0 | |||
| 7c0e6bd18f | |||
| 41b9c33c2b | |||
| 28a775db22 | |||
| e999600f8f | |||
| d3492d8d36 | |||
| 48de0c51b0 | |||
| 93bda5f1dc | |||
| 2d75ef6b30 | |||
| f97160f715 | |||
| eb59c776ca | |||
| 9128201c78 | |||
| 2cbfd37072 | |||
| 0939214c51 | |||
| 8c60f5aea7 | |||
| da0ed4021d | |||
| 99f905403b | |||
| aa0654332d | |||
| af376c22c3 | |||
| 477eb7b70e | |||
| 026e91a68c | |||
| bfbedac919 | |||
| 4036ff88ed | |||
| 1b4da41f97 | |||
| f61d452814 | |||
| f9866097a0 | |||
| 8b769fcc20 | |||
| 9c954264f4 | |||
| 77a6a9d4e1 | |||
| 547d843cca | |||
| 005cb84593 | |||
| fd5b5e9aa5 | |||
| 0647a7c1fe | |||
| 85c73ef8de | |||
| 5ba1796a7e | |||
| e52b7b380f | |||
| 84c7419652 | |||
| 536221b1f9 | |||
| fc55ac70d3 | |||
| 2afe5b4ef9 | |||
| b4ec2595c9 | |||
| c3ac7b675a | |||
| 24614e44bf | |||
| 9b868ef831 | |||
| c3eaf76430 | |||
| 32958dec3b | |||
| d64a97ba1e | |||
| c3df3372a1 | |||
| 8764353fe2 | |||
| e08c5de5dd | |||
| 74b538b8ec | |||
| 291ec50d98 | |||
| bc5af506bd | |||
| 21c7fd1bd5 | |||
| 28a092261b | |||
| 18dcae0a77 | |||
| 58c307f9cc | |||
| 8cc4809414 | |||
| 1179158def | |||
| 91aa40e749 | |||
| 595019e979 | |||
| b0d7917f88 | |||
| 8e6637a1e4 | |||
| 694fe05953 | |||
| f3dfd2a237 | |||
| 9e6158e9e6 | |||
| d577c4a2ce | |||
| fee0b82a40 | |||
| 7aafa26314 | |||
| d5bc546ca5 | |||
| 45ae217728 | |||
| b7b97dda73 | |||
| 5d3c563d46 | |||
| 376185458b | |||
| a8f14840ab | |||
| 9e32a40b1c | |||
| 87f635e54d | |||
| d4b2dfbde2 | |||
| f6ab0bc56f | |||
| 48cc2a6327 | |||
| 3202a6858a | |||
| b4f1bc16ff | |||
| 029ab46f71 | |||
| 220ca0ca9d | |||
| 20306f493a | |||
| cc865ac523 | |||
| 2d973c1805 | |||
| 6ca499b2dd | |||
| 1477028717 | |||
| 789740be0d | |||
| 8fe7539bb2 | |||
| f6623a3654 | |||
| c4fb923335 | |||
| 65b7eea775 | |||
| 01cd696fd1 | |||
| c13b3f7e68 | |||
| a6571ee0da | |||
| 6e38afff49 | |||
| 3d1b19ed90 | |||
| 5b24f88e73 | |||
| 74d06708dd | |||
| 3bfab6ef7f | |||
| 364cd8d8f7 | |||
| 9f8c579bf5 | |||
| eae7c2d684 | |||
| 3f43fca90d | |||
| 76449488ca | |||
| 00c4b0cef8 | |||
| 89139e2c11 | |||
| c9cab490f7 | |||
| 5fa711d75d | |||
| f5d8ae423a | |||
| 1d54973b92 | |||
| b8fafefd85 | |||
| c42ebb8839 | |||
| 9e680620b5 | |||
| 846f865de2 | |||
| ede9762a58 | |||
| d438c2d67b | |||
| bef110b92a | |||
| b971ef11d1 | |||
| 29d9f69399 | |||
| aecb4083bd | |||
| 1c675ab3a3 | |||
| dd619630a3 | |||
| 111803ab07 | |||
| 0024f74f34 | |||
| 03eec42c33 | |||
| 196f311ae2 | |||
| 38d9e0b209 | |||
| 6502b31316 | |||
| 6f1a8c3115 | |||
| d883c94cc9 | |||
| 961fa59a74 | |||
| 8bc71def55 | |||
| 7c65a9dc0e | |||
| d2d977a6c6 | |||
| a9a6ee9d7f | |||
| 28c5d215c1 | |||
| 3d6a789562 | |||
| a0e68698e3 | |||
| 44613c7d01 | |||
| c706281df3 | |||
| 65d75b8fe3 | |||
| 332b0cba40 | |||
| 8c4af41296 | |||
| 24a78f071b | |||
| c91c33436d | |||
| 2083f43a28 | |||
| 4676abdc9e | |||
| 35700eb23e | |||
| 59892e7b58 | |||
| 97dd07a7d1 | |||
| c8bc97c81b | |||
| 9dacfbbd69 | |||
| 05c2509d07 | |||
| 64f949b37b | |||
| 3f339de36a | |||
| 215c2b71ef | |||
| ce8fef7e0b | |||
| 0726b2c479 | |||
| 589549d7e6 | |||
| edb1a586d8 | |||
| 11c2917040 | |||
| 6ba9c12ff6 | |||
| d2fcc346b9 | |||
| a091a07aa2 | |||
| bb4135cae8 | |||
| 8de84b7ca3 | |||
| 9cdf332029 | |||
| e94fc65f01 | |||
| b908487476 | |||
| 2a7c7faeb4 | |||
| e302e15eea | |||
| 1cbea892cf | |||
| 37a32857a2 | |||
| 8c4aad3a59 | |||
| 1774794b19 | |||
| 1af23f1f2d | |||
| 94fbc3d388 | |||
| 8da2058e2a | |||
| 09993a904a | |||
| 5d1403461c | |||
| ddc1ee564b | |||
| 00a54b848a | |||
| dad15b9fd0 | |||
| d89c19f903 | |||
| 146dc0fad5 | |||
| d26852deef | |||
| e1c27243b2 | |||
| dbbfe7abe2 | |||
| dc24d12ce0 | |||
| 4e7533e872 | |||
| 5d036515ed | |||
| a741293886 | |||
| f8d0b26d2f | |||
| 7007c1bea5 | |||
| 0d8ebac7e1 | |||
| 212c42c74f | |||
| 919f6be887 | |||
| c0f70428ba | |||
| e797c90ebd | |||
| fc04d1219a | |||
| df4ad5884d | |||
| 3c7dd6fc1e | |||
| 8b196ea1c8 | |||
| 1f8c8ad5a3 | |||
| 870925d503 | |||
| d593215ef7 | |||
| 110af31b48 | |||
| 360606b9a6 | |||
| 151b45ae36 | |||
| 2ea7e1300f | |||
| 4617c957b9 | |||
| 2547ed83ca | |||
| bfcd44704f | |||
| 7b548db91e | |||
| d39dea85ad | |||
| 5e56c9fba8 | |||
| 8d3a03288b | |||
| cb490ec16d | |||
| 45271c3676 | |||
| 6a097eca93 | |||
| 84d1c5385d | |||
| 05a5b7e3c6 | |||
| 3bff326fb3 | |||
| aaadd93878 | |||
| 9c864f0b02 | |||
| 070cce0d9c | |||
| b58e8bac12 | |||
| ada50b077e | |||
| 5e313a7330 | |||
| 2a36d11238 | |||
| b6685cf9dd | |||
| fc273c6763 | |||
| bd7b0e257e | |||
| 73931692b2 | |||
| 4d93e9f44c | |||
| da55ed7e8d | |||
| e3617e1f28 | |||
| 431636af0d | |||
| 891ced598a | |||
| 57dc0ee769 | |||
| 5e102c4da1 | |||
| 5c33931422 | |||
| 4aa54222f4 | |||
| ef4f860384 | |||
| 9bd52ec5c1 | |||
| 5cdd82d41e | |||
| 110bf10481 | |||
| 43035f6f37 | |||
| 83b1a3d5dc | |||
| 878110a4bc | |||
| 59ec8b7db2 | |||
| cae1b43829 | |||
| 99fde4ee3e | |||
| afe3872810 | |||
| 3a3e6bf103 | |||
| 935f0bd70f | |||
| 5c18bfeda6 | |||
| 957ef206d1 | |||
| 0c367838cc | |||
| 2023cf345f | |||
| 033d05f70b | |||
| fe4e3fd3b0 | |||
| 8a8a8a70ee | |||
| 339dca828d | |||
| 0d7349fa4e | |||
| 830528d5b1 | |||
| ab90fbc7c9 | |||
| 3eec111e8f | |||
| 9388433642 | |||
| ba12070665 | |||
| a57a324d72 | |||
| 92e91b3557 | |||
| 4dcf791706 | |||
| e77900fb94 | |||
| 1b14cadf20 | |||
| 4f59cd217a | |||
| 27c8b2d66a | |||
| 89976c2712 | |||
| c91737fef1 | |||
| a100719bc6 | |||
| 9a0e149225 | |||
| 9350927903 | |||
| 677c13a4ec | |||
| ee8e1ee7ff | |||
| f969080b9f | |||
| c2b16bfbb0 | |||
| e7df53701c | |||
| 02c34e490b | |||
| 427c839b3d | |||
| f709fc05b0 | |||
| 089aae1292 | |||
| 855acaf112 | |||
| 7ca7720262 | |||
| 78fea3aa4a | |||
| 1efb8b6a53 | |||
| bc1960b106 | |||
| 967023b755 | |||
| bb51e68f16 | |||
| 4e5e55bf1c | |||
| 4513a9e6a7 | |||
| 4f922e4e5b | |||
| 993966600e | |||
| 5b96cf1bde | |||
| f3d50e5209 | |||
| ab21d4d017 | |||
| f7c5d93e6a | |||
| 52a7d40e6e | |||
| 348bc6b257 |
@@ -0,0 +1 @@
|
|||||||
|
github: dtolnay
|
||||||
+47
-53
@@ -18,16 +18,22 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
timeout-minutes: 45
|
timeout-minutes: 45
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v4
|
||||||
- uses: dtolnay/rust-toolchain@nightly
|
- uses: dtolnay/rust-toolchain@nightly
|
||||||
- run: cd test_suite && cargo test --features unstable
|
- run: cd test_suite && cargo test --features unstable
|
||||||
|
- uses: actions/upload-artifact@v4
|
||||||
|
if: always()
|
||||||
|
with:
|
||||||
|
name: Cargo.lock
|
||||||
|
path: Cargo.lock
|
||||||
|
continue-on-error: true
|
||||||
|
|
||||||
windows:
|
windows:
|
||||||
name: Test suite (windows)
|
name: Test suite (windows)
|
||||||
runs-on: windows-latest
|
runs-on: windows-latest
|
||||||
timeout-minutes: 45
|
timeout-minutes: 45
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v4
|
||||||
- uses: dtolnay/rust-toolchain@nightly
|
- uses: dtolnay/rust-toolchain@nightly
|
||||||
- run: cd test_suite && cargo test --features unstable -- --skip ui --exact
|
- run: cd test_suite && cargo test --features unstable -- --skip ui --exact
|
||||||
|
|
||||||
@@ -40,15 +46,16 @@ jobs:
|
|||||||
rust: [stable, beta]
|
rust: [stable, beta]
|
||||||
timeout-minutes: 45
|
timeout-minutes: 45
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v4
|
||||||
- uses: dtolnay/rust-toolchain@master
|
- uses: dtolnay/rust-toolchain@master
|
||||||
with:
|
with:
|
||||||
toolchain: ${{matrix.rust}}
|
toolchain: ${{matrix.rust}}
|
||||||
- run: cd serde && cargo build --features rc
|
- run: cd serde && cargo build --features rc
|
||||||
- run: cd serde && cargo build --no-default-features
|
- run: cd serde && cargo build --no-default-features
|
||||||
|
- run: cd test_suite/no_std && cargo build
|
||||||
|
|
||||||
nightly:
|
nightly:
|
||||||
name: Rust nightly ${{matrix.os == 'windows' && '(windows)' || ''}}
|
name: Rust nightly${{matrix.os == 'windows' && ' (windows)' || ''}}
|
||||||
runs-on: ${{matrix.os}}-latest
|
runs-on: ${{matrix.os}}-latest
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
@@ -56,14 +63,14 @@ jobs:
|
|||||||
os: [ubuntu, windows]
|
os: [ubuntu, windows]
|
||||||
timeout-minutes: 45
|
timeout-minutes: 45
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v4
|
||||||
- uses: dtolnay/rust-toolchain@nightly
|
- uses: dtolnay/rust-toolchain@nightly
|
||||||
- run: cd serde && cargo build
|
- run: cd serde && cargo build
|
||||||
- run: cd serde && cargo build --no-default-features
|
- run: cd serde && cargo build --no-default-features
|
||||||
- run: cd serde && cargo build --no-default-features --features alloc
|
- run: cd serde && cargo build --no-default-features --features alloc
|
||||||
- run: cd serde && cargo build --no-default-features --features rc,alloc
|
- run: cd serde && cargo build --no-default-features --features rc,alloc
|
||||||
- run: cd serde && cargo build --no-default-features --features unstable
|
- run: cd serde && cargo build --no-default-features --features unstable
|
||||||
- run: cd serde && cargo test --features derive,rc,unstable
|
- run: cd serde_core && cargo test --features rc,unstable
|
||||||
- run: cd test_suite/no_std && cargo build
|
- run: cd test_suite/no_std && cargo build
|
||||||
if: matrix.os != 'windows'
|
if: matrix.os != 'windows'
|
||||||
- run: cd serde_derive && cargo check --tests
|
- run: cd serde_derive && cargo check --tests
|
||||||
@@ -77,84 +84,68 @@ jobs:
|
|||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
rust: [1.19.0, 1.20.0, 1.21.0, 1.25.0, 1.26.0, 1.34.0]
|
rust: [1.56.0, 1.60.0]
|
||||||
timeout-minutes: 45
|
timeout-minutes: 45
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v4
|
||||||
- uses: dtolnay/rust-toolchain@master
|
- uses: dtolnay/rust-toolchain@master
|
||||||
with:
|
with:
|
||||||
toolchain: ${{matrix.rust}}
|
toolchain: ${{matrix.rust}}
|
||||||
|
- run: sed -i '/"test_suite"/d' Cargo.toml
|
||||||
- run: cd serde && cargo build --features rc
|
- run: cd serde && cargo build --features rc
|
||||||
- run: cd serde && cargo build --no-default-features
|
- run: cd serde && cargo build --no-default-features
|
||||||
- run: cd serde && cargo build
|
- run: cd serde && cargo build --no-default-features --features alloc
|
||||||
|
|
||||||
more:
|
|
||||||
name: Rust ${{matrix.rust}}
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
strategy:
|
|
||||||
fail-fast: false
|
|
||||||
matrix:
|
|
||||||
rust: [1.27.0, 1.28.0]
|
|
||||||
timeout-minutes: 45
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v3
|
|
||||||
- uses: dtolnay/rust-toolchain@master
|
|
||||||
with:
|
|
||||||
toolchain: ${{matrix.rust}}
|
|
||||||
# Work around failing to parse manifest because editions are unstable.
|
|
||||||
- run: sed -i /test_suite/d Cargo.toml
|
|
||||||
- run: cd serde && cargo build --no-default-features
|
|
||||||
- run: cd serde && cargo build
|
- run: cd serde && cargo build
|
||||||
|
|
||||||
derive:
|
derive:
|
||||||
name: Rust 1.56.0
|
name: Rust 1.61.0
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
timeout-minutes: 45
|
timeout-minutes: 45
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v4
|
||||||
- uses: dtolnay/rust-toolchain@1.56.0
|
- uses: dtolnay/rust-toolchain@1.61.0
|
||||||
|
- run: |
|
||||||
|
sed -i 's/proc-macro2 = { workspace = true/proc-macro2 = { version = "1"/' serde_derive*/Cargo.toml
|
||||||
|
sed -i 's/quote = { workspace = true/quote = { version = "1"/' serde_derive*/Cargo.toml
|
||||||
|
sed -i 's/syn = { workspace = true/syn = { version = "2"/' serde_derive*/Cargo.toml
|
||||||
- run: cd serde && cargo check --no-default-features
|
- run: cd serde && cargo check --no-default-features
|
||||||
- run: cd serde && cargo check
|
- run: cd serde && cargo check
|
||||||
- run: cd serde_derive && cargo check
|
- run: cd serde_derive && cargo check
|
||||||
- run: cd precompiled/serde_derive && cargo check
|
|
||||||
|
|
||||||
alloc:
|
|
||||||
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
|
|
||||||
|
|
||||||
macos:
|
|
||||||
name: macOS
|
|
||||||
runs-on: macos-latest
|
|
||||||
timeout-minutes: 45
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v3
|
|
||||||
- uses: dtolnay/rust-toolchain@stable
|
|
||||||
- run: cd precompiled/serde_derive && cargo check
|
|
||||||
|
|
||||||
minimal:
|
minimal:
|
||||||
name: Minimal versions
|
name: Minimal versions
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
timeout-minutes: 45
|
timeout-minutes: 45
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v4
|
||||||
- uses: dtolnay/rust-toolchain@nightly
|
- uses: dtolnay/rust-toolchain@nightly
|
||||||
- run: cargo generate-lockfile -Z minimal-versions
|
- run: cargo generate-lockfile -Z minimal-versions
|
||||||
- run: cargo check --locked --workspace
|
- run: cargo check --locked --workspace
|
||||||
|
|
||||||
|
doc:
|
||||||
|
name: Documentation
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
timeout-minutes: 45
|
||||||
|
env:
|
||||||
|
RUSTDOCFLAGS: -Dwarnings
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- uses: dtolnay/rust-toolchain@nightly
|
||||||
|
- uses: dtolnay/install@cargo-docs-rs
|
||||||
|
- run: cargo docs-rs -p serde
|
||||||
|
- run: cargo docs-rs -p serde_derive
|
||||||
|
- run: cargo docs-rs -p serde_derive_internals
|
||||||
|
|
||||||
clippy:
|
clippy:
|
||||||
name: Clippy
|
name: Clippy
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
if: github.event_name != 'pull_request'
|
if: github.event_name != 'pull_request'
|
||||||
timeout-minutes: 45
|
timeout-minutes: 45
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v4
|
||||||
- uses: dtolnay/rust-toolchain@clippy
|
- uses: dtolnay/rust-toolchain@clippy
|
||||||
- run: cd serde && cargo clippy --features rc,unstable -- -Dclippy::all -Dclippy::pedantic
|
- run: cd serde && cargo clippy --features rc,unstable -- -Dclippy::all -Dclippy::pedantic
|
||||||
|
- run: cd serde_core && cargo clippy --features rc,unstable -- -Dclippy::all -Dclippy::pedantic
|
||||||
- run: cd serde_derive && cargo clippy -- -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_derive_internals && cargo clippy -- -Dclippy::all -Dclippy::pedantic
|
||||||
- run: cd test_suite && cargo clippy --tests --features unstable -- -Dclippy::all -Dclippy::pedantic
|
- run: cd test_suite && cargo clippy --tests --features unstable -- -Dclippy::all -Dclippy::pedantic
|
||||||
@@ -165,10 +156,12 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
timeout-minutes: 45
|
timeout-minutes: 45
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v4
|
||||||
- uses: dtolnay/rust-toolchain@miri
|
- uses: dtolnay/rust-toolchain@miri
|
||||||
|
with:
|
||||||
|
toolchain: nightly-2025-05-16 # https://github.com/rust-lang/miri/issues/4323
|
||||||
- run: cargo miri setup
|
- run: cargo miri setup
|
||||||
- run: cd serde && cargo miri test --features derive,rc,unstable
|
- run: cd serde_core && cargo miri test --features rc,unstable
|
||||||
env:
|
env:
|
||||||
MIRIFLAGS: -Zmiri-strict-provenance
|
MIRIFLAGS: -Zmiri-strict-provenance
|
||||||
- run: cd test_suite && cargo miri test --features unstable
|
- run: cd test_suite && cargo miri test --features unstable
|
||||||
@@ -181,6 +174,7 @@ jobs:
|
|||||||
if: github.event_name != 'pull_request'
|
if: github.event_name != 'pull_request'
|
||||||
timeout-minutes: 45
|
timeout-minutes: 45
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v4
|
||||||
|
- uses: dtolnay/rust-toolchain@stable
|
||||||
- uses: dtolnay/install@cargo-outdated
|
- uses: dtolnay/install@cargo-outdated
|
||||||
- run: cargo outdated --workspace --exit-code 1
|
- run: cargo outdated --workspace --exit-code 1
|
||||||
|
|||||||
+2
-4
@@ -1,4 +1,2 @@
|
|||||||
target/
|
/target/
|
||||||
**/*.rs.bk
|
/Cargo.lock
|
||||||
*.sw[po]
|
|
||||||
Cargo.lock
|
|
||||||
|
|||||||
@@ -1,10 +1,18 @@
|
|||||||
[workspace]
|
[workspace]
|
||||||
members = [
|
members = [
|
||||||
"serde",
|
"serde",
|
||||||
|
"serde_core",
|
||||||
"serde_derive",
|
"serde_derive",
|
||||||
"serde_derive_internals",
|
"serde_derive_internals",
|
||||||
"test_suite",
|
"test_suite",
|
||||||
]
|
]
|
||||||
|
resolver = "2"
|
||||||
|
|
||||||
[patch.crates-io]
|
[patch.crates-io]
|
||||||
serde = { path = "serde" }
|
serde = { path = "serde" }
|
||||||
|
serde_core = { path = "serde_core" }
|
||||||
|
|
||||||
|
[workspace.dependencies]
|
||||||
|
proc-macro2 = { version = "1.0.74", default-features = false }
|
||||||
|
quote = { version = "1.0.35", default-features = false }
|
||||||
|
syn = { version = "2.0.81", default-features = false }
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
# Serde   [![Build Status]][actions] [![Latest Version]][crates.io] [![serde: rustc 1.19+]][Rust 1.19] [![serde_derive: rustc 1.56+]][Rust 1.56]
|
# Serde   [![Build Status]][actions] [![Latest Version]][crates.io] [![serde msrv]][Rust 1.56] [![serde_derive msrv]][Rust 1.61]
|
||||||
|
|
||||||
[Build Status]: https://img.shields.io/github/actions/workflow/status/serde-rs/serde/ci.yml?branch=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
|
[actions]: https://github.com/serde-rs/serde/actions?query=branch%3Amaster
|
||||||
[Latest Version]: https://img.shields.io/crates/v/serde.svg
|
[Latest Version]: https://img.shields.io/crates/v/serde.svg
|
||||||
[crates.io]: https://crates.io/crates/serde
|
[crates.io]: https://crates.io/crates/serde
|
||||||
[serde: rustc 1.19+]: https://img.shields.io/badge/serde-rustc_1.19+-lightgray.svg
|
[serde msrv]: https://img.shields.io/crates/msrv/serde.svg?label=serde%20msrv&color=lightgray
|
||||||
[serde_derive: rustc 1.56+]: https://img.shields.io/badge/serde_derive-rustc_1.56+-lightgray.svg
|
[serde_derive msrv]: https://img.shields.io/crates/msrv/serde_derive.svg?label=serde_derive%20msrv&color=lightgray
|
||||||
[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
|
[Rust 1.56]: https://blog.rust-lang.org/2021/10/21/Rust-1.56.0.html
|
||||||
|
[Rust 1.61]: https://blog.rust-lang.org/2022/05/19/Rust-1.61.0.html
|
||||||
|
|
||||||
**Serde is a framework for *ser*ializing and *de*serializing Rust data structures efficiently and generically.**
|
**Serde is a framework for *ser*ializing and *de*serializing Rust data structures efficiently and generically.**
|
||||||
|
|
||||||
@@ -15,7 +15,7 @@
|
|||||||
|
|
||||||
You may be looking for:
|
You may be looking for:
|
||||||
|
|
||||||
- [An overview of Serde](https://serde.rs/)
|
- [An overview of Serde](https://serde.rs)
|
||||||
- [Data formats supported by Serde](https://serde.rs/#data-formats)
|
- [Data formats supported by Serde](https://serde.rs/#data-formats)
|
||||||
- [Setting up `#[derive(Serialize, Deserialize)]`](https://serde.rs/derive.html)
|
- [Setting up `#[derive(Serialize, Deserialize)]`](https://serde.rs/derive.html)
|
||||||
- [Examples](https://serde.rs/examples.html)
|
- [Examples](https://serde.rs/examples.html)
|
||||||
@@ -27,7 +27,7 @@ You may be looking for:
|
|||||||
<details>
|
<details>
|
||||||
<summary>
|
<summary>
|
||||||
Click to show Cargo.toml.
|
Click to show Cargo.toml.
|
||||||
<a href="https://play.rust-lang.org/?edition=2018&gist=72755f28f99afc95e01d63174b28c1f5" target="_blank">Run this code in the playground.</a>
|
<a href="https://play.rust-lang.org/?edition=2021&gist=72755f28f99afc95e01d63174b28c1f5" target="_blank">Run this code in the playground.</a>
|
||||||
</summary>
|
</summary>
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
|
|||||||
+3
-3
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
You may be looking for:
|
You may be looking for:
|
||||||
|
|
||||||
- [An overview of Serde](https://serde.rs/)
|
- [An overview of Serde](https://serde.rs)
|
||||||
- [Data formats supported by Serde](https://serde.rs/#data-formats)
|
- [Data formats supported by Serde](https://serde.rs/#data-formats)
|
||||||
- [Setting up `#[derive(Serialize, Deserialize)]`](https://serde.rs/derive.html)
|
- [Setting up `#[derive(Serialize, Deserialize)]`](https://serde.rs/derive.html)
|
||||||
- [Examples](https://serde.rs/examples.html)
|
- [Examples](https://serde.rs/examples.html)
|
||||||
@@ -46,8 +46,8 @@ fn main() {
|
|||||||
Serde is one of the most widely used Rust libraries so any place that Rustaceans
|
Serde is one of the most widely used Rust libraries so any place that Rustaceans
|
||||||
congregate will be able to help you out. For chat, consider trying the
|
congregate will be able to help you out. For chat, consider trying the
|
||||||
[#rust-questions] or [#rust-beginners] channels of the unofficial community
|
[#rust-questions] or [#rust-beginners] channels of the unofficial community
|
||||||
Discord (invite: <https://discord.gg/rust-lang-community>, the [#rust-usage] or
|
Discord (invite: <https://discord.gg/rust-lang-community>), the [#rust-usage]
|
||||||
[#beginners] channels of the official Rust Project Discord (invite:
|
or [#beginners] channels of the official Rust Project Discord (invite:
|
||||||
<https://discord.gg/rust-lang>), or the [#general][zulip] stream in Zulip. For
|
<https://discord.gg/rust-lang>), or the [#general][zulip] stream in Zulip. For
|
||||||
asynchronous, consider the [\[rust\] tag on StackOverflow][stackoverflow], the
|
asynchronous, consider the [\[rust\] tag on StackOverflow][stackoverflow], the
|
||||||
[/r/rust] subreddit which has a pinned weekly easy questions post, or the Rust
|
[/r/rust] subreddit which has a pinned weekly easy questions post, or the Rust
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
/serde_derive/serde_derive-x86_64-unknown-linux-gnu
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
[workspace]
|
|
||||||
members = ["bin", "proc-macro2"]
|
|
||||||
resolver = "2"
|
|
||||||
|
|
||||||
[patch.crates-io]
|
|
||||||
proc-macro2 = { path = "proc-macro2" }
|
|
||||||
|
|
||||||
[profile.precompiled]
|
|
||||||
inherits = "release"
|
|
||||||
codegen-units = 1
|
|
||||||
lto = true
|
|
||||||
opt-level = "z"
|
|
||||||
panic = "abort"
|
|
||||||
strip = true
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "serde_derive"
|
|
||||||
version = "1.0.178"
|
|
||||||
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"] }
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
fn main() {
|
|
||||||
println!("cargo:rustc-cfg=precompiled");
|
|
||||||
println!("cargo:rustc-cfg=feature=\"deserialize_in_place\"");
|
|
||||||
}
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
extern crate proc_macro2;
|
|
||||||
|
|
||||||
use proc_macro2::watt;
|
|
||||||
use proc_macro2::watt::buffer::InputBuffer;
|
|
||||||
use std::alloc::{GlobalAlloc, Layout, System};
|
|
||||||
use std::io::{self, Read, Write};
|
|
||||||
use std::sync::atomic::Ordering;
|
|
||||||
|
|
||||||
struct MonotonicAllocator;
|
|
||||||
|
|
||||||
#[global_allocator]
|
|
||||||
static ALLOCATOR: MonotonicAllocator = MonotonicAllocator;
|
|
||||||
|
|
||||||
unsafe impl GlobalAlloc for MonotonicAllocator {
|
|
||||||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
|
||||||
System.alloc(layout)
|
|
||||||
}
|
|
||||||
|
|
||||||
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {
|
|
||||||
// Leak: this cuts 3% of code size from the precompiled macro binary.
|
|
||||||
// There is no way that serde_derive would fill up all memory on the
|
|
||||||
// host. When the subprocess exits, operating system will clean this up.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
let mut buf = Vec::new();
|
|
||||||
io::stdin().read_to_end(&mut buf).unwrap();
|
|
||||||
|
|
||||||
let mut buf = InputBuffer::new(&buf);
|
|
||||||
let derive = match buf.read_u8() {
|
|
||||||
0 => serde_derive::derive_serialize,
|
|
||||||
1 => serde_derive::derive_deserialize,
|
|
||||||
2 => {
|
|
||||||
serde_derive::DESERIALIZE_IN_PLACE.store(true, Ordering::Relaxed);
|
|
||||||
serde_derive::derive_deserialize
|
|
||||||
}
|
|
||||||
_ => unreachable!(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let input = watt::load(&mut buf);
|
|
||||||
let output = derive(input);
|
|
||||||
let bytes = watt::linearize(output);
|
|
||||||
io::stdout().write_all(&bytes).unwrap();
|
|
||||||
}
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../serde_derive/src
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null
|
|
||||||
set -e -x
|
|
||||||
|
|
||||||
# TODO: Sanitize host filesystem paths. https://github.com/rust-lang/cargo/issues/12137
|
|
||||||
|
|
||||||
cargo +nightly build \
|
|
||||||
--manifest-path bin/Cargo.toml \
|
|
||||||
--bin serde_derive \
|
|
||||||
--profile precompiled \
|
|
||||||
-Z unstable-options \
|
|
||||||
-Z build-std=std,panic_abort \
|
|
||||||
-Z build-std-features=panic_immediate_abort \
|
|
||||||
--target x86_64-unknown-linux-musl \
|
|
||||||
--out-dir serde_derive
|
|
||||||
|
|
||||||
rm -f serde_derive/serde_derive-x86_64-unknown-linux-gnu
|
|
||||||
mv serde_derive/serde_derive{,-x86_64-unknown-linux-gnu}
|
|
||||||
|
|
||||||
#upx --best --lzma serde_derive/serde_derive-x86_64-unknown-linux-gnu
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "proc-macro2"
|
|
||||||
version = "1.0.66"
|
|
||||||
edition = "2021"
|
|
||||||
publish = false
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
proc-macro2 = { package = "proc-macro2-fallback", version = "1" }
|
|
||||||
@@ -1,815 +0,0 @@
|
|||||||
#[doc(hidden)]
|
|
||||||
pub mod watt;
|
|
||||||
|
|
||||||
use crate::extra::DelimSpan;
|
|
||||||
use crate::watt::Identity;
|
|
||||||
use std::cmp::Ordering;
|
|
||||||
use std::fmt::{self, Debug, Display};
|
|
||||||
use std::hash::{Hash, Hasher};
|
|
||||||
use std::ops::RangeBounds;
|
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
pub use proc_macro2::{Delimiter, Spacing};
|
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
|
||||||
pub struct Span {
|
|
||||||
lo: u32,
|
|
||||||
hi: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Span {
|
|
||||||
pub fn call_site() -> Self {
|
|
||||||
Span { lo: 0, hi: 0 }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn join(&self, other: Self) -> Option<Self> {
|
|
||||||
Some(Span {
|
|
||||||
lo: self.lo,
|
|
||||||
hi: other.hi,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub enum TokenTree {
|
|
||||||
Group(Group),
|
|
||||||
Ident(Ident),
|
|
||||||
Punct(Punct),
|
|
||||||
Literal(Literal),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TokenTree {
|
|
||||||
pub fn span(&self) -> Span {
|
|
||||||
match self {
|
|
||||||
TokenTree::Group(group) => group.span(),
|
|
||||||
TokenTree::Ident(ident) => ident.span(),
|
|
||||||
TokenTree::Punct(punct) => punct.span(),
|
|
||||||
TokenTree::Literal(literal) => literal.span(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_span(&mut self, span: Span) {
|
|
||||||
match self {
|
|
||||||
TokenTree::Group(group) => group.set_span(span),
|
|
||||||
TokenTree::Ident(ident) => ident.set_span(span),
|
|
||||||
TokenTree::Punct(punct) => punct.set_span(span),
|
|
||||||
TokenTree::Literal(literal) => literal.set_span(span),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Group> for TokenTree {
|
|
||||||
fn from(group: Group) -> Self {
|
|
||||||
TokenTree::Group(group)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Ident> for TokenTree {
|
|
||||||
fn from(ident: Ident) -> Self {
|
|
||||||
TokenTree::Ident(ident)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Punct> for TokenTree {
|
|
||||||
fn from(punct: Punct) -> Self {
|
|
||||||
TokenTree::Punct(punct)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Literal> for TokenTree {
|
|
||||||
fn from(literal: Literal) -> Self {
|
|
||||||
TokenTree::Literal(literal)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for TokenTree {
|
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
match self {
|
|
||||||
TokenTree::Group(group) => Debug::fmt(group, formatter),
|
|
||||||
TokenTree::Ident(ident) => {
|
|
||||||
let mut debug = formatter.debug_struct("Ident");
|
|
||||||
debug.field("sym", &format_args!("{}", ident));
|
|
||||||
debug.finish()
|
|
||||||
}
|
|
||||||
TokenTree::Punct(punct) => Debug::fmt(punct, formatter),
|
|
||||||
TokenTree::Literal(literal) => Debug::fmt(literal, formatter),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Group {
|
|
||||||
delimiter: Delimiter,
|
|
||||||
stream: Vec<TokenTree>,
|
|
||||||
span: Span,
|
|
||||||
span_open: Span,
|
|
||||||
span_close: Span,
|
|
||||||
identity: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Group {
|
|
||||||
pub fn new(delimiter: Delimiter, stream: TokenStream) -> Self {
|
|
||||||
Group {
|
|
||||||
delimiter,
|
|
||||||
stream: stream.content,
|
|
||||||
span: Span::call_site(),
|
|
||||||
span_open: Span::call_site(),
|
|
||||||
span_close: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn stream(&self) -> TokenStream {
|
|
||||||
TokenStream {
|
|
||||||
content: self.stream.clone(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn delimiter(&self) -> Delimiter {
|
|
||||||
self.delimiter
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn span(&self) -> Span {
|
|
||||||
self.span
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn span_open(&self) -> Span {
|
|
||||||
self.span_open
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn span_close(&self) -> Span {
|
|
||||||
self.span_close
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn delim_span(&self) -> DelimSpan {
|
|
||||||
DelimSpan {
|
|
||||||
join: self.span,
|
|
||||||
open: self.span_open,
|
|
||||||
close: self.span_close,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_span(&mut self, span: Span) {
|
|
||||||
self.span = span;
|
|
||||||
self.identity |= Identity::RESPANNED;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for Group {
|
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
let (open, close) = match self.delimiter {
|
|
||||||
Delimiter::Parenthesis => ("(", ")"),
|
|
||||||
Delimiter::Brace => ("{ ", "}"),
|
|
||||||
Delimiter::Bracket => ("[", "]"),
|
|
||||||
Delimiter::None => ("", ""),
|
|
||||||
};
|
|
||||||
|
|
||||||
formatter.write_str(open)?;
|
|
||||||
display_tokens(&self.stream, formatter)?;
|
|
||||||
if self.delimiter == Delimiter::Brace && !self.stream.is_empty() {
|
|
||||||
formatter.write_str(" ")?;
|
|
||||||
}
|
|
||||||
formatter.write_str(close)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for Group {
|
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
let mut debug = formatter.debug_struct("Group");
|
|
||||||
debug.field("delimiter", &self.delimiter);
|
|
||||||
debug.field("stream", &self.stream);
|
|
||||||
debug.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Ident {
|
|
||||||
fallback: proc_macro2::Ident,
|
|
||||||
span: Span,
|
|
||||||
identity: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Ident {
|
|
||||||
pub fn new(string: &str, span: Span) -> Self {
|
|
||||||
Ident {
|
|
||||||
fallback: proc_macro2::Ident::new(string, proc_macro2::Span::call_site()),
|
|
||||||
span,
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new_raw(string: &str, span: Span) -> Self {
|
|
||||||
Ident {
|
|
||||||
fallback: proc_macro2::Ident::new_raw(string, proc_macro2::Span::call_site()),
|
|
||||||
span,
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn span(&self) -> Span {
|
|
||||||
self.span
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_span(&mut self, span: Span) {
|
|
||||||
self.span = span;
|
|
||||||
self.identity |= Identity::RESPANNED;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for Ident {
|
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
Display::fmt(&self.fallback, formatter)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for Ident {
|
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
Debug::fmt(&self.fallback, formatter)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Eq for Ident {}
|
|
||||||
|
|
||||||
impl PartialEq for Ident {
|
|
||||||
fn eq(&self, other: &Self) -> bool {
|
|
||||||
PartialEq::eq(&self.fallback, &other.fallback)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> PartialEq<T> for Ident
|
|
||||||
where
|
|
||||||
T: ?Sized + AsRef<str>,
|
|
||||||
{
|
|
||||||
fn eq(&self, other: &T) -> bool {
|
|
||||||
PartialEq::eq(&self.fallback, other)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Ord for Ident {
|
|
||||||
fn cmp(&self, other: &Self) -> Ordering {
|
|
||||||
Ord::cmp(&self.fallback, &other.fallback)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialOrd for Ident {
|
|
||||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
||||||
PartialOrd::partial_cmp(&self.fallback, &other.fallback)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Hash for Ident {
|
|
||||||
fn hash<H: Hasher>(&self, hasher: &mut H) {
|
|
||||||
Hash::hash(&self.fallback, hasher);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Punct {
|
|
||||||
fallback: proc_macro2::Punct,
|
|
||||||
span: Span,
|
|
||||||
identity: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Punct {
|
|
||||||
pub fn new(ch: char, spacing: Spacing) -> Self {
|
|
||||||
Punct {
|
|
||||||
fallback: proc_macro2::Punct::new(ch, spacing),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn as_char(&self) -> char {
|
|
||||||
self.fallback.as_char()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn spacing(&self) -> Spacing {
|
|
||||||
self.fallback.spacing()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn span(&self) -> Span {
|
|
||||||
self.span
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_span(&mut self, span: Span) {
|
|
||||||
self.span = span;
|
|
||||||
self.identity |= Identity::RESPANNED;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for Punct {
|
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
Display::fmt(&self.fallback, formatter)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for Punct {
|
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
Debug::fmt(&self.fallback, formatter)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Literal {
|
|
||||||
fallback: proc_macro2::Literal,
|
|
||||||
span: Span,
|
|
||||||
identity: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Literal {
|
|
||||||
pub fn u8_suffixed(n: u8) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::u8_suffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn u16_suffixed(n: u16) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::u16_suffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn u32_suffixed(n: u32) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::u32_suffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn u64_suffixed(n: u64) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::u64_suffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn u128_suffixed(n: u128) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::u128_suffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn usize_suffixed(n: usize) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::usize_suffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn i8_suffixed(n: i8) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::i8_suffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn i16_suffixed(n: i16) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::i16_suffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn i32_suffixed(n: i32) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::i32_suffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn i64_suffixed(n: i64) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::i64_suffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn i128_suffixed(n: i128) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::i128_suffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn isize_suffixed(n: isize) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::isize_suffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn u8_unsuffixed(n: u8) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::u8_unsuffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn u16_unsuffixed(n: u16) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::u16_unsuffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn u32_unsuffixed(n: u32) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::u32_unsuffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn u64_unsuffixed(n: u64) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::u64_unsuffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn u128_unsuffixed(n: u128) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::u128_unsuffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn usize_unsuffixed(n: usize) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::usize_unsuffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn i8_unsuffixed(n: i8) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::i8_unsuffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn i16_unsuffixed(n: i16) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::i16_unsuffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn i32_unsuffixed(n: i32) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::i32_unsuffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn i64_unsuffixed(n: i64) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::i64_unsuffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn i128_unsuffixed(n: i128) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::i128_unsuffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn isize_unsuffixed(n: isize) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::isize_unsuffixed(n),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn f64_unsuffixed(f: f64) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::f64_unsuffixed(f),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn f64_suffixed(f: f64) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::f64_suffixed(f),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn f32_unsuffixed(f: f32) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::f32_unsuffixed(f),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn f32_suffixed(f: f32) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::f32_suffixed(f),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn string(string: &str) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::string(string),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn character(ch: char) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::character(ch),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn byte_string(s: &[u8]) -> Self {
|
|
||||||
Literal {
|
|
||||||
fallback: proc_macro2::Literal::byte_string(s),
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn span(&self) -> Span {
|
|
||||||
self.span
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_span(&mut self, span: Span) {
|
|
||||||
self.span = span;
|
|
||||||
self.identity |= Identity::RESPANNED;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn subspan<R: RangeBounds<usize>>(&self, range: R) -> Option<Span> {
|
|
||||||
let _ = range;
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromStr for Literal {
|
|
||||||
type Err = LexError;
|
|
||||||
|
|
||||||
fn from_str(repr: &str) -> Result<Self, Self::Err> {
|
|
||||||
let fallback = match proc_macro2::Literal::from_str(repr) {
|
|
||||||
Ok(literal) => literal,
|
|
||||||
Err(error) => {
|
|
||||||
return Err(LexError {
|
|
||||||
fallback: error,
|
|
||||||
span: Span::call_site(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Ok(Literal {
|
|
||||||
fallback,
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for Literal {
|
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
Display::fmt(&self.fallback, formatter)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for Literal {
|
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
Debug::fmt(&self.fallback, formatter)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct TokenStream {
|
|
||||||
content: Vec<TokenTree>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TokenStream {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
TokenStream {
|
|
||||||
content: Vec::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_empty(&self) -> bool {
|
|
||||||
self.content.is_empty()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl IntoIterator for TokenStream {
|
|
||||||
type Item = TokenTree;
|
|
||||||
type IntoIter = token_stream::IntoIter;
|
|
||||||
|
|
||||||
fn into_iter(self) -> Self::IntoIter {
|
|
||||||
token_stream::IntoIter {
|
|
||||||
iter: self.content.into_iter(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Extend<TokenStream> for TokenStream {
|
|
||||||
fn extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I) {
|
|
||||||
self.content.extend(streams.into_iter().flatten());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Extend<TokenTree> for TokenStream {
|
|
||||||
fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, streams: I) {
|
|
||||||
self.content.extend(streams);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromIterator<TokenStream> for TokenStream {
|
|
||||||
fn from_iter<I: IntoIterator<Item = TokenStream>>(streams: I) -> Self {
|
|
||||||
let content = streams.into_iter().flatten().collect();
|
|
||||||
TokenStream { content }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromIterator<TokenTree> for TokenStream {
|
|
||||||
fn from_iter<I: IntoIterator<Item = TokenTree>>(streams: I) -> Self {
|
|
||||||
let content = streams.into_iter().collect();
|
|
||||||
TokenStream { content }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromStr for TokenStream {
|
|
||||||
type Err = LexError;
|
|
||||||
|
|
||||||
fn from_str(string: &str) -> Result<Self, Self::Err> {
|
|
||||||
let fallback = match proc_macro2::TokenStream::from_str(string) {
|
|
||||||
Ok(token_stream) => token_stream,
|
|
||||||
Err(error) => {
|
|
||||||
return Err(LexError {
|
|
||||||
fallback: error,
|
|
||||||
span: Span::call_site(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
fn convert_token_stream(stream: proc_macro2::TokenStream) -> TokenStream {
|
|
||||||
TokenStream {
|
|
||||||
content: stream.into_iter().map(convert_token_tree).collect(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn convert_token_tree(token: proc_macro2::TokenTree) -> TokenTree {
|
|
||||||
match token {
|
|
||||||
proc_macro2::TokenTree::Group(group) => TokenTree::Group(Group::new(
|
|
||||||
group.delimiter(),
|
|
||||||
convert_token_stream(group.stream()),
|
|
||||||
)),
|
|
||||||
proc_macro2::TokenTree::Ident(ident) => TokenTree::Ident(Ident {
|
|
||||||
fallback: ident,
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}),
|
|
||||||
proc_macro2::TokenTree::Punct(punct) => TokenTree::Punct(Punct {
|
|
||||||
fallback: punct,
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}),
|
|
||||||
proc_macro2::TokenTree::Literal(literal) => TokenTree::Literal(Literal {
|
|
||||||
fallback: literal,
|
|
||||||
span: Span::call_site(),
|
|
||||||
identity: Identity::NOVEL,
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(convert_token_stream(fallback))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn display_tokens(tokens: &[TokenTree], formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
let mut joint = false;
|
|
||||||
for (i, token) in tokens.iter().enumerate() {
|
|
||||||
if i != 0 && !joint {
|
|
||||||
write!(formatter, " ")?;
|
|
||||||
}
|
|
||||||
joint = false;
|
|
||||||
match token {
|
|
||||||
TokenTree::Group(group) => Display::fmt(group, formatter),
|
|
||||||
TokenTree::Ident(ident) => Display::fmt(ident, formatter),
|
|
||||||
TokenTree::Punct(punct) => {
|
|
||||||
joint = punct.spacing() == Spacing::Joint;
|
|
||||||
Display::fmt(punct, formatter)
|
|
||||||
}
|
|
||||||
TokenTree::Literal(literal) => Display::fmt(literal, formatter),
|
|
||||||
}?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for TokenStream {
|
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
display_tokens(&self.content, formatter)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for TokenStream {
|
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
formatter.write_str("TokenStream ")?;
|
|
||||||
formatter.debug_list().entries(&self.content).finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct LexError {
|
|
||||||
fallback: proc_macro2::LexError,
|
|
||||||
span: Span,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl LexError {
|
|
||||||
pub fn span(&self) -> Span {
|
|
||||||
self.span
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for LexError {
|
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
Debug::fmt(&self.fallback, formatter)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub mod token_stream {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct IntoIter {
|
|
||||||
pub(crate) iter: <Vec<TokenTree> as IntoIterator>::IntoIter,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Iterator for IntoIter {
|
|
||||||
type Item = TokenTree;
|
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
|
||||||
self.iter.next()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
||||||
self.iter.size_hint()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub mod extra {
|
|
||||||
use crate::Span;
|
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
|
||||||
pub struct DelimSpan {
|
|
||||||
pub(crate) join: Span,
|
|
||||||
pub(crate) open: Span,
|
|
||||||
pub(crate) close: Span,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DelimSpan {
|
|
||||||
pub fn join(&self) -> Span {
|
|
||||||
self.join
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn open(&self) -> Span {
|
|
||||||
self.open
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn close(&self) -> Span {
|
|
||||||
self.close
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,69 +0,0 @@
|
|||||||
use std::str;
|
|
||||||
|
|
||||||
pub struct OutputBuffer {
|
|
||||||
bytes: Vec<u8>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl OutputBuffer {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
OutputBuffer { bytes: Vec::new() }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write_u8(&mut self, value: u8) {
|
|
||||||
self.bytes.push(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write_u16(&mut self, value: u16) {
|
|
||||||
self.bytes.extend_from_slice(&value.to_le_bytes());
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write_u32(&mut self, value: u32) {
|
|
||||||
self.bytes.extend_from_slice(&value.to_le_bytes());
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write_str(&mut self, value: &str) {
|
|
||||||
self.bytes.extend_from_slice(value.as_bytes());
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn into_bytes(self) -> Vec<u8> {
|
|
||||||
self.bytes
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct InputBuffer<'a> {
|
|
||||||
bytes: &'a [u8],
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> InputBuffer<'a> {
|
|
||||||
pub fn new(bytes: &'a [u8]) -> Self {
|
|
||||||
InputBuffer { bytes }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_empty(&self) -> bool {
|
|
||||||
self.bytes.is_empty()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read_u8(&mut self) -> u8 {
|
|
||||||
let (first, rest) = self.bytes.split_first().unwrap();
|
|
||||||
self.bytes = rest;
|
|
||||||
*first
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read_u16(&mut self) -> u16 {
|
|
||||||
let (value, rest) = self.bytes.split_at(2);
|
|
||||||
self.bytes = rest;
|
|
||||||
u16::from_le_bytes([value[0], value[1]])
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read_u32(&mut self) -> u32 {
|
|
||||||
let (value, rest) = self.bytes.split_at(4);
|
|
||||||
self.bytes = rest;
|
|
||||||
u32::from_le_bytes([value[0], value[1], value[2], value[3]])
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read_str(&mut self, len: usize) -> &'a str {
|
|
||||||
let (string, rest) = self.bytes.split_at(len);
|
|
||||||
self.bytes = rest;
|
|
||||||
str::from_utf8(string).unwrap()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
pub enum Bytecode {}
|
|
||||||
|
|
||||||
impl Bytecode {
|
|
||||||
pub const GROUP_PARENTHESIS: u8 = 0;
|
|
||||||
pub const GROUP_BRACE: u8 = 1;
|
|
||||||
pub const GROUP_BRACKET: u8 = 2;
|
|
||||||
pub const GROUP_NONE: u8 = 3;
|
|
||||||
pub const IDENT: u8 = 4;
|
|
||||||
pub const PUNCT_ALONE: u8 = 5;
|
|
||||||
pub const PUNCT_JOINT: u8 = 6;
|
|
||||||
pub const LITERAL: u8 = 7;
|
|
||||||
pub const LOAD_GROUP: u8 = 8;
|
|
||||||
pub const LOAD_IDENT: u8 = 9;
|
|
||||||
pub const LOAD_PUNCT: u8 = 10;
|
|
||||||
pub const LOAD_LITERAL: u8 = 11;
|
|
||||||
pub const SET_SPAN: u8 = 12;
|
|
||||||
}
|
|
||||||
@@ -1,205 +0,0 @@
|
|||||||
pub mod buffer;
|
|
||||||
pub mod bytecode;
|
|
||||||
|
|
||||||
use crate::watt::buffer::{InputBuffer, OutputBuffer};
|
|
||||||
use crate::watt::bytecode::Bytecode;
|
|
||||||
use crate::{Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};
|
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
pub enum Kind {
|
|
||||||
Group(Delimiter),
|
|
||||||
Ident,
|
|
||||||
Punct(Spacing),
|
|
||||||
Literal,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub enum Identity {}
|
|
||||||
|
|
||||||
impl Identity {
|
|
||||||
pub const RESPANNED: u32 = 1 << 31;
|
|
||||||
pub const NOVEL: u32 = u32::MAX;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Span {
|
|
||||||
fn is_call_site(&self) -> bool {
|
|
||||||
self.lo == 0 && self.hi == 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn post_increment(counter: &mut u32) -> impl FnMut() -> u32 + '_ {
|
|
||||||
|| {
|
|
||||||
let value = *counter;
|
|
||||||
*counter += 1;
|
|
||||||
value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn load(buf: &mut InputBuffer) -> TokenStream {
|
|
||||||
let mut span_counter = 1;
|
|
||||||
let mut next_span = post_increment(&mut span_counter);
|
|
||||||
let mut next_span = || {
|
|
||||||
let next = next_span();
|
|
||||||
Span { lo: next, hi: next }
|
|
||||||
};
|
|
||||||
|
|
||||||
let [mut group_counter, mut ident_counter, mut punct_counter, mut literal_counter] = [0; 4];
|
|
||||||
let mut next_group = post_increment(&mut group_counter);
|
|
||||||
let mut next_ident = post_increment(&mut ident_counter);
|
|
||||||
let mut next_punct = post_increment(&mut punct_counter);
|
|
||||||
let mut next_literal = post_increment(&mut literal_counter);
|
|
||||||
|
|
||||||
let mut trees = Vec::new();
|
|
||||||
while !buf.is_empty() {
|
|
||||||
match match buf.read_u8() {
|
|
||||||
Bytecode::GROUP_PARENTHESIS => Kind::Group(Delimiter::Parenthesis),
|
|
||||||
Bytecode::GROUP_BRACE => Kind::Group(Delimiter::Brace),
|
|
||||||
Bytecode::GROUP_BRACKET => Kind::Group(Delimiter::Bracket),
|
|
||||||
Bytecode::GROUP_NONE => Kind::Group(Delimiter::None),
|
|
||||||
Bytecode::IDENT => Kind::Ident,
|
|
||||||
Bytecode::PUNCT_ALONE => Kind::Punct(Spacing::Alone),
|
|
||||||
Bytecode::PUNCT_JOINT => Kind::Punct(Spacing::Joint),
|
|
||||||
Bytecode::LITERAL => Kind::Literal,
|
|
||||||
_ => unreachable!(),
|
|
||||||
} {
|
|
||||||
Kind::Group(delimiter) => {
|
|
||||||
let len = buf.read_u32();
|
|
||||||
let stream = trees.drain(trees.len() - len as usize..).collect();
|
|
||||||
trees.push(TokenTree::Group(Group {
|
|
||||||
delimiter,
|
|
||||||
stream,
|
|
||||||
span: next_span(),
|
|
||||||
span_open: next_span(),
|
|
||||||
span_close: next_span(),
|
|
||||||
identity: next_group(),
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
Kind::Ident => {
|
|
||||||
let len = buf.read_u16();
|
|
||||||
let repr = buf.read_str(len as usize);
|
|
||||||
let ident = if let Some(repr) = repr.strip_prefix("r#") {
|
|
||||||
proc_macro2::Ident::new_raw(repr, proc_macro2::Span::call_site())
|
|
||||||
} else if repr == "$crate" {
|
|
||||||
proc_macro2::Ident::new("crate", proc_macro2::Span::call_site())
|
|
||||||
} else {
|
|
||||||
proc_macro2::Ident::new(repr, proc_macro2::Span::call_site())
|
|
||||||
};
|
|
||||||
trees.push(TokenTree::Ident(Ident {
|
|
||||||
fallback: ident,
|
|
||||||
span: next_span(),
|
|
||||||
identity: next_ident(),
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
Kind::Punct(spacing) => {
|
|
||||||
let ch = buf.read_u8();
|
|
||||||
assert!(ch.is_ascii());
|
|
||||||
let punct = proc_macro2::Punct::new(ch as char, spacing);
|
|
||||||
trees.push(TokenTree::Punct(Punct {
|
|
||||||
fallback: punct,
|
|
||||||
span: next_span(),
|
|
||||||
identity: next_punct(),
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
Kind::Literal => {
|
|
||||||
let len = buf.read_u16();
|
|
||||||
let repr = buf.read_str(len as usize);
|
|
||||||
let literal = proc_macro2::Literal::from_str(repr).unwrap();
|
|
||||||
trees.push(TokenTree::Literal(Literal {
|
|
||||||
fallback: literal,
|
|
||||||
span: next_span(),
|
|
||||||
identity: next_literal(),
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TokenStream { content: trees }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn linearize(tokens: TokenStream) -> Vec<u8> {
|
|
||||||
let mut buf = OutputBuffer::new();
|
|
||||||
for token in &tokens.content {
|
|
||||||
linearize_token(token, &mut buf);
|
|
||||||
}
|
|
||||||
buf.into_bytes()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn linearize_token(token: &TokenTree, buf: &mut OutputBuffer) {
|
|
||||||
let needs_span;
|
|
||||||
match token {
|
|
||||||
TokenTree::Group(group) => {
|
|
||||||
if group.identity < Identity::NOVEL {
|
|
||||||
buf.write_u8(Bytecode::LOAD_GROUP);
|
|
||||||
buf.write_u32(group.identity & !Identity::RESPANNED);
|
|
||||||
needs_span = group.identity >= Identity::RESPANNED;
|
|
||||||
} else {
|
|
||||||
let len = group.stream.len();
|
|
||||||
assert!(len <= u32::MAX as usize);
|
|
||||||
for token in &group.stream {
|
|
||||||
linearize_token(token, buf);
|
|
||||||
}
|
|
||||||
buf.write_u8(match group.delimiter {
|
|
||||||
Delimiter::Parenthesis => Bytecode::GROUP_PARENTHESIS,
|
|
||||||
Delimiter::Brace => Bytecode::GROUP_BRACE,
|
|
||||||
Delimiter::Bracket => Bytecode::GROUP_BRACKET,
|
|
||||||
Delimiter::None => Bytecode::GROUP_NONE,
|
|
||||||
});
|
|
||||||
buf.write_u32(len as u32);
|
|
||||||
needs_span = !group.span.is_call_site();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
TokenTree::Ident(ident) => {
|
|
||||||
if ident.identity < Identity::NOVEL {
|
|
||||||
buf.write_u8(Bytecode::LOAD_IDENT);
|
|
||||||
buf.write_u32(ident.identity & !Identity::RESPANNED);
|
|
||||||
needs_span = ident.identity >= Identity::RESPANNED;
|
|
||||||
} else {
|
|
||||||
buf.write_u8(Bytecode::IDENT);
|
|
||||||
let repr = ident.to_string();
|
|
||||||
assert!(repr.len() <= u16::MAX as usize);
|
|
||||||
buf.write_u16(repr.len() as u16);
|
|
||||||
buf.write_str(&repr);
|
|
||||||
linearize_span(ident.span, buf);
|
|
||||||
needs_span = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
TokenTree::Punct(punct) => {
|
|
||||||
if punct.identity < Identity::NOVEL {
|
|
||||||
buf.write_u8(Bytecode::LOAD_PUNCT);
|
|
||||||
buf.write_u32(punct.identity & !Identity::RESPANNED);
|
|
||||||
needs_span = punct.identity >= Identity::RESPANNED;
|
|
||||||
} else {
|
|
||||||
buf.write_u8(match punct.spacing() {
|
|
||||||
Spacing::Alone => Bytecode::PUNCT_ALONE,
|
|
||||||
Spacing::Joint => Bytecode::PUNCT_JOINT,
|
|
||||||
});
|
|
||||||
let ch = punct.as_char();
|
|
||||||
assert!(ch.is_ascii());
|
|
||||||
buf.write_u8(ch as u8);
|
|
||||||
needs_span = !punct.span.is_call_site();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
TokenTree::Literal(literal) => {
|
|
||||||
if literal.identity < Identity::NOVEL {
|
|
||||||
buf.write_u8(Bytecode::LOAD_LITERAL);
|
|
||||||
buf.write_u32(literal.identity & !Identity::RESPANNED);
|
|
||||||
needs_span = literal.identity >= Identity::RESPANNED;
|
|
||||||
} else {
|
|
||||||
buf.write_u8(Bytecode::LITERAL);
|
|
||||||
let repr = literal.to_string();
|
|
||||||
assert!(repr.len() <= u16::MAX as usize);
|
|
||||||
buf.write_u16(repr.len() as u16);
|
|
||||||
buf.write_str(&repr);
|
|
||||||
needs_span = !literal.span.is_call_site();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if needs_span {
|
|
||||||
buf.write_u8(Bytecode::SET_SPAN);
|
|
||||||
linearize_span(token.span(), buf);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn linearize_span(span: Span, buf: &mut OutputBuffer) {
|
|
||||||
buf.write_u32(span.lo);
|
|
||||||
buf.write_u32(span.hi);
|
|
||||||
}
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "serde_derive"
|
|
||||||
version = "1.0.178"
|
|
||||||
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 @@
|
|||||||
../../LICENSE-APACHE
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../LICENSE-MIT
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../README.md
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../crates-io.md
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../serde_derive/src/bound.rs
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../proc-macro2/src/watt/buffer.rs
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../proc-macro2/src/watt/bytecode.rs
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../serde_derive/src/de.rs
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../serde_derive/src/dummy.rs
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../serde_derive/src/fragment.rs
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../serde_derive/src/internals/
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
//! This crate provides Serde's two derive macros.
|
|
||||||
//!
|
|
||||||
//! ```edition2021
|
|
||||||
//! # use serde_derive::{Deserialize, Serialize};
|
|
||||||
//! #
|
|
||||||
//! #[derive(Serialize, Deserialize)]
|
|
||||||
//! # struct S;
|
|
||||||
//! #
|
|
||||||
//! # fn main() {}
|
|
||||||
//! ```
|
|
||||||
//!
|
|
||||||
//! Please refer to [https://serde.rs/derive.html] for how to set this up.
|
|
||||||
//!
|
|
||||||
//! [https://serde.rs/derive.html]: https://serde.rs/derive.html
|
|
||||||
|
|
||||||
#![doc(html_root_url = "https://docs.rs/serde_derive/1.0.178")]
|
|
||||||
|
|
||||||
#[cfg(not(all(target_arch = "x86_64", target_os = "linux", target_env = "gnu")))]
|
|
||||||
include!("lib_from_source.rs");
|
|
||||||
|
|
||||||
#[cfg(all(target_arch = "x86_64", target_os = "linux", target_env = "gnu"))]
|
|
||||||
include!("lib_precompiled.rs");
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
extern crate proc_macro;
|
|
||||||
extern crate proc_macro2;
|
|
||||||
extern crate quote;
|
|
||||||
extern crate syn;
|
|
||||||
|
|
||||||
#[macro_use]
|
|
||||||
mod bound;
|
|
||||||
#[macro_use]
|
|
||||||
mod fragment;
|
|
||||||
|
|
||||||
mod de;
|
|
||||||
mod dummy;
|
|
||||||
mod internals;
|
|
||||||
mod pretend;
|
|
||||||
mod ser;
|
|
||||||
mod this;
|
|
||||||
|
|
||||||
use proc_macro::TokenStream;
|
|
||||||
use syn::{parse_macro_input, DeriveInput};
|
|
||||||
|
|
||||||
#[proc_macro_derive(Serialize, attributes(serde))]
|
|
||||||
pub fn derive_serialize(input: TokenStream) -> TokenStream {
|
|
||||||
let mut input = parse_macro_input!(input as DeriveInput);
|
|
||||||
ser::expand_derive_serialize(&mut input)
|
|
||||||
.unwrap_or_else(syn::Error::into_compile_error)
|
|
||||||
.into()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[proc_macro_derive(Deserialize, attributes(serde))]
|
|
||||||
pub fn derive_deserialize(input: TokenStream) -> TokenStream {
|
|
||||||
let mut input = parse_macro_input!(input as DeriveInput);
|
|
||||||
de::expand_derive_deserialize(&mut input)
|
|
||||||
.unwrap_or_else(syn::Error::into_compile_error)
|
|
||||||
.into()
|
|
||||||
}
|
|
||||||
@@ -1,234 +0,0 @@
|
|||||||
extern crate proc_macro;
|
|
||||||
|
|
||||||
mod buffer;
|
|
||||||
mod bytecode;
|
|
||||||
|
|
||||||
use crate::buffer::{InputBuffer, OutputBuffer};
|
|
||||||
use crate::bytecode::Bytecode;
|
|
||||||
use proc_macro::{Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};
|
|
||||||
use std::io::{ErrorKind, Read, Write};
|
|
||||||
use std::iter::FromIterator;
|
|
||||||
use std::path::Path;
|
|
||||||
use std::process::{Command, ExitStatus, Stdio};
|
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
#[proc_macro_derive(Serialize, attributes(serde))]
|
|
||||||
pub fn derive_serialize(input: TokenStream) -> TokenStream {
|
|
||||||
derive(0, input)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[proc_macro_derive(Deserialize, attributes(serde))]
|
|
||||||
pub fn derive_deserialize(input: TokenStream) -> TokenStream {
|
|
||||||
derive(1 + cfg!(feature = "deserialize_in_place") as u8, input)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn derive(select: u8, input: TokenStream) -> TokenStream {
|
|
||||||
let mut memory = TokenMemory::default();
|
|
||||||
let mut buf = OutputBuffer::new();
|
|
||||||
buf.write_u8(select);
|
|
||||||
|
|
||||||
memory.spans.push(Span::call_site());
|
|
||||||
for token in input {
|
|
||||||
memory.linearize_token(token, &mut buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
let exe_path = Path::new(concat!(
|
|
||||||
env!("CARGO_MANIFEST_DIR"),
|
|
||||||
"/serde_derive-x86_64-unknown-linux-gnu",
|
|
||||||
));
|
|
||||||
let mut child = match Command::new(exe_path)
|
|
||||||
.stdin(Stdio::piped())
|
|
||||||
.stdout(Stdio::piped())
|
|
||||||
.spawn()
|
|
||||||
{
|
|
||||||
Ok(child) => child,
|
|
||||||
Err(io_error) => {
|
|
||||||
if io_error.kind() == ErrorKind::NotFound {
|
|
||||||
panic!(
|
|
||||||
"file missing from serde_derive manifest directory during macro expansion: {}",
|
|
||||||
exe_path.display(),
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
panic!("failed to spawn process: {}", io_error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut stdin = child.stdin.take().unwrap();
|
|
||||||
let mut buf = buf.into_bytes();
|
|
||||||
stdin.write_all(&buf).unwrap();
|
|
||||||
drop(stdin);
|
|
||||||
|
|
||||||
let mut stdout = child.stdout.take().unwrap();
|
|
||||||
buf.clear();
|
|
||||||
stdout.read_to_end(&mut buf).unwrap();
|
|
||||||
|
|
||||||
let success = child.wait().as_ref().map_or(true, ExitStatus::success);
|
|
||||||
if !success || buf.is_empty() {
|
|
||||||
panic!();
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut buf = InputBuffer::new(&buf);
|
|
||||||
memory.receive(&mut buf)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
struct TokenMemory {
|
|
||||||
spans: Vec<Span>,
|
|
||||||
groups: Vec<Group>,
|
|
||||||
idents: Vec<Ident>,
|
|
||||||
puncts: Vec<Punct>,
|
|
||||||
literals: Vec<Literal>,
|
|
||||||
}
|
|
||||||
|
|
||||||
enum Kind {
|
|
||||||
Group(Delimiter),
|
|
||||||
Ident,
|
|
||||||
Punct(Spacing),
|
|
||||||
Literal,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TokenMemory {
|
|
||||||
// Depth-first post-order traversal.
|
|
||||||
fn linearize_token(&mut self, token: TokenTree, buf: &mut OutputBuffer) {
|
|
||||||
match token {
|
|
||||||
TokenTree::Group(group) => {
|
|
||||||
let mut len = 0usize;
|
|
||||||
for token in group.stream() {
|
|
||||||
self.linearize_token(token, buf);
|
|
||||||
len += 1;
|
|
||||||
}
|
|
||||||
assert!(len <= u32::MAX as usize);
|
|
||||||
buf.write_u8(match group.delimiter() {
|
|
||||||
Delimiter::Parenthesis => Bytecode::GROUP_PARENTHESIS,
|
|
||||||
Delimiter::Brace => Bytecode::GROUP_BRACE,
|
|
||||||
Delimiter::Bracket => Bytecode::GROUP_BRACKET,
|
|
||||||
Delimiter::None => Bytecode::GROUP_NONE,
|
|
||||||
});
|
|
||||||
buf.write_u32(len as u32);
|
|
||||||
self.spans
|
|
||||||
.extend([group.span(), group.span_open(), group.span_close()]);
|
|
||||||
self.groups.push(group);
|
|
||||||
}
|
|
||||||
TokenTree::Ident(ident) => {
|
|
||||||
buf.write_u8(Bytecode::IDENT);
|
|
||||||
let repr = ident.to_string();
|
|
||||||
assert!(repr.len() <= u16::MAX as usize);
|
|
||||||
buf.write_u16(repr.len() as u16);
|
|
||||||
buf.write_str(&repr);
|
|
||||||
self.spans.push(ident.span());
|
|
||||||
self.idents.push(ident);
|
|
||||||
}
|
|
||||||
TokenTree::Punct(punct) => {
|
|
||||||
buf.write_u8(match punct.spacing() {
|
|
||||||
Spacing::Alone => Bytecode::PUNCT_ALONE,
|
|
||||||
Spacing::Joint => Bytecode::PUNCT_JOINT,
|
|
||||||
});
|
|
||||||
let ch = punct.as_char();
|
|
||||||
assert!(ch.is_ascii());
|
|
||||||
buf.write_u8(ch as u8);
|
|
||||||
self.spans.push(punct.span());
|
|
||||||
self.puncts.push(punct);
|
|
||||||
}
|
|
||||||
TokenTree::Literal(literal) => {
|
|
||||||
buf.write_u8(Bytecode::LITERAL);
|
|
||||||
let repr = literal.to_string();
|
|
||||||
assert!(repr.len() <= u16::MAX as usize);
|
|
||||||
buf.write_u16(repr.len() as u16);
|
|
||||||
buf.write_str(&repr);
|
|
||||||
self.spans.push(literal.span());
|
|
||||||
self.literals.push(literal);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn receive(&self, buf: &mut InputBuffer) -> TokenStream {
|
|
||||||
let mut trees = Vec::new();
|
|
||||||
while !buf.is_empty() {
|
|
||||||
match match buf.read_u8() {
|
|
||||||
Bytecode::GROUP_PARENTHESIS => Kind::Group(Delimiter::Parenthesis),
|
|
||||||
Bytecode::GROUP_BRACE => Kind::Group(Delimiter::Brace),
|
|
||||||
Bytecode::GROUP_BRACKET => Kind::Group(Delimiter::Bracket),
|
|
||||||
Bytecode::GROUP_NONE => Kind::Group(Delimiter::None),
|
|
||||||
Bytecode::IDENT => Kind::Ident,
|
|
||||||
Bytecode::PUNCT_ALONE => Kind::Punct(Spacing::Alone),
|
|
||||||
Bytecode::PUNCT_JOINT => Kind::Punct(Spacing::Joint),
|
|
||||||
Bytecode::LITERAL => Kind::Literal,
|
|
||||||
Bytecode::LOAD_GROUP => {
|
|
||||||
let identity = buf.read_u32();
|
|
||||||
let group = self.groups[identity as usize].clone();
|
|
||||||
trees.push(TokenTree::Group(group));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
Bytecode::LOAD_IDENT => {
|
|
||||||
let identity = buf.read_u32();
|
|
||||||
let ident = self.idents[identity as usize].clone();
|
|
||||||
trees.push(TokenTree::Ident(ident));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
Bytecode::LOAD_PUNCT => {
|
|
||||||
let identity = buf.read_u32();
|
|
||||||
let punct = self.puncts[identity as usize].clone();
|
|
||||||
trees.push(TokenTree::Punct(punct));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
Bytecode::LOAD_LITERAL => {
|
|
||||||
let identity = buf.read_u32();
|
|
||||||
let literal = self.literals[identity as usize].clone();
|
|
||||||
trees.push(TokenTree::Literal(literal));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
Bytecode::SET_SPAN => {
|
|
||||||
trees.last_mut().unwrap().set_span(self.read_span(buf));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
_ => unreachable!(),
|
|
||||||
} {
|
|
||||||
Kind::Group(delimiter) => {
|
|
||||||
let len = buf.read_u32();
|
|
||||||
let stream = trees.drain(trees.len() - len as usize..).collect();
|
|
||||||
let group = Group::new(delimiter, stream);
|
|
||||||
trees.push(TokenTree::Group(group));
|
|
||||||
}
|
|
||||||
Kind::Ident => {
|
|
||||||
let len = buf.read_u16();
|
|
||||||
let repr = buf.read_str(len as usize);
|
|
||||||
let span = self.read_span(buf);
|
|
||||||
let ident = if let Some(repr) = repr.strip_prefix("r#") {
|
|
||||||
Ident::new_raw(repr, span)
|
|
||||||
} else {
|
|
||||||
Ident::new(repr, span)
|
|
||||||
};
|
|
||||||
trees.push(TokenTree::Ident(ident));
|
|
||||||
}
|
|
||||||
Kind::Punct(spacing) => {
|
|
||||||
let ch = buf.read_u8();
|
|
||||||
assert!(ch.is_ascii());
|
|
||||||
let punct = Punct::new(ch as char, spacing);
|
|
||||||
trees.push(TokenTree::Punct(punct));
|
|
||||||
}
|
|
||||||
Kind::Literal => {
|
|
||||||
let len = buf.read_u16();
|
|
||||||
let repr = buf.read_str(len as usize);
|
|
||||||
let literal = Literal::from_str(repr).unwrap();
|
|
||||||
trees.push(TokenTree::Literal(literal));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TokenStream::from_iter(trees)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_span(&self, buf: &mut InputBuffer) -> Span {
|
|
||||||
let lo = buf.read_u32();
|
|
||||||
let hi = buf.read_u32();
|
|
||||||
let span = self.spans[lo as usize];
|
|
||||||
if lo == hi {
|
|
||||||
span
|
|
||||||
} else {
|
|
||||||
#[cfg(any())] // FIXME
|
|
||||||
return span.join(self.spans[hi as usize]).unwrap_or(span);
|
|
||||||
span
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../serde_derive/src/pretend.rs
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../serde_derive/src/ser.rs
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../serde_derive/src/this.rs
|
|
||||||
+16
-15
@@ -1,34 +1,35 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde"
|
name = "serde"
|
||||||
version = "1.0.178" # remember to update html_root_url and serde_derive dependency
|
version = "1.0.221"
|
||||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
||||||
build = "build.rs"
|
build = "build.rs"
|
||||||
categories = ["encoding", "no-std", "no-std::no-alloc"]
|
categories = ["encoding", "no-std", "no-std::no-alloc"]
|
||||||
description = "A generic serialization/deserialization framework"
|
description = "A generic serialization/deserialization framework"
|
||||||
documentation = "https://docs.rs/serde"
|
documentation = "https://docs.rs/serde"
|
||||||
|
edition = "2021"
|
||||||
homepage = "https://serde.rs"
|
homepage = "https://serde.rs"
|
||||||
keywords = ["serde", "serialization", "no_std"]
|
keywords = ["serde", "serialization", "no_std"]
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
readme = "crates-io.md"
|
readme = "crates-io.md"
|
||||||
repository = "https://github.com/serde-rs/serde"
|
repository = "https://github.com/serde-rs/serde"
|
||||||
rust-version = "1.19"
|
rust-version = "1.56"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
serde_derive = { version = "=1.0.178", optional = true, path = "../serde_derive" }
|
serde_core = { version = "=1.0.221", path = "../serde_core", default-features = false, features = ["result"] }
|
||||||
|
serde_derive = { version = "1", optional = true, path = "../serde_derive" }
|
||||||
[dev-dependencies]
|
|
||||||
serde_derive = { version = "1", path = "../serde_derive" }
|
|
||||||
|
|
||||||
[lib]
|
|
||||||
doc-scrape-examples = false
|
|
||||||
|
|
||||||
[package.metadata.playground]
|
[package.metadata.playground]
|
||||||
features = ["derive", "rc"]
|
features = ["derive", "rc"]
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
features = ["derive"]
|
features = ["derive", "rc", "unstable"]
|
||||||
targets = ["x86_64-unknown-linux-gnu"]
|
targets = ["x86_64-unknown-linux-gnu"]
|
||||||
rustdoc-args = ["--generate-link-to-definition"]
|
rustdoc-args = [
|
||||||
|
"--generate-link-to-definition",
|
||||||
|
"--extern-html-root-url=core=https://doc.rust-lang.org",
|
||||||
|
"--extern-html-root-url=alloc=https://doc.rust-lang.org",
|
||||||
|
"--extern-html-root-url=std=https://doc.rust-lang.org",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
### FEATURES #################################################################
|
### FEATURES #################################################################
|
||||||
@@ -41,20 +42,20 @@ derive = ["serde_derive"]
|
|||||||
|
|
||||||
# Provide impls for common standard library types like Vec<T> and HashMap<K, V>.
|
# Provide impls for common standard library types like Vec<T> and HashMap<K, V>.
|
||||||
# Requires a dependency on the Rust standard library.
|
# Requires a dependency on the Rust standard library.
|
||||||
std = []
|
std = ["serde_core/std"]
|
||||||
|
|
||||||
# Provide impls for types that require unstable functionality. For tracking and
|
# Provide impls for types that require unstable functionality. For tracking and
|
||||||
# discussion of unstable functionality please refer to this issue:
|
# discussion of unstable functionality please refer to this issue:
|
||||||
#
|
#
|
||||||
# https://github.com/serde-rs/serde/issues/812
|
# https://github.com/serde-rs/serde/issues/812
|
||||||
unstable = []
|
unstable = ["serde_core/unstable"]
|
||||||
|
|
||||||
# Provide impls for types in the Rust core allocation and collections library
|
# Provide impls for types in the Rust core allocation and collections library
|
||||||
# including String, Box<T>, Vec<T>, and Cow<T>. This is a subset of std but may
|
# including String, Box<T>, Vec<T>, and Cow<T>. This is a subset of std but may
|
||||||
# be enabled without depending on all of std.
|
# be enabled without depending on all of std.
|
||||||
alloc = []
|
alloc = ["serde_core/alloc"]
|
||||||
|
|
||||||
# Opt into impls for Rc<T> and Arc<T>. Serializing and deserializing these types
|
# Opt into impls for Rc<T> and Arc<T>. Serializing and deserializing these types
|
||||||
# does not preserve identity and may result in multiple copies of the same data.
|
# does not preserve identity and may result in multiple copies of the same data.
|
||||||
# Be sure that this is what you want before enabling this feature.
|
# Be sure that this is what you want before enabling this feature.
|
||||||
rc = []
|
rc = ["serde_core/rc"]
|
||||||
|
|||||||
+9
-126
@@ -1,6 +1,6 @@
|
|||||||
use std::env;
|
use std::env;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
use std::str::{self, FromStr};
|
use std::str;
|
||||||
|
|
||||||
// The rustc-cfg strings below are *not* public API. Please let us know by
|
// 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
|
// opening a GitHub issue if your build environment requires some way to enable
|
||||||
@@ -13,140 +13,23 @@ fn main() {
|
|||||||
None => return,
|
None => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
let target = env::var("TARGET").unwrap();
|
if minor >= 77 {
|
||||||
let emscripten = target == "asmjs-unknown-emscripten" || target == "wasm32-unknown-emscripten";
|
println!("cargo:rustc-check-cfg=cfg(no_serde_derive)");
|
||||||
|
|
||||||
// std::collections::Bound was stabilized in Rust 1.17
|
|
||||||
// but it was moved to core::ops later in Rust 1.26:
|
|
||||||
// https://doc.rust-lang.org/core/ops/enum.Bound.html
|
|
||||||
if minor < 26 {
|
|
||||||
println!("cargo:rustc-cfg=no_ops_bound");
|
|
||||||
if minor < 17 {
|
|
||||||
println!("cargo:rustc-cfg=no_collections_bound");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// core::cmp::Reverse stabilized in Rust 1.19:
|
// Current minimum supported version of serde_derive crate is Rust 1.61.
|
||||||
// https://doc.rust-lang.org/stable/core/cmp/struct.Reverse.html
|
if minor < 61 {
|
||||||
if minor < 19 {
|
|
||||||
println!("cargo:rustc-cfg=no_core_reverse");
|
|
||||||
}
|
|
||||||
|
|
||||||
// CString::into_boxed_c_str and PathBuf::into_boxed_path stabilized in Rust 1.20:
|
|
||||||
// https://doc.rust-lang.org/std/ffi/struct.CString.html#method.into_boxed_c_str
|
|
||||||
// https://doc.rust-lang.org/std/path/struct.PathBuf.html#method.into_boxed_path
|
|
||||||
if minor < 20 {
|
|
||||||
println!("cargo:rustc-cfg=no_de_boxed_c_str");
|
|
||||||
println!("cargo:rustc-cfg=no_de_boxed_path");
|
|
||||||
}
|
|
||||||
|
|
||||||
// From<Box<T>> for Rc<T> / Arc<T> stabilized in Rust 1.21:
|
|
||||||
// https://doc.rust-lang.org/std/rc/struct.Rc.html#impl-From<Box<T>>
|
|
||||||
// https://doc.rust-lang.org/std/sync/struct.Arc.html#impl-From<Box<T>>
|
|
||||||
if minor < 21 {
|
|
||||||
println!("cargo:rustc-cfg=no_de_rc_dst");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Duration available in core since Rust 1.25:
|
|
||||||
// https://blog.rust-lang.org/2018/03/29/Rust-1.25.html#library-stabilizations
|
|
||||||
if minor < 25 {
|
|
||||||
println!("cargo:rustc-cfg=no_core_duration");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 128-bit integers stabilized in Rust 1.26:
|
|
||||||
// https://blog.rust-lang.org/2018/05/10/Rust-1.26.html
|
|
||||||
//
|
|
||||||
// Disabled on Emscripten targets before Rust 1.40 since
|
|
||||||
// Emscripten did not support 128-bit integers until Rust 1.40
|
|
||||||
// (https://github.com/rust-lang/rust/pull/65251)
|
|
||||||
if minor < 26 || emscripten && minor < 40 {
|
|
||||||
println!("cargo:rustc-cfg=no_integer128");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Inclusive ranges methods stabilized in Rust 1.27:
|
|
||||||
// https://github.com/rust-lang/rust/pull/50758
|
|
||||||
// Also Iterator::try_for_each:
|
|
||||||
// https://blog.rust-lang.org/2018/06/21/Rust-1.27.html#library-stabilizations
|
|
||||||
if minor < 27 {
|
|
||||||
println!("cargo:rustc-cfg=no_range_inclusive");
|
|
||||||
println!("cargo:rustc-cfg=no_iterator_try_fold");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Non-zero integers stabilized in Rust 1.28:
|
|
||||||
// https://blog.rust-lang.org/2018/08/02/Rust-1.28.html#library-stabilizations
|
|
||||||
if minor < 28 {
|
|
||||||
println!("cargo:rustc-cfg=no_num_nonzero");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
|
||||||
// https://blog.rust-lang.org/2019/04/11/Rust-1.34.0.html#library-stabilizations
|
|
||||||
if minor < 34 {
|
|
||||||
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");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Current minimum supported version of serde_derive crate is Rust 1.56.
|
|
||||||
if minor < 56 {
|
|
||||||
println!("cargo:rustc-cfg=no_serde_derive");
|
println!("cargo:rustc-cfg=no_serde_derive");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rustc_minor_version() -> Option<u32> {
|
fn rustc_minor_version() -> Option<u32> {
|
||||||
let rustc = match env::var_os("RUSTC") {
|
let rustc = env::var_os("RUSTC")?;
|
||||||
Some(rustc) => rustc,
|
let output = Command::new(rustc).arg("--version").output().ok()?;
|
||||||
None => return None,
|
let version = str::from_utf8(&output.stdout).ok()?;
|
||||||
};
|
|
||||||
|
|
||||||
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('.');
|
let mut pieces = version.split('.');
|
||||||
if pieces.next() != Some("rustc 1") {
|
if pieces.next() != Some("rustc 1") {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
pieces.next()?.parse().ok()
|
||||||
let next = match pieces.next() {
|
|
||||||
Some(next) => next,
|
|
||||||
None => return None,
|
|
||||||
};
|
|
||||||
|
|
||||||
u32::from_str(next).ok()
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,46 +0,0 @@
|
|||||||
use lib::*;
|
|
||||||
|
|
||||||
const TAG_CONT: u8 = 0b1000_0000;
|
|
||||||
const TAG_TWO_B: u8 = 0b1100_0000;
|
|
||||||
const TAG_THREE_B: u8 = 0b1110_0000;
|
|
||||||
const TAG_FOUR_B: u8 = 0b1111_0000;
|
|
||||||
const MAX_ONE_B: u32 = 0x80;
|
|
||||||
const MAX_TWO_B: u32 = 0x800;
|
|
||||||
const MAX_THREE_B: u32 = 0x10000;
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn encode(c: char) -> Encode {
|
|
||||||
let code = c as u32;
|
|
||||||
let mut buf = [0; 4];
|
|
||||||
let pos = if code < MAX_ONE_B {
|
|
||||||
buf[3] = code as u8;
|
|
||||||
3
|
|
||||||
} else if code < MAX_TWO_B {
|
|
||||||
buf[2] = (code >> 6 & 0x1F) as u8 | TAG_TWO_B;
|
|
||||||
buf[3] = (code & 0x3F) as u8 | TAG_CONT;
|
|
||||||
2
|
|
||||||
} else if code < MAX_THREE_B {
|
|
||||||
buf[1] = (code >> 12 & 0x0F) as u8 | TAG_THREE_B;
|
|
||||||
buf[2] = (code >> 6 & 0x3F) as u8 | TAG_CONT;
|
|
||||||
buf[3] = (code & 0x3F) as u8 | TAG_CONT;
|
|
||||||
1
|
|
||||||
} else {
|
|
||||||
buf[0] = (code >> 18 & 0x07) as u8 | TAG_FOUR_B;
|
|
||||||
buf[1] = (code >> 12 & 0x3F) as u8 | TAG_CONT;
|
|
||||||
buf[2] = (code >> 6 & 0x3F) as u8 | TAG_CONT;
|
|
||||||
buf[3] = (code & 0x3F) as u8 | TAG_CONT;
|
|
||||||
0
|
|
||||||
};
|
|
||||||
Encode { buf, pos }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Encode {
|
|
||||||
buf: [u8; 4],
|
|
||||||
pos: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Encode {
|
|
||||||
pub fn as_str(&self) -> &str {
|
|
||||||
str::from_utf8(&self.buf[self.pos..]).unwrap()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+8
-76
@@ -1,82 +1,14 @@
|
|||||||
/// Conditional compilation depending on whether Serde is built with support for
|
|
||||||
/// 128-bit integers.
|
|
||||||
///
|
|
||||||
/// Data formats that wish to support Rust compiler versions older than 1.26
|
|
||||||
/// (or targets that lack 128-bit integers) may place the i128 / u128 methods
|
|
||||||
/// of their Serializer and Deserializer behind this macro.
|
|
||||||
///
|
|
||||||
/// Data formats that require a minimum Rust compiler version of at least 1.26,
|
|
||||||
/// 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.
|
|
||||||
///
|
|
||||||
/// ```edition2021
|
|
||||||
/// # use serde::__private::doc::Error;
|
|
||||||
/// #
|
|
||||||
/// # struct MySerializer;
|
|
||||||
/// #
|
|
||||||
/// use serde::{serde_if_integer128, Serializer};
|
|
||||||
///
|
|
||||||
/// impl Serializer for MySerializer {
|
|
||||||
/// type Ok = ();
|
|
||||||
/// type Error = Error;
|
|
||||||
///
|
|
||||||
/// fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error> {
|
|
||||||
/// /* ... */
|
|
||||||
/// # unimplemented!()
|
|
||||||
/// }
|
|
||||||
///
|
|
||||||
/// /* ... */
|
|
||||||
///
|
|
||||||
/// serde_if_integer128! {
|
|
||||||
/// fn serialize_i128(self, v: i128) -> Result<Self::Ok, Self::Error> {
|
|
||||||
/// /* ... */
|
|
||||||
/// # unimplemented!()
|
|
||||||
/// }
|
|
||||||
///
|
|
||||||
/// fn serialize_u128(self, v: u128) -> Result<Self::Ok, Self::Error> {
|
|
||||||
/// /* ... */
|
|
||||||
/// # unimplemented!()
|
|
||||||
/// }
|
|
||||||
/// }
|
|
||||||
/// #
|
|
||||||
/// # serde::__serialize_unimplemented! {
|
|
||||||
/// # bool i8 i16 i32 u8 u16 u32 u64 f32 f64 char str bytes none some
|
|
||||||
/// # unit unit_struct unit_variant newtype_struct newtype_variant seq
|
|
||||||
/// # tuple tuple_struct tuple_variant map struct struct_variant
|
|
||||||
/// # }
|
|
||||||
/// }
|
|
||||||
/// ```
|
|
||||||
///
|
|
||||||
/// When Serde is built with support for 128-bit integers, this macro expands
|
|
||||||
/// transparently into just the input tokens.
|
|
||||||
///
|
|
||||||
/// ```edition2021
|
|
||||||
/// macro_rules! serde_if_integer128 {
|
|
||||||
/// ($($tt:tt)*) => {
|
|
||||||
/// $($tt)*
|
|
||||||
/// };
|
|
||||||
/// }
|
|
||||||
/// ```
|
|
||||||
///
|
|
||||||
/// When built without support for 128-bit integers, this macro expands to
|
|
||||||
/// nothing.
|
|
||||||
///
|
|
||||||
/// ```edition2021
|
|
||||||
/// macro_rules! serde_if_integer128 {
|
|
||||||
/// ($($tt:tt)*) => {};
|
|
||||||
/// }
|
|
||||||
/// ```
|
|
||||||
#[cfg(not(no_integer128))]
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
|
#[deprecated = "
|
||||||
|
This macro has no effect on any version of Serde released in the past 2 years.
|
||||||
|
It was used long ago in crates that needed to support Rustc older than 1.26.0,
|
||||||
|
or Emscripten targets older than 1.40.0, which did not yet have 128-bit integer
|
||||||
|
support. These days Serde requires a Rust compiler newer than that so 128-bit
|
||||||
|
integers are always supported.
|
||||||
|
"]
|
||||||
|
#[doc(hidden)]
|
||||||
macro_rules! serde_if_integer128 {
|
macro_rules! serde_if_integer128 {
|
||||||
($($tt:tt)*) => {
|
($($tt:tt)*) => {
|
||||||
$($tt)*
|
$($tt)*
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(no_integer128)]
|
|
||||||
#[macro_export]
|
|
||||||
#[doc(hidden)]
|
|
||||||
macro_rules! serde_if_integer128 {
|
|
||||||
($($tt:tt)*) => {};
|
|
||||||
}
|
|
||||||
|
|||||||
+79
-166
@@ -9,7 +9,7 @@
|
|||||||
//! these two groups interact with each other, allowing any supported data
|
//! these two groups interact with each other, allowing any supported data
|
||||||
//! structure to be serialized and deserialized using any supported data format.
|
//! structure to be serialized and deserialized using any supported data format.
|
||||||
//!
|
//!
|
||||||
//! See the Serde website <https://serde.rs/> for additional documentation and
|
//! See the Serde website <https://serde.rs> for additional documentation and
|
||||||
//! usage examples.
|
//! usage examples.
|
||||||
//!
|
//!
|
||||||
//! ## Design
|
//! ## Design
|
||||||
@@ -63,6 +63,7 @@
|
|||||||
//! and from DynamoDB.
|
//! and from DynamoDB.
|
||||||
//! - [Hjson], a syntax extension to JSON designed around human reading and
|
//! - [Hjson], a syntax extension to JSON designed around human reading and
|
||||||
//! editing. *(deserialization only)*
|
//! editing. *(deserialization only)*
|
||||||
|
//! - [CSV], Comma-separated values is a tabular text file format.
|
||||||
//!
|
//!
|
||||||
//! [JSON]: https://github.com/serde-rs/json
|
//! [JSON]: https://github.com/serde-rs/json
|
||||||
//! [Postcard]: https://github.com/jamesmunns/postcard
|
//! [Postcard]: https://github.com/jamesmunns/postcard
|
||||||
@@ -89,68 +90,76 @@
|
|||||||
//! [DynamoDB Items]: https://docs.rs/serde_dynamo
|
//! [DynamoDB Items]: https://docs.rs/serde_dynamo
|
||||||
//! [rusoto_dynamodb]: https://docs.rs/rusoto_dynamodb
|
//! [rusoto_dynamodb]: https://docs.rs/rusoto_dynamodb
|
||||||
//! [Hjson]: https://github.com/Canop/deser-hjson
|
//! [Hjson]: https://github.com/Canop/deser-hjson
|
||||||
|
//! [CSV]: https://docs.rs/csv
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
// Serde types in rustdoc of other crates get linked to here.
|
// Serde types in rustdoc of other crates get linked to here.
|
||||||
#![doc(html_root_url = "https://docs.rs/serde/1.0.178")]
|
#![doc(html_root_url = "https://docs.rs/serde/1.0.221")]
|
||||||
// Support using Serde without the standard library!
|
// Support using Serde without the standard library!
|
||||||
#![cfg_attr(not(feature = "std"), no_std)]
|
#![cfg_attr(not(feature = "std"), no_std)]
|
||||||
|
// Show which crate feature enables conditionally compiled APIs in documentation.
|
||||||
|
#![cfg_attr(docsrs, feature(doc_cfg, rustdoc_internals))]
|
||||||
|
#![cfg_attr(docsrs, allow(internal_features))]
|
||||||
// Unstable functionality only if the user asks for it. For tracking and
|
// Unstable functionality only if the user asks for it. For tracking and
|
||||||
// discussion of these features please refer to this issue:
|
// discussion of these features please refer to this issue:
|
||||||
//
|
//
|
||||||
// https://github.com/serde-rs/serde/issues/812
|
// https://github.com/serde-rs/serde/issues/812
|
||||||
#![cfg_attr(feature = "unstable", feature(error_in_core, never_type))]
|
#![cfg_attr(feature = "unstable", feature(never_type))]
|
||||||
#![allow(unknown_lints, bare_trait_objects, deprecated)]
|
#![allow(
|
||||||
#![cfg_attr(feature = "cargo-clippy", allow(renamed_and_removed_lints))]
|
unknown_lints,
|
||||||
|
bare_trait_objects,
|
||||||
|
deprecated,
|
||||||
|
mismatched_lifetime_syntaxes
|
||||||
|
)]
|
||||||
// Ignored clippy and clippy_pedantic lints
|
// Ignored clippy and clippy_pedantic lints
|
||||||
#![cfg_attr(
|
#![allow(
|
||||||
feature = "cargo-clippy",
|
// clippy bug: https://github.com/rust-lang/rust-clippy/issues/5704
|
||||||
allow(
|
clippy::unnested_or_patterns,
|
||||||
// clippy bug: https://github.com/rust-lang/rust-clippy/issues/5704
|
// clippy bug: https://github.com/rust-lang/rust-clippy/issues/7768
|
||||||
unnested_or_patterns,
|
clippy::semicolon_if_nothing_returned,
|
||||||
// clippy bug: https://github.com/rust-lang/rust-clippy/issues/7768
|
// not available in our oldest supported compiler
|
||||||
semicolon_if_nothing_returned,
|
clippy::empty_enum,
|
||||||
// not available in our oldest supported compiler
|
clippy::type_repetition_in_bounds, // https://github.com/rust-lang/rust-clippy/issues/8772
|
||||||
empty_enum,
|
// integer and float ser/de requires these sorts of casts
|
||||||
type_repetition_in_bounds, // https://github.com/rust-lang/rust-clippy/issues/8772
|
clippy::cast_possible_truncation,
|
||||||
// integer and float ser/de requires these sorts of casts
|
clippy::cast_possible_wrap,
|
||||||
cast_possible_truncation,
|
clippy::cast_precision_loss,
|
||||||
cast_possible_wrap,
|
clippy::cast_sign_loss,
|
||||||
cast_sign_loss,
|
// things are often more readable this way
|
||||||
// things are often more readable this way
|
clippy::cast_lossless,
|
||||||
cast_lossless,
|
clippy::module_name_repetitions,
|
||||||
module_name_repetitions,
|
clippy::single_match_else,
|
||||||
option_if_let_else,
|
clippy::type_complexity,
|
||||||
single_match_else,
|
clippy::use_self,
|
||||||
type_complexity,
|
clippy::zero_prefixed_literal,
|
||||||
use_self,
|
// correctly used
|
||||||
zero_prefixed_literal,
|
clippy::derive_partial_eq_without_eq,
|
||||||
// correctly used
|
clippy::enum_glob_use,
|
||||||
derive_partial_eq_without_eq,
|
clippy::explicit_auto_deref,
|
||||||
enum_glob_use,
|
clippy::incompatible_msrv,
|
||||||
explicit_auto_deref,
|
clippy::let_underscore_untyped,
|
||||||
let_underscore_untyped,
|
clippy::map_err_ignore,
|
||||||
map_err_ignore,
|
clippy::new_without_default,
|
||||||
new_without_default,
|
clippy::result_unit_err,
|
||||||
result_unit_err,
|
clippy::wildcard_imports,
|
||||||
wildcard_imports,
|
// not practical
|
||||||
// not practical
|
clippy::needless_pass_by_value,
|
||||||
needless_pass_by_value,
|
clippy::similar_names,
|
||||||
similar_names,
|
clippy::too_many_lines,
|
||||||
too_many_lines,
|
// preference
|
||||||
// preference
|
clippy::doc_markdown,
|
||||||
doc_markdown,
|
clippy::elidable_lifetime_names,
|
||||||
unseparated_literal_suffix,
|
clippy::needless_lifetimes,
|
||||||
// false positive
|
clippy::unseparated_literal_suffix,
|
||||||
needless_doctest_main,
|
// false positive
|
||||||
// noisy
|
clippy::needless_doctest_main,
|
||||||
missing_errors_doc,
|
// noisy
|
||||||
must_use_candidate,
|
clippy::missing_errors_doc,
|
||||||
)
|
clippy::must_use_candidate,
|
||||||
)]
|
)]
|
||||||
// Restrictions
|
// Restrictions
|
||||||
#![cfg_attr(feature = "cargo-clippy", deny(question_mark_used))]
|
#![deny(clippy::question_mark_used)]
|
||||||
// Rustc lints.
|
// Rustc lints.
|
||||||
#![deny(missing_docs, unused_imports)]
|
#![deny(missing_docs, unused_imports)]
|
||||||
|
|
||||||
@@ -170,21 +179,19 @@ mod lib {
|
|||||||
pub use std::*;
|
pub use std::*;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub use self::core::{cmp, iter, mem, num, ptr, slice, str};
|
|
||||||
pub use self::core::{f32, f64};
|
pub use self::core::{f32, f64};
|
||||||
pub use self::core::{i16, i32, i64, i8, isize};
|
pub use self::core::{ptr, str};
|
||||||
pub use self::core::{u16, u32, u64, u8, usize};
|
|
||||||
|
|
||||||
pub use self::core::cell::{Cell, RefCell};
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
pub use self::core::clone::{self, Clone};
|
pub use self::core::slice;
|
||||||
pub use self::core::convert::{self, From, Into};
|
|
||||||
pub use self::core::default::{self, Default};
|
pub use self::core::clone;
|
||||||
pub use self::core::fmt::{self, Debug, Display};
|
pub use self::core::convert;
|
||||||
|
pub use self::core::default;
|
||||||
|
pub use self::core::fmt::{self, Debug, Display, Write as FmtWrite};
|
||||||
pub use self::core::marker::{self, PhantomData};
|
pub use self::core::marker::{self, PhantomData};
|
||||||
pub use self::core::num::Wrapping;
|
pub use self::core::option;
|
||||||
pub use self::core::ops::{Range, RangeFrom, RangeTo};
|
pub use self::core::result;
|
||||||
pub use self::core::option::{self, Option};
|
|
||||||
pub use self::core::result::{self, Result};
|
|
||||||
|
|
||||||
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
||||||
pub use alloc::borrow::{Cow, ToOwned};
|
pub use alloc::borrow::{Cow, ToOwned};
|
||||||
@@ -205,92 +212,14 @@ mod lib {
|
|||||||
pub use alloc::boxed::Box;
|
pub use alloc::boxed::Box;
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
pub use std::boxed::Box;
|
pub use std::boxed::Box;
|
||||||
|
|
||||||
#[cfg(all(feature = "rc", feature = "alloc", not(feature = "std")))]
|
|
||||||
pub use alloc::rc::{Rc, Weak as RcWeak};
|
|
||||||
#[cfg(all(feature = "rc", feature = "std"))]
|
|
||||||
pub use std::rc::{Rc, Weak as RcWeak};
|
|
||||||
|
|
||||||
#[cfg(all(feature = "rc", feature = "alloc", not(feature = "std")))]
|
|
||||||
pub use alloc::sync::{Arc, Weak as ArcWeak};
|
|
||||||
#[cfg(all(feature = "rc", feature = "std"))]
|
|
||||||
pub use std::sync::{Arc, Weak as ArcWeak};
|
|
||||||
|
|
||||||
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
|
||||||
pub use alloc::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque};
|
|
||||||
#[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::{OsStr, OsString};
|
|
||||||
#[cfg(feature = "std")]
|
|
||||||
pub use std::hash::{BuildHasher, Hash};
|
|
||||||
#[cfg(feature = "std")]
|
|
||||||
pub use std::io::Write;
|
|
||||||
#[cfg(feature = "std")]
|
|
||||||
pub use std::path::{Path, PathBuf};
|
|
||||||
#[cfg(feature = "std")]
|
|
||||||
pub use std::sync::{Mutex, RwLock};
|
|
||||||
#[cfg(feature = "std")]
|
|
||||||
pub use std::time::{SystemTime, UNIX_EPOCH};
|
|
||||||
|
|
||||||
#[cfg(all(feature = "std", not(no_collections_bound), no_ops_bound))]
|
|
||||||
pub use std::collections::Bound;
|
|
||||||
|
|
||||||
#[cfg(not(no_core_reverse))]
|
|
||||||
pub use self::core::cmp::Reverse;
|
|
||||||
|
|
||||||
#[cfg(not(no_ops_bound))]
|
|
||||||
pub use self::core::ops::Bound;
|
|
||||||
|
|
||||||
#[cfg(not(no_range_inclusive))]
|
|
||||||
pub use self::core::ops::RangeInclusive;
|
|
||||||
|
|
||||||
#[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", 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
|
// None of this crate's error handling needs the `From::from` error conversion
|
||||||
// performed implicitly by the `?` operator or the standard library's `try!`
|
// performed implicitly by the `?` operator or the standard library's `try!`
|
||||||
// macro. This simplified macro gives a 5.5% improvement in compile time
|
// macro. This simplified macro gives a 5.5% improvement in compile time
|
||||||
// compared to standard `try!`, and 9% improvement compared to `?`.
|
// compared to standard `try!`, and 9% improvement compared to `?`.
|
||||||
macro_rules! try {
|
#[cfg(not(no_serde_derive))]
|
||||||
|
macro_rules! tri {
|
||||||
($expr:expr) => {
|
($expr:expr) => {
|
||||||
match $expr {
|
match $expr {
|
||||||
Ok(val) => val,
|
Ok(val) => val,
|
||||||
@@ -301,36 +230,18 @@ macro_rules! try {
|
|||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#[macro_use]
|
pub use serde_core::{
|
||||||
mod macros;
|
de, forward_to_deserialize_any, ser, Deserialize, Deserializer, Serialize, Serializer,
|
||||||
|
};
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod integer128;
|
mod integer128;
|
||||||
|
|
||||||
pub mod de;
|
|
||||||
pub mod ser;
|
|
||||||
|
|
||||||
#[doc(inline)]
|
|
||||||
pub use de::{Deserialize, Deserializer};
|
|
||||||
#[doc(inline)]
|
|
||||||
pub use ser::{Serialize, Serializer};
|
|
||||||
|
|
||||||
// Used by generated code and doc tests. Not public API.
|
// Used by generated code and doc tests. Not public API.
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
#[path = "private/mod.rs"]
|
#[path = "private/mod.rs"]
|
||||||
pub mod __private;
|
pub mod __private;
|
||||||
|
|
||||||
#[allow(unused_imports)]
|
|
||||||
use self::__private as export;
|
|
||||||
#[allow(unused_imports)]
|
|
||||||
use self::__private as private;
|
|
||||||
|
|
||||||
#[path = "de/seed.rs"]
|
|
||||||
mod seed;
|
|
||||||
|
|
||||||
#[cfg(not(any(feature = "std", feature = "unstable")))]
|
|
||||||
mod std_error;
|
|
||||||
|
|
||||||
// Re-export #[derive(Serialize, Deserialize)].
|
// Re-export #[derive(Serialize, Deserialize)].
|
||||||
//
|
//
|
||||||
// The reason re-exporting is not enabled by default is that disabling it would
|
// The reason re-exporting is not enabled by default is that disabling it would
|
||||||
@@ -341,9 +252,11 @@ extern crate serde_derive;
|
|||||||
|
|
||||||
/// Derive macro available if serde is built with `features = ["derive"]`.
|
/// Derive macro available if serde is built with `features = ["derive"]`.
|
||||||
#[cfg(feature = "serde_derive")]
|
#[cfg(feature = "serde_derive")]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(feature = "derive")))]
|
||||||
pub use serde_derive::{Deserialize, Serialize};
|
pub use serde_derive::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[cfg(all(not(no_serde_derive), any(feature = "std", feature = "alloc")))]
|
#[macro_export]
|
||||||
mod actually_private {
|
#[doc(hidden)]
|
||||||
pub struct T;
|
macro_rules! __require_serde_not_serde_core {
|
||||||
|
() => {};
|
||||||
}
|
}
|
||||||
|
|||||||
+884
-663
File diff suppressed because it is too large
Load Diff
+10
-42
@@ -3,48 +3,16 @@ pub mod de;
|
|||||||
#[cfg(not(no_serde_derive))]
|
#[cfg(not(no_serde_derive))]
|
||||||
pub mod ser;
|
pub mod ser;
|
||||||
|
|
||||||
pub mod size_hint;
|
pub use crate::lib::clone::Clone;
|
||||||
|
pub use crate::lib::convert::{From, Into, TryFrom};
|
||||||
|
pub use crate::lib::default::Default;
|
||||||
|
pub use crate::lib::fmt::{self, Formatter};
|
||||||
|
pub use crate::lib::marker::PhantomData;
|
||||||
|
pub use crate::lib::option::Option::{self, None, Some};
|
||||||
|
pub use crate::lib::ptr;
|
||||||
|
pub use crate::lib::result::Result::{self, Err, Ok};
|
||||||
|
|
||||||
// FIXME: #[cfg(doctest)] once https://github.com/rust-lang/rust/issues/67295 is fixed.
|
pub use serde_core::__private::string::from_utf8_lossy;
|
||||||
pub mod doc;
|
|
||||||
|
|
||||||
pub use lib::clone::Clone;
|
|
||||||
pub use lib::convert::{From, Into};
|
|
||||||
pub use lib::default::Default;
|
|
||||||
pub use lib::fmt::{self, Formatter};
|
|
||||||
pub use lib::marker::PhantomData;
|
|
||||||
pub use lib::option::Option::{self, None, Some};
|
|
||||||
pub use lib::ptr;
|
|
||||||
pub use lib::result::Result::{self, Err, Ok};
|
|
||||||
|
|
||||||
pub use self::string::from_utf8_lossy;
|
|
||||||
|
|
||||||
#[cfg(any(feature = "alloc", feature = "std"))]
|
#[cfg(any(feature = "alloc", feature = "std"))]
|
||||||
pub use lib::{ToString, Vec};
|
pub use crate::lib::{ToString, Vec};
|
||||||
|
|
||||||
#[cfg(not(no_core_try_from))]
|
|
||||||
pub use lib::convert::TryFrom;
|
|
||||||
|
|
||||||
mod string {
|
|
||||||
use lib::*;
|
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
|
||||||
pub fn from_utf8_lossy(bytes: &[u8]) -> Cow<str> {
|
|
||||||
String::from_utf8_lossy(bytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
// The generated code calls this like:
|
|
||||||
//
|
|
||||||
// let value = &_serde::__private::from_utf8_lossy(bytes);
|
|
||||||
// Err(_serde::de::Error::unknown_variant(value, VARIANTS))
|
|
||||||
//
|
|
||||||
// so it is okay for the return type to be different from the std case as long
|
|
||||||
// as the above works.
|
|
||||||
#[cfg(not(any(feature = "std", feature = "alloc")))]
|
|
||||||
pub fn from_utf8_lossy(bytes: &[u8]) -> &str {
|
|
||||||
// Three unicode replacement characters if it fails. They look like a
|
|
||||||
// white-on-black question mark. The user will recognize it as invalid
|
|
||||||
// UTF-8.
|
|
||||||
str::from_utf8(bytes).unwrap_or("\u{fffd}\u{fffd}\u{fffd}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
+200
-148
@@ -1,6 +1,6 @@
|
|||||||
use lib::*;
|
use crate::lib::*;
|
||||||
|
|
||||||
use ser::{self, Impossible, Serialize, SerializeMap, SerializeStruct, Serializer};
|
use crate::ser::{self, Impossible, Serialize, SerializeMap, SerializeStruct, Serializer};
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
use self::content::{
|
use self::content::{
|
||||||
@@ -51,11 +51,10 @@ enum Unsupported {
|
|||||||
String,
|
String,
|
||||||
ByteArray,
|
ByteArray,
|
||||||
Optional,
|
Optional,
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
|
||||||
UnitStruct,
|
|
||||||
Sequence,
|
Sequence,
|
||||||
Tuple,
|
Tuple,
|
||||||
TupleStruct,
|
TupleStruct,
|
||||||
|
#[cfg(not(any(feature = "std", feature = "alloc")))]
|
||||||
Enum,
|
Enum,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,11 +68,10 @@ impl Display for Unsupported {
|
|||||||
Unsupported::String => formatter.write_str("a string"),
|
Unsupported::String => formatter.write_str("a string"),
|
||||||
Unsupported::ByteArray => formatter.write_str("a byte array"),
|
Unsupported::ByteArray => formatter.write_str("a byte array"),
|
||||||
Unsupported::Optional => formatter.write_str("an optional"),
|
Unsupported::Optional => formatter.write_str("an optional"),
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
|
||||||
Unsupported::UnitStruct => formatter.write_str("unit struct"),
|
|
||||||
Unsupported::Sequence => formatter.write_str("a sequence"),
|
Unsupported::Sequence => formatter.write_str("a sequence"),
|
||||||
Unsupported::Tuple => formatter.write_str("a tuple"),
|
Unsupported::Tuple => formatter.write_str("a tuple"),
|
||||||
Unsupported::TupleStruct => formatter.write_str("a tuple struct"),
|
Unsupported::TupleStruct => formatter.write_str("a tuple struct"),
|
||||||
|
#[cfg(not(any(feature = "std", feature = "alloc")))]
|
||||||
Unsupported::Enum => formatter.write_str("an enum"),
|
Unsupported::Enum => formatter.write_str("an enum"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -174,22 +172,22 @@ where
|
|||||||
Err(self.bad_type(Unsupported::Optional))
|
Err(self.bad_type(Unsupported::Optional))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_some<T: ?Sized>(self, _: &T) -> Result<Self::Ok, Self::Error>
|
fn serialize_some<T>(self, _: &T) -> Result<Self::Ok, Self::Error>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
Err(self.bad_type(Unsupported::Optional))
|
Err(self.bad_type(Unsupported::Optional))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
|
fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
|
||||||
let mut map = try!(self.delegate.serialize_map(Some(1)));
|
let mut map = tri!(self.delegate.serialize_map(Some(1)));
|
||||||
try!(map.serialize_entry(self.tag, self.variant_name));
|
tri!(map.serialize_entry(self.tag, self.variant_name));
|
||||||
map.end()
|
map.end()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_unit_struct(self, _: &'static str) -> Result<Self::Ok, Self::Error> {
|
fn serialize_unit_struct(self, _: &'static str) -> Result<Self::Ok, Self::Error> {
|
||||||
let mut map = try!(self.delegate.serialize_map(Some(1)));
|
let mut map = tri!(self.delegate.serialize_map(Some(1)));
|
||||||
try!(map.serialize_entry(self.tag, self.variant_name));
|
tri!(map.serialize_entry(self.tag, self.variant_name));
|
||||||
map.end()
|
map.end()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -199,24 +197,24 @@ where
|
|||||||
_: u32,
|
_: u32,
|
||||||
inner_variant: &'static str,
|
inner_variant: &'static str,
|
||||||
) -> Result<Self::Ok, Self::Error> {
|
) -> Result<Self::Ok, Self::Error> {
|
||||||
let mut map = try!(self.delegate.serialize_map(Some(2)));
|
let mut map = tri!(self.delegate.serialize_map(Some(2)));
|
||||||
try!(map.serialize_entry(self.tag, self.variant_name));
|
tri!(map.serialize_entry(self.tag, self.variant_name));
|
||||||
try!(map.serialize_entry(inner_variant, &()));
|
tri!(map.serialize_entry(inner_variant, &()));
|
||||||
map.end()
|
map.end()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_newtype_struct<T: ?Sized>(
|
fn serialize_newtype_struct<T>(
|
||||||
self,
|
self,
|
||||||
_: &'static str,
|
_: &'static str,
|
||||||
value: &T,
|
value: &T,
|
||||||
) -> Result<Self::Ok, Self::Error>
|
) -> Result<Self::Ok, Self::Error>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
value.serialize(self)
|
value.serialize(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_newtype_variant<T: ?Sized>(
|
fn serialize_newtype_variant<T>(
|
||||||
self,
|
self,
|
||||||
_: &'static str,
|
_: &'static str,
|
||||||
_: u32,
|
_: u32,
|
||||||
@@ -224,11 +222,11 @@ where
|
|||||||
inner_value: &T,
|
inner_value: &T,
|
||||||
) -> Result<Self::Ok, Self::Error>
|
) -> Result<Self::Ok, Self::Error>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
let mut map = try!(self.delegate.serialize_map(Some(2)));
|
let mut map = tri!(self.delegate.serialize_map(Some(2)));
|
||||||
try!(map.serialize_entry(self.tag, self.variant_name));
|
tri!(map.serialize_entry(self.tag, self.variant_name));
|
||||||
try!(map.serialize_entry(inner_variant, inner_value));
|
tri!(map.serialize_entry(inner_variant, inner_value));
|
||||||
map.end()
|
map.end()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -269,9 +267,9 @@ where
|
|||||||
inner_variant: &'static str,
|
inner_variant: &'static str,
|
||||||
len: usize,
|
len: usize,
|
||||||
) -> Result<Self::SerializeTupleVariant, Self::Error> {
|
) -> Result<Self::SerializeTupleVariant, Self::Error> {
|
||||||
let mut map = try!(self.delegate.serialize_map(Some(2)));
|
let mut map = tri!(self.delegate.serialize_map(Some(2)));
|
||||||
try!(map.serialize_entry(self.tag, self.variant_name));
|
tri!(map.serialize_entry(self.tag, self.variant_name));
|
||||||
try!(map.serialize_key(inner_variant));
|
tri!(map.serialize_key(inner_variant));
|
||||||
Ok(SerializeTupleVariantAsMapValue::new(
|
Ok(SerializeTupleVariantAsMapValue::new(
|
||||||
map,
|
map,
|
||||||
inner_variant,
|
inner_variant,
|
||||||
@@ -280,8 +278,8 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
|
fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
|
||||||
let mut map = try!(self.delegate.serialize_map(len.map(|len| len + 1)));
|
let mut map = tri!(self.delegate.serialize_map(len.map(|len| len + 1)));
|
||||||
try!(map.serialize_entry(self.tag, self.variant_name));
|
tri!(map.serialize_entry(self.tag, self.variant_name));
|
||||||
Ok(map)
|
Ok(map)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -290,8 +288,8 @@ where
|
|||||||
name: &'static str,
|
name: &'static str,
|
||||||
len: usize,
|
len: usize,
|
||||||
) -> Result<Self::SerializeStruct, Self::Error> {
|
) -> Result<Self::SerializeStruct, Self::Error> {
|
||||||
let mut state = try!(self.delegate.serialize_struct(name, len + 1));
|
let mut state = tri!(self.delegate.serialize_struct(name, len + 1));
|
||||||
try!(state.serialize_field(self.tag, self.variant_name));
|
tri!(state.serialize_field(self.tag, self.variant_name));
|
||||||
Ok(state)
|
Ok(state)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -316,9 +314,9 @@ where
|
|||||||
inner_variant: &'static str,
|
inner_variant: &'static str,
|
||||||
len: usize,
|
len: usize,
|
||||||
) -> Result<Self::SerializeStructVariant, Self::Error> {
|
) -> Result<Self::SerializeStructVariant, Self::Error> {
|
||||||
let mut map = try!(self.delegate.serialize_map(Some(2)));
|
let mut map = tri!(self.delegate.serialize_map(Some(2)));
|
||||||
try!(map.serialize_entry(self.tag, self.variant_name));
|
tri!(map.serialize_entry(self.tag, self.variant_name));
|
||||||
try!(map.serialize_key(inner_variant));
|
tri!(map.serialize_key(inner_variant));
|
||||||
Ok(SerializeStructVariantAsMapValue::new(
|
Ok(SerializeStructVariantAsMapValue::new(
|
||||||
map,
|
map,
|
||||||
inner_variant,
|
inner_variant,
|
||||||
@@ -327,9 +325,9 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(any(feature = "std", feature = "alloc")))]
|
#[cfg(not(any(feature = "std", feature = "alloc")))]
|
||||||
fn collect_str<T: ?Sized>(self, _: &T) -> Result<Self::Ok, Self::Error>
|
fn collect_str<T>(self, _: &T) -> Result<Self::Ok, Self::Error>
|
||||||
where
|
where
|
||||||
T: Display,
|
T: ?Sized + Display,
|
||||||
{
|
{
|
||||||
Err(self.bad_type(Unsupported::String))
|
Err(self.bad_type(Unsupported::String))
|
||||||
}
|
}
|
||||||
@@ -337,9 +335,9 @@ where
|
|||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
mod content {
|
mod content {
|
||||||
use lib::*;
|
use crate::lib::*;
|
||||||
|
|
||||||
use ser::{self, Serialize, Serializer};
|
use crate::ser::{self, Serialize, Serializer};
|
||||||
|
|
||||||
pub struct SerializeTupleVariantAsMapValue<M> {
|
pub struct SerializeTupleVariantAsMapValue<M> {
|
||||||
map: M,
|
map: M,
|
||||||
@@ -364,17 +362,17 @@ mod content {
|
|||||||
type Ok = M::Ok;
|
type Ok = M::Ok;
|
||||||
type Error = M::Error;
|
type Error = M::Error;
|
||||||
|
|
||||||
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), M::Error>
|
fn serialize_field<T>(&mut self, value: &T) -> Result<(), M::Error>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
let value = try!(value.serialize(ContentSerializer::<M::Error>::new()));
|
let value = tri!(value.serialize(ContentSerializer::<M::Error>::new()));
|
||||||
self.fields.push(value);
|
self.fields.push(value);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn end(mut self) -> Result<M::Ok, M::Error> {
|
fn end(mut self) -> Result<M::Ok, M::Error> {
|
||||||
try!(self
|
tri!(self
|
||||||
.map
|
.map
|
||||||
.serialize_value(&Content::TupleStruct(self.name, self.fields)));
|
.serialize_value(&Content::TupleStruct(self.name, self.fields)));
|
||||||
self.map.end()
|
self.map.end()
|
||||||
@@ -404,21 +402,17 @@ mod content {
|
|||||||
type Ok = M::Ok;
|
type Ok = M::Ok;
|
||||||
type Error = M::Error;
|
type Error = M::Error;
|
||||||
|
|
||||||
fn serialize_field<T: ?Sized>(
|
fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), M::Error>
|
||||||
&mut self,
|
|
||||||
key: &'static str,
|
|
||||||
value: &T,
|
|
||||||
) -> Result<(), M::Error>
|
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
let value = try!(value.serialize(ContentSerializer::<M::Error>::new()));
|
let value = tri!(value.serialize(ContentSerializer::<M::Error>::new()));
|
||||||
self.fields.push((key, value));
|
self.fields.push((key, value));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn end(mut self) -> Result<M::Ok, M::Error> {
|
fn end(mut self) -> Result<M::Ok, M::Error> {
|
||||||
try!(self
|
tri!(self
|
||||||
.map
|
.map
|
||||||
.serialize_value(&Content::Struct(self.name, self.fields)));
|
.serialize_value(&Content::Struct(self.name, self.fields)));
|
||||||
self.map.end()
|
self.map.end()
|
||||||
@@ -499,50 +493,50 @@ mod content {
|
|||||||
}
|
}
|
||||||
Content::Seq(ref elements) => elements.serialize(serializer),
|
Content::Seq(ref elements) => elements.serialize(serializer),
|
||||||
Content::Tuple(ref elements) => {
|
Content::Tuple(ref elements) => {
|
||||||
use ser::SerializeTuple;
|
use crate::ser::SerializeTuple;
|
||||||
let mut tuple = try!(serializer.serialize_tuple(elements.len()));
|
let mut tuple = tri!(serializer.serialize_tuple(elements.len()));
|
||||||
for e in elements {
|
for e in elements {
|
||||||
try!(tuple.serialize_element(e));
|
tri!(tuple.serialize_element(e));
|
||||||
}
|
}
|
||||||
tuple.end()
|
tuple.end()
|
||||||
}
|
}
|
||||||
Content::TupleStruct(n, ref fields) => {
|
Content::TupleStruct(n, ref fields) => {
|
||||||
use ser::SerializeTupleStruct;
|
use crate::ser::SerializeTupleStruct;
|
||||||
let mut ts = try!(serializer.serialize_tuple_struct(n, fields.len()));
|
let mut ts = tri!(serializer.serialize_tuple_struct(n, fields.len()));
|
||||||
for f in fields {
|
for f in fields {
|
||||||
try!(ts.serialize_field(f));
|
tri!(ts.serialize_field(f));
|
||||||
}
|
}
|
||||||
ts.end()
|
ts.end()
|
||||||
}
|
}
|
||||||
Content::TupleVariant(n, i, v, ref fields) => {
|
Content::TupleVariant(n, i, v, ref fields) => {
|
||||||
use ser::SerializeTupleVariant;
|
use crate::ser::SerializeTupleVariant;
|
||||||
let mut tv = try!(serializer.serialize_tuple_variant(n, i, v, fields.len()));
|
let mut tv = tri!(serializer.serialize_tuple_variant(n, i, v, fields.len()));
|
||||||
for f in fields {
|
for f in fields {
|
||||||
try!(tv.serialize_field(f));
|
tri!(tv.serialize_field(f));
|
||||||
}
|
}
|
||||||
tv.end()
|
tv.end()
|
||||||
}
|
}
|
||||||
Content::Map(ref entries) => {
|
Content::Map(ref entries) => {
|
||||||
use ser::SerializeMap;
|
use crate::ser::SerializeMap;
|
||||||
let mut map = try!(serializer.serialize_map(Some(entries.len())));
|
let mut map = tri!(serializer.serialize_map(Some(entries.len())));
|
||||||
for (k, v) in entries {
|
for (k, v) in entries {
|
||||||
try!(map.serialize_entry(k, v));
|
tri!(map.serialize_entry(k, v));
|
||||||
}
|
}
|
||||||
map.end()
|
map.end()
|
||||||
}
|
}
|
||||||
Content::Struct(n, ref fields) => {
|
Content::Struct(n, ref fields) => {
|
||||||
use ser::SerializeStruct;
|
use crate::ser::SerializeStruct;
|
||||||
let mut s = try!(serializer.serialize_struct(n, fields.len()));
|
let mut s = tri!(serializer.serialize_struct(n, fields.len()));
|
||||||
for &(k, ref v) in fields {
|
for &(k, ref v) in fields {
|
||||||
try!(s.serialize_field(k, v));
|
tri!(s.serialize_field(k, v));
|
||||||
}
|
}
|
||||||
s.end()
|
s.end()
|
||||||
}
|
}
|
||||||
Content::StructVariant(n, i, v, ref fields) => {
|
Content::StructVariant(n, i, v, ref fields) => {
|
||||||
use ser::SerializeStructVariant;
|
use crate::ser::SerializeStructVariant;
|
||||||
let mut sv = try!(serializer.serialize_struct_variant(n, i, v, fields.len()));
|
let mut sv = tri!(serializer.serialize_struct_variant(n, i, v, fields.len()));
|
||||||
for &(k, ref v) in fields {
|
for &(k, ref v) in fields {
|
||||||
try!(sv.serialize_field(k, v));
|
tri!(sv.serialize_field(k, v));
|
||||||
}
|
}
|
||||||
sv.end()
|
sv.end()
|
||||||
}
|
}
|
||||||
@@ -635,11 +629,11 @@ mod content {
|
|||||||
Ok(Content::None)
|
Ok(Content::None)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Content, E>
|
fn serialize_some<T>(self, value: &T) -> Result<Content, E>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
Ok(Content::Some(Box::new(try!(value.serialize(self)))))
|
Ok(Content::Some(Box::new(tri!(value.serialize(self)))))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_unit(self) -> Result<Content, E> {
|
fn serialize_unit(self) -> Result<Content, E> {
|
||||||
@@ -659,21 +653,17 @@ mod content {
|
|||||||
Ok(Content::UnitVariant(name, variant_index, variant))
|
Ok(Content::UnitVariant(name, variant_index, variant))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_newtype_struct<T: ?Sized>(
|
fn serialize_newtype_struct<T>(self, name: &'static str, value: &T) -> Result<Content, E>
|
||||||
self,
|
|
||||||
name: &'static str,
|
|
||||||
value: &T,
|
|
||||||
) -> Result<Content, E>
|
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
Ok(Content::NewtypeStruct(
|
Ok(Content::NewtypeStruct(
|
||||||
name,
|
name,
|
||||||
Box::new(try!(value.serialize(self))),
|
Box::new(tri!(value.serialize(self))),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_newtype_variant<T: ?Sized>(
|
fn serialize_newtype_variant<T>(
|
||||||
self,
|
self,
|
||||||
name: &'static str,
|
name: &'static str,
|
||||||
variant_index: u32,
|
variant_index: u32,
|
||||||
@@ -681,13 +671,13 @@ mod content {
|
|||||||
value: &T,
|
value: &T,
|
||||||
) -> Result<Content, E>
|
) -> Result<Content, E>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
Ok(Content::NewtypeVariant(
|
Ok(Content::NewtypeVariant(
|
||||||
name,
|
name,
|
||||||
variant_index,
|
variant_index,
|
||||||
variant,
|
variant,
|
||||||
Box::new(try!(value.serialize(self))),
|
Box::new(tri!(value.serialize(self))),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -782,11 +772,11 @@ mod content {
|
|||||||
type Ok = Content;
|
type Ok = Content;
|
||||||
type Error = E;
|
type Error = E;
|
||||||
|
|
||||||
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), E>
|
fn serialize_element<T>(&mut self, value: &T) -> Result<(), E>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
let value = try!(value.serialize(ContentSerializer::<E>::new()));
|
let value = tri!(value.serialize(ContentSerializer::<E>::new()));
|
||||||
self.elements.push(value);
|
self.elements.push(value);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -808,11 +798,11 @@ mod content {
|
|||||||
type Ok = Content;
|
type Ok = Content;
|
||||||
type Error = E;
|
type Error = E;
|
||||||
|
|
||||||
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), E>
|
fn serialize_element<T>(&mut self, value: &T) -> Result<(), E>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
let value = try!(value.serialize(ContentSerializer::<E>::new()));
|
let value = tri!(value.serialize(ContentSerializer::<E>::new()));
|
||||||
self.elements.push(value);
|
self.elements.push(value);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -835,11 +825,11 @@ mod content {
|
|||||||
type Ok = Content;
|
type Ok = Content;
|
||||||
type Error = E;
|
type Error = E;
|
||||||
|
|
||||||
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), E>
|
fn serialize_field<T>(&mut self, value: &T) -> Result<(), E>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
let value = try!(value.serialize(ContentSerializer::<E>::new()));
|
let value = tri!(value.serialize(ContentSerializer::<E>::new()));
|
||||||
self.fields.push(value);
|
self.fields.push(value);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -864,11 +854,11 @@ mod content {
|
|||||||
type Ok = Content;
|
type Ok = Content;
|
||||||
type Error = E;
|
type Error = E;
|
||||||
|
|
||||||
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), E>
|
fn serialize_field<T>(&mut self, value: &T) -> Result<(), E>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
let value = try!(value.serialize(ContentSerializer::<E>::new()));
|
let value = tri!(value.serialize(ContentSerializer::<E>::new()));
|
||||||
self.fields.push(value);
|
self.fields.push(value);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -896,24 +886,24 @@ mod content {
|
|||||||
type Ok = Content;
|
type Ok = Content;
|
||||||
type Error = E;
|
type Error = E;
|
||||||
|
|
||||||
fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), E>
|
fn serialize_key<T>(&mut self, key: &T) -> Result<(), E>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
let key = try!(key.serialize(ContentSerializer::<E>::new()));
|
let key = tri!(key.serialize(ContentSerializer::<E>::new()));
|
||||||
self.key = Some(key);
|
self.key = Some(key);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), E>
|
fn serialize_value<T>(&mut self, value: &T) -> Result<(), E>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
let key = self
|
let key = self
|
||||||
.key
|
.key
|
||||||
.take()
|
.take()
|
||||||
.expect("serialize_value called before serialize_key");
|
.expect("serialize_value called before serialize_key");
|
||||||
let value = try!(value.serialize(ContentSerializer::<E>::new()));
|
let value = tri!(value.serialize(ContentSerializer::<E>::new()));
|
||||||
self.entries.push((key, value));
|
self.entries.push((key, value));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -922,13 +912,13 @@ mod content {
|
|||||||
Ok(Content::Map(self.entries))
|
Ok(Content::Map(self.entries))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_entry<K: ?Sized, V: ?Sized>(&mut self, key: &K, value: &V) -> Result<(), E>
|
fn serialize_entry<K, V>(&mut self, key: &K, value: &V) -> Result<(), E>
|
||||||
where
|
where
|
||||||
K: Serialize,
|
K: ?Sized + Serialize,
|
||||||
V: Serialize,
|
V: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
let key = try!(key.serialize(ContentSerializer::<E>::new()));
|
let key = tri!(key.serialize(ContentSerializer::<E>::new()));
|
||||||
let value = try!(value.serialize(ContentSerializer::<E>::new()));
|
let value = tri!(value.serialize(ContentSerializer::<E>::new()));
|
||||||
self.entries.push((key, value));
|
self.entries.push((key, value));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -947,11 +937,11 @@ mod content {
|
|||||||
type Ok = Content;
|
type Ok = Content;
|
||||||
type Error = E;
|
type Error = E;
|
||||||
|
|
||||||
fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<(), E>
|
fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), E>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
let value = try!(value.serialize(ContentSerializer::<E>::new()));
|
let value = tri!(value.serialize(ContentSerializer::<E>::new()));
|
||||||
self.fields.push((key, value));
|
self.fields.push((key, value));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -976,11 +966,11 @@ mod content {
|
|||||||
type Ok = Content;
|
type Ok = Content;
|
||||||
type Error = E;
|
type Error = E;
|
||||||
|
|
||||||
fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<(), E>
|
fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), E>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
let value = try!(value.serialize(ContentSerializer::<E>::new()));
|
let value = tri!(value.serialize(ContentSerializer::<E>::new()));
|
||||||
self.fields.push((key, value));
|
self.fields.push((key, value));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -1025,7 +1015,7 @@ where
|
|||||||
type SerializeTupleStruct = Impossible<Self::Ok, M::Error>;
|
type SerializeTupleStruct = Impossible<Self::Ok, M::Error>;
|
||||||
type SerializeMap = FlatMapSerializeMap<'a, M>;
|
type SerializeMap = FlatMapSerializeMap<'a, M>;
|
||||||
type SerializeStruct = FlatMapSerializeStruct<'a, M>;
|
type SerializeStruct = FlatMapSerializeStruct<'a, M>;
|
||||||
type SerializeTupleVariant = Impossible<Self::Ok, M::Error>;
|
type SerializeTupleVariant = FlatMapSerializeTupleVariantAsMapValue<'a, M>;
|
||||||
type SerializeStructVariant = FlatMapSerializeStructVariantAsMapValue<'a, M>;
|
type SerializeStructVariant = FlatMapSerializeStructVariantAsMapValue<'a, M>;
|
||||||
|
|
||||||
fn serialize_bool(self, _: bool) -> Result<Self::Ok, Self::Error> {
|
fn serialize_bool(self, _: bool) -> Result<Self::Ok, Self::Error> {
|
||||||
@@ -1088,9 +1078,9 @@ where
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
|
fn serialize_some<T>(self, value: &T) -> Result<Self::Ok, Self::Error>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
value.serialize(self)
|
value.serialize(self)
|
||||||
}
|
}
|
||||||
@@ -1100,30 +1090,30 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_unit_struct(self, _: &'static str) -> Result<Self::Ok, Self::Error> {
|
fn serialize_unit_struct(self, _: &'static str) -> Result<Self::Ok, Self::Error> {
|
||||||
Err(Self::bad_type(Unsupported::UnitStruct))
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_unit_variant(
|
fn serialize_unit_variant(
|
||||||
self,
|
self,
|
||||||
_: &'static str,
|
_: &'static str,
|
||||||
_: u32,
|
_: u32,
|
||||||
_: &'static str,
|
variant: &'static str,
|
||||||
) -> Result<Self::Ok, Self::Error> {
|
) -> Result<Self::Ok, Self::Error> {
|
||||||
Err(Self::bad_type(Unsupported::Enum))
|
self.0.serialize_entry(variant, &())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_newtype_struct<T: ?Sized>(
|
fn serialize_newtype_struct<T>(
|
||||||
self,
|
self,
|
||||||
_: &'static str,
|
_: &'static str,
|
||||||
value: &T,
|
value: &T,
|
||||||
) -> Result<Self::Ok, Self::Error>
|
) -> Result<Self::Ok, Self::Error>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
value.serialize(self)
|
value.serialize(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_newtype_variant<T: ?Sized>(
|
fn serialize_newtype_variant<T>(
|
||||||
self,
|
self,
|
||||||
_: &'static str,
|
_: &'static str,
|
||||||
_: u32,
|
_: u32,
|
||||||
@@ -1131,10 +1121,9 @@ where
|
|||||||
value: &T,
|
value: &T,
|
||||||
) -> Result<Self::Ok, Self::Error>
|
) -> Result<Self::Ok, Self::Error>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
try!(self.0.serialize_key(variant));
|
self.0.serialize_entry(variant, value)
|
||||||
self.0.serialize_value(value)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_seq(self, _: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
|
fn serialize_seq(self, _: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
|
||||||
@@ -1157,10 +1146,11 @@ where
|
|||||||
self,
|
self,
|
||||||
_: &'static str,
|
_: &'static str,
|
||||||
_: u32,
|
_: u32,
|
||||||
_: &'static str,
|
variant: &'static str,
|
||||||
_: usize,
|
_: usize,
|
||||||
) -> Result<Self::SerializeTupleVariant, Self::Error> {
|
) -> Result<Self::SerializeTupleVariant, Self::Error> {
|
||||||
Err(Self::bad_type(Unsupported::Enum))
|
tri!(self.0.serialize_key(variant));
|
||||||
|
Ok(FlatMapSerializeTupleVariantAsMapValue::new(self.0))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_map(self, _: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
|
fn serialize_map(self, _: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
|
||||||
@@ -1182,7 +1172,7 @@ where
|
|||||||
inner_variant: &'static str,
|
inner_variant: &'static str,
|
||||||
_: usize,
|
_: usize,
|
||||||
) -> Result<Self::SerializeStructVariant, Self::Error> {
|
) -> Result<Self::SerializeStructVariant, Self::Error> {
|
||||||
try!(self.0.serialize_key(inner_variant));
|
tri!(self.0.serialize_key(inner_variant));
|
||||||
Ok(FlatMapSerializeStructVariantAsMapValue::new(
|
Ok(FlatMapSerializeStructVariantAsMapValue::new(
|
||||||
self.0,
|
self.0,
|
||||||
inner_variant,
|
inner_variant,
|
||||||
@@ -1201,28 +1191,24 @@ where
|
|||||||
type Ok = ();
|
type Ok = ();
|
||||||
type Error = M::Error;
|
type Error = M::Error;
|
||||||
|
|
||||||
fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), Self::Error>
|
fn serialize_key<T>(&mut self, key: &T) -> Result<(), Self::Error>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
self.0.serialize_key(key)
|
self.0.serialize_key(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
|
fn serialize_value<T>(&mut self, value: &T) -> Result<(), Self::Error>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
self.0.serialize_value(value)
|
self.0.serialize_value(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_entry<K: ?Sized, V: ?Sized>(
|
fn serialize_entry<K, V>(&mut self, key: &K, value: &V) -> Result<(), Self::Error>
|
||||||
&mut self,
|
|
||||||
key: &K,
|
|
||||||
value: &V,
|
|
||||||
) -> Result<(), Self::Error>
|
|
||||||
where
|
where
|
||||||
K: Serialize,
|
K: ?Sized + Serialize,
|
||||||
V: Serialize,
|
V: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
self.0.serialize_entry(key, value)
|
self.0.serialize_entry(key, value)
|
||||||
}
|
}
|
||||||
@@ -1243,13 +1229,9 @@ where
|
|||||||
type Ok = ();
|
type Ok = ();
|
||||||
type Error = M::Error;
|
type Error = M::Error;
|
||||||
|
|
||||||
fn serialize_field<T: ?Sized>(
|
fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error>
|
||||||
&mut self,
|
|
||||||
key: &'static str,
|
|
||||||
value: &T,
|
|
||||||
) -> Result<(), Self::Error>
|
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
self.0.serialize_entry(key, value)
|
self.0.serialize_entry(key, value)
|
||||||
}
|
}
|
||||||
@@ -1259,6 +1241,52 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
|
pub struct FlatMapSerializeTupleVariantAsMapValue<'a, M: 'a> {
|
||||||
|
map: &'a mut M,
|
||||||
|
fields: Vec<Content>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
|
impl<'a, M> FlatMapSerializeTupleVariantAsMapValue<'a, M>
|
||||||
|
where
|
||||||
|
M: SerializeMap + 'a,
|
||||||
|
{
|
||||||
|
fn new(map: &'a mut M) -> Self {
|
||||||
|
FlatMapSerializeTupleVariantAsMapValue {
|
||||||
|
map,
|
||||||
|
fields: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
|
impl<'a, M> ser::SerializeTupleVariant for FlatMapSerializeTupleVariantAsMapValue<'a, M>
|
||||||
|
where
|
||||||
|
M: SerializeMap + 'a,
|
||||||
|
{
|
||||||
|
type Ok = ();
|
||||||
|
type Error = M::Error;
|
||||||
|
|
||||||
|
fn serialize_field<T>(&mut self, value: &T) -> Result<(), Self::Error>
|
||||||
|
where
|
||||||
|
T: ?Sized + Serialize,
|
||||||
|
{
|
||||||
|
let value = tri!(value.serialize(ContentSerializer::<M::Error>::new()));
|
||||||
|
self.fields.push(value);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn end(self) -> Result<(), Self::Error> {
|
||||||
|
tri!(self.map.serialize_value(&Content::Seq(self.fields)));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
pub struct FlatMapSerializeStructVariantAsMapValue<'a, M: 'a> {
|
pub struct FlatMapSerializeStructVariantAsMapValue<'a, M: 'a> {
|
||||||
map: &'a mut M,
|
map: &'a mut M,
|
||||||
@@ -1288,23 +1316,47 @@ where
|
|||||||
type Ok = ();
|
type Ok = ();
|
||||||
type Error = M::Error;
|
type Error = M::Error;
|
||||||
|
|
||||||
fn serialize_field<T: ?Sized>(
|
fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error>
|
||||||
&mut self,
|
|
||||||
key: &'static str,
|
|
||||||
value: &T,
|
|
||||||
) -> Result<(), Self::Error>
|
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
let value = try!(value.serialize(ContentSerializer::<M::Error>::new()));
|
let value = tri!(value.serialize(ContentSerializer::<M::Error>::new()));
|
||||||
self.fields.push((key, value));
|
self.fields.push((key, value));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn end(self) -> Result<(), Self::Error> {
|
fn end(self) -> Result<(), Self::Error> {
|
||||||
try!(self
|
tri!(self
|
||||||
.map
|
.map
|
||||||
.serialize_value(&Content::Struct(self.name, self.fields)));
|
.serialize_value(&Content::Struct(self.name, self.fields)));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct AdjacentlyTaggedEnumVariant {
|
||||||
|
pub enum_name: &'static str,
|
||||||
|
pub variant_index: u32,
|
||||||
|
pub variant_name: &'static str,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Serialize for AdjacentlyTaggedEnumVariant {
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: Serializer,
|
||||||
|
{
|
||||||
|
serializer.serialize_unit_variant(self.enum_name, self.variant_index, self.variant_name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error when Serialize for a non_exhaustive remote enum encounters a variant
|
||||||
|
// that is not recognized.
|
||||||
|
pub struct CannotSerializeVariant<T>(pub T);
|
||||||
|
|
||||||
|
impl<T> Display for CannotSerializeVariant<T>
|
||||||
|
where
|
||||||
|
T: Debug,
|
||||||
|
{
|
||||||
|
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(formatter, "enum variant cannot be serialized: {:?}", self.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,69 @@
|
|||||||
|
[package]
|
||||||
|
name = "serde_core"
|
||||||
|
version = "1.0.221"
|
||||||
|
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
||||||
|
build = "build.rs"
|
||||||
|
categories = ["encoding", "no-std", "no-std::no-alloc"]
|
||||||
|
description = "Serde traits only, with no support for derive -- use the `serde` crate instead"
|
||||||
|
documentation = "https://docs.rs/serde_core"
|
||||||
|
edition = "2021"
|
||||||
|
homepage = "https://serde.rs"
|
||||||
|
keywords = ["serde", "serialization", "no_std"]
|
||||||
|
license = "MIT OR Apache-2.0"
|
||||||
|
repository = "https://github.com/serde-rs/serde"
|
||||||
|
rust-version = "1.56"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
serde = { version = "1", path = "../serde" }
|
||||||
|
serde_derive = { version = "1", path = "../serde_derive" }
|
||||||
|
|
||||||
|
[package.metadata.playground]
|
||||||
|
features = ["rc", "result"]
|
||||||
|
|
||||||
|
[package.metadata.docs.rs]
|
||||||
|
features = ["rc", "result", "unstable"]
|
||||||
|
targets = ["x86_64-unknown-linux-gnu"]
|
||||||
|
rustdoc-args = [
|
||||||
|
"--generate-link-to-definition",
|
||||||
|
"--extern-html-root-url=core=https://doc.rust-lang.org",
|
||||||
|
"--extern-html-root-url=alloc=https://doc.rust-lang.org",
|
||||||
|
"--extern-html-root-url=std=https://doc.rust-lang.org",
|
||||||
|
]
|
||||||
|
|
||||||
|
# This cfg cannot be enabled, but it still forces Cargo to keep serde_derive's
|
||||||
|
# version in lockstep with serde's, even if someone depends on the two crates
|
||||||
|
# separately with serde's "derive" feature disabled. Every serde_derive release
|
||||||
|
# is compatible with exactly one serde release because the generated code
|
||||||
|
# involves nonpublic APIs which are not bound by semver.
|
||||||
|
[target.'cfg(any())'.dependencies]
|
||||||
|
serde_derive = { version = "=1.0.221", path = "../serde_derive" }
|
||||||
|
|
||||||
|
|
||||||
|
### FEATURES #################################################################
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = ["std", "result"]
|
||||||
|
|
||||||
|
# Provide impls for common standard library types like Vec<T> and HashMap<K, V>.
|
||||||
|
# Requires a dependency on the Rust standard library.
|
||||||
|
std = []
|
||||||
|
|
||||||
|
# Provide impls for types that require unstable functionality. For tracking and
|
||||||
|
# discussion of unstable functionality please refer to this issue:
|
||||||
|
#
|
||||||
|
# https://github.com/serde-rs/serde/issues/812
|
||||||
|
unstable = []
|
||||||
|
|
||||||
|
# Provide impls for types in the Rust core allocation and collections library
|
||||||
|
# including String, Box<T>, Vec<T>, and Cow<T>. This is a subset of std but may
|
||||||
|
# be enabled without depending on all of std.
|
||||||
|
alloc = []
|
||||||
|
|
||||||
|
# Opt into impls for Rc<T> and Arc<T>. Serializing and deserializing these types
|
||||||
|
# does not preserve identity and may result in multiple copies of the same data.
|
||||||
|
# Be sure that this is what you want before enabling this feature.
|
||||||
|
rc = []
|
||||||
|
|
||||||
|
# Provide impls for Result<T, E>. Convenient in some contexts but can lead to
|
||||||
|
# confusion if ? or unwrap are used incautiously.
|
||||||
|
result = []
|
||||||
Symlink
+1
@@ -0,0 +1 @@
|
|||||||
|
../LICENSE-APACHE
|
||||||
Symlink
+1
@@ -0,0 +1 @@
|
|||||||
|
../LICENSE-MIT
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
The `serde_core` crate contains Serde's trait definitions with **no support for
|
||||||
|
#\[derive()\]**.
|
||||||
|
|
||||||
|
In crates that derive an implementation of `Serialize` or `Deserialize`, you
|
||||||
|
must depend on the [`serde`] crate, not `serde_core`.
|
||||||
|
|
||||||
|
[`serde`]: https://crates.io/crates/serde
|
||||||
|
|
||||||
|
In crates that handwrite implementations of Serde traits, or only use them as
|
||||||
|
trait bounds, depending on `serde_core` is permitted. But `serde` re-exports all
|
||||||
|
of these traits and can be used for this use case too. If in doubt, disregard
|
||||||
|
`serde_core` and always use `serde`.
|
||||||
|
|
||||||
|
Crates that depend on `serde_core` instead of `serde` are able to compile in
|
||||||
|
parallel with `serde_derive` even when `serde`'s "derive" feature is turned on,
|
||||||
|
as shown in the following build timings.
|
||||||
|
|
||||||
|
<br>
|
||||||
|
|
||||||
|
| When `serde_json` depends on `serde` |
|
||||||
|
|---|
|
||||||
|
| <img src="https://github.com/user-attachments/assets/78dc179c-6ab1-4059-928c-1474b0d9d0bb"> |
|
||||||
|
|
||||||
|
<br>
|
||||||
|
|
||||||
|
| When `serde_json` depends on `serde_core` |
|
||||||
|
|---|
|
||||||
|
| <img src="https://github.com/user-attachments/assets/6b6cff5e-3e45-4ac7-9db1-d99ee8b9f5f7"> |
|
||||||
@@ -0,0 +1,97 @@
|
|||||||
|
use std::env;
|
||||||
|
use std::process::Command;
|
||||||
|
use std::str;
|
||||||
|
|
||||||
|
// The rustc-cfg strings below are *not* public API. Please let us know by
|
||||||
|
// opening a GitHub issue if your build environment requires some way to enable
|
||||||
|
// these cfgs other than by executing our build script.
|
||||||
|
fn main() {
|
||||||
|
println!("cargo:rerun-if-changed=build.rs");
|
||||||
|
|
||||||
|
let minor = match rustc_minor_version() {
|
||||||
|
Some(minor) => minor,
|
||||||
|
None => return,
|
||||||
|
};
|
||||||
|
|
||||||
|
if minor >= 77 {
|
||||||
|
println!("cargo:rustc-check-cfg=cfg(no_core_cstr)");
|
||||||
|
println!("cargo:rustc-check-cfg=cfg(no_core_error)");
|
||||||
|
println!("cargo:rustc-check-cfg=cfg(no_core_net)");
|
||||||
|
println!("cargo:rustc-check-cfg=cfg(no_core_num_saturating)");
|
||||||
|
println!("cargo:rustc-check-cfg=cfg(no_diagnostic_namespace)");
|
||||||
|
println!("cargo:rustc-check-cfg=cfg(no_serde_derive)");
|
||||||
|
println!("cargo:rustc-check-cfg=cfg(no_std_atomic)");
|
||||||
|
println!("cargo:rustc-check-cfg=cfg(no_std_atomic64)");
|
||||||
|
println!("cargo:rustc-check-cfg=cfg(no_target_has_atomic)");
|
||||||
|
}
|
||||||
|
|
||||||
|
let target = env::var("TARGET").unwrap();
|
||||||
|
let emscripten = target == "asmjs-unknown-emscripten" || target == "wasm32-unknown-emscripten";
|
||||||
|
|
||||||
|
// 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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Current minimum supported version of serde_derive crate is Rust 1.61.
|
||||||
|
if minor < 61 {
|
||||||
|
println!("cargo:rustc-cfg=no_serde_derive");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Support for core::num::Saturating and std::num::Saturating stabilized in Rust 1.74
|
||||||
|
// https://blog.rust-lang.org/2023/11/16/Rust-1.74.0.html#stabilized-apis
|
||||||
|
if minor < 74 {
|
||||||
|
println!("cargo:rustc-cfg=no_core_num_saturating");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Support for core::net stabilized in Rust 1.77.
|
||||||
|
// https://blog.rust-lang.org/2024/03/21/Rust-1.77.0.html
|
||||||
|
if minor < 77 {
|
||||||
|
println!("cargo:rustc-cfg=no_core_net");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Support for the `#[diagnostic]` tool attribute namespace
|
||||||
|
// https://blog.rust-lang.org/2024/05/02/Rust-1.78.0.html#diagnostic-attributes
|
||||||
|
if minor < 78 {
|
||||||
|
println!("cargo:rustc-cfg=no_diagnostic_namespace");
|
||||||
|
}
|
||||||
|
|
||||||
|
// The Error trait became available in core in 1.81.
|
||||||
|
// https://blog.rust-lang.org/2024/09/05/Rust-1.81.0.html#coreerrorerror
|
||||||
|
if minor < 81 {
|
||||||
|
println!("cargo:rustc-cfg=no_core_error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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()
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
use lib::*;
|
use crate::lib::*;
|
||||||
|
|
||||||
use de::{
|
use crate::de::{
|
||||||
Deserialize, Deserializer, EnumAccess, Error, MapAccess, SeqAccess, VariantAccess, Visitor,
|
Deserialize, Deserializer, EnumAccess, Error, MapAccess, SeqAccess, VariantAccess, Visitor,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -129,12 +129,10 @@ impl<'de> Visitor<'de> for IgnoredAny {
|
|||||||
Ok(IgnoredAny)
|
Ok(IgnoredAny)
|
||||||
}
|
}
|
||||||
|
|
||||||
serde_if_integer128! {
|
#[inline]
|
||||||
#[inline]
|
fn visit_i128<E>(self, x: i128) -> Result<Self::Value, E> {
|
||||||
fn visit_i128<E>(self, x: i128) -> Result<Self::Value, E> {
|
let _ = x;
|
||||||
let _ = x;
|
Ok(IgnoredAny)
|
||||||
Ok(IgnoredAny)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@@ -143,12 +141,10 @@ impl<'de> Visitor<'de> for IgnoredAny {
|
|||||||
Ok(IgnoredAny)
|
Ok(IgnoredAny)
|
||||||
}
|
}
|
||||||
|
|
||||||
serde_if_integer128! {
|
#[inline]
|
||||||
#[inline]
|
fn visit_u128<E>(self, x: u128) -> Result<Self::Value, E> {
|
||||||
fn visit_u128<E>(self, x: u128) -> Result<Self::Value, E> {
|
let _ = x;
|
||||||
let _ = x;
|
Ok(IgnoredAny)
|
||||||
Ok(IgnoredAny)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@@ -197,7 +193,7 @@ impl<'de> Visitor<'de> for IgnoredAny {
|
|||||||
where
|
where
|
||||||
A: SeqAccess<'de>,
|
A: SeqAccess<'de>,
|
||||||
{
|
{
|
||||||
while let Some(IgnoredAny) = try!(seq.next_element()) {
|
while let Some(IgnoredAny) = tri!(seq.next_element()) {
|
||||||
// Gobble
|
// Gobble
|
||||||
}
|
}
|
||||||
Ok(IgnoredAny)
|
Ok(IgnoredAny)
|
||||||
@@ -208,7 +204,7 @@ impl<'de> Visitor<'de> for IgnoredAny {
|
|||||||
where
|
where
|
||||||
A: MapAccess<'de>,
|
A: MapAccess<'de>,
|
||||||
{
|
{
|
||||||
while let Some((IgnoredAny, IgnoredAny)) = try!(map.next_entry()) {
|
while let Some((IgnoredAny, IgnoredAny)) = tri!(map.next_entry()) {
|
||||||
// Gobble
|
// Gobble
|
||||||
}
|
}
|
||||||
Ok(IgnoredAny)
|
Ok(IgnoredAny)
|
||||||
@@ -227,7 +223,7 @@ impl<'de> Visitor<'de> for IgnoredAny {
|
|||||||
where
|
where
|
||||||
A: EnumAccess<'de>,
|
A: EnumAccess<'de>,
|
||||||
{
|
{
|
||||||
try!(data.variant::<IgnoredAny>()).1.newtype_variant()
|
tri!(data.variant::<IgnoredAny>()).1.newtype_variant()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -64,8 +64,8 @@
|
|||||||
//! - RefCell\<T\>
|
//! - RefCell\<T\>
|
||||||
//! - Mutex\<T\>
|
//! - Mutex\<T\>
|
||||||
//! - RwLock\<T\>
|
//! - RwLock\<T\>
|
||||||
//! - Rc\<T\> *(if* features = ["rc"] *is enabled)*
|
//! - Rc\<T\> *(if* features = \["rc"\] *is enabled)*
|
||||||
//! - Arc\<T\> *(if* features = ["rc"] *is enabled)*
|
//! - Arc\<T\> *(if* features = \["rc"\] *is enabled)*
|
||||||
//! - **Collection types**:
|
//! - **Collection types**:
|
||||||
//! - BTreeMap\<K, V\>
|
//! - BTreeMap\<K, V\>
|
||||||
//! - BTreeSet\<T\>
|
//! - BTreeSet\<T\>
|
||||||
@@ -101,8 +101,8 @@
|
|||||||
//! - SocketAddrV6
|
//! - SocketAddrV6
|
||||||
//!
|
//!
|
||||||
//! [Implementing `Deserialize`]: https://serde.rs/impl-deserialize.html
|
//! [Implementing `Deserialize`]: https://serde.rs/impl-deserialize.html
|
||||||
//! [`Deserialize`]: ../trait.Deserialize.html
|
//! [`Deserialize`]: crate::Deserialize
|
||||||
//! [`Deserializer`]: ../trait.Deserializer.html
|
//! [`Deserializer`]: crate::Deserializer
|
||||||
//! [`LinkedHashMap<K, V>`]: https://docs.rs/linked-hash-map/*/linked_hash_map/struct.LinkedHashMap.html
|
//! [`LinkedHashMap<K, V>`]: https://docs.rs/linked-hash-map/*/linked_hash_map/struct.LinkedHashMap.html
|
||||||
//! [`postcard`]: https://github.com/jamesmunns/postcard
|
//! [`postcard`]: https://github.com/jamesmunns/postcard
|
||||||
//! [`linked-hash-map`]: https://crates.io/crates/linked-hash-map
|
//! [`linked-hash-map`]: https://crates.io/crates/linked-hash-map
|
||||||
@@ -112,29 +112,26 @@
|
|||||||
//! [derive section of the manual]: https://serde.rs/derive.html
|
//! [derive section of the manual]: https://serde.rs/derive.html
|
||||||
//! [data formats]: https://serde.rs/#data-formats
|
//! [data formats]: https://serde.rs/#data-formats
|
||||||
|
|
||||||
use lib::*;
|
use crate::lib::*;
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
pub mod value;
|
pub mod value;
|
||||||
|
|
||||||
#[cfg(not(no_integer128))]
|
|
||||||
mod format;
|
|
||||||
mod ignored_any;
|
mod ignored_any;
|
||||||
mod impls;
|
mod impls;
|
||||||
mod utf8;
|
|
||||||
|
|
||||||
pub use self::ignored_any::IgnoredAny;
|
pub use self::ignored_any::IgnoredAny;
|
||||||
|
pub use crate::private::InPlaceSeed;
|
||||||
#[cfg(all(feature = "unstable", not(feature = "std")))]
|
#[cfg(all(not(feature = "std"), no_core_error))]
|
||||||
|
#[doc(no_inline)]
|
||||||
|
pub use crate::std_error::Error as StdError;
|
||||||
|
#[cfg(not(any(feature = "std", no_core_error)))]
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
pub use core::error::Error as StdError;
|
pub use core::error::Error as StdError;
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
pub use std::error::Error as StdError;
|
pub use std::error::Error as StdError;
|
||||||
#[cfg(not(any(feature = "std", feature = "unstable")))]
|
|
||||||
#[doc(no_inline)]
|
|
||||||
pub use std_error::Error as StdError;
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
@@ -160,6 +157,12 @@ macro_rules! declare_error_trait {
|
|||||||
/// type appropriate for a basic JSON data format.
|
/// type appropriate for a basic JSON data format.
|
||||||
///
|
///
|
||||||
/// [example data format]: https://serde.rs/data-format.html
|
/// [example data format]: https://serde.rs/data-format.html
|
||||||
|
#[cfg_attr(
|
||||||
|
not(no_diagnostic_namespace),
|
||||||
|
diagnostic::on_unimplemented(
|
||||||
|
message = "the trait bound `{Self}: serde::de::Error` is not satisfied",
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub trait Error: Sized $(+ $($supertrait)::+)* {
|
pub trait Error: Sized $(+ $($supertrait)::+)* {
|
||||||
/// Raised when there is general error when deserializing a type.
|
/// Raised when there is general error when deserializing a type.
|
||||||
///
|
///
|
||||||
@@ -208,7 +211,7 @@ macro_rules! declare_error_trait {
|
|||||||
/// containing an integer, the unexpected type is the integer and the
|
/// containing an integer, the unexpected type is the integer and the
|
||||||
/// expected type is the string.
|
/// expected type is the string.
|
||||||
#[cold]
|
#[cold]
|
||||||
fn invalid_type(unexp: Unexpected, exp: &Expected) -> Self {
|
fn invalid_type(unexp: Unexpected, exp: &dyn Expected) -> Self {
|
||||||
Error::custom(format_args!("invalid type: {}, expected {}", unexp, exp))
|
Error::custom(format_args!("invalid type: {}, expected {}", unexp, exp))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -226,7 +229,7 @@ macro_rules! declare_error_trait {
|
|||||||
/// that is not valid UTF-8, the unexpected value is the bytes and the
|
/// that is not valid UTF-8, the unexpected value is the bytes and the
|
||||||
/// expected value is a string.
|
/// expected value is a string.
|
||||||
#[cold]
|
#[cold]
|
||||||
fn invalid_value(unexp: Unexpected, exp: &Expected) -> Self {
|
fn invalid_value(unexp: Unexpected, exp: &dyn Expected) -> Self {
|
||||||
Error::custom(format_args!("invalid value: {}, expected {}", unexp, exp))
|
Error::custom(format_args!("invalid value: {}, expected {}", unexp, exp))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -240,7 +243,7 @@ macro_rules! declare_error_trait {
|
|||||||
/// expected. For example `exp` might say that a tuple of size 6 was
|
/// expected. For example `exp` might say that a tuple of size 6 was
|
||||||
/// expected.
|
/// expected.
|
||||||
#[cold]
|
#[cold]
|
||||||
fn invalid_length(len: usize, exp: &Expected) -> Self {
|
fn invalid_length(len: usize, exp: &dyn Expected) -> Self {
|
||||||
Error::custom(format_args!("invalid length {}, expected {}", len, exp))
|
Error::custom(format_args!("invalid length {}, expected {}", len, exp))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -403,20 +406,20 @@ impl<'a> fmt::Display for Unexpected<'a> {
|
|||||||
Bool(b) => write!(formatter, "boolean `{}`", b),
|
Bool(b) => write!(formatter, "boolean `{}`", b),
|
||||||
Unsigned(i) => write!(formatter, "integer `{}`", i),
|
Unsigned(i) => write!(formatter, "integer `{}`", i),
|
||||||
Signed(i) => write!(formatter, "integer `{}`", i),
|
Signed(i) => write!(formatter, "integer `{}`", i),
|
||||||
Float(f) => write!(formatter, "floating point `{}`", f),
|
Float(f) => write!(formatter, "floating point `{}`", WithDecimalPoint(f)),
|
||||||
Char(c) => write!(formatter, "character `{}`", c),
|
Char(c) => write!(formatter, "character `{}`", c),
|
||||||
Str(s) => write!(formatter, "string {:?}", s),
|
Str(s) => write!(formatter, "string {:?}", s),
|
||||||
Bytes(_) => write!(formatter, "byte array"),
|
Bytes(_) => formatter.write_str("byte array"),
|
||||||
Unit => write!(formatter, "unit value"),
|
Unit => formatter.write_str("unit value"),
|
||||||
Option => write!(formatter, "Option value"),
|
Option => formatter.write_str("Option value"),
|
||||||
NewtypeStruct => write!(formatter, "newtype struct"),
|
NewtypeStruct => formatter.write_str("newtype struct"),
|
||||||
Seq => write!(formatter, "sequence"),
|
Seq => formatter.write_str("sequence"),
|
||||||
Map => write!(formatter, "map"),
|
Map => formatter.write_str("map"),
|
||||||
Enum => write!(formatter, "enum"),
|
Enum => formatter.write_str("enum"),
|
||||||
UnitVariant => write!(formatter, "unit variant"),
|
UnitVariant => formatter.write_str("unit variant"),
|
||||||
NewtypeVariant => write!(formatter, "newtype variant"),
|
NewtypeVariant => formatter.write_str("newtype variant"),
|
||||||
TupleVariant => write!(formatter, "tuple variant"),
|
TupleVariant => formatter.write_str("tuple variant"),
|
||||||
StructVariant => write!(formatter, "struct variant"),
|
StructVariant => formatter.write_str("struct variant"),
|
||||||
Other(other) => formatter.write_str(other),
|
Other(other) => formatter.write_str(other),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -473,6 +476,12 @@ impl<'a> fmt::Display for Unexpected<'a> {
|
|||||||
/// ));
|
/// ));
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
|
#[cfg_attr(
|
||||||
|
not(no_diagnostic_namespace),
|
||||||
|
diagnostic::on_unimplemented(
|
||||||
|
message = "the trait bound `{Self}: serde::de::Expected` is not satisfied",
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub trait Expected {
|
pub trait Expected {
|
||||||
/// Format an explanation of what data was being expected. Same signature as
|
/// Format an explanation of what data was being expected. Same signature as
|
||||||
/// the `Display` and `Debug` traits.
|
/// the `Display` and `Debug` traits.
|
||||||
@@ -488,13 +497,13 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Expected for &'a str {
|
impl Expected for &str {
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||||
formatter.write_str(self)
|
formatter.write_str(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Display for Expected + 'a {
|
impl Display for dyn Expected + '_ {
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||||
Expected::fmt(self, formatter)
|
Expected::fmt(self, formatter)
|
||||||
}
|
}
|
||||||
@@ -533,6 +542,16 @@ impl<'a> Display for Expected + 'a {
|
|||||||
/// deserializer lifetimes] for a more detailed explanation of these lifetimes.
|
/// deserializer lifetimes] for a more detailed explanation of these lifetimes.
|
||||||
///
|
///
|
||||||
/// [Understanding deserializer lifetimes]: https://serde.rs/lifetimes.html
|
/// [Understanding deserializer lifetimes]: https://serde.rs/lifetimes.html
|
||||||
|
#[cfg_attr(
|
||||||
|
not(no_diagnostic_namespace),
|
||||||
|
diagnostic::on_unimplemented(
|
||||||
|
// Prevents `serde_core::de::Deserialize` appearing in the error message
|
||||||
|
// in projects with no direct dependency on serde_core.
|
||||||
|
message = "the trait bound `{Self}: serde::Deserialize<'de>` is not satisfied",
|
||||||
|
note = "for local types consider adding `#[derive(serde::Deserialize)]` to your `{Self}` type",
|
||||||
|
note = "for types from other crates check whether the crate offers a `serde` feature flag",
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub trait Deserialize<'de>: Sized {
|
pub trait Deserialize<'de>: Sized {
|
||||||
/// Deserialize this value from the given Serde deserializer.
|
/// Deserialize this value from the given Serde deserializer.
|
||||||
///
|
///
|
||||||
@@ -569,7 +588,7 @@ pub trait Deserialize<'de>: Sized {
|
|||||||
D: Deserializer<'de>,
|
D: Deserializer<'de>,
|
||||||
{
|
{
|
||||||
// Default implementation just delegates to `deserialize` impl.
|
// Default implementation just delegates to `deserialize` impl.
|
||||||
*place = try!(Deserialize::deserialize(deserializer));
|
*place = tri!(Deserialize::deserialize(deserializer));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -605,6 +624,12 @@ pub trait Deserialize<'de>: Sized {
|
|||||||
/// lifetimes].
|
/// lifetimes].
|
||||||
///
|
///
|
||||||
/// [Understanding deserializer lifetimes]: https://serde.rs/lifetimes.html
|
/// [Understanding deserializer lifetimes]: https://serde.rs/lifetimes.html
|
||||||
|
#[cfg_attr(
|
||||||
|
not(no_diagnostic_namespace),
|
||||||
|
diagnostic::on_unimplemented(
|
||||||
|
message = "the trait bound `{Self}: serde::de::DeserializeOwned` is not satisfied",
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub trait DeserializeOwned: for<'de> Deserialize<'de> {}
|
pub trait DeserializeOwned: for<'de> Deserialize<'de> {}
|
||||||
impl<T> DeserializeOwned for T where T: for<'de> Deserialize<'de> {}
|
impl<T> DeserializeOwned for T where T: for<'de> Deserialize<'de> {}
|
||||||
|
|
||||||
@@ -770,6 +795,12 @@ impl<T> DeserializeOwned for T where T: for<'de> Deserialize<'de> {}
|
|||||||
/// # Ok(())
|
/// # Ok(())
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
|
#[cfg_attr(
|
||||||
|
not(no_diagnostic_namespace),
|
||||||
|
diagnostic::on_unimplemented(
|
||||||
|
message = "the trait bound `{Self}: serde::de::DeserializeSeed<'de>` is not satisfied",
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub trait DeserializeSeed<'de>: Sized {
|
pub trait DeserializeSeed<'de>: Sized {
|
||||||
/// The type produced by using this seed.
|
/// The type produced by using this seed.
|
||||||
type Value;
|
type Value;
|
||||||
@@ -906,6 +937,12 @@ where
|
|||||||
/// a basic JSON `Deserializer`.
|
/// a basic JSON `Deserializer`.
|
||||||
///
|
///
|
||||||
/// [example data format]: https://serde.rs/data-format.html
|
/// [example data format]: https://serde.rs/data-format.html
|
||||||
|
#[cfg_attr(
|
||||||
|
not(no_diagnostic_namespace),
|
||||||
|
diagnostic::on_unimplemented(
|
||||||
|
message = "the trait bound `{Self}: serde::de::Deserializer<'de>` is not satisfied",
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub trait Deserializer<'de>: Sized {
|
pub trait Deserializer<'de>: Sized {
|
||||||
/// The error type that can be returned if some error occurs during
|
/// The error type that can be returned if some error occurs during
|
||||||
/// deserialization.
|
/// deserialization.
|
||||||
@@ -949,18 +986,15 @@ pub trait Deserializer<'de>: Sized {
|
|||||||
where
|
where
|
||||||
V: Visitor<'de>;
|
V: Visitor<'de>;
|
||||||
|
|
||||||
serde_if_integer128! {
|
/// Hint that the `Deserialize` type is expecting an `i128` value.
|
||||||
/// Hint that the `Deserialize` type is expecting an `i128` value.
|
///
|
||||||
///
|
/// The default behavior unconditionally returns an error.
|
||||||
/// This method is available only on Rust compiler versions >=1.26. The
|
fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
/// default behavior unconditionally returns an error.
|
where
|
||||||
fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
V: Visitor<'de>,
|
||||||
where
|
{
|
||||||
V: Visitor<'de>
|
let _ = visitor;
|
||||||
{
|
Err(Error::custom("i128 is not supported"))
|
||||||
let _ = visitor;
|
|
||||||
Err(Error::custom("i128 is not supported"))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Hint that the `Deserialize` type is expecting a `u8` value.
|
/// Hint that the `Deserialize` type is expecting a `u8` value.
|
||||||
@@ -983,18 +1017,15 @@ pub trait Deserializer<'de>: Sized {
|
|||||||
where
|
where
|
||||||
V: Visitor<'de>;
|
V: Visitor<'de>;
|
||||||
|
|
||||||
serde_if_integer128! {
|
/// Hint that the `Deserialize` type is expecting an `u128` value.
|
||||||
/// Hint that the `Deserialize` type is expecting an `u128` value.
|
///
|
||||||
///
|
/// The default behavior unconditionally returns an error.
|
||||||
/// This method is available only on Rust compiler versions >=1.26. The
|
fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
/// default behavior unconditionally returns an error.
|
where
|
||||||
fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
V: Visitor<'de>,
|
||||||
where
|
{
|
||||||
V: Visitor<'de>
|
let _ = visitor;
|
||||||
{
|
Err(Error::custom("u128 is not supported"))
|
||||||
let _ = visitor;
|
|
||||||
Err(Error::custom("u128 is not supported"))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Hint that the `Deserialize` type is expecting a `f32` value.
|
/// Hint that the `Deserialize` type is expecting a `f32` value.
|
||||||
@@ -1227,13 +1258,9 @@ pub trait Deserializer<'de>: Sized {
|
|||||||
// Not public API.
|
// Not public API.
|
||||||
#[cfg(all(not(no_serde_derive), any(feature = "std", feature = "alloc")))]
|
#[cfg(all(not(no_serde_derive), any(feature = "std", feature = "alloc")))]
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
fn __deserialize_content<V>(
|
fn __deserialize_content_v1<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||||
self,
|
|
||||||
_: ::actually_private::T,
|
|
||||||
visitor: V,
|
|
||||||
) -> Result<::private::de::Content<'de>, Self::Error>
|
|
||||||
where
|
where
|
||||||
V: Visitor<'de, Value = ::private::de::Content<'de>>,
|
V: Visitor<'de, Value = crate::private::Content<'de>>,
|
||||||
{
|
{
|
||||||
self.deserialize_any(visitor)
|
self.deserialize_any(visitor)
|
||||||
}
|
}
|
||||||
@@ -1282,6 +1309,12 @@ pub trait Deserializer<'de>: Sized {
|
|||||||
/// }
|
/// }
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
|
#[cfg_attr(
|
||||||
|
not(no_diagnostic_namespace),
|
||||||
|
diagnostic::on_unimplemented(
|
||||||
|
message = "the trait bound `{Self}: serde::de::Visitor<'de>` is not satisfied",
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub trait Visitor<'de>: Sized {
|
pub trait Visitor<'de>: Sized {
|
||||||
/// The value produced by this visitor.
|
/// The value produced by this visitor.
|
||||||
type Value;
|
type Value;
|
||||||
@@ -1366,20 +1399,20 @@ pub trait Visitor<'de>: Sized {
|
|||||||
Err(Error::invalid_type(Unexpected::Signed(v), &self))
|
Err(Error::invalid_type(Unexpected::Signed(v), &self))
|
||||||
}
|
}
|
||||||
|
|
||||||
serde_if_integer128! {
|
/// The input contains a `i128`.
|
||||||
/// The input contains a `i128`.
|
///
|
||||||
///
|
/// The default implementation fails with a type error.
|
||||||
/// This method is available only on Rust compiler versions >=1.26. The
|
fn visit_i128<E>(self, v: i128) -> Result<Self::Value, E>
|
||||||
/// default implementation fails with a type error.
|
where
|
||||||
fn visit_i128<E>(self, v: i128) -> Result<Self::Value, E>
|
E: Error,
|
||||||
where
|
{
|
||||||
E: Error,
|
let mut buf = [0u8; 58];
|
||||||
{
|
let mut writer = crate::format::Buf::new(&mut buf);
|
||||||
let mut buf = [0u8; 58];
|
fmt::Write::write_fmt(&mut writer, format_args!("integer `{}` as i128", v)).unwrap();
|
||||||
let mut writer = format::Buf::new(&mut buf);
|
Err(Error::invalid_type(
|
||||||
fmt::Write::write_fmt(&mut writer, format_args!("integer `{}` as i128", v)).unwrap();
|
Unexpected::Other(writer.as_str()),
|
||||||
Err(Error::invalid_type(Unexpected::Other(writer.as_str()), &self))
|
&self,
|
||||||
}
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The input contains a `u8`.
|
/// The input contains a `u8`.
|
||||||
@@ -1428,20 +1461,20 @@ pub trait Visitor<'de>: Sized {
|
|||||||
Err(Error::invalid_type(Unexpected::Unsigned(v), &self))
|
Err(Error::invalid_type(Unexpected::Unsigned(v), &self))
|
||||||
}
|
}
|
||||||
|
|
||||||
serde_if_integer128! {
|
/// The input contains a `u128`.
|
||||||
/// The input contains a `u128`.
|
///
|
||||||
///
|
/// The default implementation fails with a type error.
|
||||||
/// This method is available only on Rust compiler versions >=1.26. The
|
fn visit_u128<E>(self, v: u128) -> Result<Self::Value, E>
|
||||||
/// default implementation fails with a type error.
|
where
|
||||||
fn visit_u128<E>(self, v: u128) -> Result<Self::Value, E>
|
E: Error,
|
||||||
where
|
{
|
||||||
E: Error,
|
let mut buf = [0u8; 57];
|
||||||
{
|
let mut writer = crate::format::Buf::new(&mut buf);
|
||||||
let mut buf = [0u8; 57];
|
fmt::Write::write_fmt(&mut writer, format_args!("integer `{}` as u128", v)).unwrap();
|
||||||
let mut writer = format::Buf::new(&mut buf);
|
Err(Error::invalid_type(
|
||||||
fmt::Write::write_fmt(&mut writer, format_args!("integer `{}` as u128", v)).unwrap();
|
Unexpected::Other(writer.as_str()),
|
||||||
Err(Error::invalid_type(Unexpected::Other(writer.as_str()), &self))
|
&self,
|
||||||
}
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The input contains an `f32`.
|
/// The input contains an `f32`.
|
||||||
@@ -1477,7 +1510,7 @@ pub trait Visitor<'de>: Sized {
|
|||||||
where
|
where
|
||||||
E: Error,
|
E: Error,
|
||||||
{
|
{
|
||||||
self.visit_str(utf8::encode(v).as_str())
|
self.visit_str(v.encode_utf8(&mut [0u8; 4]))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The input contains a string. The lifetime of the string is ephemeral and
|
/// The input contains a string. The lifetime of the string is ephemeral and
|
||||||
@@ -1532,6 +1565,7 @@ pub trait Visitor<'de>: Sized {
|
|||||||
/// `String`.
|
/// `String`.
|
||||||
#[inline]
|
#[inline]
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
|
||||||
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
|
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
|
||||||
where
|
where
|
||||||
E: Error,
|
E: Error,
|
||||||
@@ -1554,7 +1588,6 @@ pub trait Visitor<'de>: Sized {
|
|||||||
where
|
where
|
||||||
E: Error,
|
E: Error,
|
||||||
{
|
{
|
||||||
let _ = v;
|
|
||||||
Err(Error::invalid_type(Unexpected::Bytes(v), &self))
|
Err(Error::invalid_type(Unexpected::Bytes(v), &self))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1591,6 +1624,7 @@ pub trait Visitor<'de>: Sized {
|
|||||||
/// The default implementation forwards to `visit_bytes` and then drops the
|
/// The default implementation forwards to `visit_bytes` and then drops the
|
||||||
/// `Vec<u8>`.
|
/// `Vec<u8>`.
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
|
||||||
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
|
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
|
||||||
where
|
where
|
||||||
E: Error,
|
E: Error,
|
||||||
@@ -1707,6 +1741,12 @@ pub trait Visitor<'de>: Sized {
|
|||||||
/// implementation of `SeqAccess` for a basic JSON data format.
|
/// implementation of `SeqAccess` for a basic JSON data format.
|
||||||
///
|
///
|
||||||
/// [example data format]: https://serde.rs/data-format.html
|
/// [example data format]: https://serde.rs/data-format.html
|
||||||
|
#[cfg_attr(
|
||||||
|
not(no_diagnostic_namespace),
|
||||||
|
diagnostic::on_unimplemented(
|
||||||
|
message = "the trait bound `{Self}: serde::de::SeqAccess<'de>` is not satisfied",
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub trait SeqAccess<'de> {
|
pub trait SeqAccess<'de> {
|
||||||
/// The error type that can be returned if some error occurs during
|
/// The error type that can be returned if some error occurs during
|
||||||
/// deserialization.
|
/// deserialization.
|
||||||
@@ -1741,9 +1781,9 @@ pub trait SeqAccess<'de> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'de, 'a, A: ?Sized> SeqAccess<'de> for &'a mut A
|
impl<'de, A> SeqAccess<'de> for &mut A
|
||||||
where
|
where
|
||||||
A: SeqAccess<'de>,
|
A: ?Sized + SeqAccess<'de>,
|
||||||
{
|
{
|
||||||
type Error = A::Error;
|
type Error = A::Error;
|
||||||
|
|
||||||
@@ -1789,6 +1829,12 @@ where
|
|||||||
/// implementation of `MapAccess` for a basic JSON data format.
|
/// implementation of `MapAccess` for a basic JSON data format.
|
||||||
///
|
///
|
||||||
/// [example data format]: https://serde.rs/data-format.html
|
/// [example data format]: https://serde.rs/data-format.html
|
||||||
|
#[cfg_attr(
|
||||||
|
not(no_diagnostic_namespace),
|
||||||
|
diagnostic::on_unimplemented(
|
||||||
|
message = "the trait bound `{Self}: serde::de::MapAccess<'de>` is not satisfied",
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub trait MapAccess<'de> {
|
pub trait MapAccess<'de> {
|
||||||
/// The error type that can be returned if some error occurs during
|
/// The error type that can be returned if some error occurs during
|
||||||
/// deserialization.
|
/// deserialization.
|
||||||
@@ -1834,9 +1880,9 @@ pub trait MapAccess<'de> {
|
|||||||
K: DeserializeSeed<'de>,
|
K: DeserializeSeed<'de>,
|
||||||
V: DeserializeSeed<'de>,
|
V: DeserializeSeed<'de>,
|
||||||
{
|
{
|
||||||
match try!(self.next_key_seed(kseed)) {
|
match tri!(self.next_key_seed(kseed)) {
|
||||||
Some(key) => {
|
Some(key) => {
|
||||||
let value = try!(self.next_value_seed(vseed));
|
let value = tri!(self.next_value_seed(vseed));
|
||||||
Ok(Some((key, value)))
|
Ok(Some((key, value)))
|
||||||
}
|
}
|
||||||
None => Ok(None),
|
None => Ok(None),
|
||||||
@@ -1894,9 +1940,9 @@ pub trait MapAccess<'de> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'de, 'a, A: ?Sized> MapAccess<'de> for &'a mut A
|
impl<'de, A> MapAccess<'de> for &mut A
|
||||||
where
|
where
|
||||||
A: MapAccess<'de>,
|
A: ?Sized + MapAccess<'de>,
|
||||||
{
|
{
|
||||||
type Error = A::Error;
|
type Error = A::Error;
|
||||||
|
|
||||||
@@ -1981,6 +2027,12 @@ where
|
|||||||
/// implementation of `EnumAccess` for a basic JSON data format.
|
/// implementation of `EnumAccess` for a basic JSON data format.
|
||||||
///
|
///
|
||||||
/// [example data format]: https://serde.rs/data-format.html
|
/// [example data format]: https://serde.rs/data-format.html
|
||||||
|
#[cfg_attr(
|
||||||
|
not(no_diagnostic_namespace),
|
||||||
|
diagnostic::on_unimplemented(
|
||||||
|
message = "the trait bound `{Self}: serde::de::EnumAccess<'de>` is not satisfied",
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub trait EnumAccess<'de>: Sized {
|
pub trait EnumAccess<'de>: Sized {
|
||||||
/// The error type that can be returned if some error occurs during
|
/// The error type that can be returned if some error occurs during
|
||||||
/// deserialization.
|
/// deserialization.
|
||||||
@@ -2028,6 +2080,12 @@ pub trait EnumAccess<'de>: Sized {
|
|||||||
/// implementation of `VariantAccess` for a basic JSON data format.
|
/// implementation of `VariantAccess` for a basic JSON data format.
|
||||||
///
|
///
|
||||||
/// [example data format]: https://serde.rs/data-format.html
|
/// [example data format]: https://serde.rs/data-format.html
|
||||||
|
#[cfg_attr(
|
||||||
|
not(no_diagnostic_namespace),
|
||||||
|
diagnostic::on_unimplemented(
|
||||||
|
message = "the trait bound `{Self}: serde::de::VariantAccess<'de>` is not satisfied",
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub trait VariantAccess<'de>: Sized {
|
pub trait VariantAccess<'de>: Sized {
|
||||||
/// The error type that can be returned if some error occurs during
|
/// The error type that can be returned if some error occurs during
|
||||||
/// deserialization. Must match the error type of our `EnumAccess`.
|
/// deserialization. Must match the error type of our `EnumAccess`.
|
||||||
@@ -2284,15 +2342,52 @@ impl Display for OneOf {
|
|||||||
1 => write!(formatter, "`{}`", self.names[0]),
|
1 => write!(formatter, "`{}`", self.names[0]),
|
||||||
2 => write!(formatter, "`{}` or `{}`", self.names[0], self.names[1]),
|
2 => write!(formatter, "`{}` or `{}`", self.names[0], self.names[1]),
|
||||||
_ => {
|
_ => {
|
||||||
try!(write!(formatter, "one of "));
|
tri!(formatter.write_str("one of "));
|
||||||
for (i, alt) in self.names.iter().enumerate() {
|
for (i, alt) in self.names.iter().enumerate() {
|
||||||
if i > 0 {
|
if i > 0 {
|
||||||
try!(write!(formatter, ", "));
|
tri!(formatter.write_str(", "));
|
||||||
}
|
}
|
||||||
try!(write!(formatter, "`{}`", alt));
|
tri!(write!(formatter, "`{}`", alt));
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct WithDecimalPoint(f64);
|
||||||
|
|
||||||
|
impl Display for WithDecimalPoint {
|
||||||
|
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
struct LookForDecimalPoint<'f, 'a> {
|
||||||
|
formatter: &'f mut fmt::Formatter<'a>,
|
||||||
|
has_decimal_point: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'f, 'a> fmt::Write for LookForDecimalPoint<'f, 'a> {
|
||||||
|
fn write_str(&mut self, fragment: &str) -> fmt::Result {
|
||||||
|
self.has_decimal_point |= fragment.contains('.');
|
||||||
|
self.formatter.write_str(fragment)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_char(&mut self, ch: char) -> fmt::Result {
|
||||||
|
self.has_decimal_point |= ch == '.';
|
||||||
|
self.formatter.write_char(ch)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.0.is_finite() {
|
||||||
|
let mut writer = LookForDecimalPoint {
|
||||||
|
formatter,
|
||||||
|
has_decimal_point: false,
|
||||||
|
};
|
||||||
|
tri!(write!(writer, "{}", self.0));
|
||||||
|
if !writer.has_decimal_point {
|
||||||
|
tri!(formatter.write_str(".0"));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
tri!(write!(formatter, "{}", self.0));
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -21,12 +21,12 @@
|
|||||||
//! }
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
use lib::*;
|
use crate::lib::*;
|
||||||
|
|
||||||
use self::private::{First, Second};
|
use self::private::{First, Second};
|
||||||
use __private::size_hint;
|
use crate::de::{self, Deserializer, Expected, IntoDeserializer, SeqAccess, Visitor};
|
||||||
use de::{self, Deserializer, Expected, IntoDeserializer, SeqAccess, Visitor};
|
use crate::private::size_hint;
|
||||||
use ser;
|
use crate::ser;
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
@@ -113,6 +113,7 @@ impl Debug for Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
||||||
impl error::Error for Error {
|
impl error::Error for Error {
|
||||||
fn description(&self) -> &str {
|
fn description(&self) -> &str {
|
||||||
&self.err
|
&self.err
|
||||||
@@ -175,6 +176,17 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'de, E> IntoDeserializer<'de, E> for UnitDeserializer<E>
|
||||||
|
where
|
||||||
|
E: de::Error,
|
||||||
|
{
|
||||||
|
type Deserializer = Self;
|
||||||
|
|
||||||
|
fn into_deserializer(self) -> Self {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<E> Debug for UnitDeserializer<E> {
|
impl<E> Debug for UnitDeserializer<E> {
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||||
formatter.debug_struct("UnitDeserializer").finish()
|
formatter.debug_struct("UnitDeserializer").finish()
|
||||||
@@ -185,12 +197,14 @@ impl<E> Debug for UnitDeserializer<E> {
|
|||||||
|
|
||||||
/// A deserializer that cannot be instantiated.
|
/// A deserializer that cannot be instantiated.
|
||||||
#[cfg(feature = "unstable")]
|
#[cfg(feature = "unstable")]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
|
||||||
pub struct NeverDeserializer<E> {
|
pub struct NeverDeserializer<E> {
|
||||||
never: !,
|
never: !,
|
||||||
marker: PhantomData<E>,
|
marker: PhantomData<E>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "unstable")]
|
#[cfg(feature = "unstable")]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
|
||||||
impl<'de, E> IntoDeserializer<'de, E> for !
|
impl<'de, E> IntoDeserializer<'de, E> for !
|
||||||
where
|
where
|
||||||
E: de::Error,
|
E: de::Error,
|
||||||
@@ -223,6 +237,18 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "unstable")]
|
||||||
|
impl<'de, E> IntoDeserializer<'de, E> for NeverDeserializer<E>
|
||||||
|
where
|
||||||
|
E: de::Error,
|
||||||
|
{
|
||||||
|
type Deserializer = Self;
|
||||||
|
|
||||||
|
fn into_deserializer(self) -> Self {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
macro_rules! primitive_deserializer {
|
macro_rules! primitive_deserializer {
|
||||||
@@ -277,6 +303,17 @@ macro_rules! primitive_deserializer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'de, E> IntoDeserializer<'de, E> for $name<E>
|
||||||
|
where
|
||||||
|
E: de::Error,
|
||||||
|
{
|
||||||
|
type Deserializer = Self;
|
||||||
|
|
||||||
|
fn into_deserializer(self) -> Self {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<E> Debug for $name<E> {
|
impl<E> Debug for $name<E> {
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||||
formatter
|
formatter
|
||||||
@@ -293,20 +330,17 @@ primitive_deserializer!(i8, "an `i8`.", I8Deserializer, visit_i8);
|
|||||||
primitive_deserializer!(i16, "an `i16`.", I16Deserializer, visit_i16);
|
primitive_deserializer!(i16, "an `i16`.", I16Deserializer, visit_i16);
|
||||||
primitive_deserializer!(i32, "an `i32`.", I32Deserializer, visit_i32);
|
primitive_deserializer!(i32, "an `i32`.", I32Deserializer, visit_i32);
|
||||||
primitive_deserializer!(i64, "an `i64`.", I64Deserializer, visit_i64);
|
primitive_deserializer!(i64, "an `i64`.", I64Deserializer, visit_i64);
|
||||||
|
primitive_deserializer!(i128, "an `i128`.", I128Deserializer, visit_i128);
|
||||||
primitive_deserializer!(isize, "an `isize`.", IsizeDeserializer, visit_i64 as i64);
|
primitive_deserializer!(isize, "an `isize`.", IsizeDeserializer, visit_i64 as i64);
|
||||||
primitive_deserializer!(u8, "a `u8`.", U8Deserializer, visit_u8);
|
primitive_deserializer!(u8, "a `u8`.", U8Deserializer, visit_u8);
|
||||||
primitive_deserializer!(u16, "a `u16`.", U16Deserializer, visit_u16);
|
primitive_deserializer!(u16, "a `u16`.", U16Deserializer, visit_u16);
|
||||||
primitive_deserializer!(u64, "a `u64`.", U64Deserializer, visit_u64);
|
primitive_deserializer!(u64, "a `u64`.", U64Deserializer, visit_u64);
|
||||||
|
primitive_deserializer!(u128, "a `u128`.", U128Deserializer, visit_u128);
|
||||||
primitive_deserializer!(usize, "a `usize`.", UsizeDeserializer, visit_u64 as u64);
|
primitive_deserializer!(usize, "a `usize`.", UsizeDeserializer, visit_u64 as u64);
|
||||||
primitive_deserializer!(f32, "an `f32`.", F32Deserializer, visit_f32);
|
primitive_deserializer!(f32, "an `f32`.", F32Deserializer, visit_f32);
|
||||||
primitive_deserializer!(f64, "an `f64`.", F64Deserializer, visit_f64);
|
primitive_deserializer!(f64, "an `f64`.", F64Deserializer, visit_f64);
|
||||||
primitive_deserializer!(char, "a `char`.", CharDeserializer, visit_char);
|
primitive_deserializer!(char, "a `char`.", CharDeserializer, visit_char);
|
||||||
|
|
||||||
serde_if_integer128! {
|
|
||||||
primitive_deserializer!(i128, "an `i128`.", I128Deserializer, visit_i128);
|
|
||||||
primitive_deserializer!(u128, "a `u128`.", U128Deserializer, visit_u128);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A deserializer holding a `u32`.
|
/// A deserializer holding a `u32`.
|
||||||
pub struct U32Deserializer<E> {
|
pub struct U32Deserializer<E> {
|
||||||
value: u32,
|
value: u32,
|
||||||
@@ -370,6 +404,17 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'de, E> IntoDeserializer<'de, E> for U32Deserializer<E>
|
||||||
|
where
|
||||||
|
E: de::Error,
|
||||||
|
{
|
||||||
|
type Deserializer = Self;
|
||||||
|
|
||||||
|
fn into_deserializer(self) -> Self {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'de, E> de::EnumAccess<'de> for U32Deserializer<E>
|
impl<'de, E> de::EnumAccess<'de> for U32Deserializer<E>
|
||||||
where
|
where
|
||||||
E: de::Error,
|
E: de::Error,
|
||||||
@@ -459,6 +504,17 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'de, 'a, E> IntoDeserializer<'de, E> for StrDeserializer<'a, E>
|
||||||
|
where
|
||||||
|
E: de::Error,
|
||||||
|
{
|
||||||
|
type Deserializer = Self;
|
||||||
|
|
||||||
|
fn into_deserializer(self) -> Self {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'de, 'a, E> de::EnumAccess<'de> for StrDeserializer<'a, E>
|
impl<'de, 'a, E> de::EnumAccess<'de> for StrDeserializer<'a, E>
|
||||||
where
|
where
|
||||||
E: de::Error,
|
E: de::Error,
|
||||||
@@ -538,6 +594,17 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'de, E> IntoDeserializer<'de, E> for BorrowedStrDeserializer<'de, E>
|
||||||
|
where
|
||||||
|
E: de::Error,
|
||||||
|
{
|
||||||
|
type Deserializer = Self;
|
||||||
|
|
||||||
|
fn into_deserializer(self) -> Self {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'de, E> de::EnumAccess<'de> for BorrowedStrDeserializer<'de, E>
|
impl<'de, E> de::EnumAccess<'de> for BorrowedStrDeserializer<'de, E>
|
||||||
where
|
where
|
||||||
E: de::Error,
|
E: de::Error,
|
||||||
@@ -566,6 +633,7 @@ impl<'de, E> Debug for BorrowedStrDeserializer<'de, E> {
|
|||||||
|
|
||||||
/// A deserializer holding a `String`.
|
/// A deserializer holding a `String`.
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
|
||||||
pub struct StringDeserializer<E> {
|
pub struct StringDeserializer<E> {
|
||||||
value: String,
|
value: String,
|
||||||
marker: PhantomData<E>,
|
marker: PhantomData<E>,
|
||||||
@@ -582,6 +650,7 @@ impl<E> Clone for StringDeserializer<E> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
|
||||||
impl<'de, E> IntoDeserializer<'de, E> for String
|
impl<'de, E> IntoDeserializer<'de, E> for String
|
||||||
where
|
where
|
||||||
E: de::Error,
|
E: de::Error,
|
||||||
@@ -639,6 +708,18 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
|
impl<'de, E> IntoDeserializer<'de, E> for StringDeserializer<E>
|
||||||
|
where
|
||||||
|
E: de::Error,
|
||||||
|
{
|
||||||
|
type Deserializer = Self;
|
||||||
|
|
||||||
|
fn into_deserializer(self) -> Self {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
impl<'de, E> de::EnumAccess<'de> for StringDeserializer<E>
|
impl<'de, E> de::EnumAccess<'de> for StringDeserializer<E>
|
||||||
where
|
where
|
||||||
@@ -669,6 +750,7 @@ impl<E> Debug for StringDeserializer<E> {
|
|||||||
|
|
||||||
/// A deserializer holding a `Cow<str>`.
|
/// A deserializer holding a `Cow<str>`.
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
|
||||||
pub struct CowStrDeserializer<'a, E> {
|
pub struct CowStrDeserializer<'a, E> {
|
||||||
value: Cow<'a, str>,
|
value: Cow<'a, str>,
|
||||||
marker: PhantomData<E>,
|
marker: PhantomData<E>,
|
||||||
@@ -685,6 +767,7 @@ impl<'a, E> Clone for CowStrDeserializer<'a, E> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
|
||||||
impl<'de, 'a, E> IntoDeserializer<'de, E> for Cow<'a, str>
|
impl<'de, 'a, E> IntoDeserializer<'de, E> for Cow<'a, str>
|
||||||
where
|
where
|
||||||
E: de::Error,
|
E: de::Error,
|
||||||
@@ -745,6 +828,18 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
|
impl<'de, 'a, E> IntoDeserializer<'de, E> for CowStrDeserializer<'a, E>
|
||||||
|
where
|
||||||
|
E: de::Error,
|
||||||
|
{
|
||||||
|
type Deserializer = Self;
|
||||||
|
|
||||||
|
fn into_deserializer(self) -> Self {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
impl<'de, 'a, E> de::EnumAccess<'de> for CowStrDeserializer<'a, E>
|
impl<'de, 'a, E> de::EnumAccess<'de> for CowStrDeserializer<'a, E>
|
||||||
where
|
where
|
||||||
@@ -822,6 +917,17 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'de, 'a, E> IntoDeserializer<'de, E> for BytesDeserializer<'a, E>
|
||||||
|
where
|
||||||
|
E: de::Error,
|
||||||
|
{
|
||||||
|
type Deserializer = Self;
|
||||||
|
|
||||||
|
fn into_deserializer(self) -> Self {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a, E> Debug for BytesDeserializer<'a, E> {
|
impl<'a, E> Debug for BytesDeserializer<'a, E> {
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||||
formatter
|
formatter
|
||||||
@@ -870,6 +976,17 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'de, E> IntoDeserializer<'de, E> for BorrowedBytesDeserializer<'de, E>
|
||||||
|
where
|
||||||
|
E: de::Error,
|
||||||
|
{
|
||||||
|
type Deserializer = Self;
|
||||||
|
|
||||||
|
fn into_deserializer(self) -> Self {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'de, E> Debug for BorrowedBytesDeserializer<'de, E> {
|
impl<'de, E> Debug for BorrowedBytesDeserializer<'de, E> {
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||||
formatter
|
formatter
|
||||||
@@ -937,8 +1054,8 @@ where
|
|||||||
where
|
where
|
||||||
V: de::Visitor<'de>,
|
V: de::Visitor<'de>,
|
||||||
{
|
{
|
||||||
let v = try!(visitor.visit_seq(&mut self));
|
let v = tri!(visitor.visit_seq(&mut self));
|
||||||
try!(self.end());
|
tri!(self.end());
|
||||||
Ok(v)
|
Ok(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -949,6 +1066,19 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'de, I, T, E> IntoDeserializer<'de, E> for SeqDeserializer<I, E>
|
||||||
|
where
|
||||||
|
I: Iterator<Item = T>,
|
||||||
|
T: IntoDeserializer<'de, E>,
|
||||||
|
E: de::Error,
|
||||||
|
{
|
||||||
|
type Deserializer = Self;
|
||||||
|
|
||||||
|
fn into_deserializer(self) -> Self {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'de, I, T, E> de::SeqAccess<'de> for SeqDeserializer<I, E>
|
impl<'de, I, T, E> de::SeqAccess<'de> for SeqDeserializer<I, E>
|
||||||
where
|
where
|
||||||
I: Iterator<Item = T>,
|
I: Iterator<Item = T>,
|
||||||
@@ -980,7 +1110,7 @@ struct ExpectedInSeq(usize);
|
|||||||
impl Expected for ExpectedInSeq {
|
impl Expected for ExpectedInSeq {
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||||
if self.0 == 1 {
|
if self.0 == 1 {
|
||||||
write!(formatter, "1 element in sequence")
|
formatter.write_str("1 element in sequence")
|
||||||
} else {
|
} else {
|
||||||
write!(formatter, "{} elements in sequence", self.0)
|
write!(formatter, "{} elements in sequence", self.0)
|
||||||
}
|
}
|
||||||
@@ -1003,6 +1133,7 @@ where
|
|||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
|
||||||
impl<'de, T, E> IntoDeserializer<'de, E> for Vec<T>
|
impl<'de, T, E> IntoDeserializer<'de, E> for Vec<T>
|
||||||
where
|
where
|
||||||
T: IntoDeserializer<'de, E>,
|
T: IntoDeserializer<'de, E>,
|
||||||
@@ -1016,6 +1147,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
|
||||||
impl<'de, T, E> IntoDeserializer<'de, E> for BTreeSet<T>
|
impl<'de, T, E> IntoDeserializer<'de, E> for BTreeSet<T>
|
||||||
where
|
where
|
||||||
T: IntoDeserializer<'de, E> + Eq + Ord,
|
T: IntoDeserializer<'de, E> + Eq + Ord,
|
||||||
@@ -1029,6 +1161,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
||||||
impl<'de, T, S, E> IntoDeserializer<'de, E> for HashSet<T, S>
|
impl<'de, T, S, E> IntoDeserializer<'de, E> for HashSet<T, S>
|
||||||
where
|
where
|
||||||
T: IntoDeserializer<'de, E> + Eq + Hash,
|
T: IntoDeserializer<'de, E> + Eq + Hash,
|
||||||
@@ -1077,6 +1210,17 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'de, A> IntoDeserializer<'de, A::Error> for SeqAccessDeserializer<A>
|
||||||
|
where
|
||||||
|
A: de::SeqAccess<'de>,
|
||||||
|
{
|
||||||
|
type Deserializer = Self;
|
||||||
|
|
||||||
|
fn into_deserializer(self) -> Self {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
/// A deserializer that iterates over a map.
|
/// A deserializer that iterates over a map.
|
||||||
@@ -1162,8 +1306,8 @@ where
|
|||||||
where
|
where
|
||||||
V: de::Visitor<'de>,
|
V: de::Visitor<'de>,
|
||||||
{
|
{
|
||||||
let value = try!(visitor.visit_map(&mut self));
|
let value = tri!(visitor.visit_map(&mut self));
|
||||||
try!(self.end());
|
tri!(self.end());
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1171,8 +1315,8 @@ where
|
|||||||
where
|
where
|
||||||
V: de::Visitor<'de>,
|
V: de::Visitor<'de>,
|
||||||
{
|
{
|
||||||
let value = try!(visitor.visit_seq(&mut self));
|
let value = tri!(visitor.visit_seq(&mut self));
|
||||||
try!(self.end());
|
tri!(self.end());
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1191,6 +1335,21 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'de, I, E> IntoDeserializer<'de, E> for MapDeserializer<'de, I, E>
|
||||||
|
where
|
||||||
|
I: Iterator,
|
||||||
|
I::Item: private::Pair,
|
||||||
|
First<I::Item>: IntoDeserializer<'de, E>,
|
||||||
|
Second<I::Item>: IntoDeserializer<'de, E>,
|
||||||
|
E: de::Error,
|
||||||
|
{
|
||||||
|
type Deserializer = Self;
|
||||||
|
|
||||||
|
fn into_deserializer(self) -> Self {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'de, I, E> de::MapAccess<'de> for MapDeserializer<'de, I, E>
|
impl<'de, I, E> de::MapAccess<'de> for MapDeserializer<'de, I, E>
|
||||||
where
|
where
|
||||||
I: Iterator,
|
I: Iterator,
|
||||||
@@ -1236,8 +1395,8 @@ where
|
|||||||
{
|
{
|
||||||
match self.next_pair() {
|
match self.next_pair() {
|
||||||
Some((key, value)) => {
|
Some((key, value)) => {
|
||||||
let key = try!(kseed.deserialize(key.into_deserializer()));
|
let key = tri!(kseed.deserialize(key.into_deserializer()));
|
||||||
let value = try!(vseed.deserialize(value.into_deserializer()));
|
let value = tri!(vseed.deserialize(value.into_deserializer()));
|
||||||
Ok(Some((key, value)))
|
Ok(Some((key, value)))
|
||||||
}
|
}
|
||||||
None => Ok(None),
|
None => Ok(None),
|
||||||
@@ -1341,7 +1500,7 @@ where
|
|||||||
V: de::Visitor<'de>,
|
V: de::Visitor<'de>,
|
||||||
{
|
{
|
||||||
let mut pair_visitor = PairVisitor(Some(self.0), Some(self.1), PhantomData);
|
let mut pair_visitor = PairVisitor(Some(self.0), Some(self.1), PhantomData);
|
||||||
let pair = try!(visitor.visit_seq(&mut pair_visitor));
|
let pair = tri!(visitor.visit_seq(&mut pair_visitor));
|
||||||
if pair_visitor.1.is_none() {
|
if pair_visitor.1.is_none() {
|
||||||
Ok(pair)
|
Ok(pair)
|
||||||
} else {
|
} else {
|
||||||
@@ -1405,7 +1564,7 @@ struct ExpectedInMap(usize);
|
|||||||
impl Expected for ExpectedInMap {
|
impl Expected for ExpectedInMap {
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||||
if self.0 == 1 {
|
if self.0 == 1 {
|
||||||
write!(formatter, "1 element in map")
|
formatter.write_str("1 element in map")
|
||||||
} else {
|
} else {
|
||||||
write!(formatter, "{} elements in map", self.0)
|
write!(formatter, "{} elements in map", self.0)
|
||||||
}
|
}
|
||||||
@@ -1415,6 +1574,7 @@ impl Expected for ExpectedInMap {
|
|||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
|
||||||
impl<'de, K, V, E> IntoDeserializer<'de, E> for BTreeMap<K, V>
|
impl<'de, K, V, E> IntoDeserializer<'de, E> for BTreeMap<K, V>
|
||||||
where
|
where
|
||||||
K: IntoDeserializer<'de, E> + Eq + Ord,
|
K: IntoDeserializer<'de, E> + Eq + Ord,
|
||||||
@@ -1429,6 +1589,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
||||||
impl<'de, K, V, S, E> IntoDeserializer<'de, E> for HashMap<K, V, S>
|
impl<'de, K, V, S, E> IntoDeserializer<'de, E> for HashMap<K, V, S>
|
||||||
where
|
where
|
||||||
K: IntoDeserializer<'de, E> + Eq + Hash,
|
K: IntoDeserializer<'de, E> + Eq + Hash,
|
||||||
@@ -1490,6 +1651,17 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'de, A> IntoDeserializer<'de, A::Error> for MapAccessDeserializer<A>
|
||||||
|
where
|
||||||
|
A: de::MapAccess<'de>,
|
||||||
|
{
|
||||||
|
type Deserializer = Self;
|
||||||
|
|
||||||
|
fn into_deserializer(self) -> Self {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'de, A> de::EnumAccess<'de> for MapAccessDeserializer<A>
|
impl<'de, A> de::EnumAccess<'de> for MapAccessDeserializer<A>
|
||||||
where
|
where
|
||||||
A: de::MapAccess<'de>,
|
A: de::MapAccess<'de>,
|
||||||
@@ -1501,7 +1673,7 @@ where
|
|||||||
where
|
where
|
||||||
T: de::DeserializeSeed<'de>,
|
T: de::DeserializeSeed<'de>,
|
||||||
{
|
{
|
||||||
match try!(self.map.next_key_seed(seed)) {
|
match tri!(self.map.next_key_seed(seed)) {
|
||||||
Some(key) => Ok((key, private::map_as_enum(self.map))),
|
Some(key) => Ok((key, private::map_as_enum(self.map))),
|
||||||
None => Err(de::Error::invalid_type(de::Unexpected::Map, &"enum")),
|
None => Err(de::Error::invalid_type(de::Unexpected::Map, &"enum")),
|
||||||
}
|
}
|
||||||
@@ -1543,12 +1715,25 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'de, A> IntoDeserializer<'de, A::Error> for EnumAccessDeserializer<A>
|
||||||
|
where
|
||||||
|
A: de::EnumAccess<'de>,
|
||||||
|
{
|
||||||
|
type Deserializer = Self;
|
||||||
|
|
||||||
|
fn into_deserializer(self) -> Self {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
mod private {
|
mod private {
|
||||||
use lib::*;
|
use crate::lib::*;
|
||||||
|
|
||||||
use de::{self, DeserializeSeed, Deserializer, MapAccess, Unexpected, VariantAccess, Visitor};
|
use crate::de::{
|
||||||
|
self, DeserializeSeed, Deserializer, MapAccess, Unexpected, VariantAccess, Visitor,
|
||||||
|
};
|
||||||
|
|
||||||
pub struct UnitOnly<E> {
|
pub struct UnitOnly<E> {
|
||||||
marker: PhantomData<E>,
|
marker: PhantomData<E>,
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
use lib::fmt::{self, Write};
|
use crate::lib::fmt::{self, Write};
|
||||||
use lib::str;
|
use crate::lib::str;
|
||||||
|
|
||||||
pub(super) struct Buf<'a> {
|
pub(super) struct Buf<'a> {
|
||||||
bytes: &'a mut [u8],
|
bytes: &'a mut [u8],
|
||||||
@@ -0,0 +1,270 @@
|
|||||||
|
//! Serde is a framework for ***ser***ializing and ***de***serializing Rust data
|
||||||
|
//! structures efficiently and generically.
|
||||||
|
//!
|
||||||
|
//! The `serde_core` crate contains Serde's trait definitions with **no support
|
||||||
|
//! for #\[derive()\]**.
|
||||||
|
//!
|
||||||
|
//! In crates that derive an implementation of `Serialize` or `Deserialize`, you
|
||||||
|
//! must depend on the [`serde`] crate, not `serde_core`.
|
||||||
|
//!
|
||||||
|
//! [`serde`]: https://crates.io/crates/serde
|
||||||
|
//!
|
||||||
|
//! In crates that handwrite implementations of Serde traits, or only use them
|
||||||
|
//! as trait bounds, depending on `serde_core` is permitted. But `serde`
|
||||||
|
//! re-exports all of these traits and can be used for this use case too. If in
|
||||||
|
//! doubt, disregard `serde_core` and always use `serde`.
|
||||||
|
//!
|
||||||
|
//! Crates that depend on `serde_core` instead of `serde` are able to compile in
|
||||||
|
//! parallel with `serde_derive` even when `serde`'s "derive" feature is turned on,
|
||||||
|
//! as shown in the following build timings.
|
||||||
|
//!
|
||||||
|
//! <br>
|
||||||
|
//!
|
||||||
|
//! <table>
|
||||||
|
//! <tr><td align="center">When <code>serde_json</code> depends on <code>serde</code></td></tr>
|
||||||
|
//! <tr><td><img src="https://github.com/user-attachments/assets/78dc179c-6ab1-4059-928c-1474b0d9d0bb"></td></tr>
|
||||||
|
//! </table>
|
||||||
|
//!
|
||||||
|
//! <br>
|
||||||
|
//!
|
||||||
|
//! <table>
|
||||||
|
//! <tr><td align="center">When <code>serde_json</code> depends on <code>serde_core</code></td></tr>
|
||||||
|
//! <tr><td><img src="https://github.com/user-attachments/assets/6b6cff5e-3e45-4ac7-9db1-d99ee8b9f5f7"></td></tr>
|
||||||
|
//! </table>
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
// Serde types in rustdoc of other crates get linked to here.
|
||||||
|
#![doc(html_root_url = "https://docs.rs/serde/1.0.221")]
|
||||||
|
// Support using Serde without the standard library!
|
||||||
|
#![cfg_attr(not(feature = "std"), no_std)]
|
||||||
|
// Show which crate feature enables conditionally compiled APIs in documentation.
|
||||||
|
#![cfg_attr(docsrs, feature(doc_cfg, rustdoc_internals))]
|
||||||
|
#![cfg_attr(docsrs, allow(internal_features))]
|
||||||
|
// 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))]
|
||||||
|
#![allow(unknown_lints, bare_trait_objects, deprecated)]
|
||||||
|
// Ignored clippy and clippy_pedantic lints
|
||||||
|
#![allow(
|
||||||
|
// clippy bug: https://github.com/rust-lang/rust-clippy/issues/5704
|
||||||
|
clippy::unnested_or_patterns,
|
||||||
|
// clippy bug: https://github.com/rust-lang/rust-clippy/issues/7768
|
||||||
|
clippy::semicolon_if_nothing_returned,
|
||||||
|
// not available in our oldest supported compiler
|
||||||
|
clippy::empty_enum,
|
||||||
|
clippy::type_repetition_in_bounds, // https://github.com/rust-lang/rust-clippy/issues/8772
|
||||||
|
// integer and float ser/de requires these sorts of casts
|
||||||
|
clippy::cast_possible_truncation,
|
||||||
|
clippy::cast_possible_wrap,
|
||||||
|
clippy::cast_precision_loss,
|
||||||
|
clippy::cast_sign_loss,
|
||||||
|
// things are often more readable this way
|
||||||
|
clippy::cast_lossless,
|
||||||
|
clippy::module_name_repetitions,
|
||||||
|
clippy::single_match_else,
|
||||||
|
clippy::type_complexity,
|
||||||
|
clippy::use_self,
|
||||||
|
clippy::zero_prefixed_literal,
|
||||||
|
// correctly used
|
||||||
|
clippy::derive_partial_eq_without_eq,
|
||||||
|
clippy::enum_glob_use,
|
||||||
|
clippy::explicit_auto_deref,
|
||||||
|
clippy::incompatible_msrv,
|
||||||
|
clippy::let_underscore_untyped,
|
||||||
|
clippy::map_err_ignore,
|
||||||
|
clippy::new_without_default,
|
||||||
|
clippy::result_unit_err,
|
||||||
|
clippy::wildcard_imports,
|
||||||
|
// not practical
|
||||||
|
clippy::needless_pass_by_value,
|
||||||
|
clippy::similar_names,
|
||||||
|
clippy::too_many_lines,
|
||||||
|
// preference
|
||||||
|
clippy::doc_markdown,
|
||||||
|
clippy::elidable_lifetime_names,
|
||||||
|
clippy::needless_lifetimes,
|
||||||
|
clippy::unseparated_literal_suffix,
|
||||||
|
// false positive
|
||||||
|
clippy::needless_doctest_main,
|
||||||
|
// noisy
|
||||||
|
clippy::missing_errors_doc,
|
||||||
|
clippy::must_use_candidate,
|
||||||
|
)]
|
||||||
|
// Restrictions
|
||||||
|
#![deny(clippy::question_mark_used)]
|
||||||
|
// Rustc lints.
|
||||||
|
#![deny(missing_docs, unused_imports)]
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#[cfg(feature = "alloc")]
|
||||||
|
extern crate alloc;
|
||||||
|
|
||||||
|
/// A facade around all the types we need from the `std`, `core`, and `alloc`
|
||||||
|
/// crates. This avoids elaborate import wrangling having to happen in every
|
||||||
|
/// module.
|
||||||
|
mod lib {
|
||||||
|
mod core {
|
||||||
|
#[cfg(not(feature = "std"))]
|
||||||
|
pub use core::*;
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::*;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub use self::core::{f32, f64};
|
||||||
|
pub use self::core::{iter, num, str};
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
|
pub use self::core::{cmp, mem};
|
||||||
|
|
||||||
|
pub use self::core::cell::{Cell, RefCell};
|
||||||
|
pub use self::core::cmp::Reverse;
|
||||||
|
pub use self::core::fmt::{self, Debug, Display, Write as FmtWrite};
|
||||||
|
pub use self::core::marker::PhantomData;
|
||||||
|
pub use self::core::num::Wrapping;
|
||||||
|
pub use self::core::ops::{Bound, Range, RangeFrom, RangeInclusive, RangeTo};
|
||||||
|
pub use self::core::result;
|
||||||
|
pub use self::core::time::Duration;
|
||||||
|
|
||||||
|
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
||||||
|
pub use alloc::borrow::{Cow, ToOwned};
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::borrow::{Cow, ToOwned};
|
||||||
|
|
||||||
|
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
||||||
|
pub use alloc::string::{String, ToString};
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::string::{String, ToString};
|
||||||
|
|
||||||
|
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
||||||
|
pub use alloc::vec::Vec;
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::vec::Vec;
|
||||||
|
|
||||||
|
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
||||||
|
pub use alloc::boxed::Box;
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::boxed::Box;
|
||||||
|
|
||||||
|
#[cfg(all(feature = "rc", feature = "alloc", not(feature = "std")))]
|
||||||
|
pub use alloc::rc::{Rc, Weak as RcWeak};
|
||||||
|
#[cfg(all(feature = "rc", feature = "std"))]
|
||||||
|
pub use std::rc::{Rc, Weak as RcWeak};
|
||||||
|
|
||||||
|
#[cfg(all(feature = "rc", feature = "alloc", not(feature = "std")))]
|
||||||
|
pub use alloc::sync::{Arc, Weak as ArcWeak};
|
||||||
|
#[cfg(all(feature = "rc", feature = "std"))]
|
||||||
|
pub use std::sync::{Arc, Weak as ArcWeak};
|
||||||
|
|
||||||
|
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
||||||
|
pub use alloc::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque};
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque};
|
||||||
|
|
||||||
|
#[cfg(all(not(no_core_cstr), not(feature = "std")))]
|
||||||
|
pub use self::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(all(not(no_core_net), not(feature = "std")))]
|
||||||
|
pub use self::core::net;
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::net;
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::error;
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::collections::{HashMap, HashSet};
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::ffi::{OsStr, OsString};
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::hash::{BuildHasher, Hash};
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::io::Write;
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::path::{Path, PathBuf};
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::sync::{Mutex, RwLock};
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
|
#[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", 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(not(no_core_num_saturating))]
|
||||||
|
pub use self::core::num::Saturating;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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! tri {
|
||||||
|
($expr:expr) => {
|
||||||
|
match $expr {
|
||||||
|
Ok(val) => val,
|
||||||
|
Err(err) => return Err(err),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
mod macros;
|
||||||
|
|
||||||
|
pub mod de;
|
||||||
|
pub mod ser;
|
||||||
|
|
||||||
|
mod format;
|
||||||
|
|
||||||
|
#[doc(inline)]
|
||||||
|
pub use crate::de::{Deserialize, Deserializer};
|
||||||
|
#[doc(inline)]
|
||||||
|
pub use crate::ser::{Serialize, Serializer};
|
||||||
|
|
||||||
|
// Used by generated code. Not public API.
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[path = "private/mod.rs"]
|
||||||
|
pub mod __private;
|
||||||
|
use self::__private as private;
|
||||||
|
|
||||||
|
#[cfg(all(not(feature = "std"), no_core_error))]
|
||||||
|
mod std_error;
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
#[doc(hidden)]
|
||||||
|
macro_rules! __require_serde_not_serde_core {
|
||||||
|
() => {
|
||||||
|
::core::compile_error!(
|
||||||
|
"Serde derive requires a dependency on the serde crate, not serde_core"
|
||||||
|
);
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
// Super explicit first paragraph because this shows up at the top level and
|
// Super explicit first paragraph because this shows up at the top level and
|
||||||
// trips up people who are just looking for basic Serialize / Deserialize
|
// trips up people who are just looking for basic Serialize / Deserialize
|
||||||
// documentation.
|
// documentation.
|
||||||
//
|
|
||||||
/// Helper macro when implementing the `Deserializer` part of a new data format
|
/// Helper macro when implementing the `Deserializer` part of a new data format
|
||||||
/// for Serde.
|
/// for Serde.
|
||||||
///
|
///
|
||||||
@@ -104,9 +103,9 @@
|
|||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// [`Deserializer`]: trait.Deserializer.html
|
/// [`Deserializer`]: crate::Deserializer
|
||||||
/// [`Visitor`]: de/trait.Visitor.html
|
/// [`Visitor`]: crate::de::Visitor
|
||||||
/// [`Deserializer::deserialize_any`]: trait.Deserializer.html#tymethod.deserialize_any
|
/// [`Deserializer::deserialize_any`]: crate::Deserializer::deserialize_any
|
||||||
#[macro_export(local_inner_macros)]
|
#[macro_export(local_inner_macros)]
|
||||||
macro_rules! forward_to_deserialize_any {
|
macro_rules! forward_to_deserialize_any {
|
||||||
(<$visitor:ident: Visitor<$lifetime:tt>> $($func:ident)*) => {
|
(<$visitor:ident: Visitor<$lifetime:tt>> $($func:ident)*) => {
|
||||||
@@ -123,7 +122,7 @@ macro_rules! forward_to_deserialize_any {
|
|||||||
macro_rules! forward_to_deserialize_any_method {
|
macro_rules! forward_to_deserialize_any_method {
|
||||||
($func:ident<$l:tt, $v:ident>($($arg:ident : $ty:ty),*)) => {
|
($func:ident<$l:tt, $v:ident>($($arg:ident : $ty:ty),*)) => {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn $func<$v>(self, $($arg: $ty,)* visitor: $v) -> $crate::__private::Result<$v::Value, Self::Error>
|
fn $func<$v>(self, $($arg: $ty,)* visitor: $v) -> $crate::__private::Result<$v::Value, <Self as $crate::de::Deserializer<$l>>::Error>
|
||||||
where
|
where
|
||||||
$v: $crate::de::Visitor<$l>,
|
$v: $crate::de::Visitor<$l>,
|
||||||
{
|
{
|
||||||
@@ -154,9 +153,7 @@ macro_rules! forward_to_deserialize_any_helper {
|
|||||||
forward_to_deserialize_any_method!{deserialize_i64<$l, $v>()}
|
forward_to_deserialize_any_method!{deserialize_i64<$l, $v>()}
|
||||||
};
|
};
|
||||||
(i128<$l:tt, $v:ident>) => {
|
(i128<$l:tt, $v:ident>) => {
|
||||||
serde_if_integer128! {
|
forward_to_deserialize_any_method!{deserialize_i128<$l, $v>()}
|
||||||
forward_to_deserialize_any_method!{deserialize_i128<$l, $v>()}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
(u8<$l:tt, $v:ident>) => {
|
(u8<$l:tt, $v:ident>) => {
|
||||||
forward_to_deserialize_any_method!{deserialize_u8<$l, $v>()}
|
forward_to_deserialize_any_method!{deserialize_u8<$l, $v>()}
|
||||||
@@ -171,9 +168,7 @@ macro_rules! forward_to_deserialize_any_helper {
|
|||||||
forward_to_deserialize_any_method!{deserialize_u64<$l, $v>()}
|
forward_to_deserialize_any_method!{deserialize_u64<$l, $v>()}
|
||||||
};
|
};
|
||||||
(u128<$l:tt, $v:ident>) => {
|
(u128<$l:tt, $v:ident>) => {
|
||||||
serde_if_integer128! {
|
forward_to_deserialize_any_method!{deserialize_u128<$l, $v>()}
|
||||||
forward_to_deserialize_any_method!{deserialize_u128<$l, $v>()}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
(f32<$l:tt, $v:ident>) => {
|
(f32<$l:tt, $v:ident>) => {
|
||||||
forward_to_deserialize_any_method!{deserialize_f32<$l, $v>()}
|
forward_to_deserialize_any_method!{deserialize_f32<$l, $v>()}
|
||||||
@@ -0,0 +1,264 @@
|
|||||||
|
use crate::de::{self, Deserialize, Deserializer, EnumAccess, MapAccess, SeqAccess, Visitor};
|
||||||
|
use crate::lib::*;
|
||||||
|
use crate::private::size_hint;
|
||||||
|
|
||||||
|
// Used from generated code to buffer the contents of the Deserializer when
|
||||||
|
// deserializing untagged enums and internally tagged enums.
|
||||||
|
//
|
||||||
|
// Not public API. Use serde-value instead.
|
||||||
|
//
|
||||||
|
// Obsoleted by format-specific buffer types (https://github.com/serde-rs/serde/pull/2912).
|
||||||
|
#[derive(Clone)]
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub enum Content<'de> {
|
||||||
|
Bool(bool),
|
||||||
|
|
||||||
|
U8(u8),
|
||||||
|
U16(u16),
|
||||||
|
U32(u32),
|
||||||
|
U64(u64),
|
||||||
|
|
||||||
|
I8(i8),
|
||||||
|
I16(i16),
|
||||||
|
I32(i32),
|
||||||
|
I64(i64),
|
||||||
|
|
||||||
|
F32(f32),
|
||||||
|
F64(f64),
|
||||||
|
|
||||||
|
Char(char),
|
||||||
|
String(String),
|
||||||
|
Str(&'de str),
|
||||||
|
ByteBuf(Vec<u8>),
|
||||||
|
Bytes(&'de [u8]),
|
||||||
|
|
||||||
|
None,
|
||||||
|
Some(Box<Content<'de>>),
|
||||||
|
|
||||||
|
Unit,
|
||||||
|
Newtype(Box<Content<'de>>),
|
||||||
|
Seq(Vec<Content<'de>>),
|
||||||
|
Map(Vec<(Content<'de>, Content<'de>)>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de> Deserialize<'de> for Content<'de> {
|
||||||
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
// Untagged and internally tagged enums are only supported in
|
||||||
|
// self-describing formats.
|
||||||
|
let visitor = ContentVisitor { value: PhantomData };
|
||||||
|
deserializer.__deserialize_content_v1(visitor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub struct ContentVisitor<'de> {
|
||||||
|
value: PhantomData<Content<'de>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de> ContentVisitor<'de> {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
ContentVisitor { value: PhantomData }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de> Visitor<'de> for ContentVisitor<'de> {
|
||||||
|
type Value = Content<'de>;
|
||||||
|
|
||||||
|
fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
fmt.write_str("any value")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_bool<F>(self, value: bool) -> Result<Self::Value, F>
|
||||||
|
where
|
||||||
|
F: de::Error,
|
||||||
|
{
|
||||||
|
Ok(Content::Bool(value))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_i8<F>(self, value: i8) -> Result<Self::Value, F>
|
||||||
|
where
|
||||||
|
F: de::Error,
|
||||||
|
{
|
||||||
|
Ok(Content::I8(value))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_i16<F>(self, value: i16) -> Result<Self::Value, F>
|
||||||
|
where
|
||||||
|
F: de::Error,
|
||||||
|
{
|
||||||
|
Ok(Content::I16(value))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_i32<F>(self, value: i32) -> Result<Self::Value, F>
|
||||||
|
where
|
||||||
|
F: de::Error,
|
||||||
|
{
|
||||||
|
Ok(Content::I32(value))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_i64<F>(self, value: i64) -> Result<Self::Value, F>
|
||||||
|
where
|
||||||
|
F: de::Error,
|
||||||
|
{
|
||||||
|
Ok(Content::I64(value))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_u8<F>(self, value: u8) -> Result<Self::Value, F>
|
||||||
|
where
|
||||||
|
F: de::Error,
|
||||||
|
{
|
||||||
|
Ok(Content::U8(value))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_u16<F>(self, value: u16) -> Result<Self::Value, F>
|
||||||
|
where
|
||||||
|
F: de::Error,
|
||||||
|
{
|
||||||
|
Ok(Content::U16(value))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_u32<F>(self, value: u32) -> Result<Self::Value, F>
|
||||||
|
where
|
||||||
|
F: de::Error,
|
||||||
|
{
|
||||||
|
Ok(Content::U32(value))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_u64<F>(self, value: u64) -> Result<Self::Value, F>
|
||||||
|
where
|
||||||
|
F: de::Error,
|
||||||
|
{
|
||||||
|
Ok(Content::U64(value))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_f32<F>(self, value: f32) -> Result<Self::Value, F>
|
||||||
|
where
|
||||||
|
F: de::Error,
|
||||||
|
{
|
||||||
|
Ok(Content::F32(value))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_f64<F>(self, value: f64) -> Result<Self::Value, F>
|
||||||
|
where
|
||||||
|
F: de::Error,
|
||||||
|
{
|
||||||
|
Ok(Content::F64(value))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_char<F>(self, value: char) -> Result<Self::Value, F>
|
||||||
|
where
|
||||||
|
F: de::Error,
|
||||||
|
{
|
||||||
|
Ok(Content::Char(value))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_str<F>(self, value: &str) -> Result<Self::Value, F>
|
||||||
|
where
|
||||||
|
F: de::Error,
|
||||||
|
{
|
||||||
|
Ok(Content::String(value.into()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_borrowed_str<F>(self, value: &'de str) -> Result<Self::Value, F>
|
||||||
|
where
|
||||||
|
F: de::Error,
|
||||||
|
{
|
||||||
|
Ok(Content::Str(value))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_string<F>(self, value: String) -> Result<Self::Value, F>
|
||||||
|
where
|
||||||
|
F: de::Error,
|
||||||
|
{
|
||||||
|
Ok(Content::String(value))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_bytes<F>(self, value: &[u8]) -> Result<Self::Value, F>
|
||||||
|
where
|
||||||
|
F: de::Error,
|
||||||
|
{
|
||||||
|
Ok(Content::ByteBuf(value.into()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_borrowed_bytes<F>(self, value: &'de [u8]) -> Result<Self::Value, F>
|
||||||
|
where
|
||||||
|
F: de::Error,
|
||||||
|
{
|
||||||
|
Ok(Content::Bytes(value))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_byte_buf<F>(self, value: Vec<u8>) -> Result<Self::Value, F>
|
||||||
|
where
|
||||||
|
F: de::Error,
|
||||||
|
{
|
||||||
|
Ok(Content::ByteBuf(value))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_unit<F>(self) -> Result<Self::Value, F>
|
||||||
|
where
|
||||||
|
F: de::Error,
|
||||||
|
{
|
||||||
|
Ok(Content::Unit)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_none<F>(self) -> Result<Self::Value, F>
|
||||||
|
where
|
||||||
|
F: de::Error,
|
||||||
|
{
|
||||||
|
Ok(Content::None)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
let v = tri!(Deserialize::deserialize(deserializer));
|
||||||
|
Ok(Content::Some(Box::new(v)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
let v = tri!(Deserialize::deserialize(deserializer));
|
||||||
|
Ok(Content::Newtype(Box::new(v)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_seq<V>(self, mut visitor: V) -> Result<Self::Value, V::Error>
|
||||||
|
where
|
||||||
|
V: SeqAccess<'de>,
|
||||||
|
{
|
||||||
|
let mut vec =
|
||||||
|
Vec::<Content>::with_capacity(size_hint::cautious::<Content>(visitor.size_hint()));
|
||||||
|
while let Some(e) = tri!(visitor.next_element()) {
|
||||||
|
vec.push(e);
|
||||||
|
}
|
||||||
|
Ok(Content::Seq(vec))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_map<V>(self, mut visitor: V) -> Result<Self::Value, V::Error>
|
||||||
|
where
|
||||||
|
V: MapAccess<'de>,
|
||||||
|
{
|
||||||
|
let mut vec = Vec::<(Content, Content)>::with_capacity(size_hint::cautious::<(
|
||||||
|
Content,
|
||||||
|
Content,
|
||||||
|
)>(visitor.size_hint()));
|
||||||
|
while let Some(kv) = tri!(visitor.next_entry()) {
|
||||||
|
vec.push(kv);
|
||||||
|
}
|
||||||
|
Ok(Content::Map(vec))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_enum<V>(self, _visitor: V) -> Result<Self::Value, V::Error>
|
||||||
|
where
|
||||||
|
V: EnumAccess<'de>,
|
||||||
|
{
|
||||||
|
Err(de::Error::custom(
|
||||||
|
"untagged and internally tagged enums do not support enum input",
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
// Used only by Serde doc tests. Not public API.
|
// Used only by Serde doc tests. Not public API.
|
||||||
|
|
||||||
use lib::*;
|
use crate::lib::*;
|
||||||
|
|
||||||
use ser;
|
use crate::ser;
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -56,7 +56,10 @@ macro_rules! __serialize_unimplemented {
|
|||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! __serialize_unimplemented_method {
|
macro_rules! __serialize_unimplemented_method {
|
||||||
($func:ident $(<$t:ident>)* ($($arg:ty),*) -> $ret:ident) => {
|
($func:ident $(<$t:ident>)* ($($arg:ty),*) -> $ret:ident) => {
|
||||||
fn $func $(<$t: ?Sized + $crate::Serialize>)* (self $(, _: $arg)*) -> $crate::__private::Result<Self::$ret, Self::Error> {
|
fn $func $(<$t>)* (self $(, _: $arg)*) -> $crate::__private::Result<Self::$ret, Self::Error>
|
||||||
|
where
|
||||||
|
$($t: ?Sized + $crate::Serialize,)*
|
||||||
|
{
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
#[cfg(all(not(no_serde_derive), any(feature = "std", feature = "alloc")))]
|
||||||
|
mod content;
|
||||||
|
mod seed;
|
||||||
|
|
||||||
|
// FIXME: #[cfg(doctest)] once https://github.com/rust-lang/rust/issues/67295 is fixed.
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub mod doc;
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub mod size_hint;
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub mod string;
|
||||||
|
|
||||||
|
#[cfg(all(not(no_serde_derive), any(feature = "std", feature = "alloc")))]
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub use self::content::{Content, ContentVisitor};
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub use self::seed::InPlaceSeed;
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub use crate::lib::result::Result;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
use de::{Deserialize, DeserializeSeed, Deserializer};
|
use crate::de::{Deserialize, DeserializeSeed, Deserializer};
|
||||||
|
|
||||||
/// A DeserializeSeed helper for implementing deserialize_in_place Visitors.
|
/// A DeserializeSeed helper for implementing deserialize_in_place Visitors.
|
||||||
///
|
///
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
use lib::*;
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
|
use crate::lib::*;
|
||||||
|
|
||||||
pub fn from_bounds<I>(iter: &I) -> Option<usize>
|
pub fn from_bounds<I>(iter: &I) -> Option<usize>
|
||||||
where
|
where
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
use crate::lib::*;
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub fn from_utf8_lossy(bytes: &[u8]) -> Cow<'_, str> {
|
||||||
|
String::from_utf8_lossy(bytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The generated code calls this like:
|
||||||
|
//
|
||||||
|
// let value = &_serde::__private::from_utf8_lossy(bytes);
|
||||||
|
// Err(_serde::de::Error::unknown_variant(value, VARIANTS))
|
||||||
|
//
|
||||||
|
// so it is okay for the return type to be different from the std case as long
|
||||||
|
// as the above works.
|
||||||
|
#[cfg(not(any(feature = "std", feature = "alloc")))]
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub fn from_utf8_lossy(bytes: &[u8]) -> &str {
|
||||||
|
// Three unicode replacement characters if it fails. They look like a
|
||||||
|
// white-on-black question mark. The user will recognize it as invalid
|
||||||
|
// UTF-8.
|
||||||
|
str::from_utf8(bytes).unwrap_or("\u{fffd}\u{fffd}\u{fffd}")
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
use lib::*;
|
use crate::lib::*;
|
||||||
use ser::{Error, Impossible, Serialize, Serializer};
|
use crate::ser::{Error, Impossible, Serialize, Serializer};
|
||||||
|
|
||||||
impl Error for fmt::Error {
|
impl Error for fmt::Error {
|
||||||
fn custom<T: Display>(_msg: T) -> Self {
|
fn custom<T: Display>(_msg: T) -> Self {
|
||||||
@@ -35,7 +35,7 @@ macro_rules! fmt_primitives {
|
|||||||
/// }
|
/// }
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
impl<'a, 'b> Serializer for &'a mut fmt::Formatter<'b> {
|
impl<'a> Serializer for &mut fmt::Formatter<'a> {
|
||||||
type Ok = ();
|
type Ok = ();
|
||||||
type Error = fmt::Error;
|
type Error = fmt::Error;
|
||||||
type SerializeSeq = Impossible<(), fmt::Error>;
|
type SerializeSeq = Impossible<(), fmt::Error>;
|
||||||
@@ -52,10 +52,12 @@ impl<'a, 'b> Serializer for &'a mut fmt::Formatter<'b> {
|
|||||||
serialize_i16: i16,
|
serialize_i16: i16,
|
||||||
serialize_i32: i32,
|
serialize_i32: i32,
|
||||||
serialize_i64: i64,
|
serialize_i64: i64,
|
||||||
|
serialize_i128: i128,
|
||||||
serialize_u8: u8,
|
serialize_u8: u8,
|
||||||
serialize_u16: u16,
|
serialize_u16: u16,
|
||||||
serialize_u32: u32,
|
serialize_u32: u32,
|
||||||
serialize_u64: u64,
|
serialize_u64: u64,
|
||||||
|
serialize_u128: u128,
|
||||||
serialize_f32: f32,
|
serialize_f32: f32,
|
||||||
serialize_f64: f64,
|
serialize_f64: f64,
|
||||||
serialize_char: char,
|
serialize_char: char,
|
||||||
@@ -63,13 +65,6 @@ impl<'a, 'b> Serializer for &'a mut fmt::Formatter<'b> {
|
|||||||
serialize_unit_struct: &'static str,
|
serialize_unit_struct: &'static str,
|
||||||
}
|
}
|
||||||
|
|
||||||
serde_if_integer128! {
|
|
||||||
fmt_primitives! {
|
|
||||||
serialize_i128: i128,
|
|
||||||
serialize_u128: u128,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn serialize_unit_variant(
|
fn serialize_unit_variant(
|
||||||
self,
|
self,
|
||||||
_name: &'static str,
|
_name: &'static str,
|
||||||
@@ -79,9 +74,9 @@ impl<'a, 'b> Serializer for &'a mut fmt::Formatter<'b> {
|
|||||||
Display::fmt(variant, self)
|
Display::fmt(variant, self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_newtype_struct<T: ?Sized>(self, _name: &'static str, value: &T) -> fmt::Result
|
fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> fmt::Result
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
Serialize::serialize(value, self)
|
Serialize::serialize(value, self)
|
||||||
}
|
}
|
||||||
@@ -94,9 +89,9 @@ impl<'a, 'b> Serializer for &'a mut fmt::Formatter<'b> {
|
|||||||
Err(fmt::Error)
|
Err(fmt::Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_some<T: ?Sized>(self, _value: &T) -> fmt::Result
|
fn serialize_some<T>(self, _value: &T) -> fmt::Result
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
Err(fmt::Error)
|
Err(fmt::Error)
|
||||||
}
|
}
|
||||||
@@ -105,7 +100,7 @@ impl<'a, 'b> Serializer for &'a mut fmt::Formatter<'b> {
|
|||||||
Err(fmt::Error)
|
Err(fmt::Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_newtype_variant<T: ?Sized>(
|
fn serialize_newtype_variant<T>(
|
||||||
self,
|
self,
|
||||||
_name: &'static str,
|
_name: &'static str,
|
||||||
_variant_index: u32,
|
_variant_index: u32,
|
||||||
@@ -113,7 +108,7 @@ impl<'a, 'b> Serializer for &'a mut fmt::Formatter<'b> {
|
|||||||
_value: &T,
|
_value: &T,
|
||||||
) -> fmt::Result
|
) -> fmt::Result
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
Err(fmt::Error)
|
Err(fmt::Error)
|
||||||
}
|
}
|
||||||
@@ -166,9 +161,9 @@ impl<'a, 'b> Serializer for &'a mut fmt::Formatter<'b> {
|
|||||||
Err(fmt::Error)
|
Err(fmt::Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn collect_str<T: ?Sized>(self, value: &T) -> fmt::Result
|
fn collect_str<T>(self, value: &T) -> fmt::Result
|
||||||
where
|
where
|
||||||
T: Display,
|
T: ?Sized + Display,
|
||||||
{
|
{
|
||||||
Display::fmt(value, self)
|
Display::fmt(value, self)
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
use lib::*;
|
use crate::lib::*;
|
||||||
|
|
||||||
use ser::{Error, Serialize, SerializeTuple, Serializer};
|
use crate::ser::{Error, Serialize, SerializeTuple, Serializer};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
@@ -24,20 +24,17 @@ primitive_impl!(i8, serialize_i8);
|
|||||||
primitive_impl!(i16, serialize_i16);
|
primitive_impl!(i16, serialize_i16);
|
||||||
primitive_impl!(i32, serialize_i32);
|
primitive_impl!(i32, serialize_i32);
|
||||||
primitive_impl!(i64, serialize_i64);
|
primitive_impl!(i64, serialize_i64);
|
||||||
|
primitive_impl!(i128, serialize_i128);
|
||||||
primitive_impl!(usize, serialize_u64 as u64);
|
primitive_impl!(usize, serialize_u64 as u64);
|
||||||
primitive_impl!(u8, serialize_u8);
|
primitive_impl!(u8, serialize_u8);
|
||||||
primitive_impl!(u16, serialize_u16);
|
primitive_impl!(u16, serialize_u16);
|
||||||
primitive_impl!(u32, serialize_u32);
|
primitive_impl!(u32, serialize_u32);
|
||||||
primitive_impl!(u64, serialize_u64);
|
primitive_impl!(u64, serialize_u64);
|
||||||
|
primitive_impl!(u128, serialize_u128);
|
||||||
primitive_impl!(f32, serialize_f32);
|
primitive_impl!(f32, serialize_f32);
|
||||||
primitive_impl!(f64, serialize_f64);
|
primitive_impl!(f64, serialize_f64);
|
||||||
primitive_impl!(char, serialize_char);
|
primitive_impl!(char, serialize_char);
|
||||||
|
|
||||||
serde_if_integer128! {
|
|
||||||
primitive_impl!(i128, serialize_i128);
|
|
||||||
primitive_impl!(u128, serialize_u128);
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
impl Serialize for str {
|
impl Serialize for str {
|
||||||
@@ -51,6 +48,7 @@ impl Serialize for str {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
|
||||||
impl Serialize for String {
|
impl Serialize for String {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
@@ -73,6 +71,7 @@ impl<'a> Serialize for fmt::Arguments<'a> {
|
|||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#[cfg(any(feature = "std", not(no_core_cstr)))]
|
#[cfg(any(feature = "std", not(no_core_cstr)))]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
||||||
impl Serialize for CStr {
|
impl Serialize for CStr {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
@@ -84,6 +83,7 @@ impl Serialize for CStr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))]
|
#[cfg(any(feature = "std", all(not(no_core_cstr), feature = "alloc")))]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
|
||||||
impl Serialize for CString {
|
impl Serialize for CString {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
@@ -114,7 +114,10 @@ where
|
|||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
impl<T: ?Sized> Serialize for PhantomData<T> {
|
impl<T> Serialize for PhantomData<T>
|
||||||
|
where
|
||||||
|
T: ?Sized,
|
||||||
|
{
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
@@ -133,7 +136,7 @@ impl<T> Serialize for [T; 0] {
|
|||||||
where
|
where
|
||||||
S: Serializer,
|
S: Serializer,
|
||||||
{
|
{
|
||||||
try!(serializer.serialize_tuple(0)).end()
|
tri!(serializer.serialize_tuple(0)).end()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,9 +152,9 @@ macro_rules! array_impls {
|
|||||||
where
|
where
|
||||||
S: Serializer,
|
S: Serializer,
|
||||||
{
|
{
|
||||||
let mut seq = try!(serializer.serialize_tuple($len));
|
let mut seq = tri!(serializer.serialize_tuple($len));
|
||||||
for e in self {
|
for e in self {
|
||||||
try!(seq.serialize_element(e));
|
tri!(seq.serialize_element(e));
|
||||||
}
|
}
|
||||||
seq.end()
|
seq.end()
|
||||||
}
|
}
|
||||||
@@ -182,9 +185,12 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(all(any(feature = "std", feature = "alloc"), not(no_relaxed_trait_bounds)))]
|
|
||||||
macro_rules! seq_impl {
|
macro_rules! seq_impl {
|
||||||
($ty:ident <T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound:ident)*>) => {
|
(
|
||||||
|
$(#[$attr:meta])*
|
||||||
|
$ty:ident <T $(, $typaram:ident : $bound:ident)*>
|
||||||
|
) => {
|
||||||
|
$(#[$attr])*
|
||||||
impl<T $(, $typaram)*> Serialize for $ty<T $(, $typaram)*>
|
impl<T $(, $typaram)*> Serialize for $ty<T $(, $typaram)*>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: Serialize,
|
||||||
@@ -200,42 +206,41 @@ macro_rules! seq_impl {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(all(any(feature = "std", feature = "alloc"), no_relaxed_trait_bounds))]
|
seq_impl! {
|
||||||
macro_rules! seq_impl {
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
($ty:ident <T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound:ident)*>) => {
|
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
|
||||||
impl<T $(, $typaram)*> Serialize for $ty<T $(, $typaram)*>
|
BinaryHeap<T>
|
||||||
where
|
|
||||||
T: Serialize $(+ $tbound1 $(+ $tbound2)*)*,
|
|
||||||
$($typaram: $bound,)*
|
|
||||||
{
|
|
||||||
#[inline]
|
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
||||||
where
|
|
||||||
S: Serializer,
|
|
||||||
{
|
|
||||||
serializer.collect_seq(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
seq_impl! {
|
||||||
seq_impl!(BinaryHeap<T: Ord>);
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
|
||||||
|
BTreeSet<T>
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
seq_impl! {
|
||||||
seq_impl!(BTreeSet<T: Ord>);
|
#[cfg(feature = "std")]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
||||||
|
HashSet<T, H: BuildHasher>
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
seq_impl! {
|
||||||
seq_impl!(HashSet<T: Eq + Hash, H: BuildHasher>);
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
|
||||||
|
LinkedList<T>
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
seq_impl! {
|
||||||
seq_impl!(LinkedList<T>);
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
|
||||||
|
Vec<T>
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
seq_impl! {
|
||||||
seq_impl!(Vec<T>);
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
VecDeque<T>
|
||||||
seq_impl!(VecDeque<T>);
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
@@ -248,9 +253,9 @@ where
|
|||||||
S: Serializer,
|
S: Serializer,
|
||||||
{
|
{
|
||||||
use super::SerializeStruct;
|
use super::SerializeStruct;
|
||||||
let mut state = try!(serializer.serialize_struct("Range", 2));
|
let mut state = tri!(serializer.serialize_struct("Range", 2));
|
||||||
try!(state.serialize_field("start", &self.start));
|
tri!(state.serialize_field("start", &self.start));
|
||||||
try!(state.serialize_field("end", &self.end));
|
tri!(state.serialize_field("end", &self.end));
|
||||||
state.end()
|
state.end()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -266,15 +271,14 @@ where
|
|||||||
S: Serializer,
|
S: Serializer,
|
||||||
{
|
{
|
||||||
use super::SerializeStruct;
|
use super::SerializeStruct;
|
||||||
let mut state = try!(serializer.serialize_struct("RangeFrom", 1));
|
let mut state = tri!(serializer.serialize_struct("RangeFrom", 1));
|
||||||
try!(state.serialize_field("start", &self.start));
|
tri!(state.serialize_field("start", &self.start));
|
||||||
state.end()
|
state.end()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#[cfg(not(no_range_inclusive))]
|
|
||||||
impl<Idx> Serialize for RangeInclusive<Idx>
|
impl<Idx> Serialize for RangeInclusive<Idx>
|
||||||
where
|
where
|
||||||
Idx: Serialize,
|
Idx: Serialize,
|
||||||
@@ -284,9 +288,9 @@ where
|
|||||||
S: Serializer,
|
S: Serializer,
|
||||||
{
|
{
|
||||||
use super::SerializeStruct;
|
use super::SerializeStruct;
|
||||||
let mut state = try!(serializer.serialize_struct("RangeInclusive", 2));
|
let mut state = tri!(serializer.serialize_struct("RangeInclusive", 2));
|
||||||
try!(state.serialize_field("start", &self.start()));
|
tri!(state.serialize_field("start", &self.start()));
|
||||||
try!(state.serialize_field("end", &self.end()));
|
tri!(state.serialize_field("end", &self.end()));
|
||||||
state.end()
|
state.end()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -302,15 +306,14 @@ where
|
|||||||
S: Serializer,
|
S: Serializer,
|
||||||
{
|
{
|
||||||
use super::SerializeStruct;
|
use super::SerializeStruct;
|
||||||
let mut state = try!(serializer.serialize_struct("RangeTo", 1));
|
let mut state = tri!(serializer.serialize_struct("RangeTo", 1));
|
||||||
try!(state.serialize_field("end", &self.end));
|
tri!(state.serialize_field("end", &self.end));
|
||||||
state.end()
|
state.end()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#[cfg(any(not(no_ops_bound), all(feature = "std", not(no_collections_bound))))]
|
|
||||||
impl<T> Serialize for Bound<T>
|
impl<T> Serialize for Bound<T>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: Serialize,
|
||||||
@@ -344,6 +347,7 @@ impl Serialize for () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "unstable")]
|
#[cfg(feature = "unstable")]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
|
||||||
impl Serialize for ! {
|
impl Serialize for ! {
|
||||||
fn serialize<S>(&self, _serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, _serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
@@ -358,28 +362,46 @@ impl Serialize for ! {
|
|||||||
macro_rules! tuple_impls {
|
macro_rules! tuple_impls {
|
||||||
($($len:expr => ($($n:tt $name:ident)+))+) => {
|
($($len:expr => ($($n:tt $name:ident)+))+) => {
|
||||||
$(
|
$(
|
||||||
|
#[cfg_attr(docsrs, doc(hidden))]
|
||||||
impl<$($name),+> Serialize for ($($name,)+)
|
impl<$($name),+> Serialize for ($($name,)+)
|
||||||
where
|
where
|
||||||
$($name: Serialize,)+
|
$($name: Serialize,)+
|
||||||
{
|
{
|
||||||
#[inline]
|
tuple_impl_body!($len => ($($n)+));
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
||||||
where
|
|
||||||
S: Serializer,
|
|
||||||
{
|
|
||||||
let mut tuple = try!(serializer.serialize_tuple($len));
|
|
||||||
$(
|
|
||||||
try!(tuple.serialize_element(&self.$n));
|
|
||||||
)+
|
|
||||||
tuple.end()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
)+
|
)+
|
||||||
}
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! tuple_impl_body {
|
||||||
|
($len:expr => ($($n:tt)+)) => {
|
||||||
|
#[inline]
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: Serializer,
|
||||||
|
{
|
||||||
|
let mut tuple = tri!(serializer.serialize_tuple($len));
|
||||||
|
$(
|
||||||
|
tri!(tuple.serialize_element(&self.$n));
|
||||||
|
)+
|
||||||
|
tuple.end()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg_attr(docsrs, doc(fake_variadic))]
|
||||||
|
#[cfg_attr(
|
||||||
|
docsrs,
|
||||||
|
doc = "This trait is implemented for tuples up to 16 items long."
|
||||||
|
)]
|
||||||
|
impl<T> Serialize for (T,)
|
||||||
|
where
|
||||||
|
T: Serialize,
|
||||||
|
{
|
||||||
|
tuple_impl_body!(1 => (0));
|
||||||
}
|
}
|
||||||
|
|
||||||
tuple_impls! {
|
tuple_impls! {
|
||||||
1 => (0 T0)
|
|
||||||
2 => (0 T0 1 T1)
|
2 => (0 T0 1 T1)
|
||||||
3 => (0 T0 1 T1 2 T2)
|
3 => (0 T0 1 T1 2 T2)
|
||||||
4 => (0 T0 1 T1 2 T2 3 T3)
|
4 => (0 T0 1 T1 2 T2 3 T3)
|
||||||
@@ -399,9 +421,12 @@ tuple_impls! {
|
|||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#[cfg(all(any(feature = "std", feature = "alloc"), not(no_relaxed_trait_bounds)))]
|
|
||||||
macro_rules! map_impl {
|
macro_rules! map_impl {
|
||||||
($ty:ident <K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound:ident)*>) => {
|
(
|
||||||
|
$(#[$attr:meta])*
|
||||||
|
$ty:ident <K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound:ident)*>
|
||||||
|
) => {
|
||||||
|
$(#[$attr])*
|
||||||
impl<K, V $(, $typaram)*> Serialize for $ty<K, V $(, $typaram)*>
|
impl<K, V $(, $typaram)*> Serialize for $ty<K, V $(, $typaram)*>
|
||||||
where
|
where
|
||||||
K: Serialize,
|
K: Serialize,
|
||||||
@@ -418,40 +443,26 @@ macro_rules! map_impl {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(all(any(feature = "std", feature = "alloc"), no_relaxed_trait_bounds))]
|
map_impl! {
|
||||||
macro_rules! map_impl {
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
($ty:ident <K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound:ident)*>) => {
|
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
|
||||||
impl<K, V $(, $typaram)*> Serialize for $ty<K, V $(, $typaram)*>
|
BTreeMap<K: Ord, V>
|
||||||
where
|
|
||||||
K: Serialize $(+ $kbound1 $(+ $kbound2)*)*,
|
|
||||||
V: Serialize,
|
|
||||||
$($typaram: $bound,)*
|
|
||||||
{
|
|
||||||
#[inline]
|
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
||||||
where
|
|
||||||
S: Serializer,
|
|
||||||
{
|
|
||||||
serializer.collect_map(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
map_impl! {
|
||||||
map_impl!(BTreeMap<K: Ord, V>);
|
#[cfg(feature = "std")]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
||||||
#[cfg(feature = "std")]
|
HashMap<K: Eq + Hash, V, H: BuildHasher>
|
||||||
map_impl!(HashMap<K: Eq + Hash, V, H: BuildHasher>);
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
macro_rules! deref_impl {
|
macro_rules! deref_impl {
|
||||||
(
|
(
|
||||||
$(#[doc = $doc:tt])*
|
$(#[$attr:meta])*
|
||||||
<$($desc:tt)+
|
<$($desc:tt)+
|
||||||
) => {
|
) => {
|
||||||
$(#[doc = $doc])*
|
$(#[$attr])*
|
||||||
impl <$($desc)+ {
|
impl <$($desc)+ {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
@@ -464,13 +475,20 @@ macro_rules! deref_impl {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
deref_impl!(<'a, T: ?Sized> Serialize for &'a T where T: Serialize);
|
deref_impl! {
|
||||||
deref_impl!(<'a, T: ?Sized> Serialize for &'a mut T where T: Serialize);
|
<'a, T> Serialize for &'a T where T: ?Sized + Serialize
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
deref_impl! {
|
||||||
deref_impl!(<T: ?Sized> Serialize for Box<T> where T: Serialize);
|
<'a, T> Serialize for &'a mut T where T: ?Sized + Serialize
|
||||||
|
}
|
||||||
|
|
||||||
|
deref_impl! {
|
||||||
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
|
||||||
|
<T> Serialize for Box<T> where T: ?Sized + Serialize
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))]
|
|
||||||
deref_impl! {
|
deref_impl! {
|
||||||
/// This impl requires the [`"rc"`] Cargo feature of Serde.
|
/// This impl requires the [`"rc"`] Cargo feature of Serde.
|
||||||
///
|
///
|
||||||
@@ -480,10 +498,11 @@ deref_impl! {
|
|||||||
/// repeated data.
|
/// repeated data.
|
||||||
///
|
///
|
||||||
/// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc
|
/// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc
|
||||||
<T: ?Sized> Serialize for Rc<T> where T: Serialize
|
#[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))))]
|
||||||
|
<T> Serialize for Rc<T> where T: ?Sized + Serialize
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))]
|
|
||||||
deref_impl! {
|
deref_impl! {
|
||||||
/// This impl requires the [`"rc"`] Cargo feature of Serde.
|
/// This impl requires the [`"rc"`] Cargo feature of Serde.
|
||||||
///
|
///
|
||||||
@@ -493,11 +512,16 @@ deref_impl! {
|
|||||||
/// repeated data.
|
/// repeated data.
|
||||||
///
|
///
|
||||||
/// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc
|
/// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc
|
||||||
<T: ?Sized> Serialize for Arc<T> where T: Serialize
|
#[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))))]
|
||||||
|
<T> Serialize for Arc<T> where T: ?Sized + Serialize
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
deref_impl! {
|
||||||
deref_impl!(<'a, T: ?Sized> Serialize for Cow<'a, T> where T: Serialize + ToOwned);
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
|
||||||
|
<'a, T> Serialize for Cow<'a, T> where T: ?Sized + Serialize + ToOwned
|
||||||
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
@@ -505,9 +529,13 @@ deref_impl!(<'a, T: ?Sized> Serialize for Cow<'a, T> where T: Serialize + ToOwne
|
|||||||
///
|
///
|
||||||
/// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc
|
/// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc
|
||||||
#[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))]
|
#[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))]
|
||||||
impl<T: ?Sized> Serialize for RcWeak<T>
|
#[cfg_attr(
|
||||||
|
docsrs,
|
||||||
|
doc(cfg(all(feature = "rc", any(feature = "std", feature = "alloc"))))
|
||||||
|
)]
|
||||||
|
impl<T> Serialize for RcWeak<T>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
@@ -521,9 +549,13 @@ where
|
|||||||
///
|
///
|
||||||
/// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc
|
/// [`"rc"`]: https://serde.rs/feature-flags.html#-features-rc
|
||||||
#[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))]
|
#[cfg(all(feature = "rc", any(feature = "std", feature = "alloc")))]
|
||||||
impl<T: ?Sized> Serialize for ArcWeak<T>
|
#[cfg_attr(
|
||||||
|
docsrs,
|
||||||
|
doc(cfg(all(feature = "rc", any(feature = "std", feature = "alloc"))))
|
||||||
|
)]
|
||||||
|
impl<T> Serialize for ArcWeak<T>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
@@ -538,7 +570,6 @@ where
|
|||||||
macro_rules! nonzero_integers {
|
macro_rules! nonzero_integers {
|
||||||
($($T:ident,)+) => {
|
($($T:ident,)+) => {
|
||||||
$(
|
$(
|
||||||
#[cfg(not(no_num_nonzero))]
|
|
||||||
impl Serialize for num::$T {
|
impl Serialize for num::$T {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
@@ -551,34 +582,19 @@ macro_rules! nonzero_integers {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
nonzero_integers! {
|
|
||||||
NonZeroU8,
|
|
||||||
NonZeroU16,
|
|
||||||
NonZeroU32,
|
|
||||||
NonZeroU64,
|
|
||||||
NonZeroUsize,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(no_num_nonzero_signed))]
|
|
||||||
nonzero_integers! {
|
nonzero_integers! {
|
||||||
NonZeroI8,
|
NonZeroI8,
|
||||||
NonZeroI16,
|
NonZeroI16,
|
||||||
NonZeroI32,
|
NonZeroI32,
|
||||||
NonZeroI64,
|
NonZeroI64,
|
||||||
|
NonZeroI128,
|
||||||
NonZeroIsize,
|
NonZeroIsize,
|
||||||
}
|
NonZeroU8,
|
||||||
|
NonZeroU16,
|
||||||
// Currently 128-bit integers do not work on Emscripten targets so we need an
|
NonZeroU32,
|
||||||
// additional `#[cfg]`
|
NonZeroU64,
|
||||||
serde_if_integer128! {
|
NonZeroU128,
|
||||||
nonzero_integers! {
|
NonZeroUsize,
|
||||||
NonZeroU128,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(no_num_nonzero_signed))]
|
|
||||||
nonzero_integers! {
|
|
||||||
NonZeroI128,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Serialize for Cell<T>
|
impl<T> Serialize for Cell<T>
|
||||||
@@ -593,9 +609,9 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: ?Sized> Serialize for RefCell<T>
|
impl<T> Serialize for RefCell<T>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
@@ -609,9 +625,10 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl<T: ?Sized> Serialize for Mutex<T>
|
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
||||||
|
impl<T> Serialize for Mutex<T>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
@@ -625,9 +642,10 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl<T: ?Sized> Serialize for RwLock<T>
|
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
||||||
|
impl<T> Serialize for RwLock<T>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
@@ -642,6 +660,8 @@ where
|
|||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#[cfg(feature = "result")]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(feature = "result")))]
|
||||||
impl<T, E> Serialize for Result<T, E>
|
impl<T, E> Serialize for Result<T, E>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: Serialize,
|
||||||
@@ -662,16 +682,15 @@ where
|
|||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#[cfg(any(feature = "std", not(no_core_duration)))]
|
|
||||||
impl Serialize for Duration {
|
impl Serialize for Duration {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
S: Serializer,
|
S: Serializer,
|
||||||
{
|
{
|
||||||
use super::SerializeStruct;
|
use super::SerializeStruct;
|
||||||
let mut state = try!(serializer.serialize_struct("Duration", 2));
|
let mut state = tri!(serializer.serialize_struct("Duration", 2));
|
||||||
try!(state.serialize_field("secs", &self.as_secs()));
|
tri!(state.serialize_field("secs", &self.as_secs()));
|
||||||
try!(state.serialize_field("nanos", &self.subsec_nanos()));
|
tri!(state.serialize_field("nanos", &self.subsec_nanos()));
|
||||||
state.end()
|
state.end()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -679,6 +698,7 @@ impl Serialize for Duration {
|
|||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
||||||
impl Serialize for SystemTime {
|
impl Serialize for SystemTime {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
@@ -689,9 +709,9 @@ impl Serialize for SystemTime {
|
|||||||
Ok(duration_since_epoch) => duration_since_epoch,
|
Ok(duration_since_epoch) => duration_since_epoch,
|
||||||
Err(_) => return Err(S::Error::custom("SystemTime must be later than UNIX_EPOCH")),
|
Err(_) => return Err(S::Error::custom("SystemTime must be later than UNIX_EPOCH")),
|
||||||
};
|
};
|
||||||
let mut state = try!(serializer.serialize_struct("SystemTime", 2));
|
let mut state = tri!(serializer.serialize_struct("SystemTime", 2));
|
||||||
try!(state.serialize_field("secs_since_epoch", &duration_since_epoch.as_secs()));
|
tri!(state.serialize_field("secs_since_epoch", &duration_since_epoch.as_secs()));
|
||||||
try!(state.serialize_field("nanos_since_epoch", &duration_since_epoch.subsec_nanos()));
|
tri!(state.serialize_field("nanos_since_epoch", &duration_since_epoch.subsec_nanos()));
|
||||||
state.end()
|
state.end()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -702,27 +722,17 @@ impl Serialize for SystemTime {
|
|||||||
/// statically known to never have more than a constant `MAX_LEN` bytes.
|
/// statically known to never have more than a constant `MAX_LEN` bytes.
|
||||||
///
|
///
|
||||||
/// Panics if the `Display` impl tries to write more than `MAX_LEN` bytes.
|
/// Panics if the `Display` impl tries to write more than `MAX_LEN` bytes.
|
||||||
#[cfg(feature = "std")]
|
#[cfg(any(feature = "std", not(no_core_net)))]
|
||||||
macro_rules! serialize_display_bounded_length {
|
macro_rules! serialize_display_bounded_length {
|
||||||
($value:expr, $max:expr, $serializer:expr) => {{
|
($value:expr, $max:expr, $serializer:expr) => {{
|
||||||
let mut buffer = [0u8; $max];
|
let mut buffer = [0u8; $max];
|
||||||
let remaining_len = {
|
let mut writer = crate::format::Buf::new(&mut buffer);
|
||||||
let mut remaining = &mut buffer[..];
|
write!(&mut writer, "{}", $value).unwrap();
|
||||||
write!(remaining, "{}", $value).unwrap();
|
$serializer.serialize_str(writer.as_str())
|
||||||
remaining.len()
|
|
||||||
};
|
|
||||||
let written_len = buffer.len() - remaining_len;
|
|
||||||
let written = &buffer[..written_len];
|
|
||||||
|
|
||||||
// write! only provides fmt::Formatter to Display implementations, which
|
|
||||||
// has methods write_str and write_char but no method to write arbitrary
|
|
||||||
// bytes. Therefore `written` must be valid UTF-8.
|
|
||||||
let written_str = str::from_utf8(written).expect("must be valid UTF-8");
|
|
||||||
$serializer.serialize_str(written_str)
|
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(any(feature = "std", not(no_core_net)))]
|
||||||
impl Serialize for net::IpAddr {
|
impl Serialize for net::IpAddr {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
@@ -746,7 +756,7 @@ impl Serialize for net::IpAddr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(any(feature = "std", not(no_core_net)))]
|
||||||
const DEC_DIGITS_LUT: &[u8] = b"\
|
const DEC_DIGITS_LUT: &[u8] = b"\
|
||||||
0001020304050607080910111213141516171819\
|
0001020304050607080910111213141516171819\
|
||||||
2021222324252627282930313233343536373839\
|
2021222324252627282930313233343536373839\
|
||||||
@@ -754,7 +764,7 @@ const DEC_DIGITS_LUT: &[u8] = b"\
|
|||||||
6061626364656667686970717273747576777879\
|
6061626364656667686970717273747576777879\
|
||||||
8081828384858687888990919293949596979899";
|
8081828384858687888990919293949596979899";
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(any(feature = "std", not(no_core_net)))]
|
||||||
#[inline]
|
#[inline]
|
||||||
fn format_u8(mut n: u8, out: &mut [u8]) -> usize {
|
fn format_u8(mut n: u8, out: &mut [u8]) -> usize {
|
||||||
if n >= 100 {
|
if n >= 100 {
|
||||||
@@ -775,7 +785,7 @@ fn format_u8(mut n: u8, out: &mut [u8]) -> usize {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(any(feature = "std", not(no_core_net)))]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_format_u8() {
|
fn test_format_u8() {
|
||||||
let mut i = 0u8;
|
let mut i = 0u8;
|
||||||
@@ -792,7 +802,7 @@ fn test_format_u8() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(any(feature = "std", not(no_core_net)))]
|
||||||
impl Serialize for net::Ipv4Addr {
|
impl Serialize for net::Ipv4Addr {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
@@ -816,7 +826,7 @@ impl Serialize for net::Ipv4Addr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(any(feature = "std", not(no_core_net)))]
|
||||||
impl Serialize for net::Ipv6Addr {
|
impl Serialize for net::Ipv6Addr {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
@@ -832,7 +842,7 @@ impl Serialize for net::Ipv6Addr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(any(feature = "std", not(no_core_net)))]
|
||||||
impl Serialize for net::SocketAddr {
|
impl Serialize for net::SocketAddr {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
@@ -856,7 +866,7 @@ impl Serialize for net::SocketAddr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(any(feature = "std", not(no_core_net)))]
|
||||||
impl Serialize for net::SocketAddrV4 {
|
impl Serialize for net::SocketAddrV4 {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
@@ -872,7 +882,7 @@ impl Serialize for net::SocketAddrV4 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(any(feature = "std", not(no_core_net)))]
|
||||||
impl Serialize for net::SocketAddrV6 {
|
impl Serialize for net::SocketAddrV6 {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
@@ -894,6 +904,7 @@ impl Serialize for net::SocketAddrV6 {
|
|||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
||||||
impl Serialize for Path {
|
impl Serialize for Path {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
@@ -907,6 +918,7 @@ impl Serialize for Path {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
||||||
impl Serialize for PathBuf {
|
impl Serialize for PathBuf {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
@@ -917,6 +929,7 @@ impl Serialize for PathBuf {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(all(feature = "std", any(unix, windows)))]
|
#[cfg(all(feature = "std", any(unix, windows)))]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(all(feature = "std", any(unix, windows)))))]
|
||||||
impl Serialize for OsStr {
|
impl Serialize for OsStr {
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
@@ -939,6 +952,7 @@ impl Serialize for OsStr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(all(feature = "std", any(unix, windows)))]
|
#[cfg(all(feature = "std", any(unix, windows)))]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(all(feature = "std", any(unix, windows)))))]
|
||||||
impl Serialize for OsString {
|
impl Serialize for OsString {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
@@ -963,7 +977,20 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(no_core_reverse))]
|
#[cfg(not(no_core_num_saturating))]
|
||||||
|
impl<T> Serialize for Saturating<T>
|
||||||
|
where
|
||||||
|
T: Serialize,
|
||||||
|
{
|
||||||
|
#[inline]
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: Serializer,
|
||||||
|
{
|
||||||
|
self.0.serialize(serializer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T> Serialize for Reverse<T>
|
impl<T> Serialize for Reverse<T>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: Serialize,
|
||||||
@@ -984,6 +1011,7 @@ macro_rules! atomic_impl {
|
|||||||
($($ty:ident $size:expr)*) => {
|
($($ty:ident $size:expr)*) => {
|
||||||
$(
|
$(
|
||||||
#[cfg(any(no_target_has_atomic, target_has_atomic = $size))]
|
#[cfg(any(no_target_has_atomic, target_has_atomic = $size))]
|
||||||
|
#[cfg_attr(docsrs, doc(cfg(all(feature = "std", target_has_atomic = $size))))]
|
||||||
impl Serialize for $ty {
|
impl Serialize for $ty {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
//! This module contains `Impossible` serializer and its implementations.
|
//! This module contains `Impossible` serializer and its implementations.
|
||||||
|
|
||||||
use lib::*;
|
use crate::lib::*;
|
||||||
|
|
||||||
use ser::{
|
use crate::ser::{
|
||||||
self, Serialize, SerializeMap, SerializeSeq, SerializeStruct, SerializeStructVariant,
|
self, Serialize, SerializeMap, SerializeSeq, SerializeStruct, SerializeStructVariant,
|
||||||
SerializeTuple, SerializeTupleStruct, SerializeTupleVariant,
|
SerializeTuple, SerializeTupleStruct, SerializeTupleVariant,
|
||||||
};
|
};
|
||||||
@@ -17,7 +17,7 @@ use ser::{
|
|||||||
///
|
///
|
||||||
/// ```edition2021
|
/// ```edition2021
|
||||||
/// # use serde::ser::{Serializer, Impossible};
|
/// # use serde::ser::{Serializer, Impossible};
|
||||||
/// # use serde::__private::doc::Error;
|
/// # use serde_core::__private::doc::Error;
|
||||||
/// #
|
/// #
|
||||||
/// # struct MySerializer;
|
/// # struct MySerializer;
|
||||||
/// #
|
/// #
|
||||||
@@ -41,7 +41,7 @@ use ser::{
|
|||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// /* other Serializer methods */
|
/// /* other Serializer methods */
|
||||||
/// # serde::__serialize_unimplemented! {
|
/// # serde_core::__serialize_unimplemented! {
|
||||||
/// # bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str bytes none some
|
/// # bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str bytes none some
|
||||||
/// # unit unit_struct unit_variant newtype_struct newtype_variant
|
/// # unit unit_struct unit_variant newtype_struct newtype_variant
|
||||||
/// # tuple tuple_struct tuple_variant map struct struct_variant
|
/// # tuple tuple_struct tuple_variant map struct struct_variant
|
||||||
@@ -49,14 +49,14 @@ use ser::{
|
|||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// [`Serializer`]: trait.Serializer.html
|
/// [`Serializer`]: crate::Serializer
|
||||||
/// [`SerializeSeq`]: trait.SerializeSeq.html
|
/// [`SerializeSeq`]: crate::ser::SerializeSeq
|
||||||
/// [`SerializeTuple`]: trait.SerializeTuple.html
|
/// [`SerializeTuple`]: crate::ser::SerializeTuple
|
||||||
/// [`SerializeTupleStruct`]: trait.SerializeTupleStruct.html
|
/// [`SerializeTupleStruct`]: crate::ser::SerializeTupleStruct
|
||||||
/// [`SerializeTupleVariant`]: trait.SerializeTupleVariant.html
|
/// [`SerializeTupleVariant`]: crate::ser::SerializeTupleVariant
|
||||||
/// [`SerializeMap`]: trait.SerializeMap.html
|
/// [`SerializeMap`]: crate::ser::SerializeMap
|
||||||
/// [`SerializeStruct`]: trait.SerializeStruct.html
|
/// [`SerializeStruct`]: crate::ser::SerializeStruct
|
||||||
/// [`SerializeStructVariant`]: trait.SerializeStructVariant.html
|
/// [`SerializeStructVariant`]: crate::ser::SerializeStructVariant
|
||||||
pub struct Impossible<Ok, Error> {
|
pub struct Impossible<Ok, Error> {
|
||||||
void: Void,
|
void: Void,
|
||||||
ok: PhantomData<Ok>,
|
ok: PhantomData<Ok>,
|
||||||
@@ -72,9 +72,9 @@ where
|
|||||||
type Ok = Ok;
|
type Ok = Ok;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
|
fn serialize_element<T>(&mut self, value: &T) -> Result<(), Error>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
let _ = value;
|
let _ = value;
|
||||||
match self.void {}
|
match self.void {}
|
||||||
@@ -92,9 +92,9 @@ where
|
|||||||
type Ok = Ok;
|
type Ok = Ok;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
|
fn serialize_element<T>(&mut self, value: &T) -> Result<(), Error>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
let _ = value;
|
let _ = value;
|
||||||
match self.void {}
|
match self.void {}
|
||||||
@@ -112,9 +112,9 @@ where
|
|||||||
type Ok = Ok;
|
type Ok = Ok;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
|
fn serialize_field<T>(&mut self, value: &T) -> Result<(), Error>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
let _ = value;
|
let _ = value;
|
||||||
match self.void {}
|
match self.void {}
|
||||||
@@ -132,9 +132,9 @@ where
|
|||||||
type Ok = Ok;
|
type Ok = Ok;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
|
fn serialize_field<T>(&mut self, value: &T) -> Result<(), Error>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
let _ = value;
|
let _ = value;
|
||||||
match self.void {}
|
match self.void {}
|
||||||
@@ -152,17 +152,17 @@ where
|
|||||||
type Ok = Ok;
|
type Ok = Ok;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), Error>
|
fn serialize_key<T>(&mut self, key: &T) -> Result<(), Error>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
let _ = key;
|
let _ = key;
|
||||||
match self.void {}
|
match self.void {}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
|
fn serialize_value<T>(&mut self, value: &T) -> Result<(), Error>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
let _ = value;
|
let _ = value;
|
||||||
match self.void {}
|
match self.void {}
|
||||||
@@ -180,9 +180,9 @@ where
|
|||||||
type Ok = Ok;
|
type Ok = Ok;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<(), Error>
|
fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Error>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
let _ = key;
|
let _ = key;
|
||||||
let _ = value;
|
let _ = value;
|
||||||
@@ -201,9 +201,9 @@ where
|
|||||||
type Ok = Ok;
|
type Ok = Ok;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<(), Error>
|
fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Error>
|
||||||
where
|
where
|
||||||
T: Serialize,
|
T: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
let _ = key;
|
let _ = key;
|
||||||
let _ = value;
|
let _ = value;
|
||||||
@@ -61,8 +61,8 @@
|
|||||||
//! - RefCell\<T\>
|
//! - RefCell\<T\>
|
||||||
//! - Mutex\<T\>
|
//! - Mutex\<T\>
|
||||||
//! - RwLock\<T\>
|
//! - RwLock\<T\>
|
||||||
//! - Rc\<T\> *(if* features = ["rc"] *is enabled)*
|
//! - Rc\<T\> *(if* features = \["rc"\] *is enabled)*
|
||||||
//! - Arc\<T\> *(if* features = ["rc"] *is enabled)*
|
//! - Arc\<T\> *(if* features = \["rc"\] *is enabled)*
|
||||||
//! - **Collection types**:
|
//! - **Collection types**:
|
||||||
//! - BTreeMap\<K, V\>
|
//! - BTreeMap\<K, V\>
|
||||||
//! - BTreeSet\<T\>
|
//! - BTreeSet\<T\>
|
||||||
@@ -97,8 +97,8 @@
|
|||||||
//!
|
//!
|
||||||
//! [Implementing `Serialize`]: https://serde.rs/impl-serialize.html
|
//! [Implementing `Serialize`]: https://serde.rs/impl-serialize.html
|
||||||
//! [`LinkedHashMap<K, V>`]: https://docs.rs/linked-hash-map/*/linked_hash_map/struct.LinkedHashMap.html
|
//! [`LinkedHashMap<K, V>`]: https://docs.rs/linked-hash-map/*/linked_hash_map/struct.LinkedHashMap.html
|
||||||
//! [`Serialize`]: ../trait.Serialize.html
|
//! [`Serialize`]: crate::Serialize
|
||||||
//! [`Serializer`]: ../trait.Serializer.html
|
//! [`Serializer`]: crate::Serializer
|
||||||
//! [`postcard`]: https://github.com/jamesmunns/postcard
|
//! [`postcard`]: https://github.com/jamesmunns/postcard
|
||||||
//! [`linked-hash-map`]: https://crates.io/crates/linked-hash-map
|
//! [`linked-hash-map`]: https://crates.io/crates/linked-hash-map
|
||||||
//! [`serde_derive`]: https://crates.io/crates/serde_derive
|
//! [`serde_derive`]: https://crates.io/crates/serde_derive
|
||||||
@@ -107,7 +107,7 @@
|
|||||||
//! [derive section of the manual]: https://serde.rs/derive.html
|
//! [derive section of the manual]: https://serde.rs/derive.html
|
||||||
//! [data formats]: https://serde.rs/#data-formats
|
//! [data formats]: https://serde.rs/#data-formats
|
||||||
|
|
||||||
use lib::*;
|
use crate::lib::*;
|
||||||
|
|
||||||
mod fmt;
|
mod fmt;
|
||||||
mod impls;
|
mod impls;
|
||||||
@@ -115,15 +115,15 @@ mod impossible;
|
|||||||
|
|
||||||
pub use self::impossible::Impossible;
|
pub use self::impossible::Impossible;
|
||||||
|
|
||||||
#[cfg(all(feature = "unstable", not(feature = "std")))]
|
#[cfg(all(not(feature = "std"), no_core_error))]
|
||||||
|
#[doc(no_inline)]
|
||||||
|
pub use crate::std_error::Error as StdError;
|
||||||
|
#[cfg(not(any(feature = "std", no_core_error)))]
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
pub use core::error::Error as StdError;
|
pub use core::error::Error as StdError;
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
pub use std::error::Error as StdError;
|
pub use std::error::Error as StdError;
|
||||||
#[cfg(not(any(feature = "std", feature = "unstable")))]
|
|
||||||
#[doc(no_inline)]
|
|
||||||
pub use std_error::Error as StdError;
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
@@ -139,6 +139,12 @@ macro_rules! declare_error_trait {
|
|||||||
/// type appropriate for a basic JSON data format.
|
/// type appropriate for a basic JSON data format.
|
||||||
///
|
///
|
||||||
/// [example data format]: https://serde.rs/data-format.html
|
/// [example data format]: https://serde.rs/data-format.html
|
||||||
|
#[cfg_attr(
|
||||||
|
not(no_diagnostic_namespace),
|
||||||
|
diagnostic::on_unimplemented(
|
||||||
|
message = "the trait bound `{Self}: serde::ser::Error` is not satisfied",
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub trait Error: Sized $(+ $($supertrait)::+)* {
|
pub trait Error: Sized $(+ $($supertrait)::+)* {
|
||||||
/// Used when a [`Serialize`] implementation encounters any error
|
/// Used when a [`Serialize`] implementation encounters any error
|
||||||
/// while serializing a type.
|
/// while serializing a type.
|
||||||
@@ -173,8 +179,8 @@ macro_rules! declare_error_trait {
|
|||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// [`Path`]: https://doc.rust-lang.org/std/path/struct.Path.html
|
/// [`Path`]: std::path::Path
|
||||||
/// [`Serialize`]: ../trait.Serialize.html
|
/// [`Serialize`]: crate::Serialize
|
||||||
fn custom<T>(msg: T) -> Self
|
fn custom<T>(msg: T) -> Self
|
||||||
where
|
where
|
||||||
T: Display;
|
T: Display;
|
||||||
@@ -215,6 +221,16 @@ declare_error_trait!(Error: Sized + Debug + Display);
|
|||||||
/// [`linked-hash-map`]: https://crates.io/crates/linked-hash-map
|
/// [`linked-hash-map`]: https://crates.io/crates/linked-hash-map
|
||||||
/// [`serde_derive`]: https://crates.io/crates/serde_derive
|
/// [`serde_derive`]: https://crates.io/crates/serde_derive
|
||||||
/// [derive section of the manual]: https://serde.rs/derive.html
|
/// [derive section of the manual]: https://serde.rs/derive.html
|
||||||
|
#[cfg_attr(
|
||||||
|
not(no_diagnostic_namespace),
|
||||||
|
diagnostic::on_unimplemented(
|
||||||
|
// Prevents `serde_core::ser::Serialize` appearing in the error message
|
||||||
|
// in projects with no direct dependency on serde_core.
|
||||||
|
message = "the trait bound `{Self}: serde::Serialize` is not satisfied",
|
||||||
|
note = "for local types consider adding `#[derive(serde::Serialize)]` to your `{Self}` type",
|
||||||
|
note = "for types from other crates check whether the crate offers a `serde` feature flag",
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub trait Serialize {
|
pub trait Serialize {
|
||||||
/// Serialize this value into the given Serde serializer.
|
/// Serialize this value into the given Serde serializer.
|
||||||
///
|
///
|
||||||
@@ -330,6 +346,12 @@ pub trait Serialize {
|
|||||||
/// a basic JSON `Serializer`.
|
/// a basic JSON `Serializer`.
|
||||||
///
|
///
|
||||||
/// [example data format]: https://serde.rs/data-format.html
|
/// [example data format]: https://serde.rs/data-format.html
|
||||||
|
#[cfg_attr(
|
||||||
|
not(no_diagnostic_namespace),
|
||||||
|
diagnostic::on_unimplemented(
|
||||||
|
message = "the trait bound `{Self}: serde::Serializer` is not satisfied",
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub trait Serializer: Sized {
|
pub trait Serializer: Sized {
|
||||||
/// The output type produced by this `Serializer` during successful
|
/// The output type produced by this `Serializer` during successful
|
||||||
/// serialization. Most serializers that produce text or binary output
|
/// serialization. Most serializers that produce text or binary output
|
||||||
@@ -338,7 +360,7 @@ pub trait Serializer: Sized {
|
|||||||
/// in-memory data structures may be simplified by using `Ok` to propagate
|
/// in-memory data structures may be simplified by using `Ok` to propagate
|
||||||
/// the data structure around.
|
/// the data structure around.
|
||||||
///
|
///
|
||||||
/// [`io::Write`]: https://doc.rust-lang.org/std/io/trait.Write.html
|
/// [`io::Write`]: std::io::Write
|
||||||
type Ok;
|
type Ok;
|
||||||
|
|
||||||
/// The error type when some error occurs during serialization.
|
/// The error type when some error occurs during serialization.
|
||||||
@@ -391,7 +413,7 @@ pub trait Serializer: Sized {
|
|||||||
/// ```edition2021
|
/// ```edition2021
|
||||||
/// # use serde::Serializer;
|
/// # use serde::Serializer;
|
||||||
/// #
|
/// #
|
||||||
/// # serde::__private_serialize!();
|
/// # serde_core::__private_serialize!();
|
||||||
/// #
|
/// #
|
||||||
/// impl Serialize for bool {
|
/// impl Serialize for bool {
|
||||||
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
@@ -413,7 +435,7 @@ pub trait Serializer: Sized {
|
|||||||
/// ```edition2021
|
/// ```edition2021
|
||||||
/// # use serde::Serializer;
|
/// # use serde::Serializer;
|
||||||
/// #
|
/// #
|
||||||
/// # serde::__private_serialize!();
|
/// # serde_core::__private_serialize!();
|
||||||
/// #
|
/// #
|
||||||
/// impl Serialize for i8 {
|
/// impl Serialize for i8 {
|
||||||
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
@@ -435,7 +457,7 @@ pub trait Serializer: Sized {
|
|||||||
/// ```edition2021
|
/// ```edition2021
|
||||||
/// # use serde::Serializer;
|
/// # use serde::Serializer;
|
||||||
/// #
|
/// #
|
||||||
/// # serde::__private_serialize!();
|
/// # serde_core::__private_serialize!();
|
||||||
/// #
|
/// #
|
||||||
/// impl Serialize for i16 {
|
/// impl Serialize for i16 {
|
||||||
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
@@ -457,7 +479,7 @@ pub trait Serializer: Sized {
|
|||||||
/// ```edition2021
|
/// ```edition2021
|
||||||
/// # use serde::Serializer;
|
/// # use serde::Serializer;
|
||||||
/// #
|
/// #
|
||||||
/// # serde::__private_serialize!();
|
/// # serde_core::__private_serialize!();
|
||||||
/// #
|
/// #
|
||||||
/// impl Serialize for i32 {
|
/// impl Serialize for i32 {
|
||||||
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
@@ -475,7 +497,7 @@ pub trait Serializer: Sized {
|
|||||||
/// ```edition2021
|
/// ```edition2021
|
||||||
/// # use serde::Serializer;
|
/// # use serde::Serializer;
|
||||||
/// #
|
/// #
|
||||||
/// # serde::__private_serialize!();
|
/// # serde_core::__private_serialize!();
|
||||||
/// #
|
/// #
|
||||||
/// impl Serialize for i64 {
|
/// impl Serialize for i64 {
|
||||||
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
@@ -488,30 +510,27 @@ pub trait Serializer: Sized {
|
|||||||
/// ```
|
/// ```
|
||||||
fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error>;
|
fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error>;
|
||||||
|
|
||||||
serde_if_integer128! {
|
/// Serialize an `i128` value.
|
||||||
/// Serialize an `i128` value.
|
///
|
||||||
///
|
/// ```edition2021
|
||||||
/// ```edition2021
|
/// # use serde::Serializer;
|
||||||
/// # use serde::Serializer;
|
/// #
|
||||||
/// #
|
/// # serde_core::__private_serialize!();
|
||||||
/// # serde::__private_serialize!();
|
/// #
|
||||||
/// #
|
/// impl Serialize for i128 {
|
||||||
/// impl Serialize for i128 {
|
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
/// where
|
||||||
/// where
|
/// S: Serializer,
|
||||||
/// S: Serializer,
|
/// {
|
||||||
/// {
|
/// serializer.serialize_i128(*self)
|
||||||
/// serializer.serialize_i128(*self)
|
/// }
|
||||||
/// }
|
/// }
|
||||||
/// }
|
/// ```
|
||||||
/// ```
|
///
|
||||||
///
|
/// The default behavior unconditionally returns an error.
|
||||||
/// This method is available only on Rust compiler versions >=1.26. The
|
fn serialize_i128(self, v: i128) -> Result<Self::Ok, Self::Error> {
|
||||||
/// default behavior unconditionally returns an error.
|
let _ = v;
|
||||||
fn serialize_i128(self, v: i128) -> Result<Self::Ok, Self::Error> {
|
Err(Error::custom("i128 is not supported"))
|
||||||
let _ = v;
|
|
||||||
Err(Error::custom("i128 is not supported"))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Serialize a `u8` value.
|
/// Serialize a `u8` value.
|
||||||
@@ -523,7 +542,7 @@ pub trait Serializer: Sized {
|
|||||||
/// ```edition2021
|
/// ```edition2021
|
||||||
/// # use serde::Serializer;
|
/// # use serde::Serializer;
|
||||||
/// #
|
/// #
|
||||||
/// # serde::__private_serialize!();
|
/// # serde_core::__private_serialize!();
|
||||||
/// #
|
/// #
|
||||||
/// impl Serialize for u8 {
|
/// impl Serialize for u8 {
|
||||||
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
@@ -545,7 +564,7 @@ pub trait Serializer: Sized {
|
|||||||
/// ```edition2021
|
/// ```edition2021
|
||||||
/// # use serde::Serializer;
|
/// # use serde::Serializer;
|
||||||
/// #
|
/// #
|
||||||
/// # serde::__private_serialize!();
|
/// # serde_core::__private_serialize!();
|
||||||
/// #
|
/// #
|
||||||
/// impl Serialize for u16 {
|
/// impl Serialize for u16 {
|
||||||
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
@@ -567,7 +586,7 @@ pub trait Serializer: Sized {
|
|||||||
/// ```edition2021
|
/// ```edition2021
|
||||||
/// # use serde::Serializer;
|
/// # use serde::Serializer;
|
||||||
/// #
|
/// #
|
||||||
/// # serde::__private_serialize!();
|
/// # serde_core::__private_serialize!();
|
||||||
/// #
|
/// #
|
||||||
/// impl Serialize for u32 {
|
/// impl Serialize for u32 {
|
||||||
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
@@ -585,7 +604,7 @@ pub trait Serializer: Sized {
|
|||||||
/// ```edition2021
|
/// ```edition2021
|
||||||
/// # use serde::Serializer;
|
/// # use serde::Serializer;
|
||||||
/// #
|
/// #
|
||||||
/// # serde::__private_serialize!();
|
/// # serde_core::__private_serialize!();
|
||||||
/// #
|
/// #
|
||||||
/// impl Serialize for u64 {
|
/// impl Serialize for u64 {
|
||||||
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
@@ -598,30 +617,27 @@ pub trait Serializer: Sized {
|
|||||||
/// ```
|
/// ```
|
||||||
fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error>;
|
fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error>;
|
||||||
|
|
||||||
serde_if_integer128! {
|
/// Serialize a `u128` value.
|
||||||
/// Serialize a `u128` value.
|
///
|
||||||
///
|
/// ```edition2021
|
||||||
/// ```edition2021
|
/// # use serde::Serializer;
|
||||||
/// # use serde::Serializer;
|
/// #
|
||||||
/// #
|
/// # serde_core::__private_serialize!();
|
||||||
/// # serde::__private_serialize!();
|
/// #
|
||||||
/// #
|
/// impl Serialize for u128 {
|
||||||
/// impl Serialize for u128 {
|
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
/// where
|
||||||
/// where
|
/// S: Serializer,
|
||||||
/// S: Serializer,
|
/// {
|
||||||
/// {
|
/// serializer.serialize_u128(*self)
|
||||||
/// serializer.serialize_u128(*self)
|
/// }
|
||||||
/// }
|
/// }
|
||||||
/// }
|
/// ```
|
||||||
/// ```
|
///
|
||||||
///
|
/// The default behavior unconditionally returns an error.
|
||||||
/// This method is available only on Rust compiler versions >=1.26. The
|
fn serialize_u128(self, v: u128) -> Result<Self::Ok, Self::Error> {
|
||||||
/// default behavior unconditionally returns an error.
|
let _ = v;
|
||||||
fn serialize_u128(self, v: u128) -> Result<Self::Ok, Self::Error> {
|
Err(Error::custom("u128 is not supported"))
|
||||||
let _ = v;
|
|
||||||
Err(Error::custom("u128 is not supported"))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Serialize an `f32` value.
|
/// Serialize an `f32` value.
|
||||||
@@ -633,7 +649,7 @@ pub trait Serializer: Sized {
|
|||||||
/// ```edition2021
|
/// ```edition2021
|
||||||
/// # use serde::Serializer;
|
/// # use serde::Serializer;
|
||||||
/// #
|
/// #
|
||||||
/// # serde::__private_serialize!();
|
/// # serde_core::__private_serialize!();
|
||||||
/// #
|
/// #
|
||||||
/// impl Serialize for f32 {
|
/// impl Serialize for f32 {
|
||||||
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
@@ -651,7 +667,7 @@ pub trait Serializer: Sized {
|
|||||||
/// ```edition2021
|
/// ```edition2021
|
||||||
/// # use serde::Serializer;
|
/// # use serde::Serializer;
|
||||||
/// #
|
/// #
|
||||||
/// # serde::__private_serialize!();
|
/// # serde_core::__private_serialize!();
|
||||||
/// #
|
/// #
|
||||||
/// impl Serialize for f64 {
|
/// impl Serialize for f64 {
|
||||||
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
@@ -672,7 +688,7 @@ pub trait Serializer: Sized {
|
|||||||
/// ```edition2021
|
/// ```edition2021
|
||||||
/// # use serde::Serializer;
|
/// # use serde::Serializer;
|
||||||
/// #
|
/// #
|
||||||
/// # serde::__private_serialize!();
|
/// # serde_core::__private_serialize!();
|
||||||
/// #
|
/// #
|
||||||
/// impl Serialize for char {
|
/// impl Serialize for char {
|
||||||
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
@@ -690,7 +706,7 @@ pub trait Serializer: Sized {
|
|||||||
/// ```edition2021
|
/// ```edition2021
|
||||||
/// # use serde::Serializer;
|
/// # use serde::Serializer;
|
||||||
/// #
|
/// #
|
||||||
/// # serde::__private_serialize!();
|
/// # serde_core::__private_serialize!();
|
||||||
/// #
|
/// #
|
||||||
/// impl Serialize for str {
|
/// impl Serialize for str {
|
||||||
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
@@ -713,7 +729,7 @@ pub trait Serializer: Sized {
|
|||||||
///
|
///
|
||||||
/// ```edition2021
|
/// ```edition2021
|
||||||
/// # use serde::ser::{Serializer, SerializeSeq};
|
/// # use serde::ser::{Serializer, SerializeSeq};
|
||||||
/// # use serde::__private::doc::Error;
|
/// # use serde_core::__private::doc::Error;
|
||||||
/// #
|
/// #
|
||||||
/// # struct MySerializer;
|
/// # struct MySerializer;
|
||||||
/// #
|
/// #
|
||||||
@@ -729,7 +745,7 @@ pub trait Serializer: Sized {
|
|||||||
/// seq.end()
|
/// seq.end()
|
||||||
/// }
|
/// }
|
||||||
/// #
|
/// #
|
||||||
/// # serde::__serialize_unimplemented! {
|
/// # serde_core::__serialize_unimplemented! {
|
||||||
/// # bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str none some
|
/// # bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str none some
|
||||||
/// # unit unit_struct unit_variant newtype_struct newtype_variant
|
/// # unit unit_struct unit_variant newtype_struct newtype_variant
|
||||||
/// # seq tuple tuple_struct tuple_variant map struct struct_variant
|
/// # seq tuple tuple_struct tuple_variant map struct struct_variant
|
||||||
@@ -768,7 +784,7 @@ pub trait Serializer: Sized {
|
|||||||
/// # fn main() {}
|
/// # fn main() {}
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None
|
/// [`None`]: core::option::Option::None
|
||||||
fn serialize_none(self) -> Result<Self::Ok, Self::Error>;
|
fn serialize_none(self) -> Result<Self::Ok, Self::Error>;
|
||||||
|
|
||||||
/// Serialize a [`Some(T)`] value.
|
/// Serialize a [`Some(T)`] value.
|
||||||
@@ -801,17 +817,17 @@ pub trait Serializer: Sized {
|
|||||||
/// # fn main() {}
|
/// # fn main() {}
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// [`Some(T)`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.Some
|
/// [`Some(T)`]: core::option::Option::Some
|
||||||
fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
|
fn serialize_some<T>(self, value: &T) -> Result<Self::Ok, Self::Error>
|
||||||
where
|
where
|
||||||
T: Serialize;
|
T: ?Sized + Serialize;
|
||||||
|
|
||||||
/// Serialize a `()` value.
|
/// Serialize a `()` value.
|
||||||
///
|
///
|
||||||
/// ```edition2021
|
/// ```edition2021
|
||||||
/// # use serde::Serializer;
|
/// # use serde::Serializer;
|
||||||
/// #
|
/// #
|
||||||
/// # serde::__private_serialize!();
|
/// # serde_core::__private_serialize!();
|
||||||
/// #
|
/// #
|
||||||
/// impl Serialize for () {
|
/// impl Serialize for () {
|
||||||
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
@@ -897,13 +913,13 @@ pub trait Serializer: Sized {
|
|||||||
/// }
|
/// }
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
fn serialize_newtype_struct<T: ?Sized>(
|
fn serialize_newtype_struct<T>(
|
||||||
self,
|
self,
|
||||||
name: &'static str,
|
name: &'static str,
|
||||||
value: &T,
|
value: &T,
|
||||||
) -> Result<Self::Ok, Self::Error>
|
) -> Result<Self::Ok, Self::Error>
|
||||||
where
|
where
|
||||||
T: Serialize;
|
T: ?Sized + Serialize;
|
||||||
|
|
||||||
/// Serialize a newtype variant like `E::N` in `enum E { N(u8) }`.
|
/// Serialize a newtype variant like `E::N` in `enum E { N(u8) }`.
|
||||||
///
|
///
|
||||||
@@ -931,7 +947,7 @@ pub trait Serializer: Sized {
|
|||||||
/// }
|
/// }
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
fn serialize_newtype_variant<T: ?Sized>(
|
fn serialize_newtype_variant<T>(
|
||||||
self,
|
self,
|
||||||
name: &'static str,
|
name: &'static str,
|
||||||
variant_index: u32,
|
variant_index: u32,
|
||||||
@@ -939,7 +955,7 @@ pub trait Serializer: Sized {
|
|||||||
value: &T,
|
value: &T,
|
||||||
) -> Result<Self::Ok, Self::Error>
|
) -> Result<Self::Ok, Self::Error>
|
||||||
where
|
where
|
||||||
T: Serialize;
|
T: ?Sized + Serialize;
|
||||||
|
|
||||||
/// Begin to serialize a variably sized sequence. This call must be
|
/// Begin to serialize a variably sized sequence. This call must be
|
||||||
/// followed by zero or more calls to `serialize_element`, then a call to
|
/// followed by zero or more calls to `serialize_element`, then a call to
|
||||||
@@ -1176,7 +1192,8 @@ pub trait Serializer: Sized {
|
|||||||
/// then a call to `end`.
|
/// then a call to `end`.
|
||||||
///
|
///
|
||||||
/// The `name` is the name of the struct and the `len` is the number of
|
/// The `name` is the name of the struct and the `len` is the number of
|
||||||
/// data fields that will be serialized.
|
/// data fields that will be serialized. `len` does not include fields
|
||||||
|
/// which are skipped with [`SerializeStruct::skip_field`].
|
||||||
///
|
///
|
||||||
/// ```edition2021
|
/// ```edition2021
|
||||||
/// use serde::ser::{Serialize, SerializeStruct, Serializer};
|
/// use serde::ser::{Serialize, SerializeStruct, Serializer};
|
||||||
@@ -1213,6 +1230,8 @@ pub trait Serializer: Sized {
|
|||||||
/// The `name` is the name of the enum, the `variant_index` is the index of
|
/// The `name` is the name of the enum, the `variant_index` is the index of
|
||||||
/// this variant within the enum, the `variant` is the name of the variant,
|
/// 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.
|
/// and the `len` is the number of data fields that will be serialized.
|
||||||
|
/// `len` does not include fields which are skipped with
|
||||||
|
/// [`SerializeStructVariant::skip_field`].
|
||||||
///
|
///
|
||||||
/// ```edition2021
|
/// ```edition2021
|
||||||
/// use serde::ser::{Serialize, SerializeStructVariant, Serializer};
|
/// use serde::ser::{Serialize, SerializeStructVariant, Serializer};
|
||||||
@@ -1279,22 +1298,9 @@ pub trait Serializer: Sized {
|
|||||||
I: IntoIterator,
|
I: IntoIterator,
|
||||||
<I as IntoIterator>::Item: Serialize,
|
<I as IntoIterator>::Item: Serialize,
|
||||||
{
|
{
|
||||||
let iter = iter.into_iter();
|
let mut iter = iter.into_iter();
|
||||||
let mut serializer = try!(self.serialize_seq(iterator_len_hint(&iter)));
|
let mut serializer = tri!(self.serialize_seq(iterator_len_hint(&iter)));
|
||||||
|
tri!(iter.try_for_each(|item| serializer.serialize_element(&item)));
|
||||||
#[cfg(not(no_iterator_try_fold))]
|
|
||||||
{
|
|
||||||
let mut iter = iter;
|
|
||||||
try!(iter.try_for_each(|item| serializer.serialize_element(&item)));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(no_iterator_try_fold)]
|
|
||||||
{
|
|
||||||
for item in iter {
|
|
||||||
try!(serializer.serialize_element(&item));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
serializer.end()
|
serializer.end()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1330,22 +1336,9 @@ pub trait Serializer: Sized {
|
|||||||
V: Serialize,
|
V: Serialize,
|
||||||
I: IntoIterator<Item = (K, V)>,
|
I: IntoIterator<Item = (K, V)>,
|
||||||
{
|
{
|
||||||
let iter = iter.into_iter();
|
let mut iter = iter.into_iter();
|
||||||
let mut serializer = try!(self.serialize_map(iterator_len_hint(&iter)));
|
let mut serializer = tri!(self.serialize_map(iterator_len_hint(&iter)));
|
||||||
|
tri!(iter.try_for_each(|(key, value)| serializer.serialize_entry(&key, &value)));
|
||||||
#[cfg(not(no_iterator_try_fold))]
|
|
||||||
{
|
|
||||||
let mut iter = iter;
|
|
||||||
try!(iter.try_for_each(|(key, value)| serializer.serialize_entry(&key, &value)));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(no_iterator_try_fold)]
|
|
||||||
{
|
|
||||||
for (key, value) in iter {
|
|
||||||
try!(serializer.serialize_entry(&key, &value));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
serializer.end()
|
serializer.end()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1375,12 +1368,11 @@ pub trait Serializer: Sized {
|
|||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// [`String`]: https://doc.rust-lang.org/std/string/struct.String.html
|
/// [`serialize_str`]: Self::serialize_str
|
||||||
/// [`serialize_str`]: #tymethod.serialize_str
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
fn collect_str<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
|
fn collect_str<T>(self, value: &T) -> Result<Self::Ok, Self::Error>
|
||||||
where
|
where
|
||||||
T: Display,
|
T: ?Sized + Display,
|
||||||
{
|
{
|
||||||
self.serialize_str(&value.to_string())
|
self.serialize_str(&value.to_string())
|
||||||
}
|
}
|
||||||
@@ -1411,9 +1403,9 @@ pub trait Serializer: Sized {
|
|||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
#[cfg(not(any(feature = "std", feature = "alloc")))]
|
#[cfg(not(any(feature = "std", feature = "alloc")))]
|
||||||
fn collect_str<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
|
fn collect_str<T>(self, value: &T) -> Result<Self::Ok, Self::Error>
|
||||||
where
|
where
|
||||||
T: Display;
|
T: ?Sized + Display;
|
||||||
|
|
||||||
/// Determine whether `Serialize` implementations should serialize in
|
/// Determine whether `Serialize` implementations should serialize in
|
||||||
/// human-readable form.
|
/// human-readable form.
|
||||||
@@ -1517,6 +1509,12 @@ pub trait Serializer: Sized {
|
|||||||
/// implementation of `SerializeSeq` for a basic JSON data format.
|
/// implementation of `SerializeSeq` for a basic JSON data format.
|
||||||
///
|
///
|
||||||
/// [example data format]: https://serde.rs/data-format.html
|
/// [example data format]: https://serde.rs/data-format.html
|
||||||
|
#[cfg_attr(
|
||||||
|
not(no_diagnostic_namespace),
|
||||||
|
diagnostic::on_unimplemented(
|
||||||
|
message = "the trait bound `{Self}: serde::ser::SerializeSeq` is not satisfied",
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub trait SerializeSeq {
|
pub trait SerializeSeq {
|
||||||
/// Must match the `Ok` type of our `Serializer`.
|
/// Must match the `Ok` type of our `Serializer`.
|
||||||
type Ok;
|
type Ok;
|
||||||
@@ -1525,9 +1523,9 @@ pub trait SerializeSeq {
|
|||||||
type Error: Error;
|
type Error: Error;
|
||||||
|
|
||||||
/// Serialize a sequence element.
|
/// Serialize a sequence element.
|
||||||
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
|
fn serialize_element<T>(&mut self, value: &T) -> Result<(), Self::Error>
|
||||||
where
|
where
|
||||||
T: Serialize;
|
T: ?Sized + Serialize;
|
||||||
|
|
||||||
/// Finish serializing a sequence.
|
/// Finish serializing a sequence.
|
||||||
fn end(self) -> Result<Self::Ok, Self::Error>;
|
fn end(self) -> Result<Self::Ok, Self::Error>;
|
||||||
@@ -1617,6 +1615,12 @@ pub trait SerializeSeq {
|
|||||||
/// implementation of `SerializeTuple` for a basic JSON data format.
|
/// implementation of `SerializeTuple` for a basic JSON data format.
|
||||||
///
|
///
|
||||||
/// [example data format]: https://serde.rs/data-format.html
|
/// [example data format]: https://serde.rs/data-format.html
|
||||||
|
#[cfg_attr(
|
||||||
|
not(no_diagnostic_namespace),
|
||||||
|
diagnostic::on_unimplemented(
|
||||||
|
message = "the trait bound `{Self}: serde::ser::SerializeTuple` is not satisfied",
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub trait SerializeTuple {
|
pub trait SerializeTuple {
|
||||||
/// Must match the `Ok` type of our `Serializer`.
|
/// Must match the `Ok` type of our `Serializer`.
|
||||||
type Ok;
|
type Ok;
|
||||||
@@ -1625,9 +1629,9 @@ pub trait SerializeTuple {
|
|||||||
type Error: Error;
|
type Error: Error;
|
||||||
|
|
||||||
/// Serialize a tuple element.
|
/// Serialize a tuple element.
|
||||||
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
|
fn serialize_element<T>(&mut self, value: &T) -> Result<(), Self::Error>
|
||||||
where
|
where
|
||||||
T: Serialize;
|
T: ?Sized + Serialize;
|
||||||
|
|
||||||
/// Finish serializing a tuple.
|
/// Finish serializing a tuple.
|
||||||
fn end(self) -> Result<Self::Ok, Self::Error>;
|
fn end(self) -> Result<Self::Ok, Self::Error>;
|
||||||
@@ -1662,6 +1666,12 @@ pub trait SerializeTuple {
|
|||||||
/// implementation of `SerializeTupleStruct` for a basic JSON data format.
|
/// implementation of `SerializeTupleStruct` for a basic JSON data format.
|
||||||
///
|
///
|
||||||
/// [example data format]: https://serde.rs/data-format.html
|
/// [example data format]: https://serde.rs/data-format.html
|
||||||
|
#[cfg_attr(
|
||||||
|
not(no_diagnostic_namespace),
|
||||||
|
diagnostic::on_unimplemented(
|
||||||
|
message = "the trait bound `{Self}: serde::ser::SerializeTupleStruct` is not satisfied",
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub trait SerializeTupleStruct {
|
pub trait SerializeTupleStruct {
|
||||||
/// Must match the `Ok` type of our `Serializer`.
|
/// Must match the `Ok` type of our `Serializer`.
|
||||||
type Ok;
|
type Ok;
|
||||||
@@ -1670,9 +1680,9 @@ pub trait SerializeTupleStruct {
|
|||||||
type Error: Error;
|
type Error: Error;
|
||||||
|
|
||||||
/// Serialize a tuple struct field.
|
/// Serialize a tuple struct field.
|
||||||
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
|
fn serialize_field<T>(&mut self, value: &T) -> Result<(), Self::Error>
|
||||||
where
|
where
|
||||||
T: Serialize;
|
T: ?Sized + Serialize;
|
||||||
|
|
||||||
/// Finish serializing a tuple struct.
|
/// Finish serializing a tuple struct.
|
||||||
fn end(self) -> Result<Self::Ok, Self::Error>;
|
fn end(self) -> Result<Self::Ok, Self::Error>;
|
||||||
@@ -1720,6 +1730,12 @@ pub trait SerializeTupleStruct {
|
|||||||
/// implementation of `SerializeTupleVariant` for a basic JSON data format.
|
/// implementation of `SerializeTupleVariant` for a basic JSON data format.
|
||||||
///
|
///
|
||||||
/// [example data format]: https://serde.rs/data-format.html
|
/// [example data format]: https://serde.rs/data-format.html
|
||||||
|
#[cfg_attr(
|
||||||
|
not(no_diagnostic_namespace),
|
||||||
|
diagnostic::on_unimplemented(
|
||||||
|
message = "the trait bound `{Self}: serde::ser::SerializeTupleVariant` is not satisfied",
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub trait SerializeTupleVariant {
|
pub trait SerializeTupleVariant {
|
||||||
/// Must match the `Ok` type of our `Serializer`.
|
/// Must match the `Ok` type of our `Serializer`.
|
||||||
type Ok;
|
type Ok;
|
||||||
@@ -1728,9 +1744,9 @@ pub trait SerializeTupleVariant {
|
|||||||
type Error: Error;
|
type Error: Error;
|
||||||
|
|
||||||
/// Serialize a tuple variant field.
|
/// Serialize a tuple variant field.
|
||||||
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
|
fn serialize_field<T>(&mut self, value: &T) -> Result<(), Self::Error>
|
||||||
where
|
where
|
||||||
T: Serialize;
|
T: ?Sized + Serialize;
|
||||||
|
|
||||||
/// Finish serializing a tuple variant.
|
/// Finish serializing a tuple variant.
|
||||||
fn end(self) -> Result<Self::Ok, Self::Error>;
|
fn end(self) -> Result<Self::Ok, Self::Error>;
|
||||||
@@ -1786,6 +1802,12 @@ pub trait SerializeTupleVariant {
|
|||||||
/// implementation of `SerializeMap` for a basic JSON data format.
|
/// implementation of `SerializeMap` for a basic JSON data format.
|
||||||
///
|
///
|
||||||
/// [example data format]: https://serde.rs/data-format.html
|
/// [example data format]: https://serde.rs/data-format.html
|
||||||
|
#[cfg_attr(
|
||||||
|
not(no_diagnostic_namespace),
|
||||||
|
diagnostic::on_unimplemented(
|
||||||
|
message = "the trait bound `{Self}: serde::ser::SerializeMap` is not satisfied",
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub trait SerializeMap {
|
pub trait SerializeMap {
|
||||||
/// Must match the `Ok` type of our `Serializer`.
|
/// Must match the `Ok` type of our `Serializer`.
|
||||||
type Ok;
|
type Ok;
|
||||||
@@ -1799,9 +1821,9 @@ pub trait SerializeMap {
|
|||||||
/// `serialize_entry` instead as it may be implemented more efficiently in
|
/// `serialize_entry` instead as it may be implemented more efficiently in
|
||||||
/// some formats compared to a pair of calls to `serialize_key` and
|
/// some formats compared to a pair of calls to `serialize_key` and
|
||||||
/// `serialize_value`.
|
/// `serialize_value`.
|
||||||
fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), Self::Error>
|
fn serialize_key<T>(&mut self, key: &T) -> Result<(), Self::Error>
|
||||||
where
|
where
|
||||||
T: Serialize;
|
T: ?Sized + Serialize;
|
||||||
|
|
||||||
/// Serialize a map value.
|
/// Serialize a map value.
|
||||||
///
|
///
|
||||||
@@ -1809,9 +1831,9 @@ pub trait SerializeMap {
|
|||||||
///
|
///
|
||||||
/// Calling `serialize_value` before `serialize_key` is incorrect and is
|
/// Calling `serialize_value` before `serialize_key` is incorrect and is
|
||||||
/// allowed to panic or produce bogus results.
|
/// allowed to panic or produce bogus results.
|
||||||
fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
|
fn serialize_value<T>(&mut self, value: &T) -> Result<(), Self::Error>
|
||||||
where
|
where
|
||||||
T: Serialize;
|
T: ?Sized + Serialize;
|
||||||
|
|
||||||
/// Serialize a map entry consisting of a key and a value.
|
/// Serialize a map entry consisting of a key and a value.
|
||||||
///
|
///
|
||||||
@@ -1827,19 +1849,15 @@ pub trait SerializeMap {
|
|||||||
/// care about performance or are not able to optimize `serialize_entry` any
|
/// care about performance or are not able to optimize `serialize_entry` any
|
||||||
/// better than this.
|
/// better than this.
|
||||||
///
|
///
|
||||||
/// [`Serialize`]: ../trait.Serialize.html
|
/// [`Serialize`]: crate::Serialize
|
||||||
/// [`serialize_key`]: #tymethod.serialize_key
|
/// [`serialize_key`]: Self::serialize_key
|
||||||
/// [`serialize_value`]: #tymethod.serialize_value
|
/// [`serialize_value`]: Self::serialize_value
|
||||||
fn serialize_entry<K: ?Sized, V: ?Sized>(
|
fn serialize_entry<K, V>(&mut self, key: &K, value: &V) -> Result<(), Self::Error>
|
||||||
&mut self,
|
|
||||||
key: &K,
|
|
||||||
value: &V,
|
|
||||||
) -> Result<(), Self::Error>
|
|
||||||
where
|
where
|
||||||
K: Serialize,
|
K: ?Sized + Serialize,
|
||||||
V: Serialize,
|
V: ?Sized + Serialize,
|
||||||
{
|
{
|
||||||
try!(self.serialize_key(key));
|
tri!(self.serialize_key(key));
|
||||||
self.serialize_value(value)
|
self.serialize_value(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1880,6 +1898,12 @@ pub trait SerializeMap {
|
|||||||
/// implementation of `SerializeStruct` for a basic JSON data format.
|
/// implementation of `SerializeStruct` for a basic JSON data format.
|
||||||
///
|
///
|
||||||
/// [example data format]: https://serde.rs/data-format.html
|
/// [example data format]: https://serde.rs/data-format.html
|
||||||
|
#[cfg_attr(
|
||||||
|
not(no_diagnostic_namespace),
|
||||||
|
diagnostic::on_unimplemented(
|
||||||
|
message = "the trait bound `{Self}: serde::ser::SerializeStruct` is not satisfied",
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub trait SerializeStruct {
|
pub trait SerializeStruct {
|
||||||
/// Must match the `Ok` type of our `Serializer`.
|
/// Must match the `Ok` type of our `Serializer`.
|
||||||
type Ok;
|
type Ok;
|
||||||
@@ -1888,15 +1912,13 @@ pub trait SerializeStruct {
|
|||||||
type Error: Error;
|
type Error: Error;
|
||||||
|
|
||||||
/// Serialize a struct field.
|
/// Serialize a struct field.
|
||||||
fn serialize_field<T: ?Sized>(
|
fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error>
|
||||||
&mut self,
|
|
||||||
key: &'static str,
|
|
||||||
value: &T,
|
|
||||||
) -> Result<(), Self::Error>
|
|
||||||
where
|
where
|
||||||
T: Serialize;
|
T: ?Sized + Serialize;
|
||||||
|
|
||||||
/// Indicate that a struct field has been skipped.
|
/// Indicate that a struct field has been skipped.
|
||||||
|
///
|
||||||
|
/// The default implementation does nothing.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn skip_field(&mut self, key: &'static str) -> Result<(), Self::Error> {
|
fn skip_field(&mut self, key: &'static str) -> Result<(), Self::Error> {
|
||||||
let _ = key;
|
let _ = key;
|
||||||
@@ -1946,6 +1968,12 @@ pub trait SerializeStruct {
|
|||||||
/// implementation of `SerializeStructVariant` for a basic JSON data format.
|
/// implementation of `SerializeStructVariant` for a basic JSON data format.
|
||||||
///
|
///
|
||||||
/// [example data format]: https://serde.rs/data-format.html
|
/// [example data format]: https://serde.rs/data-format.html
|
||||||
|
#[cfg_attr(
|
||||||
|
not(no_diagnostic_namespace),
|
||||||
|
diagnostic::on_unimplemented(
|
||||||
|
message = "the trait bound `{Self}: serde::ser::SerializeStructVariant` is not satisfied",
|
||||||
|
)
|
||||||
|
)]
|
||||||
pub trait SerializeStructVariant {
|
pub trait SerializeStructVariant {
|
||||||
/// Must match the `Ok` type of our `Serializer`.
|
/// Must match the `Ok` type of our `Serializer`.
|
||||||
type Ok;
|
type Ok;
|
||||||
@@ -1954,15 +1982,13 @@ pub trait SerializeStructVariant {
|
|||||||
type Error: Error;
|
type Error: Error;
|
||||||
|
|
||||||
/// Serialize a struct variant field.
|
/// Serialize a struct variant field.
|
||||||
fn serialize_field<T: ?Sized>(
|
fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error>
|
||||||
&mut self,
|
|
||||||
key: &'static str,
|
|
||||||
value: &T,
|
|
||||||
) -> Result<(), Self::Error>
|
|
||||||
where
|
where
|
||||||
T: Serialize;
|
T: ?Sized + Serialize;
|
||||||
|
|
||||||
/// Indicate that a struct variant field has been skipped.
|
/// Indicate that a struct variant field has been skipped.
|
||||||
|
///
|
||||||
|
/// The default implementation does nothing.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn skip_field(&mut self, key: &'static str) -> Result<(), Self::Error> {
|
fn skip_field(&mut self, key: &'static str) -> Result<(), Self::Error> {
|
||||||
let _ = key;
|
let _ = key;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
use lib::{Debug, Display};
|
use crate::lib::{Debug, Display};
|
||||||
|
|
||||||
/// Either a re-export of std::error::Error or a new identical trait, depending
|
/// Either a re-export of std::error::Error or a new identical trait, depending
|
||||||
/// on whether Serde's "std" feature is enabled.
|
/// on whether Serde's "std" feature is enabled.
|
||||||
@@ -42,7 +42,7 @@ use lib::{Debug, Display};
|
|||||||
/// ```
|
/// ```
|
||||||
pub trait Error: Debug + Display {
|
pub trait Error: Debug + Display {
|
||||||
/// The underlying cause of this error, if any.
|
/// The underlying cause of this error, if any.
|
||||||
fn source(&self) -> Option<&(Error + 'static)> {
|
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+14
-6
@@ -1,16 +1,18 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde_derive"
|
name = "serde_derive"
|
||||||
version = "1.0.178" # remember to update html_root_url
|
version = "1.0.221"
|
||||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
||||||
categories = ["no-std", "no-std::no-alloc"]
|
categories = ["no-std", "no-std::no-alloc"]
|
||||||
description = "Macros 1.1 implementation of #[derive(Serialize, Deserialize)]"
|
description = "Macros 1.1 implementation of #[derive(Serialize, Deserialize)]"
|
||||||
documentation = "https://serde.rs/derive.html"
|
documentation = "https://serde.rs/derive.html"
|
||||||
|
edition = "2021"
|
||||||
|
exclude = ["build.rs"]
|
||||||
homepage = "https://serde.rs"
|
homepage = "https://serde.rs"
|
||||||
keywords = ["serde", "serialization", "no_std", "derive"]
|
keywords = ["serde", "serialization", "no_std", "derive"]
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
readme = "crates-io.md"
|
readme = "crates-io.md"
|
||||||
repository = "https://github.com/serde-rs/serde"
|
repository = "https://github.com/serde-rs/serde"
|
||||||
rust-version = "1.56"
|
rust-version = "1.61"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = []
|
||||||
@@ -21,13 +23,19 @@ name = "serde_derive"
|
|||||||
proc-macro = true
|
proc-macro = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
proc-macro2 = "1.0"
|
proc-macro2 = { workspace = true, features = ["proc-macro"] }
|
||||||
quote = "1.0"
|
quote = { workspace = true, features = ["proc-macro"] }
|
||||||
syn = "2.0.25"
|
syn = { workspace = true, features = ["clone-impls", "derive", "parsing", "printing", "proc-macro"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
serde = { version = "1", path = "../serde" }
|
serde = { version = "1", path = "../serde" }
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
targets = ["x86_64-unknown-linux-gnu"]
|
targets = ["x86_64-unknown-linux-gnu"]
|
||||||
rustdoc-args = ["--generate-link-to-definition"]
|
rustdoc-args = [
|
||||||
|
"--generate-link-to-definition",
|
||||||
|
"--extern-html-root-url=core=https://doc.rust-lang.org",
|
||||||
|
"--extern-html-root-url=alloc=https://doc.rust-lang.org",
|
||||||
|
"--extern-html-root-url=std=https://doc.rust-lang.org",
|
||||||
|
"--extern-html-root-url=proc_macro=https://doc.rust-lang.org",
|
||||||
|
]
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
fn main() {
|
||||||
|
// Warning: build.rs is not published to crates.io.
|
||||||
|
|
||||||
|
println!("cargo:rerun-if-changed=build.rs");
|
||||||
|
println!("cargo:rustc-cfg=check_cfg");
|
||||||
|
println!("cargo:rustc-check-cfg=cfg(check_cfg)");
|
||||||
|
println!("cargo:rustc-check-cfg=cfg(exhaustive)");
|
||||||
|
}
|
||||||
@@ -144,6 +144,7 @@ pub fn with_bound(
|
|||||||
|
|
||||||
fn visit_type(&mut self, ty: &'ast syn::Type) {
|
fn visit_type(&mut self, ty: &'ast syn::Type) {
|
||||||
match ty {
|
match ty {
|
||||||
|
#![cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]
|
||||||
syn::Type::Array(ty) => self.visit_type(&ty.elem),
|
syn::Type::Array(ty) => self.visit_type(&ty.elem),
|
||||||
syn::Type::BareFn(ty) => {
|
syn::Type::BareFn(ty) => {
|
||||||
for arg in &ty.inputs {
|
for arg in &ty.inputs {
|
||||||
@@ -181,7 +182,6 @@ pub fn with_bound(
|
|||||||
|
|
||||||
syn::Type::Infer(_) | syn::Type::Never(_) | syn::Type::Verbatim(_) => {}
|
syn::Type::Infer(_) | syn::Type::Never(_) | syn::Type::Verbatim(_) => {}
|
||||||
|
|
||||||
#[cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]
|
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -196,16 +196,13 @@ pub fn with_bound(
|
|||||||
syn::PathArguments::AngleBracketed(arguments) => {
|
syn::PathArguments::AngleBracketed(arguments) => {
|
||||||
for arg in &arguments.args {
|
for arg in &arguments.args {
|
||||||
match arg {
|
match arg {
|
||||||
|
#![cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]
|
||||||
syn::GenericArgument::Type(arg) => self.visit_type(arg),
|
syn::GenericArgument::Type(arg) => self.visit_type(arg),
|
||||||
syn::GenericArgument::AssocType(arg) => self.visit_type(&arg.ty),
|
syn::GenericArgument::AssocType(arg) => self.visit_type(&arg.ty),
|
||||||
syn::GenericArgument::Lifetime(_)
|
syn::GenericArgument::Lifetime(_)
|
||||||
| syn::GenericArgument::Const(_)
|
| syn::GenericArgument::Const(_)
|
||||||
| syn::GenericArgument::AssocConst(_)
|
| syn::GenericArgument::AssocConst(_)
|
||||||
| syn::GenericArgument::Constraint(_) => {}
|
| syn::GenericArgument::Constraint(_) => {}
|
||||||
#[cfg_attr(
|
|
||||||
all(test, exhaustive),
|
|
||||||
deny(non_exhaustive_omitted_patterns)
|
|
||||||
)]
|
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -228,9 +225,11 @@ pub fn with_bound(
|
|||||||
|
|
||||||
fn visit_type_param_bound(&mut self, bound: &'ast syn::TypeParamBound) {
|
fn visit_type_param_bound(&mut self, bound: &'ast syn::TypeParamBound) {
|
||||||
match bound {
|
match bound {
|
||||||
|
#![cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]
|
||||||
syn::TypeParamBound::Trait(bound) => self.visit_path(&bound.path),
|
syn::TypeParamBound::Trait(bound) => self.visit_path(&bound.path),
|
||||||
syn::TypeParamBound::Lifetime(_) | syn::TypeParamBound::Verbatim(_) => {}
|
syn::TypeParamBound::Lifetime(_)
|
||||||
#[cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]
|
| syn::TypeParamBound::PreciseCapture(_)
|
||||||
|
| syn::TypeParamBound::Verbatim(_) => {}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+361
-312
File diff suppressed because it is too large
Load Diff
@@ -14,9 +14,17 @@ pub fn wrap_in_const(serde_path: Option<&syn::Path>, code: TokenStream) -> Token
|
|||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
|
#[allow(
|
||||||
|
non_upper_case_globals,
|
||||||
|
unused_attributes,
|
||||||
|
unused_qualifications,
|
||||||
|
clippy::absolute_paths,
|
||||||
|
)]
|
||||||
const _: () = {
|
const _: () = {
|
||||||
#use_serde
|
#use_serde
|
||||||
|
|
||||||
|
_serde::__require_serde_not_serde_core!();
|
||||||
|
|
||||||
#code
|
#code
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ impl<'a> Container<'a> {
|
|||||||
item: &'a syn::DeriveInput,
|
item: &'a syn::DeriveInput,
|
||||||
derive: Derive,
|
derive: Derive,
|
||||||
) -> Option<Container<'a>> {
|
) -> Option<Container<'a>> {
|
||||||
let mut attrs = attr::Container::from_ast(cx, item);
|
let attrs = attr::Container::from_ast(cx, item);
|
||||||
|
|
||||||
let mut data = match &item.data {
|
let mut data = match &item.data {
|
||||||
syn::Data::Enum(data) => Data::Enum(enum_from_ast(cx, &data.variants, attrs.default())),
|
syn::Data::Enum(data) => Data::Enum(enum_from_ast(cx, &data.variants, attrs.default())),
|
||||||
@@ -77,15 +77,11 @@ impl<'a> Container<'a> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut has_flatten = false;
|
|
||||||
match &mut data {
|
match &mut data {
|
||||||
Data::Enum(variants) => {
|
Data::Enum(variants) => {
|
||||||
for variant in variants {
|
for variant in variants {
|
||||||
variant.attrs.rename_by_rules(attrs.rename_all_rules());
|
variant.attrs.rename_by_rules(attrs.rename_all_rules());
|
||||||
for field in &mut variant.fields {
|
for field in &mut variant.fields {
|
||||||
if field.attrs.flatten() {
|
|
||||||
has_flatten = true;
|
|
||||||
}
|
|
||||||
field.attrs.rename_by_rules(
|
field.attrs.rename_by_rules(
|
||||||
variant
|
variant
|
||||||
.attrs
|
.attrs
|
||||||
@@ -97,18 +93,11 @@ impl<'a> Container<'a> {
|
|||||||
}
|
}
|
||||||
Data::Struct(_, fields) => {
|
Data::Struct(_, fields) => {
|
||||||
for field in fields {
|
for field in fields {
|
||||||
if field.attrs.flatten() {
|
|
||||||
has_flatten = true;
|
|
||||||
}
|
|
||||||
field.attrs.rename_by_rules(attrs.rename_all_rules());
|
field.attrs.rename_by_rules(attrs.rename_all_rules());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if has_flatten {
|
|
||||||
attrs.mark_has_flatten();
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut item = Container {
|
let mut item = Container {
|
||||||
ident: item.ident.clone(),
|
ident: item.ident.clone(),
|
||||||
attrs,
|
attrs,
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
use crate::internals::name::{MultiName, Name};
|
||||||
use crate::internals::symbol::*;
|
use crate::internals::symbol::*;
|
||||||
use crate::internals::{ungroup, Ctxt};
|
use crate::internals::{ungroup, Ctxt};
|
||||||
use proc_macro2::{Spacing, Span, TokenStream, TokenTree};
|
use proc_macro2::{Spacing, Span, TokenStream, TokenTree};
|
||||||
@@ -8,6 +9,7 @@ use std::iter::FromIterator;
|
|||||||
use syn::meta::ParseNestedMeta;
|
use syn::meta::ParseNestedMeta;
|
||||||
use syn::parse::ParseStream;
|
use syn::parse::ParseStream;
|
||||||
use syn::punctuated::Punctuated;
|
use syn::punctuated::Punctuated;
|
||||||
|
use syn::spanned::Spanned;
|
||||||
use syn::{parse_quote, token, Ident, Lifetime, Token};
|
use syn::{parse_quote, token, Ident, Lifetime, Token};
|
||||||
|
|
||||||
// This module handles parsing of `#[serde(...)]` attributes. The entrypoints
|
// This module handles parsing of `#[serde(...)]` attributes. The entrypoints
|
||||||
@@ -20,7 +22,7 @@ use syn::{parse_quote, token, Ident, Lifetime, Token};
|
|||||||
|
|
||||||
pub use crate::internals::case::RenameRule;
|
pub use crate::internals::case::RenameRule;
|
||||||
|
|
||||||
struct Attr<'c, T> {
|
pub(crate) struct Attr<'c, T> {
|
||||||
cx: &'c Ctxt,
|
cx: &'c Ctxt,
|
||||||
name: Symbol,
|
name: Symbol,
|
||||||
tokens: TokenStream,
|
tokens: TokenStream,
|
||||||
@@ -61,7 +63,7 @@ impl<'c, T> Attr<'c, T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get(self) -> Option<T> {
|
pub(crate) fn get(self) -> Option<T> {
|
||||||
self.value
|
self.value
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,7 +91,7 @@ impl<'c> BoolAttr<'c> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct VecAttr<'c, T> {
|
pub(crate) struct VecAttr<'c, T> {
|
||||||
cx: &'c Ctxt,
|
cx: &'c Ctxt,
|
||||||
name: Symbol,
|
name: Symbol,
|
||||||
first_dup_tokens: TokenStream,
|
first_dup_tokens: TokenStream,
|
||||||
@@ -124,78 +126,19 @@ impl<'c, T> VecAttr<'c, T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get(self) -> Vec<T> {
|
pub(crate) fn get(self) -> Vec<T> {
|
||||||
self.values
|
self.values
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Name {
|
fn unraw(ident: &Ident) -> Ident {
|
||||||
serialize: String,
|
Ident::new(ident.to_string().trim_start_matches("r#"), ident.span())
|
||||||
serialize_renamed: bool,
|
|
||||||
deserialize: String,
|
|
||||||
deserialize_renamed: bool,
|
|
||||||
deserialize_aliases: Vec<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn unraw(ident: &Ident) -> String {
|
|
||||||
ident.to_string().trim_start_matches("r#").to_owned()
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Name {
|
|
||||||
fn from_attrs(
|
|
||||||
source_name: String,
|
|
||||||
ser_name: Attr<String>,
|
|
||||||
de_name: Attr<String>,
|
|
||||||
de_aliases: Option<VecAttr<String>>,
|
|
||||||
) -> Name {
|
|
||||||
let deserialize_aliases = match de_aliases {
|
|
||||||
Some(de_aliases) => {
|
|
||||||
let mut alias_list = BTreeSet::new();
|
|
||||||
for alias_name in de_aliases.get() {
|
|
||||||
alias_list.insert(alias_name);
|
|
||||||
}
|
|
||||||
alias_list.into_iter().collect()
|
|
||||||
}
|
|
||||||
None => Vec::new(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let ser_name = ser_name.get();
|
|
||||||
let ser_renamed = ser_name.is_some();
|
|
||||||
let de_name = de_name.get();
|
|
||||||
let de_renamed = de_name.is_some();
|
|
||||||
Name {
|
|
||||||
serialize: ser_name.unwrap_or_else(|| source_name.clone()),
|
|
||||||
serialize_renamed: ser_renamed,
|
|
||||||
deserialize: de_name.unwrap_or(source_name),
|
|
||||||
deserialize_renamed: de_renamed,
|
|
||||||
deserialize_aliases,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Return the container name for the container when serializing.
|
|
||||||
pub fn serialize_name(&self) -> String {
|
|
||||||
self.serialize.clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Return the container name for the container when deserializing.
|
|
||||||
pub fn deserialize_name(&self) -> String {
|
|
||||||
self.deserialize.clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn deserialize_aliases(&self) -> Vec<String> {
|
|
||||||
let mut aliases = self.deserialize_aliases.clone();
|
|
||||||
let main_name = self.deserialize_name();
|
|
||||||
if !aliases.contains(&main_name) {
|
|
||||||
aliases.push(main_name);
|
|
||||||
}
|
|
||||||
aliases
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
pub struct RenameAllRules {
|
pub struct RenameAllRules {
|
||||||
serialize: RenameRule,
|
pub serialize: RenameRule,
|
||||||
deserialize: RenameRule,
|
pub deserialize: RenameRule,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RenameAllRules {
|
impl RenameAllRules {
|
||||||
@@ -211,7 +154,7 @@ impl RenameAllRules {
|
|||||||
|
|
||||||
/// Represents struct or enum attribute information.
|
/// Represents struct or enum attribute information.
|
||||||
pub struct Container {
|
pub struct Container {
|
||||||
name: Name,
|
name: MultiName,
|
||||||
transparent: bool,
|
transparent: bool,
|
||||||
deny_unknown_fields: bool,
|
deny_unknown_fields: bool,
|
||||||
default: Default,
|
default: Default,
|
||||||
@@ -225,11 +168,11 @@ pub struct Container {
|
|||||||
type_into: Option<syn::Type>,
|
type_into: Option<syn::Type>,
|
||||||
remote: Option<syn::Path>,
|
remote: Option<syn::Path>,
|
||||||
identifier: Identifier,
|
identifier: Identifier,
|
||||||
has_flatten: bool,
|
|
||||||
serde_path: Option<syn::Path>,
|
serde_path: Option<syn::Path>,
|
||||||
is_packed: bool,
|
is_packed: bool,
|
||||||
/// Error message generated when type can't be deserialized
|
/// Error message generated when type can't be deserialized
|
||||||
expecting: Option<String>,
|
expecting: Option<String>,
|
||||||
|
non_exhaustive: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Styles of representing an enum.
|
/// Styles of representing an enum.
|
||||||
@@ -315,9 +258,12 @@ impl Container {
|
|||||||
let mut variant_identifier = BoolAttr::none(cx, VARIANT_IDENTIFIER);
|
let mut variant_identifier = BoolAttr::none(cx, VARIANT_IDENTIFIER);
|
||||||
let mut serde_path = Attr::none(cx, CRATE);
|
let mut serde_path = Attr::none(cx, CRATE);
|
||||||
let mut expecting = Attr::none(cx, EXPECTING);
|
let mut expecting = Attr::none(cx, EXPECTING);
|
||||||
|
let mut non_exhaustive = false;
|
||||||
|
|
||||||
for attr in &item.attrs {
|
for attr in &item.attrs {
|
||||||
if attr.path() != SERDE {
|
if attr.path() != SERDE {
|
||||||
|
non_exhaustive |=
|
||||||
|
matches!(&attr.meta, syn::Meta::Path(path) if path == NON_EXHAUSTIVE);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -332,8 +278,8 @@ impl Container {
|
|||||||
// #[serde(rename = "foo")]
|
// #[serde(rename = "foo")]
|
||||||
// #[serde(rename(serialize = "foo", deserialize = "bar"))]
|
// #[serde(rename(serialize = "foo", deserialize = "bar"))]
|
||||||
let (ser, de) = get_renames(cx, RENAME, &meta)?;
|
let (ser, de) = get_renames(cx, RENAME, &meta)?;
|
||||||
ser_name.set_opt(&meta.path, ser.as_ref().map(syn::LitStr::value));
|
ser_name.set_opt(&meta.path, ser.as_ref().map(Name::from));
|
||||||
de_name.set_opt(&meta.path, de.as_ref().map(syn::LitStr::value));
|
de_name.set_opt(&meta.path, de.as_ref().map(Name::from));
|
||||||
} else if meta.path == RENAME_ALL {
|
} else if meta.path == RENAME_ALL {
|
||||||
// #[serde(rename_all = "foo")]
|
// #[serde(rename_all = "foo")]
|
||||||
// #[serde(rename_all(serialize = "foo", deserialize = "bar"))]
|
// #[serde(rename_all(serialize = "foo", deserialize = "bar"))]
|
||||||
@@ -405,20 +351,20 @@ impl Container {
|
|||||||
if let Some(path) = parse_lit_into_expr_path(cx, DEFAULT, &meta)? {
|
if let Some(path) = parse_lit_into_expr_path(cx, DEFAULT, &meta)? {
|
||||||
match &item.data {
|
match &item.data {
|
||||||
syn::Data::Struct(syn::DataStruct { fields, .. }) => match fields {
|
syn::Data::Struct(syn::DataStruct { fields, .. }) => match fields {
|
||||||
syn::Fields::Named(_) => {
|
syn::Fields::Named(_) | syn::Fields::Unnamed(_) => {
|
||||||
default.set(&meta.path, Default::Path(path));
|
default.set(&meta.path, Default::Path(path));
|
||||||
}
|
}
|
||||||
syn::Fields::Unnamed(_) | syn::Fields::Unit => {
|
syn::Fields::Unit => {
|
||||||
let msg = "#[serde(default = \"...\")] can only be used on structs with named fields";
|
let msg = "#[serde(default = \"...\")] can only be used on structs that have fields";
|
||||||
cx.syn_error(meta.error(msg));
|
cx.syn_error(meta.error(msg));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
syn::Data::Enum(_) => {
|
syn::Data::Enum(_) => {
|
||||||
let msg = "#[serde(default = \"...\")] can only be used on structs with named fields";
|
let msg = "#[serde(default = \"...\")] can only be used on structs";
|
||||||
cx.syn_error(meta.error(msg));
|
cx.syn_error(meta.error(msg));
|
||||||
}
|
}
|
||||||
syn::Data::Union(_) => {
|
syn::Data::Union(_) => {
|
||||||
let msg = "#[serde(default = \"...\")] can only be used on structs with named fields";
|
let msg = "#[serde(default = \"...\")] can only be used on structs";
|
||||||
cx.syn_error(meta.error(msg));
|
cx.syn_error(meta.error(msg));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -427,20 +373,20 @@ impl Container {
|
|||||||
// #[serde(default)]
|
// #[serde(default)]
|
||||||
match &item.data {
|
match &item.data {
|
||||||
syn::Data::Struct(syn::DataStruct { fields, .. }) => match fields {
|
syn::Data::Struct(syn::DataStruct { fields, .. }) => match fields {
|
||||||
syn::Fields::Named(_) => {
|
syn::Fields::Named(_) | syn::Fields::Unnamed(_) => {
|
||||||
default.set(meta.path, Default::Default);
|
default.set(meta.path, Default::Default);
|
||||||
}
|
}
|
||||||
syn::Fields::Unnamed(_) | syn::Fields::Unit => {
|
syn::Fields::Unit => {
|
||||||
let msg = "#[serde(default)] can only be used on structs with named fields";
|
let msg = "#[serde(default)] can only be used on structs that have fields";
|
||||||
cx.error_spanned_by(fields, msg);
|
cx.error_spanned_by(fields, msg);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
syn::Data::Enum(_) => {
|
syn::Data::Enum(_) => {
|
||||||
let msg = "#[serde(default)] can only be used on structs with named fields";
|
let msg = "#[serde(default)] can only be used on structs";
|
||||||
cx.syn_error(meta.error(msg));
|
cx.syn_error(meta.error(msg));
|
||||||
}
|
}
|
||||||
syn::Data::Union(_) => {
|
syn::Data::Union(_) => {
|
||||||
let msg = "#[serde(default)] can only be used on structs with named fields";
|
let msg = "#[serde(default)] can only be used on structs";
|
||||||
cx.syn_error(meta.error(msg));
|
cx.syn_error(meta.error(msg));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -572,7 +518,7 @@ impl Container {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Container {
|
Container {
|
||||||
name: Name::from_attrs(unraw(&item.ident), ser_name, de_name, None),
|
name: MultiName::from_attrs(Name::from(&unraw(&item.ident)), ser_name, de_name, None),
|
||||||
transparent: transparent.get(),
|
transparent: transparent.get(),
|
||||||
deny_unknown_fields: deny_unknown_fields.get(),
|
deny_unknown_fields: deny_unknown_fields.get(),
|
||||||
default: default.get().unwrap_or(Default::None),
|
default: default.get().unwrap_or(Default::None),
|
||||||
@@ -592,14 +538,14 @@ impl Container {
|
|||||||
type_into: type_into.get(),
|
type_into: type_into.get(),
|
||||||
remote: remote.get(),
|
remote: remote.get(),
|
||||||
identifier: decide_identifier(cx, item, field_identifier, variant_identifier),
|
identifier: decide_identifier(cx, item, field_identifier, variant_identifier),
|
||||||
has_flatten: false,
|
|
||||||
serde_path: serde_path.get(),
|
serde_path: serde_path.get(),
|
||||||
is_packed,
|
is_packed,
|
||||||
expecting: expecting.get(),
|
expecting: expecting.get(),
|
||||||
|
non_exhaustive,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn name(&self) -> &Name {
|
pub fn name(&self) -> &MultiName {
|
||||||
&self.name
|
&self.name
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -659,14 +605,6 @@ impl Container {
|
|||||||
self.identifier
|
self.identifier
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn has_flatten(&self) -> bool {
|
|
||||||
self.has_flatten
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn mark_has_flatten(&mut self) {
|
|
||||||
self.has_flatten = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn custom_serde_path(&self) -> Option<&syn::Path> {
|
pub fn custom_serde_path(&self) -> Option<&syn::Path> {
|
||||||
self.serde_path.as_ref()
|
self.serde_path.as_ref()
|
||||||
}
|
}
|
||||||
@@ -681,6 +619,10 @@ impl Container {
|
|||||||
pub fn expecting(&self) -> Option<&str> {
|
pub fn expecting(&self) -> Option<&str> {
|
||||||
self.expecting.as_ref().map(String::as_ref)
|
self.expecting.as_ref().map(String::as_ref)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn non_exhaustive(&self) -> bool {
|
||||||
|
self.non_exhaustive
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn decide_tag(
|
fn decide_tag(
|
||||||
@@ -716,7 +658,7 @@ fn decide_tag(
|
|||||||
}
|
}
|
||||||
TagType::Internal { tag }
|
TagType::Internal { tag }
|
||||||
}
|
}
|
||||||
(Some((untagged_tokens, _)), Some((tag_tokens, _)), None) => {
|
(Some((untagged_tokens, ())), Some((tag_tokens, _)), None) => {
|
||||||
let msg = "enum cannot be both untagged and internally tagged";
|
let msg = "enum cannot be both untagged and internally tagged";
|
||||||
cx.error_spanned_by(untagged_tokens, msg);
|
cx.error_spanned_by(untagged_tokens, msg);
|
||||||
cx.error_spanned_by(tag_tokens, msg);
|
cx.error_spanned_by(tag_tokens, msg);
|
||||||
@@ -727,14 +669,14 @@ fn decide_tag(
|
|||||||
cx.error_spanned_by(content_tokens, msg);
|
cx.error_spanned_by(content_tokens, msg);
|
||||||
TagType::External
|
TagType::External
|
||||||
}
|
}
|
||||||
(Some((untagged_tokens, _)), None, Some((content_tokens, _))) => {
|
(Some((untagged_tokens, ())), None, Some((content_tokens, _))) => {
|
||||||
let msg = "untagged enum cannot have #[serde(content = \"...\")]";
|
let msg = "untagged enum cannot have #[serde(content = \"...\")]";
|
||||||
cx.error_spanned_by(untagged_tokens, msg);
|
cx.error_spanned_by(untagged_tokens, msg);
|
||||||
cx.error_spanned_by(content_tokens, msg);
|
cx.error_spanned_by(content_tokens, msg);
|
||||||
TagType::External
|
TagType::External
|
||||||
}
|
}
|
||||||
(None, Some((_, tag)), Some((_, content))) => TagType::Adjacent { tag, content },
|
(None, Some((_, tag)), Some((_, content))) => TagType::Adjacent { tag, content },
|
||||||
(Some((untagged_tokens, _)), Some((tag_tokens, _)), Some((content_tokens, _))) => {
|
(Some((untagged_tokens, ())), Some((tag_tokens, _)), Some((content_tokens, _))) => {
|
||||||
let msg = "untagged enum cannot have #[serde(tag = \"...\", content = \"...\")]";
|
let msg = "untagged enum cannot have #[serde(tag = \"...\", content = \"...\")]";
|
||||||
cx.error_spanned_by(untagged_tokens, msg);
|
cx.error_spanned_by(untagged_tokens, msg);
|
||||||
cx.error_spanned_by(tag_tokens, msg);
|
cx.error_spanned_by(tag_tokens, msg);
|
||||||
@@ -756,7 +698,7 @@ fn decide_identifier(
|
|||||||
variant_identifier.0.get_with_tokens(),
|
variant_identifier.0.get_with_tokens(),
|
||||||
) {
|
) {
|
||||||
(_, None, None) => Identifier::No,
|
(_, None, None) => Identifier::No,
|
||||||
(_, Some((field_identifier_tokens, _)), Some((variant_identifier_tokens, _))) => {
|
(_, Some((field_identifier_tokens, ())), Some((variant_identifier_tokens, ()))) => {
|
||||||
let msg =
|
let msg =
|
||||||
"#[serde(field_identifier)] and #[serde(variant_identifier)] cannot both be set";
|
"#[serde(field_identifier)] and #[serde(variant_identifier)] cannot both be set";
|
||||||
cx.error_spanned_by(field_identifier_tokens, msg);
|
cx.error_spanned_by(field_identifier_tokens, msg);
|
||||||
@@ -790,7 +732,7 @@ fn decide_identifier(
|
|||||||
|
|
||||||
/// Represents variant attribute information
|
/// Represents variant attribute information
|
||||||
pub struct Variant {
|
pub struct Variant {
|
||||||
name: Name,
|
name: MultiName,
|
||||||
rename_all_rules: RenameAllRules,
|
rename_all_rules: RenameAllRules,
|
||||||
ser_bound: Option<Vec<syn::WherePredicate>>,
|
ser_bound: Option<Vec<syn::WherePredicate>>,
|
||||||
de_bound: Option<Vec<syn::WherePredicate>>,
|
de_bound: Option<Vec<syn::WherePredicate>>,
|
||||||
@@ -841,15 +783,15 @@ impl Variant {
|
|||||||
// #[serde(rename = "foo")]
|
// #[serde(rename = "foo")]
|
||||||
// #[serde(rename(serialize = "foo", deserialize = "bar"))]
|
// #[serde(rename(serialize = "foo", deserialize = "bar"))]
|
||||||
let (ser, de) = get_multiple_renames(cx, &meta)?;
|
let (ser, de) = get_multiple_renames(cx, &meta)?;
|
||||||
ser_name.set_opt(&meta.path, ser.as_ref().map(syn::LitStr::value));
|
ser_name.set_opt(&meta.path, ser.as_ref().map(Name::from));
|
||||||
for de_value in de {
|
for de_value in de {
|
||||||
de_name.set_if_none(de_value.value());
|
de_name.set_if_none(Name::from(&de_value));
|
||||||
de_aliases.insert(&meta.path, de_value.value());
|
de_aliases.insert(&meta.path, Name::from(&de_value));
|
||||||
}
|
}
|
||||||
} else if meta.path == ALIAS {
|
} else if meta.path == ALIAS {
|
||||||
// #[serde(alias = "foo")]
|
// #[serde(alias = "foo")]
|
||||||
if let Some(s) = get_lit_str(cx, ALIAS, &meta)? {
|
if let Some(s) = get_lit_str(cx, ALIAS, &meta)? {
|
||||||
de_aliases.insert(&meta.path, s.value());
|
de_aliases.insert(&meta.path, Name::from(&s));
|
||||||
}
|
}
|
||||||
} else if meta.path == RENAME_ALL {
|
} else if meta.path == RENAME_ALL {
|
||||||
// #[serde(rename_all = "foo")]
|
// #[serde(rename_all = "foo")]
|
||||||
@@ -898,13 +840,13 @@ impl Variant {
|
|||||||
ser_path
|
ser_path
|
||||||
.path
|
.path
|
||||||
.segments
|
.segments
|
||||||
.push(Ident::new("serialize", Span::call_site()).into());
|
.push(Ident::new("serialize", ser_path.span()).into());
|
||||||
serialize_with.set(&meta.path, ser_path);
|
serialize_with.set(&meta.path, ser_path);
|
||||||
let mut de_path = path;
|
let mut de_path = path;
|
||||||
de_path
|
de_path
|
||||||
.path
|
.path
|
||||||
.segments
|
.segments
|
||||||
.push(Ident::new("deserialize", Span::call_site()).into());
|
.push(Ident::new("deserialize", de_path.span()).into());
|
||||||
deserialize_with.set(&meta.path, de_path);
|
deserialize_with.set(&meta.path, de_path);
|
||||||
}
|
}
|
||||||
} else if meta.path == SERIALIZE_WITH {
|
} else if meta.path == SERIALIZE_WITH {
|
||||||
@@ -956,7 +898,12 @@ impl Variant {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Variant {
|
Variant {
|
||||||
name: Name::from_attrs(unraw(&variant.ident), ser_name, de_name, Some(de_aliases)),
|
name: MultiName::from_attrs(
|
||||||
|
Name::from(&unraw(&variant.ident)),
|
||||||
|
ser_name,
|
||||||
|
de_name,
|
||||||
|
Some(de_aliases),
|
||||||
|
),
|
||||||
rename_all_rules: RenameAllRules {
|
rename_all_rules: RenameAllRules {
|
||||||
serialize: rename_all_ser_rule.get().unwrap_or(RenameRule::None),
|
serialize: rename_all_ser_rule.get().unwrap_or(RenameRule::None),
|
||||||
deserialize: rename_all_de_rule.get().unwrap_or(RenameRule::None),
|
deserialize: rename_all_de_rule.get().unwrap_or(RenameRule::None),
|
||||||
@@ -973,21 +920,27 @@ impl Variant {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn name(&self) -> &Name {
|
pub fn name(&self) -> &MultiName {
|
||||||
&self.name
|
&self.name
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn aliases(&self) -> Vec<String> {
|
pub fn aliases(&self) -> &BTreeSet<Name> {
|
||||||
self.name.deserialize_aliases()
|
self.name.deserialize_aliases()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rename_by_rules(&mut self, rules: RenameAllRules) {
|
pub fn rename_by_rules(&mut self, rules: RenameAllRules) {
|
||||||
if !self.name.serialize_renamed {
|
if !self.name.serialize_renamed {
|
||||||
self.name.serialize = rules.serialize.apply_to_variant(&self.name.serialize);
|
self.name.serialize.value =
|
||||||
|
rules.serialize.apply_to_variant(&self.name.serialize.value);
|
||||||
}
|
}
|
||||||
if !self.name.deserialize_renamed {
|
if !self.name.deserialize_renamed {
|
||||||
self.name.deserialize = rules.deserialize.apply_to_variant(&self.name.deserialize);
|
self.name.deserialize.value = rules
|
||||||
|
.deserialize
|
||||||
|
.apply_to_variant(&self.name.deserialize.value);
|
||||||
}
|
}
|
||||||
|
self.name
|
||||||
|
.deserialize_aliases
|
||||||
|
.insert(self.name.deserialize.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rename_all_rules(&self) -> RenameAllRules {
|
pub fn rename_all_rules(&self) -> RenameAllRules {
|
||||||
@@ -1029,7 +982,7 @@ impl Variant {
|
|||||||
|
|
||||||
/// Represents field attribute information
|
/// Represents field attribute information
|
||||||
pub struct Field {
|
pub struct Field {
|
||||||
name: Name,
|
name: MultiName,
|
||||||
skip_serializing: bool,
|
skip_serializing: bool,
|
||||||
skip_deserializing: bool,
|
skip_deserializing: bool,
|
||||||
skip_serializing_if: Option<syn::ExprPath>,
|
skip_serializing_if: Option<syn::ExprPath>,
|
||||||
@@ -1088,8 +1041,11 @@ impl Field {
|
|||||||
let mut flatten = BoolAttr::none(cx, FLATTEN);
|
let mut flatten = BoolAttr::none(cx, FLATTEN);
|
||||||
|
|
||||||
let ident = match &field.ident {
|
let ident = match &field.ident {
|
||||||
Some(ident) => unraw(ident),
|
Some(ident) => Name::from(&unraw(ident)),
|
||||||
None => index.to_string(),
|
None => Name {
|
||||||
|
value: index.to_string(),
|
||||||
|
span: Span::call_site(),
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(borrow_attribute) = attrs.and_then(|variant| variant.borrow.as_ref()) {
|
if let Some(borrow_attribute) = attrs.and_then(|variant| variant.borrow.as_ref()) {
|
||||||
@@ -1125,15 +1081,15 @@ impl Field {
|
|||||||
// #[serde(rename = "foo")]
|
// #[serde(rename = "foo")]
|
||||||
// #[serde(rename(serialize = "foo", deserialize = "bar"))]
|
// #[serde(rename(serialize = "foo", deserialize = "bar"))]
|
||||||
let (ser, de) = get_multiple_renames(cx, &meta)?;
|
let (ser, de) = get_multiple_renames(cx, &meta)?;
|
||||||
ser_name.set_opt(&meta.path, ser.as_ref().map(syn::LitStr::value));
|
ser_name.set_opt(&meta.path, ser.as_ref().map(Name::from));
|
||||||
for de_value in de {
|
for de_value in de {
|
||||||
de_name.set_if_none(de_value.value());
|
de_name.set_if_none(Name::from(&de_value));
|
||||||
de_aliases.insert(&meta.path, de_value.value());
|
de_aliases.insert(&meta.path, Name::from(&de_value));
|
||||||
}
|
}
|
||||||
} else if meta.path == ALIAS {
|
} else if meta.path == ALIAS {
|
||||||
// #[serde(alias = "foo")]
|
// #[serde(alias = "foo")]
|
||||||
if let Some(s) = get_lit_str(cx, ALIAS, &meta)? {
|
if let Some(s) = get_lit_str(cx, ALIAS, &meta)? {
|
||||||
de_aliases.insert(&meta.path, s.value());
|
de_aliases.insert(&meta.path, Name::from(&s));
|
||||||
}
|
}
|
||||||
} else if meta.path == DEFAULT {
|
} else if meta.path == DEFAULT {
|
||||||
if meta.input.peek(Token![=]) {
|
if meta.input.peek(Token![=]) {
|
||||||
@@ -1177,13 +1133,13 @@ impl Field {
|
|||||||
ser_path
|
ser_path
|
||||||
.path
|
.path
|
||||||
.segments
|
.segments
|
||||||
.push(Ident::new("serialize", Span::call_site()).into());
|
.push(Ident::new("serialize", ser_path.span()).into());
|
||||||
serialize_with.set(&meta.path, ser_path);
|
serialize_with.set(&meta.path, ser_path);
|
||||||
let mut de_path = path;
|
let mut de_path = path;
|
||||||
de_path
|
de_path
|
||||||
.path
|
.path
|
||||||
.segments
|
.segments
|
||||||
.push(Ident::new("deserialize", Span::call_site()).into());
|
.push(Ident::new("deserialize", de_path.span()).into());
|
||||||
deserialize_with.set(&meta.path, de_path);
|
deserialize_with.set(&meta.path, de_path);
|
||||||
}
|
}
|
||||||
} else if meta.path == BOUND {
|
} else if meta.path == BOUND {
|
||||||
@@ -1296,7 +1252,7 @@ impl Field {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Field {
|
Field {
|
||||||
name: Name::from_attrs(ident, ser_name, de_name, Some(de_aliases)),
|
name: MultiName::from_attrs(ident, ser_name, de_name, Some(de_aliases)),
|
||||||
skip_serializing: skip_serializing.get(),
|
skip_serializing: skip_serializing.get(),
|
||||||
skip_deserializing: skip_deserializing.get(),
|
skip_deserializing: skip_deserializing.get(),
|
||||||
skip_serializing_if: skip_serializing_if.get(),
|
skip_serializing_if: skip_serializing_if.get(),
|
||||||
@@ -1312,21 +1268,26 @@ impl Field {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn name(&self) -> &Name {
|
pub fn name(&self) -> &MultiName {
|
||||||
&self.name
|
&self.name
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn aliases(&self) -> Vec<String> {
|
pub fn aliases(&self) -> &BTreeSet<Name> {
|
||||||
self.name.deserialize_aliases()
|
self.name.deserialize_aliases()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rename_by_rules(&mut self, rules: RenameAllRules) {
|
pub fn rename_by_rules(&mut self, rules: RenameAllRules) {
|
||||||
if !self.name.serialize_renamed {
|
if !self.name.serialize_renamed {
|
||||||
self.name.serialize = rules.serialize.apply_to_field(&self.name.serialize);
|
self.name.serialize.value = rules.serialize.apply_to_field(&self.name.serialize.value);
|
||||||
}
|
}
|
||||||
if !self.name.deserialize_renamed {
|
if !self.name.deserialize_renamed {
|
||||||
self.name.deserialize = rules.deserialize.apply_to_field(&self.name.deserialize);
|
self.name.deserialize.value = rules
|
||||||
|
.deserialize
|
||||||
|
.apply_to_field(&self.name.deserialize.value);
|
||||||
}
|
}
|
||||||
|
self.name
|
||||||
|
.deserialize_aliases
|
||||||
|
.insert(self.name.deserialize.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn skip_serializing(&self) -> bool {
|
pub fn skip_serializing(&self) -> bool {
|
||||||
@@ -1772,7 +1733,7 @@ fn is_primitive_path(path: &syn::Path, primitive: &str) -> bool {
|
|||||||
// attribute on the field so there must be at least one borrowable lifetime.
|
// attribute on the field so there must be at least one borrowable lifetime.
|
||||||
fn borrowable_lifetimes(
|
fn borrowable_lifetimes(
|
||||||
cx: &Ctxt,
|
cx: &Ctxt,
|
||||||
name: &str,
|
name: &Name,
|
||||||
field: &syn::Field,
|
field: &syn::Field,
|
||||||
) -> Result<BTreeSet<syn::Lifetime>, ()> {
|
) -> Result<BTreeSet<syn::Lifetime>, ()> {
|
||||||
let mut lifetimes = BTreeSet::new();
|
let mut lifetimes = BTreeSet::new();
|
||||||
@@ -1788,6 +1749,7 @@ fn borrowable_lifetimes(
|
|||||||
|
|
||||||
fn collect_lifetimes(ty: &syn::Type, out: &mut BTreeSet<syn::Lifetime>) {
|
fn collect_lifetimes(ty: &syn::Type, out: &mut BTreeSet<syn::Lifetime>) {
|
||||||
match ty {
|
match ty {
|
||||||
|
#![cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]
|
||||||
syn::Type::Slice(ty) => {
|
syn::Type::Slice(ty) => {
|
||||||
collect_lifetimes(&ty.elem, out);
|
collect_lifetimes(&ty.elem, out);
|
||||||
}
|
}
|
||||||
@@ -1823,7 +1785,10 @@ fn collect_lifetimes(ty: &syn::Type, out: &mut BTreeSet<syn::Lifetime>) {
|
|||||||
syn::GenericArgument::AssocType(binding) => {
|
syn::GenericArgument::AssocType(binding) => {
|
||||||
collect_lifetimes(&binding.ty, out);
|
collect_lifetimes(&binding.ty, out);
|
||||||
}
|
}
|
||||||
_ => {}
|
syn::GenericArgument::Const(_)
|
||||||
|
| syn::GenericArgument::AssocConst(_)
|
||||||
|
| syn::GenericArgument::Constraint(_)
|
||||||
|
| _ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1845,7 +1810,6 @@ fn collect_lifetimes(ty: &syn::Type, out: &mut BTreeSet<syn::Lifetime>) {
|
|||||||
| syn::Type::Infer(_)
|
| syn::Type::Infer(_)
|
||||||
| syn::Type::Verbatim(_) => {}
|
| syn::Type::Verbatim(_) => {}
|
||||||
|
|
||||||
#[cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]
|
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
use crate::internals::ast::{Container, Data, Field, Style};
|
use crate::internals::ast::{Container, Data, Field, Style};
|
||||||
use crate::internals::attr::{Identifier, TagType};
|
use crate::internals::attr::{Default, Identifier, TagType};
|
||||||
use crate::internals::{ungroup, Ctxt, Derive};
|
use crate::internals::{ungroup, Ctxt, Derive};
|
||||||
use syn::{Member, Type};
|
use syn::{Member, Type};
|
||||||
|
|
||||||
// Cross-cutting checks that require looking at more than a single attrs object.
|
// Cross-cutting checks that require looking at more than a single attrs object.
|
||||||
// Simpler checks should happen when parsing and building the attrs.
|
// Simpler checks should happen when parsing and building the attrs.
|
||||||
pub fn check(cx: &Ctxt, cont: &mut Container, derive: Derive) {
|
pub fn check(cx: &Ctxt, cont: &mut Container, derive: Derive) {
|
||||||
|
check_default_on_tuple(cx, cont);
|
||||||
check_remote_generic(cx, cont);
|
check_remote_generic(cx, cont);
|
||||||
check_getter(cx, cont);
|
check_getter(cx, cont);
|
||||||
check_flatten(cx, cont);
|
check_flatten(cx, cont);
|
||||||
@@ -17,6 +18,39 @@ pub fn check(cx: &Ctxt, cont: &mut Container, derive: Derive) {
|
|||||||
check_from_and_try_from(cx, cont);
|
check_from_and_try_from(cx, cont);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If some field of a tuple struct is marked #[serde(default)] then all fields
|
||||||
|
// after it must also be marked with that attribute, or the struct must have a
|
||||||
|
// container-level serde(default) attribute. A field's default value is only
|
||||||
|
// used for tuple fields if the sequence is exhausted at that point; that means
|
||||||
|
// all subsequent fields will fail to deserialize if they don't have their own
|
||||||
|
// default.
|
||||||
|
fn check_default_on_tuple(cx: &Ctxt, cont: &Container) {
|
||||||
|
if let Default::None = cont.attrs.default() {
|
||||||
|
if let Data::Struct(Style::Tuple, fields) = &cont.data {
|
||||||
|
let mut first_default_index = None;
|
||||||
|
for (i, field) in fields.iter().enumerate() {
|
||||||
|
// Skipped fields automatically get the #[serde(default)]
|
||||||
|
// attribute. We are interested only on non-skipped fields here.
|
||||||
|
if field.attrs.skip_deserializing() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if let Default::None = field.attrs.default() {
|
||||||
|
if let Some(first) = first_default_index {
|
||||||
|
cx.error_spanned_by(
|
||||||
|
field.ty,
|
||||||
|
format!("field must have #[serde(default)] because previous field {} has #[serde(default)]", first),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if first_default_index.is_none() {
|
||||||
|
first_default_index = Some(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Remote derive definition type must have either all of the generics of the
|
// Remote derive definition type must have either all of the generics of the
|
||||||
// remote type:
|
// remote type:
|
||||||
//
|
//
|
||||||
@@ -284,6 +318,9 @@ fn check_internal_tag_field_name_conflict(cx: &Ctxt, cont: &Container) {
|
|||||||
for variant in variants {
|
for variant in variants {
|
||||||
match variant.style {
|
match variant.style {
|
||||||
Style::Struct => {
|
Style::Struct => {
|
||||||
|
if variant.attrs.untagged() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
for field in &variant.fields {
|
for field in &variant.fields {
|
||||||
let check_ser =
|
let check_ser =
|
||||||
!(field.attrs.skip_serializing() || variant.attrs.skip_serializing());
|
!(field.attrs.skip_serializing() || variant.attrs.skip_serializing());
|
||||||
@@ -292,13 +329,13 @@ fn check_internal_tag_field_name_conflict(cx: &Ctxt, cont: &Container) {
|
|||||||
let name = field.attrs.name();
|
let name = field.attrs.name();
|
||||||
let ser_name = name.serialize_name();
|
let ser_name = name.serialize_name();
|
||||||
|
|
||||||
if check_ser && ser_name == tag {
|
if check_ser && ser_name.value == tag {
|
||||||
diagnose_conflict();
|
diagnose_conflict();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for de_name in field.attrs.aliases() {
|
for de_name in field.attrs.aliases() {
|
||||||
if check_de && de_name == tag {
|
if check_de && de_name.value == tag {
|
||||||
diagnose_conflict();
|
diagnose_conflict();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
pub mod ast;
|
pub mod ast;
|
||||||
pub mod attr;
|
pub mod attr;
|
||||||
|
pub mod name;
|
||||||
|
|
||||||
mod case;
|
mod case;
|
||||||
mod check;
|
mod check;
|
||||||
|
|||||||
@@ -0,0 +1,113 @@
|
|||||||
|
use crate::internals::attr::{Attr, VecAttr};
|
||||||
|
use proc_macro2::{Ident, Span, TokenStream};
|
||||||
|
use quote::ToTokens;
|
||||||
|
use std::cmp::Ordering;
|
||||||
|
use std::collections::BTreeSet;
|
||||||
|
use std::fmt::{self, Display};
|
||||||
|
use syn::LitStr;
|
||||||
|
|
||||||
|
pub struct MultiName {
|
||||||
|
pub(crate) serialize: Name,
|
||||||
|
pub(crate) serialize_renamed: bool,
|
||||||
|
pub(crate) deserialize: Name,
|
||||||
|
pub(crate) deserialize_renamed: bool,
|
||||||
|
pub(crate) deserialize_aliases: BTreeSet<Name>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MultiName {
|
||||||
|
pub(crate) fn from_attrs(
|
||||||
|
source_name: Name,
|
||||||
|
ser_name: Attr<Name>,
|
||||||
|
de_name: Attr<Name>,
|
||||||
|
de_aliases: Option<VecAttr<Name>>,
|
||||||
|
) -> Self {
|
||||||
|
let mut alias_set = BTreeSet::new();
|
||||||
|
if let Some(de_aliases) = de_aliases {
|
||||||
|
for alias_name in de_aliases.get() {
|
||||||
|
alias_set.insert(alias_name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let ser_name = ser_name.get();
|
||||||
|
let ser_renamed = ser_name.is_some();
|
||||||
|
let de_name = de_name.get();
|
||||||
|
let de_renamed = de_name.is_some();
|
||||||
|
MultiName {
|
||||||
|
serialize: ser_name.unwrap_or_else(|| source_name.clone()),
|
||||||
|
serialize_renamed: ser_renamed,
|
||||||
|
deserialize: de_name.unwrap_or(source_name),
|
||||||
|
deserialize_renamed: de_renamed,
|
||||||
|
deserialize_aliases: alias_set,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return the container name for the container when serializing.
|
||||||
|
pub fn serialize_name(&self) -> &Name {
|
||||||
|
&self.serialize
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return the container name for the container when deserializing.
|
||||||
|
pub fn deserialize_name(&self) -> &Name {
|
||||||
|
&self.deserialize
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn deserialize_aliases(&self) -> &BTreeSet<Name> {
|
||||||
|
&self.deserialize_aliases
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Name {
|
||||||
|
pub value: String,
|
||||||
|
pub span: Span,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToTokens for Name {
|
||||||
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
||||||
|
LitStr::new(&self.value, self.span).to_tokens(tokens);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Ord for Name {
|
||||||
|
fn cmp(&self, other: &Self) -> Ordering {
|
||||||
|
Ord::cmp(&self.value, &other.value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialOrd for Name {
|
||||||
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||||
|
Some(Ord::cmp(self, other))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Eq for Name {}
|
||||||
|
|
||||||
|
impl PartialEq for Name {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
self.value == other.value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&Ident> for Name {
|
||||||
|
fn from(ident: &Ident) -> Self {
|
||||||
|
Name {
|
||||||
|
value: ident.to_string(),
|
||||||
|
span: ident.span(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&LitStr> for Name {
|
||||||
|
fn from(lit: &LitStr) -> Self {
|
||||||
|
Name {
|
||||||
|
value: lit.value(),
|
||||||
|
span: lit.span(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for Name {
|
||||||
|
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
Display::fmt(&self.value, formatter)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,7 +2,6 @@ use crate::internals::respan::respan;
|
|||||||
use proc_macro2::Span;
|
use proc_macro2::Span;
|
||||||
use quote::ToTokens;
|
use quote::ToTokens;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use syn::punctuated::Punctuated;
|
|
||||||
use syn::{
|
use syn::{
|
||||||
parse_quote, Data, DeriveInput, Expr, ExprPath, GenericArgument, GenericParam, Generics, Macro,
|
parse_quote, Data, DeriveInput, Expr, ExprPath, GenericArgument, GenericParam, Generics, Macro,
|
||||||
Path, PathArguments, QSelf, ReturnType, Token, Type, TypeParamBound, TypePath, WherePredicate,
|
Path, PathArguments, QSelf, ReturnType, Token, Type, TypeParamBound, TypePath, WherePredicate,
|
||||||
@@ -49,7 +48,7 @@ impl ReplaceReceiver<'_> {
|
|||||||
|
|
||||||
path.leading_colon = Some(**path.segments.pairs().next().unwrap().punct().unwrap());
|
path.leading_colon = Some(**path.segments.pairs().next().unwrap().punct().unwrap());
|
||||||
|
|
||||||
let segments = mem::replace(&mut path.segments, Punctuated::new());
|
let segments = mem::take(&mut path.segments);
|
||||||
path.segments = segments.into_pairs().skip(1).collect();
|
path.segments = segments.into_pairs().skip(1).collect();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,7 +83,7 @@ impl ReplaceReceiver<'_> {
|
|||||||
self.visit_type_mut_impl(ty);
|
self.visit_type_mut_impl(ty);
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
*ty = self.self_ty(span).into();
|
*ty = Type::Path(self.self_ty(span));
|
||||||
}
|
}
|
||||||
|
|
||||||
// `Self::Assoc` -> `<Receiver>::Assoc`
|
// `Self::Assoc` -> `<Receiver>::Assoc`
|
||||||
@@ -107,6 +106,7 @@ impl ReplaceReceiver<'_> {
|
|||||||
|
|
||||||
fn visit_type_mut_impl(&mut self, ty: &mut Type) {
|
fn visit_type_mut_impl(&mut self, ty: &mut Type) {
|
||||||
match ty {
|
match ty {
|
||||||
|
#![cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]
|
||||||
Type::Array(ty) => {
|
Type::Array(ty) => {
|
||||||
self.visit_type_mut(&mut ty.elem);
|
self.visit_type_mut(&mut ty.elem);
|
||||||
self.visit_expr_mut(&mut ty.len);
|
self.visit_expr_mut(&mut ty.len);
|
||||||
@@ -147,7 +147,6 @@ impl ReplaceReceiver<'_> {
|
|||||||
|
|
||||||
Type::Infer(_) | Type::Never(_) | Type::Verbatim(_) => {}
|
Type::Infer(_) | Type::Never(_) | Type::Verbatim(_) => {}
|
||||||
|
|
||||||
#[cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]
|
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -178,13 +177,13 @@ impl ReplaceReceiver<'_> {
|
|||||||
PathArguments::AngleBracketed(arguments) => {
|
PathArguments::AngleBracketed(arguments) => {
|
||||||
for arg in &mut arguments.args {
|
for arg in &mut arguments.args {
|
||||||
match arg {
|
match arg {
|
||||||
|
#![cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]
|
||||||
GenericArgument::Type(arg) => self.visit_type_mut(arg),
|
GenericArgument::Type(arg) => self.visit_type_mut(arg),
|
||||||
GenericArgument::AssocType(arg) => self.visit_type_mut(&mut arg.ty),
|
GenericArgument::AssocType(arg) => self.visit_type_mut(&mut arg.ty),
|
||||||
GenericArgument::Lifetime(_)
|
GenericArgument::Lifetime(_)
|
||||||
| GenericArgument::Const(_)
|
| GenericArgument::Const(_)
|
||||||
| GenericArgument::AssocConst(_)
|
| GenericArgument::AssocConst(_)
|
||||||
| GenericArgument::Constraint(_) => {}
|
| GenericArgument::Constraint(_) => {}
|
||||||
#[cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]
|
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -207,9 +206,11 @@ impl ReplaceReceiver<'_> {
|
|||||||
|
|
||||||
fn visit_type_param_bound_mut(&mut self, bound: &mut TypeParamBound) {
|
fn visit_type_param_bound_mut(&mut self, bound: &mut TypeParamBound) {
|
||||||
match bound {
|
match bound {
|
||||||
|
#![cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]
|
||||||
TypeParamBound::Trait(bound) => self.visit_path_mut(&mut bound.path),
|
TypeParamBound::Trait(bound) => self.visit_path_mut(&mut bound.path),
|
||||||
TypeParamBound::Lifetime(_) | TypeParamBound::Verbatim(_) => {}
|
TypeParamBound::Lifetime(_)
|
||||||
#[cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]
|
| TypeParamBound::PreciseCapture(_)
|
||||||
|
| TypeParamBound::Verbatim(_) => {}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -228,6 +229,7 @@ impl ReplaceReceiver<'_> {
|
|||||||
if let Some(where_clause) = &mut generics.where_clause {
|
if let Some(where_clause) = &mut generics.where_clause {
|
||||||
for predicate in &mut where_clause.predicates {
|
for predicate in &mut where_clause.predicates {
|
||||||
match predicate {
|
match predicate {
|
||||||
|
#![cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]
|
||||||
WherePredicate::Type(predicate) => {
|
WherePredicate::Type(predicate) => {
|
||||||
self.visit_type_mut(&mut predicate.bounded_ty);
|
self.visit_type_mut(&mut predicate.bounded_ty);
|
||||||
for bound in &mut predicate.bounds {
|
for bound in &mut predicate.bounds {
|
||||||
@@ -235,7 +237,6 @@ impl ReplaceReceiver<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
WherePredicate::Lifetime(_) => {}
|
WherePredicate::Lifetime(_) => {}
|
||||||
#[cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]
|
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ pub const FLATTEN: Symbol = Symbol("flatten");
|
|||||||
pub const FROM: Symbol = Symbol("from");
|
pub const FROM: Symbol = Symbol("from");
|
||||||
pub const GETTER: Symbol = Symbol("getter");
|
pub const GETTER: Symbol = Symbol("getter");
|
||||||
pub const INTO: Symbol = Symbol("into");
|
pub const INTO: Symbol = Symbol("into");
|
||||||
|
pub const NON_EXHAUSTIVE: Symbol = Symbol("non_exhaustive");
|
||||||
pub const OTHER: Symbol = Symbol("other");
|
pub const OTHER: Symbol = Symbol("other");
|
||||||
pub const REMOTE: Symbol = Symbol("remote");
|
pub const REMOTE: Symbol = Symbol("remote");
|
||||||
pub const RENAME: Symbol = Symbol("rename");
|
pub const RENAME: Symbol = Symbol("rename");
|
||||||
@@ -45,7 +46,7 @@ impl PartialEq<Symbol> for Ident {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> PartialEq<Symbol> for &'a Ident {
|
impl PartialEq<Symbol> for &Ident {
|
||||||
fn eq(&self, word: &Symbol) -> bool {
|
fn eq(&self, word: &Symbol) -> bool {
|
||||||
*self == word.0
|
*self == word.0
|
||||||
}
|
}
|
||||||
@@ -57,7 +58,7 @@ impl PartialEq<Symbol> for Path {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> PartialEq<Symbol> for &'a Path {
|
impl PartialEq<Symbol> for &Path {
|
||||||
fn eq(&self, word: &Symbol) -> bool {
|
fn eq(&self, word: &Symbol) -> bool {
|
||||||
self.is_ident(word.0)
|
self.is_ident(word.0)
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-23
@@ -13,7 +13,8 @@
|
|||||||
//!
|
//!
|
||||||
//! [https://serde.rs/derive.html]: https://serde.rs/derive.html
|
//! [https://serde.rs/derive.html]: https://serde.rs/derive.html
|
||||||
|
|
||||||
#![doc(html_root_url = "https://docs.rs/serde_derive/1.0.178")]
|
#![doc(html_root_url = "https://docs.rs/serde_derive/1.0.221")]
|
||||||
|
#![cfg_attr(not(check_cfg), allow(unexpected_cfgs))]
|
||||||
// Ignored clippy lints
|
// Ignored clippy lints
|
||||||
#![allow(
|
#![allow(
|
||||||
// clippy false positive: https://github.com/rust-lang/rust-clippy/issues/7054
|
// clippy false positive: https://github.com/rust-lang/rust-clippy/issues/7054
|
||||||
@@ -26,6 +27,7 @@
|
|||||||
// clippy bug: https://github.com/rust-lang/rust-clippy/issues/6797
|
// clippy bug: https://github.com/rust-lang/rust-clippy/issues/6797
|
||||||
clippy::manual_map,
|
clippy::manual_map,
|
||||||
clippy::match_like_matches_macro,
|
clippy::match_like_matches_macro,
|
||||||
|
clippy::needless_lifetimes,
|
||||||
clippy::needless_pass_by_value,
|
clippy::needless_pass_by_value,
|
||||||
clippy::too_many_arguments,
|
clippy::too_many_arguments,
|
||||||
clippy::trivially_copy_pass_by_ref,
|
clippy::trivially_copy_pass_by_ref,
|
||||||
@@ -39,6 +41,7 @@
|
|||||||
clippy::cast_possible_truncation,
|
clippy::cast_possible_truncation,
|
||||||
clippy::checked_conversions,
|
clippy::checked_conversions,
|
||||||
clippy::doc_markdown,
|
clippy::doc_markdown,
|
||||||
|
clippy::elidable_lifetime_names,
|
||||||
clippy::enum_glob_use,
|
clippy::enum_glob_use,
|
||||||
clippy::indexing_slicing,
|
clippy::indexing_slicing,
|
||||||
clippy::items_after_statements,
|
clippy::items_after_statements,
|
||||||
@@ -50,33 +53,28 @@
|
|||||||
clippy::match_wildcard_for_single_variants,
|
clippy::match_wildcard_for_single_variants,
|
||||||
clippy::module_name_repetitions,
|
clippy::module_name_repetitions,
|
||||||
clippy::must_use_candidate,
|
clippy::must_use_candidate,
|
||||||
clippy::option_if_let_else,
|
|
||||||
clippy::similar_names,
|
clippy::similar_names,
|
||||||
clippy::single_match_else,
|
clippy::single_match_else,
|
||||||
clippy::struct_excessive_bools,
|
clippy::struct_excessive_bools,
|
||||||
clippy::too_many_lines,
|
clippy::too_many_lines,
|
||||||
|
clippy::uninlined_format_args,
|
||||||
clippy::unseparated_literal_suffix,
|
clippy::unseparated_literal_suffix,
|
||||||
clippy::unused_self,
|
clippy::unused_self,
|
||||||
clippy::use_self,
|
clippy::use_self,
|
||||||
clippy::wildcard_imports
|
clippy::wildcard_imports
|
||||||
)]
|
)]
|
||||||
#![cfg_attr(all(test, exhaustive), feature(non_exhaustive_omitted_patterns_lint))]
|
#![cfg_attr(all(test, exhaustive), feature(non_exhaustive_omitted_patterns_lint))]
|
||||||
|
#![allow(unknown_lints, mismatched_lifetime_syntaxes)]
|
||||||
|
|
||||||
extern crate proc_macro2;
|
extern crate proc_macro2;
|
||||||
extern crate quote;
|
extern crate quote;
|
||||||
extern crate syn;
|
extern crate syn;
|
||||||
|
|
||||||
#[cfg(not(precompiled))]
|
|
||||||
extern crate proc_macro;
|
extern crate proc_macro;
|
||||||
#[cfg(precompiled)]
|
|
||||||
extern crate proc_macro2 as proc_macro;
|
|
||||||
|
|
||||||
mod internals;
|
mod internals;
|
||||||
|
|
||||||
use proc_macro::TokenStream;
|
use proc_macro::TokenStream;
|
||||||
#[cfg(precompiled)]
|
|
||||||
use std::sync::atomic::AtomicBool;
|
|
||||||
#[cfg(not(precompiled))]
|
|
||||||
use syn::parse_macro_input;
|
use syn::parse_macro_input;
|
||||||
use syn::DeriveInput;
|
use syn::DeriveInput;
|
||||||
|
|
||||||
@@ -91,20 +89,7 @@ mod pretend;
|
|||||||
mod ser;
|
mod ser;
|
||||||
mod this;
|
mod this;
|
||||||
|
|
||||||
#[cfg(precompiled)]
|
#[proc_macro_derive(Serialize, attributes(serde))]
|
||||||
macro_rules! parse_macro_input {
|
|
||||||
($tokenstream:ident as $ty:ty) => {
|
|
||||||
match syn::parse2::<$ty>($tokenstream) {
|
|
||||||
Ok(data) => data,
|
|
||||||
Err(err) => return err.to_compile_error(),
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(precompiled)]
|
|
||||||
pub static DESERIALIZE_IN_PLACE: AtomicBool = AtomicBool::new(false);
|
|
||||||
|
|
||||||
#[cfg_attr(not(precompiled), proc_macro_derive(Serialize, attributes(serde)))]
|
|
||||||
pub fn derive_serialize(input: TokenStream) -> TokenStream {
|
pub fn derive_serialize(input: TokenStream) -> TokenStream {
|
||||||
let mut input = parse_macro_input!(input as DeriveInput);
|
let mut input = parse_macro_input!(input as DeriveInput);
|
||||||
ser::expand_derive_serialize(&mut input)
|
ser::expand_derive_serialize(&mut input)
|
||||||
@@ -112,7 +97,7 @@ pub fn derive_serialize(input: TokenStream) -> TokenStream {
|
|||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg_attr(not(precompiled), proc_macro_derive(Deserialize, attributes(serde)))]
|
#[proc_macro_derive(Deserialize, attributes(serde))]
|
||||||
pub fn derive_deserialize(input: TokenStream) -> TokenStream {
|
pub fn derive_deserialize(input: TokenStream) -> TokenStream {
|
||||||
let mut input = parse_macro_input!(input as DeriveInput);
|
let mut input = parse_macro_input!(input as DeriveInput);
|
||||||
de::expand_derive_deserialize(&mut input)
|
de::expand_derive_deserialize(&mut input)
|
||||||
|
|||||||
@@ -64,14 +64,14 @@ pub fn pretend_used(cont: &Container, is_packed: bool) -> TokenStream {
|
|||||||
fn pretend_fields_used(cont: &Container, is_packed: bool) -> TokenStream {
|
fn pretend_fields_used(cont: &Container, is_packed: bool) -> TokenStream {
|
||||||
match &cont.data {
|
match &cont.data {
|
||||||
Data::Enum(variants) => pretend_fields_used_enum(cont, variants),
|
Data::Enum(variants) => pretend_fields_used_enum(cont, variants),
|
||||||
Data::Struct(Style::Struct, fields) => {
|
Data::Struct(Style::Struct | Style::Tuple | Style::Newtype, fields) => {
|
||||||
if is_packed {
|
if is_packed {
|
||||||
pretend_fields_used_struct_packed(cont, fields)
|
pretend_fields_used_struct_packed(cont, fields)
|
||||||
} else {
|
} else {
|
||||||
pretend_fields_used_struct(cont, fields)
|
pretend_fields_used_struct(cont, fields)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Data::Struct(_, _) => quote!(),
|
Data::Struct(Style::Unit, _) => quote!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,13 +115,13 @@ fn pretend_fields_used_enum(cont: &Container, variants: &[Variant]) -> TokenStre
|
|||||||
let patterns = variants
|
let patterns = variants
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|variant| match variant.style {
|
.filter_map(|variant| match variant.style {
|
||||||
Style::Struct => {
|
Style::Struct | Style::Tuple | Style::Newtype => {
|
||||||
let variant_ident = &variant.ident;
|
let variant_ident = &variant.ident;
|
||||||
let members = variant.fields.iter().map(|field| &field.member);
|
let members = variant.fields.iter().map(|field| &field.member);
|
||||||
let placeholders = (0usize..).map(|i| format_ident!("__v{}", i));
|
let placeholders = (0usize..).map(|i| format_ident!("__v{}", i));
|
||||||
Some(quote!(#type_ident::#variant_ident { #(#members: #placeholders),* }))
|
Some(quote!(#type_ident::#variant_ident { #(#members: #placeholders),* }))
|
||||||
}
|
}
|
||||||
_ => None,
|
Style::Unit => None,
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
|||||||
+70
-42
@@ -1,5 +1,6 @@
|
|||||||
use crate::fragment::{Fragment, Match, Stmts};
|
use crate::fragment::{Fragment, Match, Stmts};
|
||||||
use crate::internals::ast::{Container, Data, Field, Style, Variant};
|
use crate::internals::ast::{Container, Data, Field, Style, Variant};
|
||||||
|
use crate::internals::name::Name;
|
||||||
use crate::internals::{attr, replace_receiver, Ctxt, Derive};
|
use crate::internals::{attr, replace_receiver, Ctxt, Derive};
|
||||||
use crate::{bound, dummy, pretend, this};
|
use crate::{bound, dummy, pretend, this};
|
||||||
use proc_macro2::{Span, TokenStream};
|
use proc_macro2::{Span, TokenStream};
|
||||||
@@ -28,6 +29,7 @@ pub fn expand_derive_serialize(input: &mut syn::DeriveInput) -> syn::Result<Toke
|
|||||||
let vis = &input.vis;
|
let vis = &input.vis;
|
||||||
let used = pretend::pretend_used(&cont, params.is_packed);
|
let used = pretend::pretend_used(&cont, params.is_packed);
|
||||||
quote! {
|
quote! {
|
||||||
|
#[automatically_derived]
|
||||||
impl #impl_generics #ident #ty_generics #where_clause {
|
impl #impl_generics #ident #ty_generics #where_clause {
|
||||||
#vis fn serialize<__S>(__self: &#remote #ty_generics, __serializer: __S) -> #serde::__private::Result<__S::Ok, __S::Error>
|
#vis fn serialize<__S>(__self: &#remote #ty_generics, __serializer: __S) -> #serde::__private::Result<__S::Ok, __S::Error>
|
||||||
where
|
where
|
||||||
@@ -148,7 +150,7 @@ fn build_generics(cont: &Container) -> syn::Generics {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fields with a `skip_serializing` or `serialize_with` attribute, or which
|
// Fields with a `skip_serializing` or `serialize_with` attribute, or which
|
||||||
// belong to a variant with a 'skip_serializing` or `serialize_with` attribute,
|
// belong to a variant with a `skip_serializing` or `serialize_with` attribute,
|
||||||
// are not serialized by us so we do not generate a bound. Fields with a `bound`
|
// are not serialized by us so we do not generate a bound. Fields with a `bound`
|
||||||
// attribute specify their own bound so we do not generate one. All other fields
|
// attribute specify their own bound so we do not generate one. All other fields
|
||||||
// may need a `T: Serialize` bound where T is the type of the field.
|
// may need a `T: Serialize` bound where T is the type of the field.
|
||||||
@@ -289,9 +291,18 @@ fn serialize_tuple_struct(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_struct(params: &Parameters, fields: &[Field], cattrs: &attr::Container) -> Fragment {
|
fn serialize_struct(params: &Parameters, fields: &[Field], cattrs: &attr::Container) -> Fragment {
|
||||||
assert!(fields.len() as u64 <= u64::from(u32::max_value()));
|
assert!(
|
||||||
|
fields.len() as u64 <= u64::from(u32::MAX),
|
||||||
|
"too many fields in {}: {}, maximum supported count is {}",
|
||||||
|
cattrs.name().serialize_name(),
|
||||||
|
fields.len(),
|
||||||
|
u32::MAX,
|
||||||
|
);
|
||||||
|
|
||||||
if cattrs.has_flatten() {
|
let has_non_skipped_flatten = fields
|
||||||
|
.iter()
|
||||||
|
.any(|field| field.attrs.flatten() && !field.attrs.skip_serializing());
|
||||||
|
if has_non_skipped_flatten {
|
||||||
serialize_struct_as_map(params, fields, cattrs)
|
serialize_struct_as_map(params, fields, cattrs)
|
||||||
} else {
|
} else {
|
||||||
serialize_struct_as_struct(params, fields, cattrs)
|
serialize_struct_as_struct(params, fields, cattrs)
|
||||||
@@ -370,26 +381,8 @@ fn serialize_struct_as_map(
|
|||||||
|
|
||||||
let let_mut = mut_if(serialized_fields.peek().is_some() || tag_field_exists);
|
let let_mut = mut_if(serialized_fields.peek().is_some() || tag_field_exists);
|
||||||
|
|
||||||
let len = if cattrs.has_flatten() {
|
|
||||||
quote!(_serde::__private::None)
|
|
||||||
} else {
|
|
||||||
let len = serialized_fields
|
|
||||||
.map(|field| match field.attrs.skip_serializing_if() {
|
|
||||||
None => quote!(1),
|
|
||||||
Some(path) => {
|
|
||||||
let field_expr = get_member(params, field, &field.member);
|
|
||||||
quote!(if #path(#field_expr) { 0 } else { 1 })
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.fold(
|
|
||||||
quote!(#tag_field_exists as usize),
|
|
||||||
|sum, expr| quote!(#sum + #expr),
|
|
||||||
);
|
|
||||||
quote!(_serde::__private::Some(#len))
|
|
||||||
};
|
|
||||||
|
|
||||||
quote_block! {
|
quote_block! {
|
||||||
let #let_mut __serde_state = _serde::Serializer::serialize_map(__serializer, #len)?;
|
let #let_mut __serde_state = _serde::Serializer::serialize_map(__serializer, _serde::__private::None)?;
|
||||||
#tag_field
|
#tag_field
|
||||||
#(#serialize_fields)*
|
#(#serialize_fields)*
|
||||||
_serde::ser::SerializeMap::end(__serde_state)
|
_serde::ser::SerializeMap::end(__serde_state)
|
||||||
@@ -397,11 +390,11 @@ fn serialize_struct_as_map(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_enum(params: &Parameters, variants: &[Variant], cattrs: &attr::Container) -> Fragment {
|
fn serialize_enum(params: &Parameters, variants: &[Variant], cattrs: &attr::Container) -> Fragment {
|
||||||
assert!(variants.len() as u64 <= u64::from(u32::max_value()));
|
assert!(variants.len() as u64 <= u64::from(u32::MAX));
|
||||||
|
|
||||||
let self_var = ¶ms.self_var;
|
let self_var = ¶ms.self_var;
|
||||||
|
|
||||||
let arms: Vec<_> = variants
|
let mut arms: Vec<_> = variants
|
||||||
.iter()
|
.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.map(|(variant_index, variant)| {
|
.map(|(variant_index, variant)| {
|
||||||
@@ -409,6 +402,12 @@ fn serialize_enum(params: &Parameters, variants: &[Variant], cattrs: &attr::Cont
|
|||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
if cattrs.remote().is_some() && cattrs.non_exhaustive() {
|
||||||
|
arms.push(quote! {
|
||||||
|
ref unrecognized => _serde::__private::Err(_serde::ser::Error::custom(_serde::__private::ser::CannotSerializeVariant(unrecognized))),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
quote_expr! {
|
quote_expr! {
|
||||||
match *#self_var {
|
match *#self_var {
|
||||||
#(#arms)*
|
#(#arms)*
|
||||||
@@ -478,7 +477,14 @@ fn serialize_variant(
|
|||||||
serialize_internally_tagged_variant(params, variant, cattrs, tag)
|
serialize_internally_tagged_variant(params, variant, cattrs, tag)
|
||||||
}
|
}
|
||||||
(attr::TagType::Adjacent { tag, content }, false) => {
|
(attr::TagType::Adjacent { tag, content }, false) => {
|
||||||
serialize_adjacently_tagged_variant(params, variant, cattrs, tag, content)
|
serialize_adjacently_tagged_variant(
|
||||||
|
params,
|
||||||
|
variant,
|
||||||
|
cattrs,
|
||||||
|
variant_index,
|
||||||
|
tag,
|
||||||
|
content,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
(attr::TagType::None, _) | (_, true) => {
|
(attr::TagType::None, _) | (_, true) => {
|
||||||
serialize_untagged_variant(params, variant, cattrs)
|
serialize_untagged_variant(params, variant, cattrs)
|
||||||
@@ -559,7 +565,7 @@ fn serialize_externally_tagged_variant(
|
|||||||
},
|
},
|
||||||
params,
|
params,
|
||||||
&variant.fields,
|
&variant.fields,
|
||||||
&type_name,
|
type_name,
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -624,7 +630,7 @@ fn serialize_internally_tagged_variant(
|
|||||||
StructVariant::InternallyTagged { tag, variant_name },
|
StructVariant::InternallyTagged { tag, variant_name },
|
||||||
params,
|
params,
|
||||||
&variant.fields,
|
&variant.fields,
|
||||||
&type_name,
|
type_name,
|
||||||
),
|
),
|
||||||
Style::Tuple => unreachable!("checked in serde_derive_internals"),
|
Style::Tuple => unreachable!("checked in serde_derive_internals"),
|
||||||
}
|
}
|
||||||
@@ -634,12 +640,20 @@ fn serialize_adjacently_tagged_variant(
|
|||||||
params: &Parameters,
|
params: &Parameters,
|
||||||
variant: &Variant,
|
variant: &Variant,
|
||||||
cattrs: &attr::Container,
|
cattrs: &attr::Container,
|
||||||
|
variant_index: u32,
|
||||||
tag: &str,
|
tag: &str,
|
||||||
content: &str,
|
content: &str,
|
||||||
) -> Fragment {
|
) -> Fragment {
|
||||||
let this_type = ¶ms.this_type;
|
let this_type = ¶ms.this_type;
|
||||||
let type_name = cattrs.name().serialize_name();
|
let type_name = cattrs.name().serialize_name();
|
||||||
let variant_name = variant.attrs.name().serialize_name();
|
let variant_name = variant.attrs.name().serialize_name();
|
||||||
|
let serialize_variant = quote! {
|
||||||
|
&_serde::__private::ser::AdjacentlyTaggedEnumVariant {
|
||||||
|
enum_name: #type_name,
|
||||||
|
variant_index: #variant_index,
|
||||||
|
variant_name: #variant_name,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let inner = Stmts(if let Some(path) = variant.attrs.serialize_with() {
|
let inner = Stmts(if let Some(path) = variant.attrs.serialize_with() {
|
||||||
let ser = wrap_serialize_variant_with(params, path, variant);
|
let ser = wrap_serialize_variant_with(params, path, variant);
|
||||||
@@ -653,7 +667,7 @@ fn serialize_adjacently_tagged_variant(
|
|||||||
let mut __struct = _serde::Serializer::serialize_struct(
|
let mut __struct = _serde::Serializer::serialize_struct(
|
||||||
__serializer, #type_name, 1)?;
|
__serializer, #type_name, 1)?;
|
||||||
_serde::ser::SerializeStruct::serialize_field(
|
_serde::ser::SerializeStruct::serialize_field(
|
||||||
&mut __struct, #tag, #variant_name)?;
|
&mut __struct, #tag, #serialize_variant)?;
|
||||||
_serde::ser::SerializeStruct::end(__struct)
|
_serde::ser::SerializeStruct::end(__struct)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -670,7 +684,7 @@ fn serialize_adjacently_tagged_variant(
|
|||||||
let mut __struct = _serde::Serializer::serialize_struct(
|
let mut __struct = _serde::Serializer::serialize_struct(
|
||||||
__serializer, #type_name, 2)?;
|
__serializer, #type_name, 2)?;
|
||||||
_serde::ser::SerializeStruct::serialize_field(
|
_serde::ser::SerializeStruct::serialize_field(
|
||||||
&mut __struct, #tag, #variant_name)?;
|
&mut __struct, #tag, #serialize_variant)?;
|
||||||
#func(
|
#func(
|
||||||
&mut __struct, #content, #field_expr)?;
|
&mut __struct, #content, #field_expr)?;
|
||||||
_serde::ser::SerializeStruct::end(__struct)
|
_serde::ser::SerializeStruct::end(__struct)
|
||||||
@@ -683,13 +697,13 @@ fn serialize_adjacently_tagged_variant(
|
|||||||
StructVariant::Untagged,
|
StructVariant::Untagged,
|
||||||
params,
|
params,
|
||||||
&variant.fields,
|
&variant.fields,
|
||||||
&variant_name,
|
variant_name,
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let fields_ty = variant.fields.iter().map(|f| &f.ty);
|
let fields_ty = variant.fields.iter().map(|f| &f.ty);
|
||||||
let fields_ident: &Vec<_> = &match variant.style {
|
let fields_ident: &[_] = &match variant.style {
|
||||||
Style::Unit => {
|
Style::Unit => {
|
||||||
if variant.attrs.serialize_with().is_some() {
|
if variant.attrs.serialize_with().is_some() {
|
||||||
vec![]
|
vec![]
|
||||||
@@ -720,6 +734,7 @@ fn serialize_adjacently_tagged_variant(
|
|||||||
phantom: _serde::__private::PhantomData<#this_type #ty_generics>,
|
phantom: _serde::__private::PhantomData<#this_type #ty_generics>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[automatically_derived]
|
||||||
impl #wrapper_impl_generics _serde::Serialize for __AdjacentlyTagged #wrapper_ty_generics #where_clause {
|
impl #wrapper_impl_generics _serde::Serialize for __AdjacentlyTagged #wrapper_ty_generics #where_clause {
|
||||||
fn serialize<__S>(&self, __serializer: __S) -> _serde::__private::Result<__S::Ok, __S::Error>
|
fn serialize<__S>(&self, __serializer: __S) -> _serde::__private::Result<__S::Ok, __S::Error>
|
||||||
where
|
where
|
||||||
@@ -735,7 +750,7 @@ fn serialize_adjacently_tagged_variant(
|
|||||||
let mut __struct = _serde::Serializer::serialize_struct(
|
let mut __struct = _serde::Serializer::serialize_struct(
|
||||||
__serializer, #type_name, 2)?;
|
__serializer, #type_name, 2)?;
|
||||||
_serde::ser::SerializeStruct::serialize_field(
|
_serde::ser::SerializeStruct::serialize_field(
|
||||||
&mut __struct, #tag, #variant_name)?;
|
&mut __struct, #tag, #serialize_variant)?;
|
||||||
_serde::ser::SerializeStruct::serialize_field(
|
_serde::ser::SerializeStruct::serialize_field(
|
||||||
&mut __struct, #content, &__AdjacentlyTagged {
|
&mut __struct, #content, &__AdjacentlyTagged {
|
||||||
data: (#(#fields_ident,)*),
|
data: (#(#fields_ident,)*),
|
||||||
@@ -779,16 +794,16 @@ fn serialize_untagged_variant(
|
|||||||
Style::Tuple => serialize_tuple_variant(TupleVariant::Untagged, params, &variant.fields),
|
Style::Tuple => serialize_tuple_variant(TupleVariant::Untagged, params, &variant.fields),
|
||||||
Style::Struct => {
|
Style::Struct => {
|
||||||
let type_name = cattrs.name().serialize_name();
|
let type_name = cattrs.name().serialize_name();
|
||||||
serialize_struct_variant(StructVariant::Untagged, params, &variant.fields, &type_name)
|
serialize_struct_variant(StructVariant::Untagged, params, &variant.fields, type_name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum TupleVariant {
|
enum TupleVariant<'a> {
|
||||||
ExternallyTagged {
|
ExternallyTagged {
|
||||||
type_name: String,
|
type_name: &'a Name,
|
||||||
variant_index: u32,
|
variant_index: u32,
|
||||||
variant_name: String,
|
variant_name: &'a Name,
|
||||||
},
|
},
|
||||||
Untagged,
|
Untagged,
|
||||||
}
|
}
|
||||||
@@ -855,11 +870,11 @@ fn serialize_tuple_variant(
|
|||||||
enum StructVariant<'a> {
|
enum StructVariant<'a> {
|
||||||
ExternallyTagged {
|
ExternallyTagged {
|
||||||
variant_index: u32,
|
variant_index: u32,
|
||||||
variant_name: String,
|
variant_name: &'a Name,
|
||||||
},
|
},
|
||||||
InternallyTagged {
|
InternallyTagged {
|
||||||
tag: &'a str,
|
tag: &'a str,
|
||||||
variant_name: String,
|
variant_name: &'a Name,
|
||||||
},
|
},
|
||||||
Untagged,
|
Untagged,
|
||||||
}
|
}
|
||||||
@@ -868,7 +883,7 @@ fn serialize_struct_variant(
|
|||||||
context: StructVariant,
|
context: StructVariant,
|
||||||
params: &Parameters,
|
params: &Parameters,
|
||||||
fields: &[Field],
|
fields: &[Field],
|
||||||
name: &str,
|
name: &Name,
|
||||||
) -> Fragment {
|
) -> Fragment {
|
||||||
if fields.iter().any(|field| field.attrs.flatten()) {
|
if fields.iter().any(|field| field.attrs.flatten()) {
|
||||||
return serialize_struct_variant_with_flatten(context, params, fields, name);
|
return serialize_struct_variant_with_flatten(context, params, fields, name);
|
||||||
@@ -952,7 +967,7 @@ fn serialize_struct_variant_with_flatten(
|
|||||||
context: StructVariant,
|
context: StructVariant,
|
||||||
params: &Parameters,
|
params: &Parameters,
|
||||||
fields: &[Field],
|
fields: &[Field],
|
||||||
name: &str,
|
name: &Name,
|
||||||
) -> Fragment {
|
) -> Fragment {
|
||||||
let struct_trait = StructTrait::SerializeMap;
|
let struct_trait = StructTrait::SerializeMap;
|
||||||
let serialize_fields = serialize_struct_visitor(fields, params, true, &struct_trait);
|
let serialize_fields = serialize_struct_visitor(fields, params, true, &struct_trait);
|
||||||
@@ -984,6 +999,7 @@ fn serialize_struct_variant_with_flatten(
|
|||||||
phantom: _serde::__private::PhantomData<#this_type #ty_generics>,
|
phantom: _serde::__private::PhantomData<#this_type #ty_generics>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[automatically_derived]
|
||||||
impl #wrapper_impl_generics _serde::Serialize for __EnumFlatten #wrapper_ty_generics #where_clause {
|
impl #wrapper_impl_generics _serde::Serialize for __EnumFlatten #wrapper_ty_generics #where_clause {
|
||||||
fn serialize<__S>(&self, __serializer: __S) -> _serde::__private::Result<__S::Ok, __S::Error>
|
fn serialize<__S>(&self, __serializer: __S) -> _serde::__private::Result<__S::Ok, __S::Error>
|
||||||
where
|
where
|
||||||
@@ -1208,6 +1224,17 @@ fn wrap_serialize_with(
|
|||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let self_var = quote!(self);
|
||||||
|
let serializer_var = quote!(__s);
|
||||||
|
|
||||||
|
// If #serialize_with returns wrong type, error will be reported on here.
|
||||||
|
// We attach span of the path to this piece so error will be reported
|
||||||
|
// on the #[serde(with = "...")]
|
||||||
|
// ^^^^^
|
||||||
|
let wrapper_serialize = quote_spanned! {serialize_with.span()=>
|
||||||
|
#serialize_with(#(#self_var.values.#field_access, )* #serializer_var)
|
||||||
|
};
|
||||||
|
|
||||||
quote!({
|
quote!({
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
struct __SerializeWith #wrapper_impl_generics #where_clause {
|
struct __SerializeWith #wrapper_impl_generics #where_clause {
|
||||||
@@ -1215,12 +1242,13 @@ fn wrap_serialize_with(
|
|||||||
phantom: _serde::__private::PhantomData<#this_type #ty_generics>,
|
phantom: _serde::__private::PhantomData<#this_type #ty_generics>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[automatically_derived]
|
||||||
impl #wrapper_impl_generics _serde::Serialize for __SerializeWith #wrapper_ty_generics #where_clause {
|
impl #wrapper_impl_generics _serde::Serialize for __SerializeWith #wrapper_ty_generics #where_clause {
|
||||||
fn serialize<__S>(&self, __s: __S) -> _serde::__private::Result<__S::Ok, __S::Error>
|
fn serialize<__S>(&#self_var, #serializer_var: __S) -> _serde::__private::Result<__S::Ok, __S::Error>
|
||||||
where
|
where
|
||||||
__S: _serde::Serializer,
|
__S: _serde::Serializer,
|
||||||
{
|
{
|
||||||
#serialize_with(#(self.values.#field_access, )* __s)
|
#wrapper_serialize
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,24 +1,31 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde_derive_internals"
|
name = "serde_derive_internals"
|
||||||
version = "0.28.0" # remember to update html_root_url
|
version = "0.29.1"
|
||||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
||||||
description = "AST representation used by Serde derive macros. Unstable."
|
description = "AST representation used by Serde derive macros. Unstable."
|
||||||
documentation = "https://docs.rs/serde_derive_internals"
|
documentation = "https://docs.rs/serde_derive_internals"
|
||||||
|
edition = "2021"
|
||||||
exclude = ["build.rs"]
|
exclude = ["build.rs"]
|
||||||
homepage = "https://serde.rs"
|
homepage = "https://serde.rs"
|
||||||
keywords = ["serde", "serialization"]
|
keywords = ["serde", "serialization"]
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
repository = "https://github.com/serde-rs/serde"
|
repository = "https://github.com/serde-rs/serde"
|
||||||
rust-version = "1.56"
|
rust-version = "1.61"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
path = "lib.rs"
|
path = "lib.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
proc-macro2 = "1.0"
|
proc-macro2 = { workspace = true }
|
||||||
quote = "1.0"
|
quote = { workspace = true }
|
||||||
syn = { version = "2.0.25", default-features = false, features = ["clone-impls", "derive", "parsing", "printing"] }
|
syn = { workspace = true, features = ["clone-impls", "derive", "parsing", "printing"] }
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
targets = ["x86_64-unknown-linux-gnu"]
|
targets = ["x86_64-unknown-linux-gnu"]
|
||||||
rustdoc-args = ["--generate-link-to-definition"]
|
rustdoc-args = [
|
||||||
|
"--generate-link-to-definition",
|
||||||
|
"--extern-html-root-url=core=https://doc.rust-lang.org",
|
||||||
|
"--extern-html-root-url=alloc=https://doc.rust-lang.org",
|
||||||
|
"--extern-html-root-url=std=https://doc.rust-lang.org",
|
||||||
|
"--extern-html-root-url=proc_macro=https://doc.rust-lang.org",
|
||||||
|
]
|
||||||
|
|||||||
@@ -1,9 +1,17 @@
|
|||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
// Warning: build.rs is not published to crates.io.
|
||||||
|
|
||||||
println!("cargo:rerun-if-changed=build.rs");
|
println!("cargo:rerun-if-changed=build.rs");
|
||||||
println!("cargo:rerun-if-changed=src/mod.rs");
|
println!("cargo:rerun-if-changed=src/mod.rs");
|
||||||
|
|
||||||
|
println!("cargo:rustc-cfg=check_cfg");
|
||||||
|
println!("cargo:rustc-check-cfg=cfg(check_cfg)");
|
||||||
|
println!("cargo:rustc-check-cfg=cfg(exhaustive)");
|
||||||
|
println!("cargo:rustc-check-cfg=cfg(serde_build_from_git)");
|
||||||
|
println!("cargo:rustc-check-cfg=cfg(feature, values(\"deserialize_in_place\"))");
|
||||||
|
|
||||||
// Sometimes on Windows the git checkout does not correctly wire up the
|
// Sometimes on Windows the git checkout does not correctly wire up the
|
||||||
// symlink from serde_derive_internals/src to serde_derive/src/internals.
|
// 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
|
// When this happens we'll just build based on relative paths within the git
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#![doc(html_root_url = "https://docs.rs/serde_derive_internals/0.28.0")]
|
#![doc(html_root_url = "https://docs.rs/serde_derive_internals/0.29.1")]
|
||||||
|
#![cfg_attr(not(check_cfg), allow(unexpected_cfgs))]
|
||||||
// Ignored clippy lints
|
// Ignored clippy lints
|
||||||
#![allow(
|
#![allow(
|
||||||
clippy::cognitive_complexity,
|
clippy::cognitive_complexity,
|
||||||
@@ -8,6 +9,7 @@
|
|||||||
// clippy bug: https://github.com/rust-lang/rust-clippy/issues/6797
|
// clippy bug: https://github.com/rust-lang/rust-clippy/issues/6797
|
||||||
clippy::manual_map,
|
clippy::manual_map,
|
||||||
clippy::missing_panics_doc,
|
clippy::missing_panics_doc,
|
||||||
|
clippy::needless_lifetimes,
|
||||||
clippy::redundant_field_names,
|
clippy::redundant_field_names,
|
||||||
clippy::result_unit_err,
|
clippy::result_unit_err,
|
||||||
clippy::should_implement_trait,
|
clippy::should_implement_trait,
|
||||||
@@ -19,6 +21,7 @@
|
|||||||
// Ignored clippy_pedantic lints
|
// Ignored clippy_pedantic lints
|
||||||
#![allow(
|
#![allow(
|
||||||
clippy::doc_markdown,
|
clippy::doc_markdown,
|
||||||
|
clippy::elidable_lifetime_names,
|
||||||
clippy::enum_glob_use,
|
clippy::enum_glob_use,
|
||||||
clippy::items_after_statements,
|
clippy::items_after_statements,
|
||||||
clippy::let_underscore_untyped,
|
clippy::let_underscore_untyped,
|
||||||
@@ -34,9 +37,11 @@
|
|||||||
clippy::single_match_else,
|
clippy::single_match_else,
|
||||||
clippy::struct_excessive_bools,
|
clippy::struct_excessive_bools,
|
||||||
clippy::too_many_lines,
|
clippy::too_many_lines,
|
||||||
|
clippy::uninlined_format_args,
|
||||||
clippy::unused_self,
|
clippy::unused_self,
|
||||||
clippy::wildcard_imports
|
clippy::wildcard_imports
|
||||||
)]
|
)]
|
||||||
|
#![allow(unknown_lints, mismatched_lifetime_syntaxes)]
|
||||||
|
|
||||||
extern crate proc_macro2;
|
extern crate proc_macro2;
|
||||||
extern crate quote;
|
extern crate quote;
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
name = "serde_test_suite"
|
name = "serde_test_suite"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
||||||
edition = "2018"
|
edition = "2021"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
@@ -13,9 +13,9 @@ serde = { path = "../serde" }
|
|||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
automod = "1.0.1"
|
automod = "1.0.1"
|
||||||
fnv = "1.0"
|
foldhash = "0.2"
|
||||||
rustversion = "1.0"
|
rustversion = "1.0"
|
||||||
serde = { path = "../serde", features = ["rc"] }
|
serde = { path = "../serde", features = ["rc"] }
|
||||||
serde_derive = { path = "../serde_derive", features = ["deserialize_in_place"] }
|
serde_derive = { path = "../serde_derive", features = ["deserialize_in_place"] }
|
||||||
serde_test = "1.0.176"
|
serde_test = "1.0.176"
|
||||||
trybuild = { version = "1.0.66", features = ["diff"] }
|
trybuild = { version = "1.0.108", features = ["diff"] }
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
/target/
|
||||||
|
/Cargo.lock
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
name = "serde_derive_tests_no_std"
|
name = "serde_derive_tests_no_std"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
authors = ["David Tolnay <dtolnay@gmail.com>"]
|
authors = ["David Tolnay <dtolnay@gmail.com>"]
|
||||||
edition = "2018"
|
edition = "2021"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
@@ -10,4 +10,10 @@ libc = { version = "0.2", default-features = false }
|
|||||||
serde = { path = "../../serde", default-features = false }
|
serde = { path = "../../serde", default-features = false }
|
||||||
serde_derive = { path = "../../serde_derive" }
|
serde_derive = { path = "../../serde_derive" }
|
||||||
|
|
||||||
|
[profile.dev]
|
||||||
|
panic = "abort"
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
panic = "abort"
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
|
|||||||
@@ -1,15 +1,13 @@
|
|||||||
#![feature(lang_items, start)]
|
|
||||||
#![no_std]
|
#![no_std]
|
||||||
|
#![no_main]
|
||||||
|
|
||||||
#[start]
|
use core::ffi::c_int;
|
||||||
fn start(_argc: isize, _argv: *const *const u8) -> isize {
|
|
||||||
|
#[no_mangle]
|
||||||
|
extern "C" fn main(_argc: c_int, _argv: *const *const u8) -> c_int {
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
|
||||||
#[lang = "eh_personality"]
|
|
||||||
#[no_mangle]
|
|
||||||
pub extern "C" fn rust_eh_personality() {}
|
|
||||||
|
|
||||||
#[panic_handler]
|
#[panic_handler]
|
||||||
fn panic(_info: &core::panic::PanicInfo) -> ! {
|
fn panic(_info: &core::panic::PanicInfo) -> ! {
|
||||||
unsafe {
|
unsafe {
|
||||||
@@ -22,21 +20,21 @@ fn panic(_info: &core::panic::PanicInfo) -> ! {
|
|||||||
use serde_derive::{Deserialize, Serialize};
|
use serde_derive::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct Unit;
|
pub struct Unit;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct Newtype(u8);
|
pub struct Newtype(u8);
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct Tuple(u8, u8);
|
pub struct Tuple(u8, u8);
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct Struct {
|
pub struct Struct {
|
||||||
f: u8,
|
f: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
enum Enum {
|
pub enum Enum {
|
||||||
Unit,
|
Unit,
|
||||||
Newtype(u8),
|
Newtype(u8),
|
||||||
Tuple(u8, u8),
|
Tuple(u8, u8),
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#[cfg_attr(target_os = "emscripten", ignore)]
|
#[cfg_attr(target_os = "emscripten", ignore = "disabled on Emscripten")]
|
||||||
#[rustversion::attr(not(nightly), ignore)]
|
#[rustversion::attr(not(nightly), ignore = "requires nightly")]
|
||||||
#[cfg_attr(miri, ignore)]
|
#[cfg_attr(miri, ignore = "incompatible with miri")]
|
||||||
#[allow(unused_attributes)]
|
#[allow(unused_attributes)]
|
||||||
#[test]
|
#[test]
|
||||||
fn ui() {
|
fn ui() {
|
||||||
|
|||||||
@@ -34,9 +34,8 @@ macro_rules! hashset {
|
|||||||
$(set.insert($value);)+
|
$(set.insert($value);)+
|
||||||
set
|
set
|
||||||
}};
|
}};
|
||||||
($hasher:ident @ $($value:expr),+) => {{
|
($hasher:ty; $($value:expr),+) => {{
|
||||||
use std::hash::BuildHasherDefault;
|
let mut set = HashSet::<_, $hasher>::default();
|
||||||
let mut set = HashSet::with_hasher(BuildHasherDefault::<$hasher>::default());
|
|
||||||
$(set.insert($value);)+
|
$(set.insert($value);)+
|
||||||
set
|
set
|
||||||
}};
|
}};
|
||||||
@@ -51,9 +50,8 @@ macro_rules! hashmap {
|
|||||||
$(map.insert($key, $value);)+
|
$(map.insert($key, $value);)+
|
||||||
map
|
map
|
||||||
}};
|
}};
|
||||||
($hasher:ident @ $($key:expr => $value:expr),+) => {{
|
($hasher:ty; $($key:expr => $value:expr),+) => {{
|
||||||
use std::hash::BuildHasherDefault;
|
let mut map = HashMap::<_, _, $hasher>::default();
|
||||||
let mut map = HashMap::with_hasher(BuildHasherDefault::<$hasher>::default());
|
|
||||||
$(map.insert($key, $value);)+
|
$(map.insert($key, $value);)+
|
||||||
map
|
map
|
||||||
}};
|
}};
|
||||||
|
|||||||
@@ -0,0 +1,66 @@
|
|||||||
|
#![allow(dead_code)] // we do not read enum fields
|
||||||
|
|
||||||
|
use serde_derive::Deserialize;
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct Nested;
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub enum ExternallyTagged1 {
|
||||||
|
Tuple(f64, String),
|
||||||
|
Flatten {
|
||||||
|
#[serde(flatten)]
|
||||||
|
nested: Nested,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub enum ExternallyTagged2 {
|
||||||
|
Flatten {
|
||||||
|
#[serde(flatten)]
|
||||||
|
nested: Nested,
|
||||||
|
},
|
||||||
|
Tuple(f64, String),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Internally tagged enums cannot contain tuple variants so not tested here
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
#[serde(tag = "tag", content = "content")]
|
||||||
|
pub enum AdjacentlyTagged1 {
|
||||||
|
Tuple(f64, String),
|
||||||
|
Flatten {
|
||||||
|
#[serde(flatten)]
|
||||||
|
nested: Nested,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
#[serde(tag = "tag", content = "content")]
|
||||||
|
pub enum AdjacentlyTagged2 {
|
||||||
|
Flatten {
|
||||||
|
#[serde(flatten)]
|
||||||
|
nested: Nested,
|
||||||
|
},
|
||||||
|
Tuple(f64, String),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
#[serde(untagged)]
|
||||||
|
pub enum Untagged1 {
|
||||||
|
Tuple(f64, String),
|
||||||
|
Flatten {
|
||||||
|
#[serde(flatten)]
|
||||||
|
nested: Nested,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
#[serde(untagged)]
|
||||||
|
pub enum Untagged2 {
|
||||||
|
Flatten {
|
||||||
|
#[serde(flatten)]
|
||||||
|
nested: Nested,
|
||||||
|
},
|
||||||
|
Tuple(f64, String),
|
||||||
|
}
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
use serde_derive::Deserialize;
|
use serde_derive::Deserialize;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
@@ -7,7 +9,9 @@ pub struct Nested;
|
|||||||
pub enum ExternallyTagged {
|
pub enum ExternallyTagged {
|
||||||
Flatten {
|
Flatten {
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
|
#[allow(dead_code)]
|
||||||
nested: Nested,
|
nested: Nested,
|
||||||
|
#[allow(dead_code)]
|
||||||
string: &'static str,
|
string: &'static str,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -17,7 +21,9 @@ pub enum ExternallyTagged {
|
|||||||
pub enum InternallyTagged {
|
pub enum InternallyTagged {
|
||||||
Flatten {
|
Flatten {
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
|
#[allow(dead_code)]
|
||||||
nested: Nested,
|
nested: Nested,
|
||||||
|
#[allow(dead_code)]
|
||||||
string: &'static str,
|
string: &'static str,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -27,7 +33,9 @@ pub enum InternallyTagged {
|
|||||||
pub enum AdjacentlyTagged {
|
pub enum AdjacentlyTagged {
|
||||||
Flatten {
|
Flatten {
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
|
#[allow(dead_code)]
|
||||||
nested: Nested,
|
nested: Nested,
|
||||||
|
#[allow(dead_code)]
|
||||||
string: &'static str,
|
string: &'static str,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -37,7 +45,9 @@ pub enum AdjacentlyTagged {
|
|||||||
pub enum UntaggedWorkaround {
|
pub enum UntaggedWorkaround {
|
||||||
Flatten {
|
Flatten {
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
|
#[allow(dead_code)]
|
||||||
nested: Nested,
|
nested: Nested,
|
||||||
|
#[allow(dead_code)]
|
||||||
string: &'static str,
|
string: &'static str,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
use serde_derive::Deserialize;
|
use serde_derive::Deserialize;
|
||||||
|
|
||||||
macro_rules! bug {
|
macro_rules! bug {
|
||||||
|
|||||||
@@ -2,4 +2,5 @@ use serde_derive::Serialize;
|
|||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
#[serde()]
|
#[serde()]
|
||||||
|
#[allow(dead_code)]
|
||||||
pub struct S;
|
pub struct S;
|
||||||
|
|||||||
@@ -0,0 +1,48 @@
|
|||||||
|
use serde_derive::{Deserialize, Serialize};
|
||||||
|
use serde_test::{assert_tokens, Token};
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, PartialEq)]
|
||||||
|
enum Enum {
|
||||||
|
Simple {
|
||||||
|
a: i32,
|
||||||
|
},
|
||||||
|
Flatten {
|
||||||
|
#[serde(flatten)]
|
||||||
|
flatten: (),
|
||||||
|
a: i32,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn simple_variant() {
|
||||||
|
assert_tokens(
|
||||||
|
&Enum::Simple { a: 42 },
|
||||||
|
&[
|
||||||
|
Token::StructVariant {
|
||||||
|
name: "Enum",
|
||||||
|
variant: "Simple",
|
||||||
|
len: 1,
|
||||||
|
},
|
||||||
|
Token::Str("a"),
|
||||||
|
Token::I32(42),
|
||||||
|
Token::StructVariantEnd,
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn flatten_variant() {
|
||||||
|
assert_tokens(
|
||||||
|
&Enum::Flatten { flatten: (), a: 42 },
|
||||||
|
&[
|
||||||
|
Token::NewtypeVariant {
|
||||||
|
name: "Enum",
|
||||||
|
variant: "Flatten",
|
||||||
|
},
|
||||||
|
Token::Map { len: None },
|
||||||
|
Token::Str("a"),
|
||||||
|
Token::I32(42),
|
||||||
|
Token::MapEnd,
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
#![allow(dead_code)] // we do not read enum fields
|
||||||
|
|
||||||
|
use serde_derive::Deserialize;
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
#[serde(deny_unknown_fields)]
|
||||||
|
pub enum A {
|
||||||
|
B {
|
||||||
|
c: String,
|
||||||
|
},
|
||||||
|
D {
|
||||||
|
#[serde(flatten)]
|
||||||
|
e: E,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct E {}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
#![allow(clippy::trivially_copy_pass_by_ref, dead_code)]
|
||||||
|
|
||||||
|
use serde_derive::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
macro_rules! declare_in_macro {
|
||||||
|
($with:literal) => {
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub struct S {
|
||||||
|
#[serde(with = $with)]
|
||||||
|
f: i32,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
declare_in_macro!("with");
|
||||||
|
|
||||||
|
mod with {
|
||||||
|
use serde::{Deserializer, Serializer};
|
||||||
|
|
||||||
|
pub fn serialize<S>(_: &i32, _: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: Serializer,
|
||||||
|
{
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn deserialize<'de, D>(_: D) -> Result<i32, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
#![allow(clippy::trivially_copy_pass_by_ref, dead_code)]
|
||||||
|
|
||||||
|
use serde_derive::Deserialize;
|
||||||
|
|
||||||
|
macro_rules! declare_in_macro {
|
||||||
|
($with:literal) => {
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct S(
|
||||||
|
#[serde(with = $with)]
|
||||||
|
#[allow(dead_code)]
|
||||||
|
i32,
|
||||||
|
);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
declare_in_macro!("with");
|
||||||
|
|
||||||
|
mod with {
|
||||||
|
use serde::Deserializer;
|
||||||
|
|
||||||
|
pub fn deserialize<'de, D>(_: D) -> Result<i32, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
}
|
||||||
+1091
-1230
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