| 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 style="display:inline">#[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 style="display:inline">#[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 style="display:inline">#[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 style="display:inline">#[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 style="display:inline">#[rustversion::nightly(2025-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 style="display:inline">#[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 style="display:inline">#[rustversion::since(2025-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 style="display:inline">#[rustversion::before(</code></b><i>version or date</i><b><code style="display:inline">)]</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 style="display:inline">#[rustversion::not(</code></b><i>selector</i><b><code style="display:inline">)]</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 style="display:inline">#[rustversion::any(</code></b><i>selectors...</i><b><code style="display:inline">)]</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 style="display:inline">#[rustversion::all(</code></b><i>selectors...</i><b><code style="display:inline">)]</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 style="display:inline">#[rustversion::attr(</code></b><i>selector</i><b><code style="display:inline">, </code></b><i>attribute</i><b><code style="display:inline">)]</code></b> |
| 91 |
//! —<br> |
| 92 |
//! For conditional inclusion of attributes; analogous to |
| 93 |
//! <code style="display:inline">cfg_attr</code>. |
| 94 |
//! </p> |
| 95 |
//! |
| 96 |
//! - <p style="margin-left:50px;text-indent:-50px"> |
| 97 |
//! <b><code style="display:inline">rustversion::cfg!(</code></b><i>selector</i><b><code style="display:inline">)</code></b> |
| 98 |
//! —<br> |
| 99 |
//! An expression form of any of the above attributes; for example |
| 100 |
//! <i>if rustversion::cfg!(any(stable, beta)) { ... }</i>. |
| 101 |
//! </p> |
| 102 |
//! |
| 103 |
//! <br> |
| 104 |
//! |
| 105 |
//! # Use cases |
| 106 |
//! |
| 107 |
//! Providing additional trait impls as types are stabilized in the standard library |
| 108 |
//! without breaking compatibility with older compilers; in this case Pin\<P\> |
| 109 |
//! stabilized in [Rust 1.33][pin]: |
| 110 |
//! |
| 111 |
//! [pin]: https://blog.rust-lang.org/2019/02/28/Rust-1.33.0.html#pinning |
| 112 |
//! |
| 113 |
//! ``` |
| 114 |
//! # trait MyTrait {} |
| 115 |
//! # |
| 116 |
//! #[rustversion::since(1.33)] |
| 117 |
//! use std::pin::Pin; |
| 118 |
//! |
| 119 |
//! #[rustversion::since(1.33)] |
| 120 |
//! impl<P: MyTrait> MyTrait for Pin<P> { |
| 121 |
//! /* ... */ |
| 122 |
//! } |
| 123 |
//! ``` |
| 124 |
//! |
| 125 |
//! Similar but for language features; the ability to control alignment greater than |
| 126 |
//! 1 of packed structs was stabilized in [Rust 1.33][packed]. |
| 127 |
//! |
| 128 |
//! [packed]: https://github.com/rust-lang/rust/blob/master/RELEASES.md#version-1330-2019-02-28 |
| 129 |
//! |
| 130 |
//! ``` |
| 131 |
//! #[rustversion::attr(before(1.33), repr(packed))] |
| 132 |
//! #[rustversion::attr(since(1.33), repr(packed(2)))] |
| 133 |
//! struct Six(i16, i32); |
| 134 |
//! |
| 135 |
//! fn main() { |
| 136 |
//! println!("{}", std::mem::align_of::<Six>()); |
| 137 |
//! } |
| 138 |
//! ``` |
| 139 |
//! |
| 140 |
//! Augmenting code with `const` as const impls are stabilized in the standard |
| 141 |
//! library. This use of `const` as an attribute is recognized as a special case |
| 142 |
//! by the rustversion::attr macro. |
| 143 |
//! |
| 144 |
//! ``` |
| 145 |
//! use std::time::Duration; |
| 146 |
//! |
| 147 |
//! #[rustversion::attr(since(1.32), const)] |
| 148 |
//! fn duration_as_days(dur: Duration) -> u64 { |
| 149 |
//! dur.as_secs() / 60 / 60 / 24 |
| 150 |
//! } |
| 151 |
//! ``` |
| 152 |
//! |
| 153 |
//! Emitting Cargo cfg directives from a build script. Note that this requires |
| 154 |
//! listing `rustversion` under `[build-dependencies]` in Cargo.toml, not |
| 155 |
//! `[dependencies]`. |
| 156 |
//! |
| 157 |
//! ``` |
| 158 |
//! // build.rs |
| 159 |
//! |
| 160 |
//! fn main() { |
| 161 |
//! if rustversion::cfg!(since(1.36)) { |
| 162 |
//! println!("cargo:rustc-cfg=no_std"); |
| 163 |
//! } |
| 164 |
//! } |
| 165 |
//! ``` |
| 166 |
//! |
| 167 |
//! ``` |
| 168 |
//! // src/lib.rs |
| 169 |
//! |
| 170 |
//! #![cfg_attr(no_std, no_std)] |
| 171 |
//! |
| 172 |
//! #[cfg(no_std)] |
| 173 |
//! extern crate alloc; |
| 174 |
//! ``` |
| 175 |
//! |
| 176 |
//! <br> |
| 177 |
|
| 178 |
#![doc(html_root_url = "https://docs.rs/rustversion/1.0.23")] |
| 179 |
#![allow( |
| 180 |
clippy::cast_lossless, |
| 181 |
clippy::cast_possible_truncation, |
| 182 |
clippy::derive_partial_eq_without_eq, |
| 183 |
clippy::doc_markdown, |
| 184 |
clippy::enum_glob_use, |
| 185 |
// https://github.com/rust-lang/rust-clippy/issues/8539 |
| 186 |
clippy::iter_with_drain, |
| 187 |
clippy::module_name_repetitions, |
| 188 |
clippy::must_use_candidate, |
| 189 |
clippy::needless_doctest_main, |
| 190 |
clippy::needless_pass_by_value, |
| 191 |
clippy::redundant_else, |
| 192 |
clippy::toplevel_ref_arg, |
| 193 |
clippy::unreadable_literal |
| 194 |
)] |
| 195 |
|
| 196 |
extern crate proc_macro; |
| 197 |
|
| 198 |
mod attr; |
| 199 |
mod bound; |
| 200 |
mod constfn; |
| 201 |
mod date; |
| 202 |
mod error; |
| 203 |
mod expand; |
| 204 |
mod expr; |
| 205 |
mod iter; |
| 206 |
mod release; |
| 207 |
mod time; |
| 208 |
mod token; |
| 209 |
mod version; |
| 210 |
|
| 211 |
use crate::error::Error; |
| 212 |
use crate::version::Version; |
| 213 |
use proc_macro::TokenStream; |
| 214 |
|
| 215 |
#[cfg(not(host_os = "windows"))] |
| 216 |
const RUSTVERSION: Version = include!(concat!(env!("OUT_DIR"), "/version.expr")); |
| 217 |
|
| 218 |
#[cfg(host_os = "windows")] |
| 219 |
const RUSTVERSION: Version = include!(concat!(env!("OUT_DIR"), "\\version.expr")); |
| 220 |
|
| 221 |
#[proc_macro_attribute] |
| 222 |
pub fn stable(args: TokenStream, input: TokenStream) -> TokenStream { |
| 223 |
expand::cfg("stable", args, input) |
| 224 |
} |
| 225 |
|
| 226 |
#[proc_macro_attribute] |
| 227 |
pub fn beta(args: TokenStream, input: TokenStream) -> TokenStream { |
| 228 |
expand::cfg("beta", args, input) |
| 229 |
} |
| 230 |
|
| 231 |
#[proc_macro_attribute] |
| 232 |
pub fn nightly(args: TokenStream, input: TokenStream) -> TokenStream { |
| 233 |
expand::cfg("nightly", args, input) |
| 234 |
} |
| 235 |
|
| 236 |
#[proc_macro_attribute] |
| 237 |
pub fn since(args: TokenStream, input: TokenStream) -> TokenStream { |
| 238 |
expand::cfg("since", args, input) |
| 239 |
} |
| 240 |
|
| 241 |
#[proc_macro_attribute] |
| 242 |
pub fn before(args: TokenStream, input: TokenStream) -> TokenStream { |
| 243 |
expand::cfg("before", args, input) |
| 244 |
} |
| 245 |
|
| 246 |
#[proc_macro_attribute] |
| 247 |
pub fn not(args: TokenStream, input: TokenStream) -> TokenStream { |
| 248 |
expand::cfg("not", args, input) |
| 249 |
} |
| 250 |
|
| 251 |
#[proc_macro_attribute] |
| 252 |
pub fn any(args: TokenStream, input: TokenStream) -> TokenStream { |
| 253 |
expand::cfg("any", args, input) |
| 254 |
} |
| 255 |
|
| 256 |
#[proc_macro_attribute] |
| 257 |
pub fn all(args: TokenStream, input: TokenStream) -> TokenStream { |
| 258 |
expand::cfg("all", args, input) |
| 259 |
} |
| 260 |
|
| 261 |
#[proc_macro_attribute] |
| 262 |
pub fn attr(args: TokenStream, input: TokenStream) -> TokenStream { |
| 263 |
attr::parse(args) |
| 264 |
.and_then(|args| expand::try_attr(args, input)) |
| 265 |
.unwrap_or_else(Error::into_compile_error) |
| 266 |
} |
| 267 |
|
| 268 |
#[cfg(not(cfg_macro_not_allowed))] |
| 269 |
#[proc_macro] |
| 270 |
pub fn cfg(input: TokenStream) -> TokenStream { |
| 271 |
use proc_macro::{Ident, Span, TokenTree}; |
| 272 |
(|| { |
| 273 |
let ref mut args = iter::new(input); |
| 274 |
let expr = expr::parse(args)?; |
| 275 |
token::parse_end(args)?; |
| 276 |
let boolean = expr.eval(RUSTVERSION); |
| 277 |
let ident = Ident::new(&boolean.to_string(), Span::call_site()); |
| 278 |
Ok(TokenStream::from(TokenTree::Ident(ident))) |
| 279 |
})() |
| 280 |
.unwrap_or_else(Error::into_compile_error) |
| 281 |
} |