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::{Integer, N42};
13//!
14//! assert_eq!(-42, N42::to_i32());
15//! assert_eq!(-42, N42::I32);
16//! ```
17
18use crate::sealed::Sealed;
19
20/// A **marker trait** to designate that a type is not zero. All number types in this
21/// crate implement `NonZero` except `B0`, `U0`, and `Z0`.
22pub trait NonZero: Sealed {}
23
24/// A **marker trait** to designate that a type is zero. Only `B0`, `U0`, and `Z0`
25/// implement this trait.
26pub trait Zero: Sealed {}
27
28/// A **Marker trait** for the types `Greater`, `Equal`, and `Less`.
29pub trait Ord: Sealed {
30 #[allow(missing_docs)]
31 fn to_ordering() -> ::core::cmp::Ordering;
32}
33
34/// The **marker trait** for compile time bits.
35pub trait Bit: Sealed + Copy + Default + 'static {
36 #[allow(missing_docs)]
37 const U8: u8;
38 #[allow(missing_docs)]
39 const BOOL: bool;
40
41 /// Instantiates a singleton representing this bit.
42 fn new() -> Self;
43
44 #[allow(missing_docs)]
45 fn to_u8() -> u8;
46 #[allow(missing_docs)]
47 fn to_bool() -> bool;
48}
49
50/// The **marker trait** for compile time unsigned integers.
51///
52/// # Example
53/// ```rust
54/// use typenum::{Unsigned, U3};
55///
56/// assert_eq!(U3::to_u32(), 3);
57/// assert_eq!(U3::I32, 3);
58/// ```
59pub trait Unsigned: Sealed + Copy + Default + 'static {
60 #[allow(missing_docs)]
61 const U8: u8;
62 #[allow(missing_docs)]
63 const U16: u16;
64 #[allow(missing_docs)]
65 const U32: u32;
66 #[allow(missing_docs)]
67 const U64: u64;
68 #[cfg(feature = "i128")]
69 #[allow(missing_docs)]
70 const U128: u128;
71 #[allow(missing_docs)]
72 const USIZE: usize;
73
74 #[allow(missing_docs)]
75 const I8: i8;
76 #[allow(missing_docs)]
77 const I16: i16;
78 #[allow(missing_docs)]
79 const I32: i32;
80 #[allow(missing_docs)]
81 const I64: i64;
82 #[cfg(feature = "i128")]
83 #[allow(missing_docs)]
84 const I128: i128;
85 #[allow(missing_docs)]
86 const ISIZE: isize;
87
88 #[allow(missing_docs)]
89 fn to_u8() -> u8;
90 #[allow(missing_docs)]
91 fn to_u16() -> u16;
92 #[allow(missing_docs)]
93 fn to_u32() -> u32;
94 #[allow(missing_docs)]
95 fn to_u64() -> u64;
96 #[cfg(feature = "i128")]
97 #[allow(missing_docs)]
98 fn to_u128() -> u128;
99 #[allow(missing_docs)]
100 fn to_usize() -> usize;
101
102 #[allow(missing_docs)]
103 fn to_i8() -> i8;
104 #[allow(missing_docs)]
105 fn to_i16() -> i16;
106 #[allow(missing_docs)]
107 fn to_i32() -> i32;
108 #[allow(missing_docs)]
109 fn to_i64() -> i64;
110 #[cfg(feature = "i128")]
111 #[allow(missing_docs)]
112 fn to_i128() -> i128;
113 #[allow(missing_docs)]
114 fn to_isize() -> isize;
115}
116
117/// The **marker trait** for compile time signed integers.
118///
119/// # Example
120/// ```rust
121/// use typenum::{Integer, P3};
122///
123/// assert_eq!(P3::to_i32(), 3);
124/// assert_eq!(P3::I32, 3);
125/// ```
126pub trait Integer: Sealed + Copy + Default + 'static {
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/// Someday, it may contain an associated constant to produce a runtime array,
159/// like the other marker traits here. However, that is blocked by [this
160/// issue](https://github.com/rust-lang/rust/issues/44168).
161pub trait TypeArray: Sealed {}
162
163/// The **marker trait** for type-level numbers which are a power of two.
164///
165/// # Examples
166///
167/// Here's a working example:
168///
169/// ```rust
170/// use typenum::{PowerOfTwo, P4, P8};
171///
172/// fn only_p2<P: PowerOfTwo>() {}
173///
174/// only_p2::<P4>();
175/// only_p2::<P8>();
176/// ```
177///
178/// Numbers which are not a power of two will fail to compile in this example:
179///
180/// ```rust,compile_fail
181/// use typenum::{P9, P511, P1023, PowerOfTwo};
182///
183/// fn only_p2<P: PowerOfTwo>() { }
184///
185/// only_p2::<P9>();
186/// only_p2::<P511>();
187/// only_p2::<P1023>();
188/// ```
189pub trait PowerOfTwo: Sealed {}
190