typenum/
lib.rs

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.15.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}
75
76#[cfg(not(feature = "force_unix_path_separator"))]
77mod generated {
78    include!(env!("TYPENUM_BUILD_OP"));
79    include!(env!("TYPENUM_BUILD_CONSTS"));
80}
81
82pub mod bit;
83pub mod int;
84pub mod marker_traits;
85pub mod operator_aliases;
86pub mod private;
87pub mod type_operators;
88pub mod uint;
89
90pub mod array;
91
92pub use crate::{
93    array::{ATerm, TArr},
94    consts::*,
95    generated::consts,
96    int::{NInt, PInt},
97    marker_traits::*,
98    operator_aliases::*,
99    type_operators::*,
100    uint::{UInt, UTerm},
101};
102
103/// A potential output from `Cmp`, this is the type equivalent to the enum variant
104/// `core::cmp::Ordering::Greater`.
105#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Debug, Default)]
106#[cfg_attr(feature = "scale_info", derive(scale_info::TypeInfo))]
107pub struct Greater;
108
109/// A potential output from `Cmp`, this is the type equivalent to the enum variant
110/// `core::cmp::Ordering::Less`.
111#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Debug, Default)]
112#[cfg_attr(feature = "scale_info", derive(scale_info::TypeInfo))]
113pub struct Less;
114
115/// A potential output from `Cmp`, this is the type equivalent to the enum variant
116/// `core::cmp::Ordering::Equal`.
117#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Debug, Default)]
118#[cfg_attr(feature = "scale_info", derive(scale_info::TypeInfo))]
119pub struct Equal;
120
121/// Returns `core::cmp::Ordering::Greater`
122impl Ord for Greater {
123    #[inline]
124    fn to_ordering() -> Ordering {
125        Ordering::Greater
126    }
127}
128
129/// Returns `core::cmp::Ordering::Less`
130impl Ord for Less {
131    #[inline]
132    fn to_ordering() -> Ordering {
133        Ordering::Less
134    }
135}
136
137/// Returns `core::cmp::Ordering::Equal`
138impl Ord for Equal {
139    #[inline]
140    fn to_ordering() -> Ordering {
141        Ordering::Equal
142    }
143}
144
145/// Asserts that two types are the same.
146#[macro_export]
147macro_rules! assert_type_eq {
148    ($a:ty, $b:ty) => {
149        const _: core::marker::PhantomData<<$a as $crate::Same<$b>>::Output> =
150            core::marker::PhantomData;
151    };
152}
153
154/// Asserts that a type is `True`, aka `B1`.
155#[macro_export]
156macro_rules! assert_type {
157    ($a:ty) => {
158        const _: core::marker::PhantomData<<$a as $crate::Same<True>>::Output> =
159            core::marker::PhantomData;
160    };
161}
162
163mod sealed {
164    use crate::{
165        ATerm, Bit, Equal, Greater, Less, NInt, NonZero, PInt, TArr, UInt, UTerm, Unsigned, B0, B1,
166        Z0,
167    };
168
169    pub trait Sealed {}
170
171    impl Sealed for B0 {}
172    impl Sealed for B1 {}
173
174    impl Sealed for UTerm {}
175    impl<U: Unsigned, B: Bit> Sealed for UInt<U, B> {}
176
177    impl Sealed for Z0 {}
178    impl<U: Unsigned + NonZero> Sealed for PInt<U> {}
179    impl<U: Unsigned + NonZero> Sealed for NInt<U> {}
180
181    impl Sealed for Less {}
182    impl Sealed for Equal {}
183    impl Sealed for Greater {}
184
185    impl Sealed for ATerm {}
186    impl<V, A> Sealed for TArr<V, A> {}
187}