opt_level/lib.rs
1//! [![github]](https://github.com/dtolnay/opt-level) [![crates-io]](https://crates.io/crates/opt-level) [![docs-rs]](https://docs.rs/opt-level)
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//! Get the value of rustc's `-Copt-level=` flag at runtime.
10//!
11//! Useful for sizing tests to run fewer iterations in slow build modes.
12//!
13//! According to <https://doc.rust-lang.org/cargo/reference/profiles.html#opt-level>
14//! the possible values are:
15//!
16//! - `0`: no optimizations
17//! - `1`: basic optimizations
18//! - `2`: some optimizations
19//! - `3`: all optimizations
20//! - `s`: optimize for binary size
21//! - `z`: optimize for binary size, but also turn off loop vectorization
22//!
23//! # Example
24//!
25//! ```
26//! use rand::rngs::SmallRng;
27//! use rand::{RngCore as _, SeedableRng as _};
28//!
29//! const N: usize = if cfg!(miri) {
30//! 500
31//! } else if let b"0" = opt_level::OPT_LEVEL.as_bytes() {
32//! 10_000
33//! } else {
34//! 100_000_000
35//! };
36//!
37//! #[test]
38//! fn random_test() {
39//! let mut rng = SmallRng::from_os_rng();
40//!
41//! for _ in 0..N {
42//! let bits = rng.next_u64();
43//! # const _: &str = stringify! {
44//! ...
45//! assert_eq!(..., ...);
46//! # };
47//! }
48//! }
49//! ```
50
51#![no_std]
52#![doc(html_root_url = "https://docs.rs/opt-level/1.0.0")]
53#![allow(clippy::test_attr_in_doctest)]
54
55pub const OPT_LEVEL: &str = include_str!(concat!(env!("OUT_DIR"), "/opt-level"));