Skip to main content

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