xref: /aosp_15_r20/external/cronet/third_party/rust/chromium_crates_io/vendor/rustversion-1.0.15/src/lib.rs (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 //! [![github]](https://github.com/dtolnay/rustversion) [![crates-io]](https://crates.io/crates/rustversion) [![docs-rs]](https://docs.rs/rustversion)
2 //!
3 //! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github
4 //! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust
5 //! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs
6 //!
7 //! <br>
8 //!
9 //! This crate provides macros for conditional compilation according to rustc
10 //! compiler version, analogous to [`#[cfg(...)]`][cfg] and
11 //! [`#[cfg_attr(...)]`][cfg_attr].
12 //!
13 //! [cfg]: https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg-attribute
14 //! [cfg_attr]: https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg_attr-attribute
15 //!
16 //! <br>
17 //!
18 //! # Selectors
19 //!
20 //! - <p style="margin-left:50px;text-indent:-50px">
21 //!   <b><code>#[rustversion::stable]</code></b>
22 //!   —<br>
23 //!   True on any stable compiler.
24 //!   </p>
25 //!
26 //! - <p style="margin-left:50px;text-indent:-50px">
27 //!   <b><code>#[rustversion::stable(1.34)]</code></b>
28 //!   —<br>
29 //!   True on exactly the specified stable compiler.
30 //!   </p>
31 //!
32 //! - <p style="margin-left:50px;text-indent:-50px">
33 //!   <b><code>#[rustversion::beta]</code></b>
34 //!   —<br>
35 //!   True on any beta compiler.
36 //!   </p>
37 //!
38 //! - <p style="margin-left:50px;text-indent:-50px">
39 //!   <b><code>#[rustversion::nightly]</code></b>
40 //!   —<br>
41 //!   True on any nightly compiler or dev build.
42 //!   </p>
43 //!
44 //! - <p style="margin-left:50px;text-indent:-50px">
45 //!   <b><code>#[rustversion::nightly(2019-01-01)]</code></b>
46 //!   —<br>
47 //!   True on exactly one nightly.
48 //!   </p>
49 //!
50 //! - <p style="margin-left:50px;text-indent:-50px">
51 //!   <b><code>#[rustversion::since(1.34)]</code></b>
52 //!   —<br>
53 //!   True on that stable release and any later compiler, including beta and
54 //!   nightly.
55 //!   </p>
56 //!
57 //! - <p style="margin-left:50px;text-indent:-50px">
58 //!   <b><code>#[rustversion::since(2019-01-01)]</code></b>
59 //!   —<br>
60 //!   True on that nightly and all newer ones.
61 //!   </p>
62 //!
63 //! - <p style="margin-left:50px;text-indent:-50px">
64 //!   <b><code>#[rustversion::before(</code></b><i>version or date</i><b><code>)]</code></b>
65 //!   —<br>
66 //!   Negative of <i>#[rustversion::since(...)]</i>.
67 //!   </p>
68 //!
69 //! - <p style="margin-left:50px;text-indent:-50px">
70 //!   <b><code>#[rustversion::not(</code></b><i>selector</i><b><code>)]</code></b>
71 //!   —<br>
72 //!   Negative of any selector; for example <i>#[rustversion::not(nightly)]</i>.
73 //!   </p>
74 //!
75 //! - <p style="margin-left:50px;text-indent:-50px">
76 //!   <b><code>#[rustversion::any(</code></b><i>selectors...</i><b><code>)]</code></b>
77 //!   —<br>
78 //!   True if any of the comma-separated selectors is true; for example
79 //!   <i>#[rustversion::any(stable, beta)]</i>.
80 //!   </p>
81 //!
82 //! - <p style="margin-left:50px;text-indent:-50px">
83 //!   <b><code>#[rustversion::all(</code></b><i>selectors...</i><b><code>)]</code></b>
84 //!   —<br>
85 //!   True if all of the comma-separated selectors are true; for example
86 //!   <i>#[rustversion::all(since(1.31), before(1.34))]</i>.
87 //!   </p>
88 //!
89 //! - <p style="margin-left:50px;text-indent:-50px">
90 //!   <b><code>#[rustversion::attr(</code></b><i>selector</i><b><code>, </code></b><i>attribute</i><b><code>)]</code></b>
91 //!   —<br>
92 //!   For conditional inclusion of attributes; analogous to
93 //!   <code>cfg_attr</code>.
94 //!   </p>
95 //!
96 //! <br>
97 //!
98 //! # Use cases
99 //!
100 //! Providing additional trait impls as types are stabilized in the standard library
101 //! without breaking compatibility with older compilers; in this case Pin\<P\>
102 //! stabilized in [Rust 1.33][pin]:
103 //!
104 //! [pin]: https://blog.rust-lang.org/2019/02/28/Rust-1.33.0.html#pinning
105 //!
106 //! ```
107 //! # trait MyTrait {}
108 //! #
109 //! #[rustversion::since(1.33)]
110 //! use std::pin::Pin;
111 //!
112 //! #[rustversion::since(1.33)]
113 //! impl<P: MyTrait> MyTrait for Pin<P> {
114 //!     /* ... */
115 //! }
116 //! ```
117 //!
118 //! Similar but for language features; the ability to control alignment greater than
119 //! 1 of packed structs was stabilized in [Rust 1.33][packed].
120 //!
121 //! [packed]: https://github.com/rust-lang/rust/blob/master/RELEASES.md#version-1330-2019-02-28
122 //!
123 //! ```
124 //! #[rustversion::attr(before(1.33), repr(packed))]
125 //! #[rustversion::attr(since(1.33), repr(packed(2)))]
126 //! struct Six(i16, i32);
127 //!
128 //! fn main() {
129 //!     println!("{}", std::mem::align_of::<Six>());
130 //! }
131 //! ```
132 //!
133 //! Augmenting code with `const` as const impls are stabilized in the standard
134 //! library. This use of `const` as an attribute is recognized as a special case
135 //! by the rustversion::attr macro.
136 //!
137 //! ```
138 //! use std::time::Duration;
139 //!
140 //! #[rustversion::attr(since(1.32), const)]
141 //! fn duration_as_days(dur: Duration) -> u64 {
142 //!     dur.as_secs() / 60 / 60 / 24
143 //! }
144 //! ```
145 //!
146 //! <br>
147 
148 #![doc(html_root_url = "https://docs.rs/rustversion/1.0.15")]
149 #![allow(
150     clippy::cast_lossless,
151     clippy::cast_possible_truncation,
152     clippy::derive_partial_eq_without_eq,
153     clippy::doc_markdown,
154     clippy::enum_glob_use,
155     clippy::from_iter_instead_of_collect,
156     // https://github.com/rust-lang/rust-clippy/issues/8539
157     clippy::iter_with_drain,
158     clippy::module_name_repetitions,
159     clippy::must_use_candidate,
160     clippy::needless_doctest_main,
161     clippy::needless_pass_by_value,
162     clippy::redundant_else,
163     clippy::toplevel_ref_arg,
164     clippy::unreadable_literal
165 )]
166 
167 extern crate proc_macro;
168 
169 mod attr;
170 mod bound;
171 mod constfn;
172 mod date;
173 mod error;
174 mod expand;
175 mod expr;
176 mod iter;
177 mod release;
178 mod time;
179 mod token;
180 mod version;
181 
182 use crate::error::Error;
183 use crate::version::Version;
184 use proc_macro::TokenStream;
185 
186 const RUSTVERSION: Version = include!(concat!(env!("OUT_DIR"), "/version.expr"));
187 
188 #[proc_macro_attribute]
stable(args: TokenStream, input: TokenStream) -> TokenStream189 pub fn stable(args: TokenStream, input: TokenStream) -> TokenStream {
190     expand::cfg("stable", args, input)
191 }
192 
193 #[proc_macro_attribute]
beta(args: TokenStream, input: TokenStream) -> TokenStream194 pub fn beta(args: TokenStream, input: TokenStream) -> TokenStream {
195     expand::cfg("beta", args, input)
196 }
197 
198 #[proc_macro_attribute]
nightly(args: TokenStream, input: TokenStream) -> TokenStream199 pub fn nightly(args: TokenStream, input: TokenStream) -> TokenStream {
200     expand::cfg("nightly", args, input)
201 }
202 
203 #[proc_macro_attribute]
since(args: TokenStream, input: TokenStream) -> TokenStream204 pub fn since(args: TokenStream, input: TokenStream) -> TokenStream {
205     expand::cfg("since", args, input)
206 }
207 
208 #[proc_macro_attribute]
before(args: TokenStream, input: TokenStream) -> TokenStream209 pub fn before(args: TokenStream, input: TokenStream) -> TokenStream {
210     expand::cfg("before", args, input)
211 }
212 
213 #[proc_macro_attribute]
not(args: TokenStream, input: TokenStream) -> TokenStream214 pub fn not(args: TokenStream, input: TokenStream) -> TokenStream {
215     expand::cfg("not", args, input)
216 }
217 
218 #[proc_macro_attribute]
any(args: TokenStream, input: TokenStream) -> TokenStream219 pub fn any(args: TokenStream, input: TokenStream) -> TokenStream {
220     expand::cfg("any", args, input)
221 }
222 
223 #[proc_macro_attribute]
all(args: TokenStream, input: TokenStream) -> TokenStream224 pub fn all(args: TokenStream, input: TokenStream) -> TokenStream {
225     expand::cfg("all", args, input)
226 }
227 
228 #[proc_macro_attribute]
attr(args: TokenStream, input: TokenStream) -> TokenStream229 pub fn attr(args: TokenStream, input: TokenStream) -> TokenStream {
230     attr::parse(args)
231         .and_then(|args| expand::try_attr(args, input))
232         .unwrap_or_else(Error::into_compile_error)
233 }
234 
235 #[cfg(not(cfg_macro_not_allowed))]
236 #[proc_macro]
cfg(input: TokenStream) -> TokenStream237 pub fn cfg(input: TokenStream) -> TokenStream {
238     use proc_macro::{Ident, Span, TokenTree};
239     (|| {
240         let ref mut args = iter::new(input);
241         let expr = expr::parse(args)?;
242         token::parse_end(args)?;
243         let boolean = expr.eval(RUSTVERSION);
244         let ident = Ident::new(&boolean.to_string(), Span::call_site());
245         Ok(TokenStream::from(TokenTree::Ident(ident)))
246     })()
247     .unwrap_or_else(Error::into_compile_error)
248 }
249