зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1511725 - Update string-cache-codegen.
Generated with cargo update -p string_cache_codegen and ./mach vendor rust. Differential Revision: https://phabricator.services.mozilla.com/D13649 --HG-- extra : moz-landing-system : lando
This commit is contained in:
Родитель
61a76f095d
Коммит
eed0162b65
|
@ -1943,11 +1943,6 @@ name = "quick-error"
|
|||
version = "1.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "0.3.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "0.5.2"
|
||||
|
@ -2326,18 +2321,19 @@ dependencies = [
|
|||
"phf_shared 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"precomputed-hash 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"string_cache_codegen 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"string_cache_codegen 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"string_cache_shared 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "string_cache_codegen"
|
||||
version = "0.4.0"
|
||||
version = "0.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"phf_generator 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"phf_shared 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"proc-macro2 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"quote 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"string_cache_shared 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
|
@ -3298,7 +3294,6 @@ dependencies = [
|
|||
"checksum proc-macro2 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)" = "cccdc7557a98fe98453030f077df7f3a042052fae465bb61d2c2c41435cfd9b6"
|
||||
"checksum procedural-masquerade 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9f566249236c6ca4340f7ca78968271f0ed2b0f234007a61b66f9ecd0af09260"
|
||||
"checksum quick-error 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "eda5fe9b71976e62bc81b781206aaa076401769b2143379d3eb2118388babac4"
|
||||
"checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a"
|
||||
"checksum quote 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9949cfe66888ffe1d53e6ec9d9f3b70714083854be20fd5e271b232a017401e8"
|
||||
"checksum quote 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e44651a0dc4cdd99f71c83b561e221f714912d11af1a4dff0631f923d53af035"
|
||||
"checksum rand 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)" = "15a732abf9d20f0ad8eeb6f909bf6868722d9a06e1e50802b6a70351f40b4eb1"
|
||||
|
@ -3342,7 +3337,7 @@ dependencies = [
|
|||
"checksum stable_deref_trait 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "15132e0e364248108c5e2c02e3ab539be8d6f5d52a01ca9bbf27ed657316f02b"
|
||||
"checksum string 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "00caf261d6f90f588f8450b8e1230fa0d5be49ee6140fdfbcb55335aff350970"
|
||||
"checksum string_cache 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "25d70109977172b127fe834e5449e5ab1740b9ba49fa18a2020f509174f25423"
|
||||
"checksum string_cache_codegen 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "479cde50c3539481f33906a387f2bd17c8e87cb848c35b6021d41fb81ff9b4d7"
|
||||
"checksum string_cache_codegen 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1eea1eee654ef80933142157fdad9dd8bc43cf7c74e999e369263496f04ff4da"
|
||||
"checksum string_cache_shared 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b1884d1bc09741d466d9b14e6d37ac89d6909cbcac41dd9ae982d4d063bbedfc"
|
||||
"checksum strsim 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b4d15c810519a91cf877e7e36e63fe068815c678181439f2f29e2562147c3694"
|
||||
"checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550"
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
{"files":{"Cargo.toml":"983b4a016a497eded8adc6a23963343693ab4520826a1bc916e6f8448c9bb309","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"c9a75f18b9ab2927829a208fc6aa2cf4e63b8420887ba29cdb265d6619ae82d5","README.md":"3390fa1362c73052a268ae97a4777c5bcd8e991f866f9cce88ddde8d0963fd4d","src/ident.rs":"830077b64dce8c8ede1fb6ab664cae72f5496f4ab6be21a5b4e3b5e4e57ec425","src/lib.rs":"f799c898057a4e8e1620b32c70e13d3fee7af3a0352a5a4a1da6393942d21cc7","src/to_tokens.rs":"a871cb84506fa9e0783ac29617d686327ce5d05a9711a3a10833430b00345ccc","src/tokens.rs":"9a6010d7c0c6ccbe8b7bb1dae99722c91fc0559ea6ac6ea03ab2baafc2ec77ae","tests/test.rs":"8951d46b62d6922cc5fc130759be9723a862947738dda25cc84ff8cfdfebf729"},"package":"7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a"}
|
|
@ -1,10 +0,0 @@
|
|||
[package]
|
||||
name = "quote"
|
||||
version = "0.3.15" # don't forget to update version in readme for breaking changes
|
||||
authors = ["David Tolnay <dtolnay@gmail.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
description = "Quasi-quoting macro quote!(...)"
|
||||
repository = "https://github.com/dtolnay/quote"
|
||||
documentation = "https://docs.rs/quote/"
|
||||
keywords = ["syn"]
|
||||
include = ["Cargo.toml", "src/**/*.rs", "tests/**/*.rs", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
|
|
@ -1,201 +0,0 @@
|
|||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
|
@ -1,25 +0,0 @@
|
|||
Copyright (c) 2016 The Rust Project Developers
|
||||
|
||||
Permission is hereby granted, free of charge, to any
|
||||
person obtaining a copy of this software and associated
|
||||
documentation files (the "Software"), to deal in the
|
||||
Software without restriction, including without
|
||||
limitation the rights to use, copy, modify, merge,
|
||||
publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software
|
||||
is furnished to do so, subject to the following
|
||||
conditions:
|
||||
|
||||
The above copyright notice and this permission notice
|
||||
shall be included in all copies or substantial portions
|
||||
of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
||||
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
||||
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
|
||||
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
|
||||
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
|
@ -1,104 +0,0 @@
|
|||
Rust Quasi-Quoting
|
||||
==================
|
||||
|
||||
[![Build Status](https://api.travis-ci.org/dtolnay/quote.svg?branch=master)](https://travis-ci.org/dtolnay/quote)
|
||||
[![Latest Version](https://img.shields.io/crates/v/quote.svg)](https://crates.io/crates/quote)
|
||||
[![Rust Documentation](https://img.shields.io/badge/api-rustdoc-blue.svg)](https://docs.rs/quote/)
|
||||
|
||||
Quasi-quoting without a Syntex dependency, intended for use with [Macros
|
||||
1.1](https://github.com/rust-lang/rfcs/blob/master/text/1681-macros-1.1.md).
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
quote = "0.3"
|
||||
```
|
||||
|
||||
```rust
|
||||
#[macro_use]
|
||||
extern crate quote;
|
||||
```
|
||||
|
||||
## What is quasi-quoting?
|
||||
|
||||
Quasi-quoting is a way of writing code and treating it as data, similar to
|
||||
writing code inside of a double-quoted string literal except more friendly to
|
||||
your text editor or IDE. It does not get in the way of syntax highlighting,
|
||||
brace matching, indentation, or autocompletion, all of which you would lose by
|
||||
writing code inside of double quotes.
|
||||
|
||||
Check out
|
||||
[my meetup talk](https://air.mozilla.org/rust-meetup-december-2016-12-15/)
|
||||
on the topic to learn more about the use case. Start the video at 3:00.
|
||||
|
||||
This crate is motivated by the Macros 1.1 use case, but is a general-purpose
|
||||
Rust quasi-quoting library and is not specific to procedural macros.
|
||||
|
||||
## Syntax
|
||||
|
||||
The quote crate provides a `quote!` macro within which you can write Rust code
|
||||
that gets packaged into a `quote::Tokens` and can be treated as data. You should
|
||||
think of `quote::Tokens` as representing a fragment of Rust source code. Call
|
||||
`to_string()` or `as_str()` on a Tokens to get back the fragment of source code
|
||||
as a string.
|
||||
|
||||
Within the `quote!` macro, interpolation is done with `#var`. Any type
|
||||
implementing the `quote::ToTokens` trait can be interpolated. This includes most
|
||||
Rust primitive types as well as most of the syntax tree types from
|
||||
[`syn`](https://github.com/dtolnay/syn).
|
||||
|
||||
```rust
|
||||
let tokens = quote! {
|
||||
struct SerializeWith #generics #where_clause {
|
||||
value: &'a #field_ty,
|
||||
phantom: ::std::marker::PhantomData<#item_ty>,
|
||||
}
|
||||
|
||||
impl #generics serde::Serialize for SerializeWith #generics #where_clause {
|
||||
fn serialize<S>(&self, s: &mut S) -> Result<(), S::Error>
|
||||
where S: serde::Serializer
|
||||
{
|
||||
#path(self.value, s)
|
||||
}
|
||||
}
|
||||
|
||||
SerializeWith {
|
||||
value: #value,
|
||||
phantom: ::std::marker::PhantomData::<#item_ty>,
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
Repetition is done using `#(...)*` or `#(...),*` very similar to `macro_rules!`:
|
||||
|
||||
- `#(#var)*` - no separators
|
||||
- `#(#var),*` - the character before the asterisk is used as a separator
|
||||
- `#( struct #var; )*` - the repetition can contain other things
|
||||
- `#( #k => println!("{}", #v), )*` - even multiple interpolations
|
||||
|
||||
Tokens can be interpolated into other quotes:
|
||||
|
||||
```rust
|
||||
let t = quote! { /* ... */ };
|
||||
return quote! { /* ... */ #t /* ... */ };
|
||||
```
|
||||
|
||||
The `quote!` macro relies on deep recursion so some large invocations may fail
|
||||
with "recursion limit reached" when you compile. If it fails, bump up the
|
||||
recursion limit by adding `#![recursion_limit = "128"]` to your crate. An even
|
||||
higher limit may be necessary for especially large invocations. You don't need
|
||||
this unless the compiler tells you that you need it.
|
||||
|
||||
## License
|
||||
|
||||
Licensed under either of
|
||||
|
||||
* Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
|
||||
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
|
||||
|
||||
at your option.
|
||||
|
||||
### Contribution
|
||||
|
||||
Unless you explicitly state otherwise, any contribution intentionally submitted
|
||||
for inclusion in this crate by you, as defined in the Apache-2.0 license, shall
|
||||
be dual licensed as above, without any additional terms or conditions.
|
|
@ -1,57 +0,0 @@
|
|||
use {Tokens, ToTokens};
|
||||
use std::borrow::Cow;
|
||||
use std::fmt;
|
||||
|
||||
/// An identifier that should be interpolated without quotes.
|
||||
#[derive(Debug, Clone, Eq, Hash)]
|
||||
pub struct Ident(String);
|
||||
|
||||
impl Ident {
|
||||
pub fn new<T: Into<Ident>>(t: T) -> Self {
|
||||
t.into()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a str> for Ident {
|
||||
fn from(s: &str) -> Self {
|
||||
Ident(s.to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<Cow<'a, str>> for Ident {
|
||||
fn from(s: Cow<'a, str>) -> Self {
|
||||
Ident(s.into_owned())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for Ident {
|
||||
fn from(s: String) -> Self {
|
||||
Ident(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<str> for Ident {
|
||||
fn as_ref(&self) -> &str {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Ident {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||
self.0.fmt(formatter)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> PartialEq<T> for Ident
|
||||
where T: AsRef<str>
|
||||
{
|
||||
fn eq(&self, other: &T) -> bool {
|
||||
self.0 == other.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl ToTokens for Ident {
|
||||
fn to_tokens(&self, tokens: &mut Tokens) {
|
||||
tokens.append(self.as_ref())
|
||||
}
|
||||
}
|
|
@ -1,252 +0,0 @@
|
|||
//! Quasi-quoting without a Syntex dependency, intended for use with [Macros
|
||||
//! 1.1](https://github.com/rust-lang/rfcs/blob/master/text/1681-macros-1.1.md).
|
||||
//!
|
||||
//! ```toml
|
||||
//! [dependencies]
|
||||
//! quote = "0.3"
|
||||
//! ```
|
||||
//!
|
||||
//! ```rust,ignore
|
||||
//! #[macro_use]
|
||||
//! extern crate quote;
|
||||
//! ```
|
||||
//!
|
||||
//! Interpolation is done with `#var`:
|
||||
//!
|
||||
//! ```text
|
||||
//! let tokens = quote! {
|
||||
//! struct SerializeWith #generics #where_clause {
|
||||
//! value: &'a #field_ty,
|
||||
//! phantom: ::std::marker::PhantomData<#item_ty>,
|
||||
//! }
|
||||
//!
|
||||
//! impl #generics serde::Serialize for SerializeWith #generics #where_clause {
|
||||
//! fn serialize<S>(&self, s: &mut S) -> Result<(), S::Error>
|
||||
//! where S: serde::Serializer
|
||||
//! {
|
||||
//! #path(self.value, s)
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! SerializeWith {
|
||||
//! value: #value,
|
||||
//! phantom: ::std::marker::PhantomData::<#item_ty>,
|
||||
//! }
|
||||
//! };
|
||||
//! ```
|
||||
//!
|
||||
//! Repetition is done using `#(...)*` or `#(...),*` very similar to `macro_rules!`:
|
||||
//!
|
||||
//! - `#(#var)*` - no separators
|
||||
//! - `#(#var),*` - the character before the asterisk is used as a separator
|
||||
//! - `#( struct #var; )*` - the repetition can contain other things
|
||||
//! - `#( #k => println!("{}", #v), )*` - even multiple interpolations
|
||||
//!
|
||||
//! The return type of `quote!` is `quote::Tokens`. Tokens can be interpolated into
|
||||
//! other quotes:
|
||||
//!
|
||||
//! ```text
|
||||
//! let t = quote! { /* ... */ };
|
||||
//! return quote! { /* ... */ #t /* ... */ };
|
||||
//! ```
|
||||
//!
|
||||
//! Call `to_string()` or `as_str()` on a Tokens to get a `String` or `&str` of Rust
|
||||
//! code.
|
||||
//!
|
||||
//! The `quote!` macro relies on deep recursion so some large invocations may fail
|
||||
//! with "recursion limit reached" when you compile. If it fails, bump up the
|
||||
//! recursion limit by adding `#![recursion_limit = "128"]` to your crate. An even
|
||||
//! higher limit may be necessary for especially large invocations.
|
||||
|
||||
mod tokens;
|
||||
pub use tokens::Tokens;
|
||||
|
||||
mod to_tokens;
|
||||
pub use to_tokens::{ToTokens, ByteStr, Hex};
|
||||
|
||||
mod ident;
|
||||
pub use ident::Ident;
|
||||
|
||||
/// The whole point.
|
||||
#[macro_export]
|
||||
macro_rules! quote {
|
||||
() => {
|
||||
$crate::Tokens::new()
|
||||
};
|
||||
|
||||
($($tt:tt)+) => {
|
||||
{
|
||||
let mut _s = $crate::Tokens::new();
|
||||
quote_each_token!(_s $($tt)*);
|
||||
_s
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Extract the names of all #metavariables and pass them to the $finish macro.
|
||||
//
|
||||
// in: pounded_var_names!(then () a #b c #( #d )* #e)
|
||||
// out: then!(() b d e)
|
||||
#[macro_export]
|
||||
#[doc(hidden)]
|
||||
macro_rules! pounded_var_names {
|
||||
($finish:ident ($($found:ident)*) # ( $($inner:tt)* ) $($rest:tt)*) => {
|
||||
pounded_var_names!($finish ($($found)*) $($inner)* $($rest)*)
|
||||
};
|
||||
|
||||
($finish:ident ($($found:ident)*) # [ $($inner:tt)* ] $($rest:tt)*) => {
|
||||
pounded_var_names!($finish ($($found)*) $($inner)* $($rest)*)
|
||||
};
|
||||
|
||||
($finish:ident ($($found:ident)*) # { $($inner:tt)* } $($rest:tt)*) => {
|
||||
pounded_var_names!($finish ($($found)*) $($inner)* $($rest)*)
|
||||
};
|
||||
|
||||
($finish:ident ($($found:ident)*) # $first:ident $($rest:tt)*) => {
|
||||
pounded_var_names!($finish ($($found)* $first) $($rest)*)
|
||||
};
|
||||
|
||||
($finish:ident ($($found:ident)*) ( $($inner:tt)* ) $($rest:tt)*) => {
|
||||
pounded_var_names!($finish ($($found)*) $($inner)* $($rest)*)
|
||||
};
|
||||
|
||||
($finish:ident ($($found:ident)*) [ $($inner:tt)* ] $($rest:tt)*) => {
|
||||
pounded_var_names!($finish ($($found)*) $($inner)* $($rest)*)
|
||||
};
|
||||
|
||||
($finish:ident ($($found:ident)*) { $($inner:tt)* } $($rest:tt)*) => {
|
||||
pounded_var_names!($finish ($($found)*) $($inner)* $($rest)*)
|
||||
};
|
||||
|
||||
($finish:ident ($($found:ident)*) $ignore:tt $($rest:tt)*) => {
|
||||
pounded_var_names!($finish ($($found)*) $($rest)*)
|
||||
};
|
||||
|
||||
($finish:ident ($($found:ident)*)) => {
|
||||
$finish!(() $($found)*)
|
||||
};
|
||||
}
|
||||
|
||||
// in: nested_tuples_pat!(() a b c d e)
|
||||
// out: ((((a b) c) d) e)
|
||||
//
|
||||
// in: nested_tuples_pat!(() a)
|
||||
// out: a
|
||||
#[macro_export]
|
||||
#[doc(hidden)]
|
||||
macro_rules! nested_tuples_pat {
|
||||
(()) => {
|
||||
&()
|
||||
};
|
||||
|
||||
(() $first:ident $($rest:ident)*) => {
|
||||
nested_tuples_pat!(($first) $($rest)*)
|
||||
};
|
||||
|
||||
(($pat:pat) $first:ident $($rest:ident)*) => {
|
||||
nested_tuples_pat!((($pat, $first)) $($rest)*)
|
||||
};
|
||||
|
||||
(($done:pat)) => {
|
||||
$done
|
||||
};
|
||||
}
|
||||
|
||||
// in: multi_zip_expr!(() a b c d e)
|
||||
// out: a.into_iter().zip(b).zip(c).zip(d).zip(e)
|
||||
//
|
||||
// in: multi_zip_iter!(() a)
|
||||
// out: a
|
||||
#[macro_export]
|
||||
#[doc(hidden)]
|
||||
macro_rules! multi_zip_expr {
|
||||
(()) => {
|
||||
&[]
|
||||
};
|
||||
|
||||
(() $single:ident) => {
|
||||
$single
|
||||
};
|
||||
|
||||
(() $first:ident $($rest:ident)*) => {
|
||||
multi_zip_expr!(($first.into_iter()) $($rest)*)
|
||||
};
|
||||
|
||||
(($zips:expr) $first:ident $($rest:ident)*) => {
|
||||
multi_zip_expr!(($zips.zip($first)) $($rest)*)
|
||||
};
|
||||
|
||||
(($done:expr)) => {
|
||||
$done
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
#[doc(hidden)]
|
||||
macro_rules! quote_each_token {
|
||||
($tokens:ident) => {};
|
||||
|
||||
($tokens:ident # ! $($rest:tt)*) => {
|
||||
$tokens.append("#");
|
||||
$tokens.append("!");
|
||||
quote_each_token!($tokens $($rest)*);
|
||||
};
|
||||
|
||||
($tokens:ident # ( $($inner:tt)* ) * $($rest:tt)*) => {
|
||||
for pounded_var_names!(nested_tuples_pat () $($inner)*)
|
||||
in pounded_var_names!(multi_zip_expr () $($inner)*) {
|
||||
quote_each_token!($tokens $($inner)*);
|
||||
}
|
||||
quote_each_token!($tokens $($rest)*);
|
||||
};
|
||||
|
||||
($tokens:ident # ( $($inner:tt)* ) $sep:tt * $($rest:tt)*) => {
|
||||
for (_i, pounded_var_names!(nested_tuples_pat () $($inner)*))
|
||||
in pounded_var_names!(multi_zip_expr () $($inner)*).into_iter().enumerate() {
|
||||
if _i > 0 {
|
||||
$tokens.append(stringify!($sep));
|
||||
}
|
||||
quote_each_token!($tokens $($inner)*);
|
||||
}
|
||||
quote_each_token!($tokens $($rest)*);
|
||||
};
|
||||
|
||||
($tokens:ident # [ $($inner:tt)* ] $($rest:tt)*) => {
|
||||
$tokens.append("#");
|
||||
$tokens.append("[");
|
||||
quote_each_token!($tokens $($inner)*);
|
||||
$tokens.append("]");
|
||||
quote_each_token!($tokens $($rest)*);
|
||||
};
|
||||
|
||||
($tokens:ident # $first:ident $($rest:tt)*) => {
|
||||
$crate::ToTokens::to_tokens(&$first, &mut $tokens);
|
||||
quote_each_token!($tokens $($rest)*);
|
||||
};
|
||||
|
||||
($tokens:ident ( $($first:tt)* ) $($rest:tt)*) => {
|
||||
$tokens.append("(");
|
||||
quote_each_token!($tokens $($first)*);
|
||||
$tokens.append(")");
|
||||
quote_each_token!($tokens $($rest)*);
|
||||
};
|
||||
|
||||
($tokens:ident [ $($first:tt)* ] $($rest:tt)*) => {
|
||||
$tokens.append("[");
|
||||
quote_each_token!($tokens $($first)*);
|
||||
$tokens.append("]");
|
||||
quote_each_token!($tokens $($rest)*);
|
||||
};
|
||||
|
||||
($tokens:ident { $($first:tt)* } $($rest:tt)*) => {
|
||||
$tokens.append("{");
|
||||
quote_each_token!($tokens $($first)*);
|
||||
$tokens.append("}");
|
||||
quote_each_token!($tokens $($rest)*);
|
||||
};
|
||||
|
||||
($tokens:ident $first:tt $($rest:tt)*) => {
|
||||
$tokens.append(stringify!($first));
|
||||
quote_each_token!($tokens $($rest)*);
|
||||
};
|
||||
}
|
|
@ -1,357 +0,0 @@
|
|||
use super::Tokens;
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
/// Types that can be interpolated inside a `quote!(...)` invocation.
|
||||
pub trait ToTokens {
|
||||
/// Write `self` to the given `Tokens`.
|
||||
///
|
||||
/// Example implementation for a struct representing Rust paths like
|
||||
/// `std::cmp::PartialEq`:
|
||||
///
|
||||
/// ```ignore
|
||||
/// pub struct Path {
|
||||
/// pub global: bool,
|
||||
/// pub segments: Vec<PathSegment>,
|
||||
/// }
|
||||
///
|
||||
/// impl ToTokens for Path {
|
||||
/// fn to_tokens(&self, tokens: &mut Tokens) {
|
||||
/// for (i, segment) in self.segments.iter().enumerate() {
|
||||
/// if i > 0 || self.global {
|
||||
/// tokens.append("::");
|
||||
/// }
|
||||
/// segment.to_tokens(tokens);
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
fn to_tokens(&self, &mut Tokens);
|
||||
}
|
||||
|
||||
impl<'a, T: ?Sized + ToTokens> ToTokens for &'a T {
|
||||
fn to_tokens(&self, tokens: &mut Tokens) {
|
||||
(**self).to_tokens(tokens);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: ?Sized + ToOwned + ToTokens> ToTokens for Cow<'a, T> {
|
||||
fn to_tokens(&self, tokens: &mut Tokens) {
|
||||
(**self).to_tokens(tokens);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized + ToTokens> ToTokens for Box<T> {
|
||||
fn to_tokens(&self, tokens: &mut Tokens) {
|
||||
(**self).to_tokens(tokens);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ToTokens> ToTokens for Option<T> {
|
||||
fn to_tokens(&self, tokens: &mut Tokens) {
|
||||
if let Some(ref t) = *self {
|
||||
t.to_tokens(tokens);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ToTokens for str {
|
||||
fn to_tokens(&self, tokens: &mut Tokens) {
|
||||
let mut escaped = "\"".to_string();
|
||||
for ch in self.chars() {
|
||||
match ch {
|
||||
'\0' => escaped.push_str(r"\0"),
|
||||
'\'' => escaped.push_str("'"),
|
||||
_ => escaped.extend(ch.escape_default().map(|c| c as char)),
|
||||
}
|
||||
}
|
||||
escaped.push('"');
|
||||
|
||||
tokens.append(&escaped);
|
||||
}
|
||||
}
|
||||
|
||||
impl ToTokens for String {
|
||||
fn to_tokens(&self, tokens: &mut Tokens) {
|
||||
self.as_str().to_tokens(tokens);
|
||||
}
|
||||
}
|
||||
|
||||
impl ToTokens for char {
|
||||
fn to_tokens(&self, tokens: &mut Tokens) {
|
||||
match *self {
|
||||
'\0' => tokens.append(r"'\0'"),
|
||||
'"' => tokens.append("'\"'"),
|
||||
_ => tokens.append(&format!("{:?}", self)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Wrap a `&str` so it interpolates as a byte-string: `b"abc"`.
|
||||
#[derive(Debug)]
|
||||
pub struct ByteStr<'a>(pub &'a str);
|
||||
|
||||
impl<'a> ToTokens for ByteStr<'a> {
|
||||
fn to_tokens(&self, tokens: &mut Tokens) {
|
||||
let mut escaped = "b\"".to_string();
|
||||
for b in self.0.bytes() {
|
||||
match b {
|
||||
b'\0' => escaped.push_str(r"\0"),
|
||||
b'\t' => escaped.push_str(r"\t"),
|
||||
b'\n' => escaped.push_str(r"\n"),
|
||||
b'\r' => escaped.push_str(r"\r"),
|
||||
b'"' => escaped.push_str("\\\""),
|
||||
b'\\' => escaped.push_str("\\\\"),
|
||||
b'\x20' ... b'\x7E' => escaped.push(b as char),
|
||||
_ => escaped.push_str(&format!("\\x{:02X}", b)),
|
||||
}
|
||||
}
|
||||
escaped.push('"');
|
||||
|
||||
tokens.append(&escaped);
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_to_tokens_display {
|
||||
($ty:ty) => {
|
||||
impl ToTokens for $ty {
|
||||
fn to_tokens(&self, tokens: &mut Tokens) {
|
||||
tokens.append(&self.to_string());
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl_to_tokens_display!(Tokens);
|
||||
impl_to_tokens_display!(bool);
|
||||
|
||||
/// Wrap an integer so it interpolates as a hexadecimal.
|
||||
#[derive(Debug)]
|
||||
pub struct Hex<T>(pub T);
|
||||
|
||||
macro_rules! impl_to_tokens_integer {
|
||||
($ty:ty) => {
|
||||
impl ToTokens for $ty {
|
||||
fn to_tokens(&self, tokens: &mut Tokens) {
|
||||
tokens.append(&format!(concat!("{}", stringify!($ty)), self));
|
||||
}
|
||||
}
|
||||
|
||||
impl ToTokens for Hex<$ty> {
|
||||
fn to_tokens(&self, tokens: &mut Tokens) {
|
||||
tokens.append(&format!(concat!("0x{:X}", stringify!($ty)), self.0));
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl_to_tokens_integer!(i8);
|
||||
impl_to_tokens_integer!(i16);
|
||||
impl_to_tokens_integer!(i32);
|
||||
impl_to_tokens_integer!(i64);
|
||||
impl_to_tokens_integer!(isize);
|
||||
impl_to_tokens_integer!(u8);
|
||||
impl_to_tokens_integer!(u16);
|
||||
impl_to_tokens_integer!(u32);
|
||||
impl_to_tokens_integer!(u64);
|
||||
impl_to_tokens_integer!(usize);
|
||||
|
||||
macro_rules! impl_to_tokens_floating {
|
||||
($ty:ty) => {
|
||||
impl ToTokens for $ty {
|
||||
fn to_tokens(&self, tokens: &mut Tokens) {
|
||||
use std::num::FpCategory::*;
|
||||
match self.classify() {
|
||||
Zero | Subnormal | Normal => {
|
||||
tokens.append(&format!(concat!("{}", stringify!($ty)), self));
|
||||
}
|
||||
Nan => {
|
||||
tokens.append("::");
|
||||
tokens.append("std");
|
||||
tokens.append("::");
|
||||
tokens.append(stringify!($ty));
|
||||
tokens.append("::");
|
||||
tokens.append("NAN");
|
||||
}
|
||||
Infinite => {
|
||||
tokens.append("::");
|
||||
tokens.append("std");
|
||||
tokens.append("::");
|
||||
tokens.append(stringify!($ty));
|
||||
tokens.append("::");
|
||||
if self.is_sign_positive() {
|
||||
tokens.append("INFINITY");
|
||||
} else {
|
||||
tokens.append("NEG_INFINITY");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
impl_to_tokens_floating!(f32);
|
||||
impl_to_tokens_floating!(f64);
|
||||
|
||||
impl<T: ToTokens> ToTokens for [T] {
|
||||
fn to_tokens(&self, tokens: &mut Tokens) {
|
||||
tokens.append("[");
|
||||
for item in self {
|
||||
item.to_tokens(tokens);
|
||||
tokens.append(",");
|
||||
}
|
||||
tokens.append("]");
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ToTokens> ToTokens for Vec<T> {
|
||||
fn to_tokens(&self, tokens: &mut Tokens) {
|
||||
self[..].to_tokens(tokens)
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! array_impls {
|
||||
($($N:expr)+) => {
|
||||
$(
|
||||
impl<T: ToTokens> ToTokens for [T; $N] {
|
||||
fn to_tokens(&self, tokens: &mut Tokens) {
|
||||
self[..].to_tokens(tokens)
|
||||
}
|
||||
}
|
||||
)+
|
||||
}
|
||||
}
|
||||
|
||||
array_impls! {
|
||||
0 1 2 3 4 5 6 7 8 9
|
||||
10 11 12 13 14 15 16 17 18 19
|
||||
20 21 22 23 24 25 26 27 28 29
|
||||
30 31 32
|
||||
}
|
||||
|
||||
macro_rules! tuple_impls {
|
||||
($(
|
||||
$Tuple:ident {
|
||||
$(($idx:tt) -> $T:ident)*
|
||||
}
|
||||
)+) => {
|
||||
$(
|
||||
impl<$($T: ToTokens),*> ToTokens for ($($T,)*) {
|
||||
fn to_tokens(&self, tokens: &mut Tokens) {
|
||||
tokens.append("(");
|
||||
$(
|
||||
self.$idx.to_tokens(tokens);
|
||||
tokens.append(",");
|
||||
)*
|
||||
tokens.append(")");
|
||||
}
|
||||
}
|
||||
)+
|
||||
}
|
||||
}
|
||||
|
||||
tuple_impls! {
|
||||
Tuple0 {}
|
||||
Tuple1 {
|
||||
(0) -> A
|
||||
}
|
||||
Tuple2 {
|
||||
(0) -> A
|
||||
(1) -> B
|
||||
}
|
||||
Tuple3 {
|
||||
(0) -> A
|
||||
(1) -> B
|
||||
(2) -> C
|
||||
}
|
||||
Tuple4 {
|
||||
(0) -> A
|
||||
(1) -> B
|
||||
(2) -> C
|
||||
(3) -> D
|
||||
}
|
||||
Tuple5 {
|
||||
(0) -> A
|
||||
(1) -> B
|
||||
(2) -> C
|
||||
(3) -> D
|
||||
(4) -> E
|
||||
}
|
||||
Tuple6 {
|
||||
(0) -> A
|
||||
(1) -> B
|
||||
(2) -> C
|
||||
(3) -> D
|
||||
(4) -> E
|
||||
(5) -> F
|
||||
}
|
||||
Tuple7 {
|
||||
(0) -> A
|
||||
(1) -> B
|
||||
(2) -> C
|
||||
(3) -> D
|
||||
(4) -> E
|
||||
(5) -> F
|
||||
(6) -> G
|
||||
}
|
||||
Tuple8 {
|
||||
(0) -> A
|
||||
(1) -> B
|
||||
(2) -> C
|
||||
(3) -> D
|
||||
(4) -> E
|
||||
(5) -> F
|
||||
(6) -> G
|
||||
(7) -> H
|
||||
}
|
||||
Tuple9 {
|
||||
(0) -> A
|
||||
(1) -> B
|
||||
(2) -> C
|
||||
(3) -> D
|
||||
(4) -> E
|
||||
(5) -> F
|
||||
(6) -> G
|
||||
(7) -> H
|
||||
(8) -> I
|
||||
}
|
||||
Tuple10 {
|
||||
(0) -> A
|
||||
(1) -> B
|
||||
(2) -> C
|
||||
(3) -> D
|
||||
(4) -> E
|
||||
(5) -> F
|
||||
(6) -> G
|
||||
(7) -> H
|
||||
(8) -> I
|
||||
(9) -> J
|
||||
}
|
||||
Tuple11 {
|
||||
(0) -> A
|
||||
(1) -> B
|
||||
(2) -> C
|
||||
(3) -> D
|
||||
(4) -> E
|
||||
(5) -> F
|
||||
(6) -> G
|
||||
(7) -> H
|
||||
(8) -> I
|
||||
(9) -> J
|
||||
(10) -> K
|
||||
}
|
||||
Tuple12 {
|
||||
(0) -> A
|
||||
(1) -> B
|
||||
(2) -> C
|
||||
(3) -> D
|
||||
(4) -> E
|
||||
(5) -> F
|
||||
(6) -> G
|
||||
(7) -> H
|
||||
(8) -> I
|
||||
(9) -> J
|
||||
(10) -> K
|
||||
(11) -> L
|
||||
}
|
||||
}
|
|
@ -1,156 +0,0 @@
|
|||
use super::ToTokens;
|
||||
use std::fmt::{self, Display};
|
||||
use std::str::FromStr;
|
||||
|
||||
/// Tokens produced by a `quote!(...)` invocation.
|
||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||
pub struct Tokens(String);
|
||||
|
||||
impl Tokens {
|
||||
/// Empty tokens.
|
||||
pub fn new() -> Self {
|
||||
Tokens(String::new())
|
||||
}
|
||||
|
||||
/// For use by `ToTokens` implementations.
|
||||
///
|
||||
/// ```
|
||||
/// # #[macro_use] extern crate quote;
|
||||
/// # use quote::{Tokens, ToTokens};
|
||||
/// # fn main() {
|
||||
/// struct X;
|
||||
///
|
||||
/// impl ToTokens for X {
|
||||
/// fn to_tokens(&self, tokens: &mut Tokens) {
|
||||
/// tokens.append("a");
|
||||
/// tokens.append("b");
|
||||
/// tokens.append("c");
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// let tokens = quote!(#X);
|
||||
/// assert_eq!(tokens.as_str(), "a b c");
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn append<T: AsRef<str>>(&mut self, token: T) {
|
||||
if !self.0.is_empty() && !token.as_ref().is_empty() {
|
||||
self.0.push(' ');
|
||||
}
|
||||
self.0.push_str(token.as_ref());
|
||||
}
|
||||
|
||||
/// For use by `ToTokens` implementations.
|
||||
///
|
||||
/// ```
|
||||
/// # #[macro_use] extern crate quote;
|
||||
/// # use quote::{Tokens, ToTokens};
|
||||
/// # fn main() {
|
||||
/// struct X;
|
||||
///
|
||||
/// impl ToTokens for X {
|
||||
/// fn to_tokens(&self, tokens: &mut Tokens) {
|
||||
/// tokens.append_all(&[true, false]);
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// let tokens = quote!(#X);
|
||||
/// assert_eq!(tokens.as_str(), "true false");
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn append_all<T, I>(&mut self, iter: I)
|
||||
where T: ToTokens,
|
||||
I: IntoIterator<Item = T>
|
||||
{
|
||||
for token in iter {
|
||||
token.to_tokens(self);
|
||||
}
|
||||
}
|
||||
|
||||
/// For use by `ToTokens` implementations.
|
||||
///
|
||||
/// ```
|
||||
/// # #[macro_use] extern crate quote;
|
||||
/// # use quote::{Tokens, ToTokens};
|
||||
/// # fn main() {
|
||||
/// struct X;
|
||||
///
|
||||
/// impl ToTokens for X {
|
||||
/// fn to_tokens(&self, tokens: &mut Tokens) {
|
||||
/// tokens.append_separated(&[true, false], ",");
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// let tokens = quote!(#X);
|
||||
/// assert_eq!(tokens.as_str(), "true , false");
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn append_separated<T, I, S: AsRef<str>>(&mut self, iter: I, sep: S)
|
||||
where T: ToTokens,
|
||||
I: IntoIterator<Item = T>
|
||||
{
|
||||
for (i, token) in iter.into_iter().enumerate() {
|
||||
if i > 0 {
|
||||
self.append(sep.as_ref());
|
||||
}
|
||||
token.to_tokens(self);
|
||||
}
|
||||
}
|
||||
|
||||
/// For use by `ToTokens` implementations.
|
||||
///
|
||||
/// ```
|
||||
/// # #[macro_use] extern crate quote;
|
||||
/// # use quote::{Tokens, ToTokens};
|
||||
/// # fn main() {
|
||||
/// struct X;
|
||||
///
|
||||
/// impl ToTokens for X {
|
||||
/// fn to_tokens(&self, tokens: &mut Tokens) {
|
||||
/// tokens.append_terminated(&[true, false], ",");
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// let tokens = quote!(#X);
|
||||
/// assert_eq!(tokens.as_str(), "true , false ,");
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn append_terminated<T, I, S: AsRef<str>>(&mut self, iter: I, term: S)
|
||||
where T: ToTokens,
|
||||
I: IntoIterator<Item = T>
|
||||
{
|
||||
for token in iter {
|
||||
token.to_tokens(self);
|
||||
self.append(term.as_ref());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_str(&self) -> &str {
|
||||
&self.0
|
||||
}
|
||||
|
||||
pub fn into_string(self) -> String {
|
||||
self.0
|
||||
}
|
||||
|
||||
pub fn parse<T: FromStr>(&self) -> Result<T, T::Err> {
|
||||
FromStr::from_str(&self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Tokens {
|
||||
fn default() -> Self {
|
||||
Tokens::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Tokens {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||
self.0.fmt(formatter)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<str> for Tokens {
|
||||
fn as_ref(&self) -> &str {
|
||||
&self.0
|
||||
}
|
||||
}
|
|
@ -1,360 +0,0 @@
|
|||
use std::{f32, f64};
|
||||
use std::borrow::Cow;
|
||||
|
||||
#[macro_use]
|
||||
extern crate quote;
|
||||
|
||||
struct X;
|
||||
|
||||
impl quote::ToTokens for X {
|
||||
fn to_tokens(&self, tokens: &mut quote::Tokens) {
|
||||
tokens.append("X");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_quote_impl() {
|
||||
let tokens = quote!(
|
||||
impl<'a, T: ToTokens> ToTokens for &'a T {
|
||||
fn to_tokens(&self, tokens: &mut Tokens) {
|
||||
(**self).to_tokens(tokens)
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
let expected = concat!(
|
||||
"impl < 'a , T : ToTokens > ToTokens for & 'a T { ",
|
||||
"fn to_tokens ( & self , tokens : & mut Tokens ) { ",
|
||||
"( * * self ) . to_tokens ( tokens ) ",
|
||||
"} ",
|
||||
"}"
|
||||
);
|
||||
|
||||
assert_eq!(expected, tokens.as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_append_tokens() {
|
||||
let mut tokens = quote!(let x =);
|
||||
tokens.append(quote!("Hello World!";));
|
||||
|
||||
let expected = "let x = \"Hello World!\" ;";
|
||||
|
||||
assert_eq!(expected, tokens.as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_substitution() {
|
||||
let x = X;
|
||||
let tokens = quote!(#x <#x> (#x) [#x] {#x});
|
||||
|
||||
let expected = "X < X > ( X ) [ X ] { X }";
|
||||
|
||||
assert_eq!(expected, tokens.as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iter() {
|
||||
let primes = &[X, X, X, X];
|
||||
|
||||
assert_eq!("X X X X", quote!(#(#primes)*).as_str());
|
||||
|
||||
assert_eq!("X , X , X , X ,", quote!(#(#primes,)*).as_str());
|
||||
|
||||
assert_eq!("X , X , X , X", quote!(#(#primes),*).as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_advanced() {
|
||||
let generics = quote!( <'a, T> );
|
||||
|
||||
let where_clause = quote!( where T: Serialize );
|
||||
|
||||
let field_ty = quote!( String );
|
||||
|
||||
let item_ty = quote!( Cow<'a, str> );
|
||||
|
||||
let path = quote!( SomeTrait::serialize_with );
|
||||
|
||||
let value = quote!( self.x );
|
||||
|
||||
let tokens = quote! {
|
||||
struct SerializeWith #generics #where_clause {
|
||||
value: &'a #field_ty,
|
||||
phantom: ::std::marker::PhantomData<#item_ty>,
|
||||
}
|
||||
|
||||
impl #generics ::serde::Serialize for SerializeWith #generics #where_clause {
|
||||
fn serialize<S>(&self, s: &mut S) -> Result<(), S::Error>
|
||||
where S: ::serde::Serializer
|
||||
{
|
||||
#path(self.value, s)
|
||||
}
|
||||
}
|
||||
|
||||
SerializeWith {
|
||||
value: #value,
|
||||
phantom: ::std::marker::PhantomData::<#item_ty>,
|
||||
}
|
||||
};
|
||||
|
||||
let expected = concat!(
|
||||
"struct SerializeWith < 'a , T > where T : Serialize { ",
|
||||
"value : & 'a String , ",
|
||||
"phantom : :: std :: marker :: PhantomData < Cow < 'a , str > > , ",
|
||||
"} ",
|
||||
"impl < 'a , T > :: serde :: Serialize for SerializeWith < 'a , T > where T : Serialize { ",
|
||||
"fn serialize < S > ( & self , s : & mut S ) -> Result < ( ) , S :: Error > ",
|
||||
"where S : :: serde :: Serializer ",
|
||||
"{ ",
|
||||
"SomeTrait :: serialize_with ( self . value , s ) ",
|
||||
"} ",
|
||||
"} ",
|
||||
"SerializeWith { ",
|
||||
"value : self . x , ",
|
||||
"phantom : :: std :: marker :: PhantomData :: < Cow < 'a , str > > , ",
|
||||
"}"
|
||||
);
|
||||
|
||||
assert_eq!(expected, tokens.as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_unit() {
|
||||
let x = ();
|
||||
let tokens = quote!(#x);
|
||||
let expected = "( )";
|
||||
assert_eq!(expected, tokens.as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tuple() {
|
||||
let x = ("foo", 4_u32);
|
||||
let tokens = quote!(#x);
|
||||
let expected = "( \"foo\" , 4u32 , )";
|
||||
assert_eq!(expected, tokens.as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_array() {
|
||||
let x: [u32; 3] = [1, 2, 3];
|
||||
let tokens = quote!(#x);
|
||||
let expected = "[ 1u32 , 2u32 , 3u32 , ]";
|
||||
assert_eq!(expected, tokens.as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_slice() {
|
||||
let x: &[u32] = &[1, 2, 3];
|
||||
let tokens = quote!(&#x); // Note: explicit `&`
|
||||
let expected = "& [ 1u32 , 2u32 , 3u32 , ]";
|
||||
assert_eq!(expected, tokens.as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_vec() {
|
||||
let x: Vec<u32> = vec![1, 2, 3];
|
||||
let tokens = quote!(vec!#x); // Note: explicit `vec!`
|
||||
let expected = "vec ! [ 1u32 , 2u32 , 3u32 , ]";
|
||||
assert_eq!(expected, tokens.as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_integer() {
|
||||
let ii8 = -1i8;
|
||||
let ii16 = -1i16;
|
||||
let ii32 = -1i32;
|
||||
let ii64 = -1i64;
|
||||
let iisize = -1isize;
|
||||
let uu8 = 1u8;
|
||||
let uu16 = 1u16;
|
||||
let uu32 = 1u32;
|
||||
let uu64 = 1u64;
|
||||
let uusize = 1usize;
|
||||
|
||||
let tokens = quote! {
|
||||
#ii8 #ii16 #ii32 #ii64 #iisize
|
||||
#uu8 #uu16 #uu32 #uu64 #uusize
|
||||
};
|
||||
let expected = "-1i8 -1i16 -1i32 -1i64 -1isize 1u8 1u16 1u32 1u64 1usize";
|
||||
assert_eq!(expected, tokens.as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hex() {
|
||||
let hex = quote::Hex(0xFFFF_0000_u32);
|
||||
let tokens = quote!(#hex);
|
||||
let expected = "0xFFFF0000u32";
|
||||
assert_eq!(expected, tokens.as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_floating() {
|
||||
let e32 = 2.71828f32;
|
||||
let nan32 = f32::NAN;
|
||||
let inf32 = f32::INFINITY;
|
||||
let neginf32 = f32::NEG_INFINITY;
|
||||
|
||||
let e64 = 2.71828f64;
|
||||
let nan64 = f64::NAN;
|
||||
let inf64 = f64::INFINITY;
|
||||
let neginf64 = f64::NEG_INFINITY;
|
||||
|
||||
let tokens = quote! {
|
||||
#e32 @ #nan32 @ #inf32 @ #neginf32
|
||||
#e64 @ #nan64 @ #inf64 @ #neginf64
|
||||
};
|
||||
let expected = concat!(
|
||||
"2.71828f32 @ :: std :: f32 :: NAN @ :: std :: f32 :: INFINITY @ :: std :: f32 :: NEG_INFINITY ",
|
||||
"2.71828f64 @ :: std :: f64 :: NAN @ :: std :: f64 :: INFINITY @ :: std :: f64 :: NEG_INFINITY",
|
||||
);
|
||||
assert_eq!(expected, tokens.as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_char() {
|
||||
let zero = '\0';
|
||||
let pound = '#';
|
||||
let quote = '"';
|
||||
let apost = '\'';
|
||||
let newline = '\n';
|
||||
let heart = '\u{2764}';
|
||||
|
||||
let tokens = quote! {
|
||||
#zero #pound #quote #apost #newline #heart
|
||||
};
|
||||
let expected = "'\\0' '#' '\"' '\\'' '\\n' '\u{2764}'";
|
||||
assert_eq!(expected, tokens.as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_str() {
|
||||
let s = "\0 a 'b \" c";
|
||||
let tokens = quote!(#s);
|
||||
let expected = "\"\\0 a 'b \\\" c\"";
|
||||
assert_eq!(expected, tokens.as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_string() {
|
||||
let s = "\0 a 'b \" c".to_string();
|
||||
let tokens = quote!(#s);
|
||||
let expected = "\"\\0 a 'b \\\" c\"";
|
||||
assert_eq!(expected, tokens.as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_byte_str() {
|
||||
let s = quote::ByteStr("\0 a 'b \" c");
|
||||
let tokens = quote!(#s);
|
||||
let expected = "b\"\\0 a 'b \\\" c\"";
|
||||
assert_eq!(expected, tokens.as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_byte_str_escape() {
|
||||
let s = quote::ByteStr("\u{3c3} \\ \" \n");
|
||||
let tokens = quote!(#s);
|
||||
let expected = "b\"\\xCF\\x83 \\\\ \\\" \\n\"";
|
||||
assert_eq!(expected, tokens.as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ident() {
|
||||
let foo = quote::Ident::from("Foo");
|
||||
let bar = quote::Ident::from(format!("Bar{}", 7));
|
||||
let tokens = quote!(struct #foo; enum #bar {});
|
||||
let expected = "struct Foo ; enum Bar7 { }";
|
||||
assert_eq!(expected, tokens.as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_duplicate() {
|
||||
let ch = 'x';
|
||||
|
||||
let tokens = quote!(#ch #ch);
|
||||
|
||||
let expected = "'x' 'x'";
|
||||
assert_eq!(expected, tokens.as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fancy_repetition() {
|
||||
let foo = vec!["a", "b"];
|
||||
let bar = vec![true, false];
|
||||
|
||||
let tokens = quote! {
|
||||
#(#foo: #bar),*
|
||||
};
|
||||
|
||||
let expected = r#""a" : true , "b" : false"#;
|
||||
assert_eq!(expected, tokens.as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_nested_fancy_repetition() {
|
||||
let nested = vec![vec!['a', 'b', 'c'], vec!['x', 'y', 'z']];
|
||||
|
||||
let tokens = quote! {
|
||||
#(
|
||||
#(#nested)*
|
||||
),*
|
||||
};
|
||||
|
||||
let expected = "'a' 'b' 'c' , 'x' 'y' 'z'";
|
||||
assert_eq!(expected, tokens.as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_empty_repetition() {
|
||||
let tokens = quote!(#(a b)* #(c d),*);
|
||||
assert_eq!("", tokens.as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_variable_name_conflict() {
|
||||
// The implementation of `#(...),*` uses the variable `_i` but it should be
|
||||
// fine, if a little confusing when debugging.
|
||||
let _i = vec!['a', 'b'];
|
||||
let tokens = quote! { #(#_i),* };
|
||||
let expected = "'a' , 'b'";
|
||||
assert_eq!(expected, tokens.as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_empty_quote() {
|
||||
let tokens = quote!();
|
||||
assert_eq!("", tokens.as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_box_str() {
|
||||
let b = "str".to_owned().into_boxed_str();
|
||||
let tokens = quote! { #b };
|
||||
assert_eq!("\"str\"", tokens.as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cow() {
|
||||
let owned: Cow<quote::Ident> = Cow::Owned(quote::Ident::from("owned"));
|
||||
|
||||
let ident = quote::Ident::from("borrowed");
|
||||
let borrowed = Cow::Borrowed(&ident);
|
||||
|
||||
let tokens = quote! { #owned #borrowed };
|
||||
assert_eq!("owned borrowed", tokens.as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_closure() {
|
||||
fn field_i(i: usize) -> quote::Ident {
|
||||
quote::Ident::new(format!("__field{}", i))
|
||||
}
|
||||
|
||||
let fields = (0usize..3)
|
||||
.map(field_i as fn(_) -> _)
|
||||
.map(|var| quote! { #var });
|
||||
|
||||
let tokens = quote! { #(#fields)* };
|
||||
assert_eq!("__field0 __field1 __field2", tokens.as_str());
|
||||
}
|
|
@ -1 +1 @@
|
|||
{"files":{"Cargo.toml":"3a227982587f7b714d78032d3fee9490c289a8907aae153ef897e7f0867ff351","lib.rs":"2379ed4126b680fc8ccd20b6087f304b3797459a2b5e300cda09e982872522ff"},"package":"479cde50c3539481f33906a387f2bd17c8e87cb848c35b6021d41fb81ff9b4d7"}
|
||||
{"files":{"Cargo.toml":"09315a82c3590f231d29e4c2c3693f452f93c47cc0a6d140603609fc7f32c9fa","lib.rs":"f63e3706cdf6677065740e70c04685ef5bdf536e6f03165ac0fac8e43f7b0124"},"package":"1eea1eee654ef80933142157fdad9dd8bc43cf7c74e999e369263496f04ff4da"}
|
|
@ -1,19 +1,38 @@
|
|||
[package]
|
||||
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
|
||||
#
|
||||
# When uploading crates to the registry Cargo will automatically
|
||||
# "normalize" Cargo.toml files for maximal compatibility
|
||||
# with all versions of Cargo and also rewrite `path` dependencies
|
||||
# to registry (e.g. crates.io) dependencies
|
||||
#
|
||||
# If you believe there's an error in this file please file an
|
||||
# issue against the rust-lang/cargo repository. If you're
|
||||
# editing this file be aware that the upstream Cargo.toml
|
||||
# will likely look very different (and much more reasonable)
|
||||
|
||||
[package]
|
||||
name = "string_cache_codegen"
|
||||
version = "0.4.0" # Also update ../README.md when making a semver-breaking change
|
||||
authors = [ "The Servo Project Developers" ]
|
||||
version = "0.4.2"
|
||||
authors = ["The Servo Project Developers"]
|
||||
description = "A codegen library for string-cache, developed as part of the Servo project."
|
||||
documentation = "https://docs.rs/string_cache_codegen/"
|
||||
license = "MIT / Apache-2.0"
|
||||
repository = "https://github.com/servo/string-cache"
|
||||
documentation = "https://docs.rs/string_cache_codegen/"
|
||||
|
||||
[lib]
|
||||
name = "string_cache_codegen"
|
||||
path = "lib.rs"
|
||||
[dependencies.phf_generator]
|
||||
version = "0.7.15"
|
||||
|
||||
[dependencies]
|
||||
string_cache_shared = {path = "../shared", version = "0.3"}
|
||||
phf_generator = "0.7.15"
|
||||
phf_shared = "0.7.4"
|
||||
quote = "0.3.9"
|
||||
[dependencies.phf_shared]
|
||||
version = "0.7.4"
|
||||
|
||||
[dependencies.proc-macro2]
|
||||
version = "0.4"
|
||||
|
||||
[dependencies.quote]
|
||||
version = "0.6"
|
||||
|
||||
[dependencies.string_cache_shared]
|
||||
version = "0.3"
|
||||
|
|
|
@ -6,6 +6,65 @@
|
|||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
//! A crate to create static string caches at compiletime.
|
||||
//!
|
||||
//! # Examples
|
||||
//!
|
||||
//! With static atoms:
|
||||
//!
|
||||
//! In `Cargo.toml`:
|
||||
//!
|
||||
//! ```toml
|
||||
//! [package]
|
||||
//! build = "build.rs"
|
||||
//!
|
||||
//! [dependencies]
|
||||
//! string_cache = "0.7"
|
||||
//!
|
||||
//! [build-dependencies]
|
||||
//! string_cache_codegen = "0.4"
|
||||
//! ```
|
||||
//!
|
||||
//! In `build.rs`:
|
||||
//!
|
||||
//! ```no_run
|
||||
//! extern crate string_cache_codegen;
|
||||
//!
|
||||
//! use std::env;
|
||||
//! use std::path::Path;
|
||||
//!
|
||||
//! fn main() {
|
||||
//! string_cache_codegen::AtomType::new("foo::FooAtom", "foo_atom!")
|
||||
//! .atoms(&["foo", "bar"])
|
||||
//! .write_to_file(&Path::new(&env::var("OUT_DIR").unwrap()).join("foo_atom.rs"))
|
||||
//! .unwrap()
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//! In `lib.rs`:
|
||||
//!
|
||||
//! ```ignore
|
||||
//! extern crate string_cache;
|
||||
//!
|
||||
//! mod foo {
|
||||
//! include!(concat!(env!("OUT_DIR"), "/foo_atom.rs"));
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//! The generated code will define a `FooAtom` type and a `foo_atom!` macro.
|
||||
//! The macro can be used in expression or patterns, with strings listed in `build.rs`.
|
||||
//! For example:
|
||||
//!
|
||||
//! ```ignore
|
||||
//! fn compute_something(input: &foo::FooAtom) -> u32 {
|
||||
//! match *input {
|
||||
//! foo_atom!("foo") => 1,
|
||||
//! foo_atom!("bar") => 2,
|
||||
//! _ => 3,
|
||||
//! }
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
|
||||
#![recursion_limit = "128"]
|
||||
|
||||
|
@ -13,6 +72,7 @@ extern crate phf_generator;
|
|||
extern crate phf_shared;
|
||||
extern crate string_cache_shared as shared;
|
||||
#[macro_use] extern crate quote;
|
||||
extern crate proc_macro2;
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::fs::File;
|
||||
|
@ -23,7 +83,10 @@ use std::path::Path;
|
|||
/// A builder for a static atom set and relevant macros
|
||||
pub struct AtomType {
|
||||
path: String,
|
||||
atom_doc: Option<String>,
|
||||
static_set_doc: Option<String>,
|
||||
macro_name: String,
|
||||
macro_doc: Option<String>,
|
||||
atoms: HashSet<String>,
|
||||
}
|
||||
|
||||
|
@ -38,7 +101,7 @@ impl AtomType {
|
|||
///
|
||||
/// For example, `AtomType::new("foo::FooAtom", "foo_atom!")` will generate:
|
||||
///
|
||||
/// ```rust
|
||||
/// ```ignore
|
||||
/// pub type FooAtom = ::string_cache::Atom<FooAtomStaticSet>;
|
||||
/// pub struct FooAtomStaticSet;
|
||||
/// impl ::string_cache::StaticAtomSet for FooAtomStaticSet {
|
||||
|
@ -48,15 +111,48 @@ impl AtomType {
|
|||
/// macro_rules foo_atom {
|
||||
/// // Expands to: $crate::foo::FooAtom { … }
|
||||
/// }
|
||||
/// ```
|
||||
pub fn new(path: &str, macro_name: &str) -> Self {
|
||||
assert!(macro_name.ends_with("!"));
|
||||
assert!(macro_name.ends_with("!"), "`macro_name` must end with '!'");
|
||||
AtomType {
|
||||
path: path.to_owned(),
|
||||
macro_name: macro_name[..macro_name.len() - "!".len()].to_owned(),
|
||||
atom_doc: None,
|
||||
static_set_doc: None,
|
||||
macro_doc: None,
|
||||
atoms: HashSet::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Add some documentation to the generated Atom type alias.
|
||||
///
|
||||
/// This can help the user know that the type uses interned strings.
|
||||
///
|
||||
/// Note that `docs` should not contain the `///` at the front of normal docs.
|
||||
pub fn with_atom_doc(&mut self, docs: &str) -> &mut Self {
|
||||
self.atom_doc = Some(docs.to_owned());
|
||||
self
|
||||
}
|
||||
|
||||
/// Add some documentation to the generated static set.
|
||||
///
|
||||
/// This can help the user know that this type is zero-sized and just references a static
|
||||
/// lookup table, or point them to the `Atom` type alias for more info.
|
||||
///
|
||||
/// Note that `docs` should not contain the `///` at the front of normal docs.
|
||||
pub fn with_static_set_doc(&mut self, docs: &str) -> &mut Self {
|
||||
self.static_set_doc = Some(docs.to_owned());
|
||||
self
|
||||
}
|
||||
|
||||
/// Add some documentation to the generated macro.
|
||||
///
|
||||
/// Note that `docs` should not contain the `///` at the front of normal docs.
|
||||
pub fn with_macro_doc(&mut self, docs: &str) -> &mut Self {
|
||||
self.macro_doc = Some(docs.to_owned());
|
||||
self
|
||||
}
|
||||
|
||||
/// Adds an atom to the builder
|
||||
pub fn atom(&mut self, s: &str) -> &mut Self {
|
||||
self.atoms.insert(s.to_owned());
|
||||
|
@ -74,7 +170,7 @@ impl AtomType {
|
|||
pub fn write_to<W>(&mut self, mut destination: W) -> io::Result<()> where W: Write {
|
||||
destination.write_all(
|
||||
self.to_tokens()
|
||||
.as_str()
|
||||
.to_string()
|
||||
// Insert some newlines to make the generated code slightly easier to read.
|
||||
.replace(" [ \"", "[\n\"")
|
||||
.replace("\" , ", "\",\n")
|
||||
|
@ -83,7 +179,7 @@ impl AtomType {
|
|||
.as_bytes())
|
||||
}
|
||||
|
||||
fn to_tokens(&mut self) -> quote::Tokens {
|
||||
fn to_tokens(&mut self) -> proc_macro2::TokenStream {
|
||||
// `impl Default for Atom` requires the empty string to be in the static set.
|
||||
// This also makes sure the set in non-empty,
|
||||
// which would cause divisions by zero in rust-phf.
|
||||
|
@ -92,9 +188,18 @@ impl AtomType {
|
|||
let atoms: Vec<&str> = self.atoms.iter().map(|s| &**s).collect();
|
||||
let hash_state = phf_generator::generate_hash(&atoms);
|
||||
let phf_generator::HashState { key, disps, map } = hash_state;
|
||||
let (disps0, disps1): (Vec<_>, Vec<_>) = disps.into_iter().unzip();
|
||||
let atoms: Vec<&str> = map.iter().map(|&idx| atoms[idx]).collect();
|
||||
let atoms_ref = &atoms;
|
||||
let empty_string_index = atoms.iter().position(|s| s.is_empty()).unwrap() as u32;
|
||||
let data = (0..atoms.len()).map(|i| quote::Hex(shared::pack_static(i as u32)));
|
||||
let data = (0..atoms.len()).map(|i| {
|
||||
format!("0x{:X}u64", shared::pack_static(i as u32))
|
||||
.parse::<proc_macro2::TokenStream>()
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.next()
|
||||
.unwrap()
|
||||
});
|
||||
|
||||
let hashes: Vec<u32> =
|
||||
atoms.iter().map(|string| {
|
||||
|
@ -107,21 +212,36 @@ impl AtomType {
|
|||
} else {
|
||||
&self.path
|
||||
};
|
||||
let static_set_name = quote::Ident::from(format!("{}StaticSet", type_name));
|
||||
let type_name = quote::Ident::from(type_name);
|
||||
let macro_name = quote::Ident::from(&*self.macro_name);
|
||||
let path = iter::repeat(quote::Ident::from(&*self.path));
|
||||
let atom_doc = match self.atom_doc {
|
||||
Some(ref doc) => quote!(#[doc = #doc]),
|
||||
None => quote!()
|
||||
};
|
||||
let static_set_doc = match self.static_set_doc {
|
||||
Some(ref doc) => quote!(#[doc = #doc]),
|
||||
None => quote!()
|
||||
};
|
||||
let macro_doc = match self.macro_doc {
|
||||
Some(ref doc) => quote!(#[doc = #doc]),
|
||||
None => quote!()
|
||||
};
|
||||
let new_term = |string: &str| proc_macro2::Ident::new(string, proc_macro2::Span::call_site());
|
||||
let static_set_name = new_term(&format!("{}StaticSet", type_name));
|
||||
let type_name = new_term(type_name);
|
||||
let macro_name = new_term(&*self.macro_name);
|
||||
let path = iter::repeat(self.path.parse::<proc_macro2::TokenStream>().unwrap());
|
||||
|
||||
quote! {
|
||||
#atom_doc
|
||||
pub type #type_name = ::string_cache::Atom<#static_set_name>;
|
||||
#static_set_doc
|
||||
pub struct #static_set_name;
|
||||
impl ::string_cache::StaticAtomSet for #static_set_name {
|
||||
fn get() -> &'static ::string_cache::PhfStrSet {
|
||||
static SET: ::string_cache::PhfStrSet = ::string_cache::PhfStrSet {
|
||||
key: #key,
|
||||
disps: &#disps,
|
||||
atoms: &#atoms,
|
||||
hashes: &#hashes
|
||||
disps: &[#((#disps0, #disps1)),*],
|
||||
atoms: &[#(#atoms_ref),*],
|
||||
hashes: &[#(#hashes),*]
|
||||
};
|
||||
&SET
|
||||
}
|
||||
|
@ -129,10 +249,11 @@ impl AtomType {
|
|||
#empty_string_index
|
||||
}
|
||||
}
|
||||
#macro_doc
|
||||
#[macro_export]
|
||||
macro_rules! #macro_name {
|
||||
#(
|
||||
(#atoms) => {
|
||||
(#atoms_ref) => {
|
||||
$crate::#path {
|
||||
unsafe_data: #data,
|
||||
phantom: ::std::marker::PhantomData,
|
||||
|
|
Загрузка…
Ссылка в новой задаче