1//! This crate provides type-level numbers evaluated at compile time. It depends only on libcore.
2//!
3//! The traits defined or used in this crate are used in a typical manner. They can be divided into
4//! two categories: **marker traits** and **type operators**.
5//!
6//! Many of the marker traits have functions defined, but they all do essentially the same thing:
7//! convert a type into its runtime counterpart, and are really just there for debugging. For
8//! example,
9//!
10//! ```rust
11//! use typenum::{Integer, N4};
12//!
13//! assert_eq!(N4::to_i32(), -4);
14//! ```
15//!
16//! **Type operators** are traits that behave as functions at the type level. These are the meat of
17//! this library. Where possible, traits defined in libcore have been used, but their attached
18//! functions have not been implemented.
19//!
20//! For example, the `Add` trait is implemented for both unsigned and signed integers, but the
21//! `add` function is not. As there are never any objects of the types defined here, it wouldn't
22//! make sense to implement it. What is important is its associated type `Output`, which is where
23//! the addition happens.
24//!
25//! ```rust
26//! use std::ops::Add;
27//! use typenum::{Integer, P3, P4};
28//!
29//! type X = <P3 as Add<P4>>::Output;
30//! assert_eq!(<X as Integer>::to_i32(), 7);
31//! ```
32//!
33//! In addition, helper aliases are defined for type operators. For example, the above snippet
34//! could be replaced with
35//!
36//! ```rust
37//! use typenum::{Integer, Sum, P3, P4};
38//!
39//! type X = Sum<P3, P4>;
40//! assert_eq!(<X as Integer>::to_i32(), 7);
41//! ```
42//!
43//! Documented in each module is the full list of type operators implemented.
44
45#![no_std]
46#![forbid(unsafe_code)]
47#![warn(missing_docs)]
48#![cfg_attr(feature = "strict", deny(missing_docs))]
49#![cfg_attr(feature = "strict", deny(warnings))]
50#![cfg_attr(
51 feature = "cargo-clippy",
52 allow(
53 clippy::len_without_is_empty,
54 clippy::many_single_char_names,
55 clippy::new_without_default,
56 clippy::suspicious_arithmetic_impl,
57 clippy::type_complexity,
58 clippy::wrong_self_convention,
59 )
60)]
61#![cfg_attr(feature = "cargo-clippy", deny(clippy::missing_inline_in_public_items))]
62#![doc(html_root_url = "https://docs.rs/typenum/1.16.0")]
63
64// For debugging macros:
65// #![feature(trace_macros)]
66// trace_macros!(true);
67
68use core::cmp::Ordering;
69
70#[cfg(feature = "force_unix_path_separator")]
71mod generated {
72 include!(concat!(env!("OUT_DIR"), "/op.rs"));
73 include!(concat!(env!("OUT_DIR"), "/consts.rs"));
74 #[cfg(feature = "const-generics")]
75 include!(concat!(env!("OUT_DIR"), "/generic_const_mappings.rs"));
76}
77
78#[cfg(not(feature = "force_unix_path_separator"))]
79mod generated {
80 include!(env!("TYPENUM_BUILD_OP"));
81 include!(env!("TYPENUM_BUILD_CONSTS"));
82 #[cfg(feature = "const-generics")]
83 include!(env!("TYPENUM_BUILD_GENERIC_CONSTS"));
84}
85
86pub mod bit;
87pub mod int;
88pub mod marker_traits;
89pub mod operator_aliases;
90pub mod private;
91pub mod type_operators;
92pub mod uint;
93
94pub mod array;
95
96pub use crate::{
97 array::{ATerm, TArr},
98 generated::consts,
99 int::{NInt, PInt},
100 marker_traits::*,
101 operator_aliases::*,
102 type_operators::*,
103 uint::{UInt, UTerm},
104};
105
106#[doc(no_inline)]
107#[rustfmt::skip]
108pub use consts::{
109 False, True, B0, B1,
110 U0, U1, U2, *,
111 N1, N2, Z0, P1, P2, *,
112};
113
114#[cfg(feature = "const-generics")]
115pub use crate::generated::generic_const_mappings;
116
117#[cfg(feature = "const-generics")]
118#[doc(no_inline)]
119pub use generic_const_mappings::{Const, ToUInt, U};
120
121/// A potential output from `Cmp`, this is the type equivalent to the enum variant
122/// `core::cmp::Ordering::Greater`.
123#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Debug, Default)]
124#[cfg_attr(feature = "scale_info", derive(scale_info::TypeInfo))]
125pub struct Greater;
126
127/// A potential output from `Cmp`, this is the type equivalent to the enum variant
128/// `core::cmp::Ordering::Less`.
129#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Debug, Default)]
130#[cfg_attr(feature = "scale_info", derive(scale_info::TypeInfo))]
131pub struct Less;
132
133/// A potential output from `Cmp`, this is the type equivalent to the enum variant
134/// `core::cmp::Ordering::Equal`.
135#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Debug, Default)]
136#[cfg_attr(feature = "scale_info", derive(scale_info::TypeInfo))]
137pub struct Equal;
138
139/// Returns `core::cmp::Ordering::Greater`
140impl Ord for Greater {
141 #[inline]
142 fn to_ordering() -> Ordering {
143 Ordering::Greater
144 }
145}
146
147/// Returns `core::cmp::Ordering::Less`
148impl Ord for Less {
149 #[inline]
150 fn to_ordering() -> Ordering {
151 Ordering::Less
152 }
153}
154
155/// Returns `core::cmp::Ordering::Equal`
156impl Ord for Equal {
157 #[inline]
158 fn to_ordering() -> Ordering {
159 Ordering::Equal
160 }
161}
162
163/// Asserts that two types are the same.
164#[macro_export]
165macro_rules! assert_type_eq {
166 ($a:ty, $b:ty) => {
167 const _: core::marker::PhantomData<<$a as $crate::Same<$b>>::Output> =
168 core::marker::PhantomData;
169 };
170}
171
172/// Asserts that a type is `True`, aka `B1`.
173#[macro_export]
174macro_rules! assert_type {
175 ($a:ty) => {
176 const _: core::marker::PhantomData<<$a as $crate::Same<True>>::Output> =
177 core::marker::PhantomData;
178 };
179}
180
181mod sealed {
182 use crate::{
183 ATerm, Bit, Equal, Greater, Less, NInt, NonZero, PInt, TArr, UInt, UTerm, Unsigned, B0, B1,
184 Z0,
185 };
186
187 pub trait Sealed {}
188
189 impl Sealed for B0 {}
190 impl Sealed for B1 {}
191
192 impl Sealed for UTerm {}
193 impl<U: Unsigned, B: Bit> Sealed for UInt<U, B> {}
194
195 impl Sealed for Z0 {}
196 impl<U: Unsigned + NonZero> Sealed for PInt<U> {}
197 impl<U: Unsigned + NonZero> Sealed for NInt<U> {}
198
199 impl Sealed for Less {}
200 impl Sealed for Equal {}
201 impl Sealed for Greater {}
202
203 impl Sealed for ATerm {}
204 impl<V, A> Sealed for TArr<V, A> {}
205}
206