twenty_first/lib.rs
1#![warn(clippy::shadow_unrelated)]
2//
3// If code coverage tool `cargo-llvm-cov` is running with the nightly toolchain,
4// enable the unstable “coverage” attribute. This allows using the annotation
5// `#[coverage(off)]` to explicitly exclude certain parts of the code from
6// being considered as “code under test.” Most prominently, the annotation
7// should be added to every `#[cfg(test)]` module. Since the “coverage”
8// feature is enable only conditionally, the annotation to use is:
9// #[cfg_attr(coverage_nightly, coverage(off))]
10//
11// See also:
12// - https://github.com/taiki-e/cargo-llvm-cov#exclude-code-from-coverage
13// - https://github.com/rust-lang/rust/issues/84605
14#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
15
16pub mod config;
17pub mod error;
18pub mod math;
19pub mod prelude;
20pub mod tip5;
21pub mod util_types;
22
23// This is needed for `#[derive(BFieldCodec)]` macro to work consistently across crates.
24// Specifically:
25// From inside the `twenty-first` crate, we need to refer to `twenty-first` by `crate`.
26// However, from outside the `twenty-first` crate, we need to refer to it by `twenty_first`.
27// The re-export below allows using identifier `twenty_first` even from inside `twenty-first`.
28//
29// See also:
30// https://github.com/bkchr/proc-macro-crate/issues/2#issuecomment-572914520
31extern crate self as twenty_first;
32
33// re-export crates used in our public API
34pub use bfieldcodec_derive;
35
36#[cfg(test)]
37#[cfg_attr(coverage_nightly, coverage(off))]
38pub(crate) mod tests {
39 use prelude::*;
40
41 use super::*;
42
43 /// A crate-specific replacement of the `#[test]` attribute for tests that
44 /// should also be executed on `wasm` targets (which is almost all tests).
45 ///
46 /// If you specifically want to exclude a test from `wasm` targets, use the
47 /// usual `#[test]` attribute instead.
48 ///
49 /// # Usage
50 ///
51 /// ```
52 /// #[macro_rules_attr::apply(test)]
53 /// fn foo() {
54 /// assert_eq!(4, 2 + 2);
55 /// }
56 /// ```
57 macro_rules! test {
58 ($item:item) => {
59 #[test]
60 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
61 $item
62 };
63 }
64 pub(crate) use test;
65
66 /// A crate-specific replacement of the `#[test_strategy::proptest]`
67 /// attribute for tests that should also be executed on `wasm` targets
68 /// (which is almost all tests).
69 ///
70 /// If you specifically want to exclude a test from `wasm` targets, use the
71 /// usual `#[test_strategy::proptest]` attribute instead.
72 ///
73 /// # Usage
74 ///
75 /// ```
76 /// # use proptest::prop_assert_eq;
77 /// #[macro_rules_attr::apply(proptest)]
78 /// fn foo(#[strategy(0..=42)] x: i32) {
79 /// prop_assert_eq!(2 * x, x + x);
80 /// }
81 /// ```
82 ///
83 /// If you want to configure the test, use the usual syntax defined by
84 /// [`test_strategy`]:
85 /// ```
86 /// # use proptest::prop_assert_eq;
87 /// #[macro_rules_attr::apply(proptest(cases = 10, max_local_rejects = 5))]
88 /// fn foo(#[strategy(0..=42)] x: i32) {
89 /// prop_assert_eq!(2 * x, x + x);
90 /// }
91 /// ```
92 macro_rules! proptest {
93 ($item:item $(($($config:tt)*))?) => {
94 #[test_strategy::proptest $(($($config)*))?]
95 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
96 $item
97 };
98 }
99 pub(crate) use proptest;
100
101 /// The compiler automatically adds any applicable auto trait (all of which are
102 /// marker traits) to self-defined types. This implies that these trait bounds
103 /// might vanish if the necessary pre-conditions are no longer met. That'd be a
104 /// breaking API change!
105 ///
106 /// To prevent _accidental_ removal of auto trait implementations, this method
107 /// tests for their presence. If you are re-designing any of the types below
108 /// and a test fails as a result, that might be fine. You are now definitely
109 /// aware of a consequence you might not have known about otherwise. (If you
110 /// were already aware you know how subtle this stuff can be and are hopefully
111 /// fine with reading this comment.)
112 ///
113 /// Inspired by “Rust for Rustaceans” by Jon Gjengset.
114 pub fn implements_usual_auto_traits<T: Sized + Send + Sync + Unpin>() {}
115
116 #[macro_rules_attr::apply(test)]
117 fn types_in_prelude_implement_the_usual_auto_traits() {
118 implements_usual_auto_traits::<BFieldElement>();
119 implements_usual_auto_traits::<Polynomial<BFieldElement>>();
120 implements_usual_auto_traits::<Polynomial<XFieldElement>>();
121 implements_usual_auto_traits::<Digest>();
122 implements_usual_auto_traits::<Tip5>();
123 implements_usual_auto_traits::<XFieldElement>();
124 implements_usual_auto_traits::<MerkleTree>();
125 implements_usual_auto_traits::<MerkleTreeInclusionProof>();
126 implements_usual_auto_traits::<MmrMembershipProof>();
127 }
128
129 #[macro_rules_attr::apply(test)]
130 fn public_types_implement_the_usual_auto_traits() {
131 implements_usual_auto_traits::<math::lattice::CyclotomicRingElement>();
132 implements_usual_auto_traits::<math::lattice::ModuleElement<42>>();
133 implements_usual_auto_traits::<math::lattice::kem::SecretKey>();
134 implements_usual_auto_traits::<math::lattice::kem::PublicKey>();
135 implements_usual_auto_traits::<math::lattice::kem::Ciphertext>();
136 implements_usual_auto_traits::<util_types::sponge::Domain>();
137 implements_usual_auto_traits::<util_types::mmr::mmr_accumulator::MmrAccumulator>();
138 implements_usual_auto_traits::<math::zerofier_tree::Branch<BFieldElement>>();
139 implements_usual_auto_traits::<math::zerofier_tree::Leaf<BFieldElement>>();
140 implements_usual_auto_traits::<math::zerofier_tree::ZerofierTree<BFieldElement>>();
141 implements_usual_auto_traits::<
142 math::polynomial::ModularInterpolationPreprocessingData<BFieldElement>,
143 >();
144 }
145
146 #[macro_rules_attr::apply(test)]
147 fn errors_implement_the_usual_auto_traits() {
148 implements_usual_auto_traits::<error::BFieldCodecError>();
149 implements_usual_auto_traits::<error::PolynomialBFieldCodecError>();
150 implements_usual_auto_traits::<error::MerkleTreeError>();
151 implements_usual_auto_traits::<error::ParseBFieldElementError>();
152 implements_usual_auto_traits::<error::TryFromDigestError>();
153 implements_usual_auto_traits::<error::TryFromHexDigestError>();
154 implements_usual_auto_traits::<error::TryFromU32sError>();
155 implements_usual_auto_traits::<error::TryFromXFieldElementError>();
156 }
157}