]> git.proxmox.com Git - cargo.git/blob - vendor/typenum/src/marker_traits.rs
New upstream version 0.33.0
[cargo.git] / vendor / typenum / src / marker_traits.rs
1 //! All of the **marker traits** used in typenum.
2 //!
3 //! Note that the definition here for marker traits is slightly different than
4 //! the conventional one -- we include traits with functions that convert a type
5 //! to the corresponding value, as well as associated constants that do the
6 //! same.
7 //!
8 //! For example, the `Integer` trait includes the function (among others) `fn
9 //! to_i32() -> i32` and the associated constant `I32` so that one can do this:
10 //!
11 //! ```
12 //! use typenum::{N42, Integer};
13 //!
14 //! assert_eq!(-42, N42::to_i32());
15 //! assert_eq!(-42, N42::I32);
16 //! ```
17 //!
18
19 /// A **marker trait** to designate that a type is not zero. All number types in this
20 /// crate implement `NonZero` except `B0`, `U0`, and `Z0`.
21 pub trait NonZero {}
22
23 /// A **Marker trait** for the types `Greater`, `Equal`, and `Less`.
24 ///
25 /// This trait should not be implemented for anything outside this crate.
26 pub trait Ord {
27 #[allow(missing_docs)]
28 fn to_ordering() -> ::core::cmp::Ordering;
29 }
30
31 /// The **marker trait** for compile time bits.
32 ///
33 /// This trait should not be implemented for anything outside this crate.
34 pub trait Bit {
35 #[allow(missing_docs)]
36 const U8: u8;
37 #[allow(missing_docs)]
38 const BOOL: bool;
39
40 #[allow(missing_docs)]
41 fn to_u8() -> u8;
42 #[allow(missing_docs)]
43 fn to_bool() -> bool;
44 }
45
46 /// The **marker trait** for compile time unsigned integers.
47 ///
48 /// This trait should not be implemented for anything outside this crate.
49 ///
50 /// # Example
51 /// ```rust
52 /// use typenum::{U3, Unsigned};
53 ///
54 /// assert_eq!(U3::to_u32(), 3);
55 /// assert_eq!(U3::I32, 3);
56 /// ```
57 pub trait Unsigned {
58 #[allow(missing_docs)]
59 const U8: u8;
60 #[allow(missing_docs)]
61 const U16: u16;
62 #[allow(missing_docs)]
63 const U32: u32;
64 #[allow(missing_docs)]
65 const U64: u64;
66 #[cfg(feature = "i128")]
67 #[allow(missing_docs)]
68 const U128: u128;
69 #[allow(missing_docs)]
70 const USIZE: usize;
71
72 #[allow(missing_docs)]
73 const I8: i8;
74 #[allow(missing_docs)]
75 const I16: i16;
76 #[allow(missing_docs)]
77 const I32: i32;
78 #[allow(missing_docs)]
79 const I64: i64;
80 #[cfg(feature = "i128")]
81 #[allow(missing_docs)]
82 const I128: i128;
83 #[allow(missing_docs)]
84 const ISIZE: isize;
85
86 #[allow(missing_docs)]
87 fn to_u8() -> u8;
88 #[allow(missing_docs)]
89 fn to_u16() -> u16;
90 #[allow(missing_docs)]
91 fn to_u32() -> u32;
92 #[allow(missing_docs)]
93 fn to_u64() -> u64;
94 #[cfg(feature = "i128")]
95 #[allow(missing_docs)]
96 fn to_u128() -> u128;
97 #[allow(missing_docs)]
98 fn to_usize() -> usize;
99
100 #[allow(missing_docs)]
101 fn to_i8() -> i8;
102 #[allow(missing_docs)]
103 fn to_i16() -> i16;
104 #[allow(missing_docs)]
105 fn to_i32() -> i32;
106 #[allow(missing_docs)]
107 fn to_i64() -> i64;
108 #[cfg(feature = "i128")]
109 #[allow(missing_docs)]
110 fn to_i128() -> i128;
111 #[allow(missing_docs)]
112 fn to_isize() -> isize;
113 }
114
115 /// The **marker trait** for compile time signed integers.
116 ///
117 /// This trait should not be implemented for anything outside this crate.
118 ///
119 /// # Example
120 /// ```rust
121 /// use typenum::{P3, Integer};
122 ///
123 /// assert_eq!(P3::to_i32(), 3);
124 /// assert_eq!(P3::I32, 3);
125 /// ```
126 pub trait Integer {
127 #[allow(missing_docs)]
128 const I8: i8;
129 #[allow(missing_docs)]
130 const I16: i16;
131 #[allow(missing_docs)]
132 const I32: i32;
133 #[allow(missing_docs)]
134 const I64: i64;
135 #[cfg(feature = "i128")]
136 #[allow(missing_docs)]
137 const I128: i128;
138 #[allow(missing_docs)]
139 const ISIZE: isize;
140
141 #[allow(missing_docs)]
142 fn to_i8() -> i8;
143 #[allow(missing_docs)]
144 fn to_i16() -> i16;
145 #[allow(missing_docs)]
146 fn to_i32() -> i32;
147 #[allow(missing_docs)]
148 fn to_i64() -> i64;
149 #[cfg(feature = "i128")]
150 #[allow(missing_docs)]
151 fn to_i128() -> i128;
152 #[allow(missing_docs)]
153 fn to_isize() -> isize;
154 }
155
156 /// The **marker trait** for type-level arrays of type-level numbers.
157 ///
158 /// This trait should not be implemented for anything outside this crate.
159 ///
160 /// Someday, it may contain an associated constant to produce a runtime array,
161 /// like the other marker traits here. However, that is blocked by [this
162 /// issue](https://github.com/rust-lang/rust/issues/44168).
163 pub trait TypeArray {}
164
165 /// The **marker trait** for type-level numbers which are a power of two.
166 ///
167 /// This trait should not be implemented for anything outside this crate.
168 ///
169 /// # Examples
170 ///
171 /// Here's a working example:
172 ///
173 /// ```rust
174 /// use typenum::{P4, P8, PowerOfTwo};
175 ///
176 /// fn only_p2<P: PowerOfTwo>() { }
177 ///
178 /// only_p2::<P4>();
179 /// only_p2::<P8>();
180 /// ```
181 ///
182 /// Numbers which are not a power of two will fail to compile in this example:
183 ///
184 /// ```rust,compile_fail
185 /// use typenum::{P9, P511, P1023, PowerOfTwo};
186 ///
187 /// fn only_p2<P: PowerOfTwo>() { }
188 ///
189 /// only_p2::<P9>();
190 /// only_p2::<P511>();
191 /// only_p2::<P1023>();
192 /// ```
193
194 pub trait PowerOfTwo {}