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    /// The compiler automatically adds any applicable auto trait (all of which are
44    /// marker traits) to self-defined types. This implies that these trait bounds
45    /// might vanish if the necessary pre-conditions are no longer met. That'd be a
46    /// breaking API change!
47    ///
48    /// To prevent _accidental_ removal of auto trait implementations, this method
49    /// tests for their presence. If you are re-designing any of the types below
50    /// and a test fails as a result, that might be fine. You are now definitely
51    /// aware of a consequence you might not have known about otherwise. (If you
52    /// were already aware you know how subtle this stuff can be and are hopefully
53    /// fine with reading this comment.)
54    ///
55    /// Inspired by “Rust for Rustaceans” by Jon Gjengset.
56    pub fn implements_usual_auto_traits<T: Sized + Send + Sync + Unpin>() {}
57
58    #[test]
59    fn types_in_prelude_implement_the_usual_auto_traits() {
60        implements_usual_auto_traits::<BFieldElement>();
61        implements_usual_auto_traits::<Polynomial<BFieldElement>>();
62        implements_usual_auto_traits::<Polynomial<XFieldElement>>();
63        implements_usual_auto_traits::<Digest>();
64        implements_usual_auto_traits::<Tip5>();
65        implements_usual_auto_traits::<XFieldElement>();
66        implements_usual_auto_traits::<MerkleTree>();
67        implements_usual_auto_traits::<MerkleTreeInclusionProof>();
68        implements_usual_auto_traits::<MmrMembershipProof>();
69    }
70
71    #[test]
72    fn public_types_implement_the_usual_auto_traits() {
73        implements_usual_auto_traits::<math::lattice::CyclotomicRingElement>();
74        implements_usual_auto_traits::<math::lattice::ModuleElement<42>>();
75        implements_usual_auto_traits::<math::lattice::kem::SecretKey>();
76        implements_usual_auto_traits::<math::lattice::kem::PublicKey>();
77        implements_usual_auto_traits::<math::lattice::kem::Ciphertext>();
78        implements_usual_auto_traits::<util_types::sponge::Domain>();
79        implements_usual_auto_traits::<util_types::mmr::mmr_accumulator::MmrAccumulator>();
80        implements_usual_auto_traits::<math::zerofier_tree::Branch<BFieldElement>>();
81        implements_usual_auto_traits::<math::zerofier_tree::Leaf<BFieldElement>>();
82        implements_usual_auto_traits::<math::zerofier_tree::ZerofierTree<BFieldElement>>();
83        implements_usual_auto_traits::<
84            math::polynomial::ModularInterpolationPreprocessingData<BFieldElement>,
85        >();
86    }
87
88    #[test]
89    fn errors_implement_the_usual_auto_traits() {
90        implements_usual_auto_traits::<error::BFieldCodecError>();
91        implements_usual_auto_traits::<error::PolynomialBFieldCodecError>();
92        implements_usual_auto_traits::<error::MerkleTreeError>();
93        implements_usual_auto_traits::<error::ParseBFieldElementError>();
94        implements_usual_auto_traits::<error::TryFromDigestError>();
95        implements_usual_auto_traits::<error::TryFromHexDigestError>();
96        implements_usual_auto_traits::<error::TryFromU32sError>();
97        implements_usual_auto_traits::<error::TryFromXFieldElementError>();
98    }
99}