• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /// Asserts that the trait is a child of all of the other traits.
2 ///
3 /// Related:
4 /// - [`assert_trait_super_all!`]
5 ///
6 /// # Examples
7 ///
8 /// All types that implement [`Copy`] must implement [`Clone`]:
9 ///
10 /// ```
11 /// # #[macro_use] extern crate static_assertions; fn main() {}
12 /// assert_trait_sub_all!(Copy: Clone);
13 /// ```
14 ///
15 /// All types that implement [`Ord`] must implement [`PartialEq`], [`Eq`], and
16 /// [`PartialOrd`]:
17 ///
18 /// ```
19 /// # #[macro_use] extern crate static_assertions; fn main() {}
20 /// assert_trait_sub_all!(Ord: PartialEq, Eq, PartialOrd);
21 /// ```
22 ///
23 /// The following example fails to compile because [`Eq`] is not required for
24 /// [`PartialOrd`]:
25 ///
26 /// ```compile_fail
27 /// # #[macro_use] extern crate static_assertions; fn main() {}
28 /// assert_trait_sub_all!(PartialOrd: Eq);
29 /// ```
30 ///
31 /// [`assert_trait_super_all!`]: macro.assert_trait_super_all.html
32 ///
33 /// [`Copy`]:       https://doc.rust-lang.org/std/marker/trait.Copy.html
34 /// [`Clone`]:      https://doc.rust-lang.org/std/clone/trait.Clone.html
35 /// [`Ord`]:        https://doc.rust-lang.org/std/cmp/trait.Ord.html
36 /// [`PartialOrd`]: https://doc.rust-lang.org/std/cmp/trait.PartialOrd.html
37 /// [`Eq`]:         https://doc.rust-lang.org/std/cmp/trait.Eq.html
38 /// [`PartialEq`]:  https://doc.rust-lang.org/std/cmp/trait.PartialEq.html
39 #[macro_export]
40 macro_rules! assert_trait_sub_all {
41     ($sub:path: $($super:path),+ $(,)?) => {
42         const _: () = {
43             // One scope per super-trait.
44             $({
45                 #[allow(non_camel_case_types)]
46                 trait __Impl_Implication: $super {}
47 
48                 // Can only be implemented for `$sub` types if `$super` is
49                 // also implemented.
50                 impl<T: $sub> __Impl_Implication for T {}
51             })+
52         };
53     };
54 }
55 
56 /// Asserts that the trait is a parent of all of the other traits.
57 ///
58 /// Related:
59 /// - [`assert_trait_sub_all!`]
60 ///
61 /// # Examples
62 ///
63 /// With this, traits `A` and `B` can both be tested to require [`Copy`] on a
64 /// single line:
65 ///
66 /// ```
67 /// # use static_assertions::assert_trait_super_all;
68 /// trait A: Copy {}
69 /// trait B: Copy {}
70 ///
71 /// assert_trait_super_all!(Copy: A, B);
72 /// ```
73 ///
74 /// Otherwise, each sub-trait would require its own call to
75 /// [`assert_trait_sub_all!`]:
76 ///
77 /// ```
78 /// # #[macro_use] extern crate static_assertions; fn main() {}
79 /// # trait A: Copy {}
80 /// # trait B: Copy {}
81 /// assert_trait_sub_all!(A: Copy);
82 /// assert_trait_sub_all!(B: Copy);
83 /// ```
84 ///
85 /// The following example fails to compile because trait `C` does not require
86 /// [`Copy`]:
87 ///
88 /// ```compile_fail
89 /// # use static_assertions::assert_trait_super_all;
90 /// # trait A: Copy {}
91 /// # trait B: Copy {}
92 /// trait C {}
93 ///
94 /// assert_trait_super_all!(Copy: A, B, C);
95 /// ```
96 ///
97 /// [`assert_trait_sub_all!`]: macro.assert_trait_sub_all.html
98 ///
99 /// [`Copy`]: https://doc.rust-lang.org/std/marker/trait.Copy.html
100 #[macro_export(local_inner_macros)]
101 macro_rules! assert_trait_super_all {
102     ($super:path: $($sub:path),+ $(,)?) => {
103         $(assert_trait_sub_all!($sub: $super);)+
104     };
105 }
106