1 //! This crate provides type-level numbers evaluated at compile time. It depends only on libcore.
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**.
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
11 //! use typenum::{Integer, N4};
13 //! assert_eq!(N4::to_i32(), -4);
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.
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.
26 //! use std::ops::Add;
27 //! use typenum::{Integer, P3, P4};
29 //! type X = <P3 as Add<P4>>::Output;
30 //! assert_eq!(<X as Integer>::to_i32(), 7);
33 //! In addition, helper aliases are defined for type operators. For example, the above snippet
34 //! could be replaced with
37 //! use typenum::{Integer, Sum, P3, P4};
39 //! type X = Sum<P3, P4>;
40 //! assert_eq!(<X as Integer>::to_i32(), 7);
43 //! Documented in each module is the full list of type operators implemented.
46 #![forbid(unsafe_code)]
47 #![warn(missing_docs)]
48 #![cfg_attr(feature = "strict", deny(missing_docs))]
49 #![cfg_attr(feature = "strict", deny(warnings))]
51 feature
= "cargo-clippy",
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
,
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")]
64 // For debugging macros:
65 // #![feature(trace_macros)]
66 // trace_macros!(true);
68 use core
::cmp
::Ordering
;
70 #[cfg(feature = "force_unix_path_separator")]
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"));
78 #[cfg(not(feature = "force_unix_path_separator"))]
80 include
!(env
!("TYPENUM_BUILD_OP"));
81 include
!(env
!("TYPENUM_BUILD_CONSTS"));
82 #[cfg(feature = "const-generics")]
83 include
!(env
!("TYPENUM_BUILD_GENERIC_CONSTS"));
88 pub mod marker_traits
;
89 pub mod operator_aliases
;
91 pub mod type_operators
;
111 N1
, N2
, Z0
, P1
, P2
, *,
114 #[cfg(feature = "const-generics")]
115 pub use crate::generated
::generic_const_mappings
;
117 #[cfg(feature = "const-generics")]
119 pub use generic_const_mappings
::{Const, ToUInt, U}
;
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))]
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))]
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))]
139 /// Returns `core::cmp::Ordering::Greater`
140 impl Ord
for Greater
{
142 fn to_ordering() -> Ordering
{
147 /// Returns `core::cmp::Ordering::Less`
150 fn to_ordering() -> Ordering
{
155 /// Returns `core::cmp::Ordering::Equal`
158 fn to_ordering() -> Ordering
{
163 /// Asserts that two types are the same.
165 macro_rules
! assert_type_eq
{
167 const _
: core
::marker
::PhantomData
<<$a
as $
crate::Same
<$b
>>::Output
> =
168 core
::marker
::PhantomData
;
172 /// Asserts that a type is `True`, aka `B1`.
174 macro_rules
! assert_type
{
176 const _
: core
::marker
::PhantomData
<<$a
as $
crate::Same
<True
>>::Output
> =
177 core
::marker
::PhantomData
;
183 ATerm
, Bit
, Equal
, Greater
, Less
, NInt
, NonZero
, PInt
, TArr
, UInt
, UTerm
, Unsigned
, B0
, B1
,
189 impl Sealed
for B0 {}
190 impl Sealed
for B1 {}
192 impl Sealed
for UTerm {}
193 impl<U
: Unsigned
, B
: Bit
> Sealed
for UInt
<U
, B
> {}
195 impl Sealed
for Z0 {}
196 impl<U
: Unsigned
+ NonZero
> Sealed
for PInt
<U
> {}
197 impl<U
: Unsigned
+ NonZero
> Sealed
for NInt
<U
> {}
199 impl Sealed
for Less {}
200 impl Sealed
for Equal {}
201 impl Sealed
for Greater {}
203 impl Sealed
for ATerm {}
204 impl<V
, A
> Sealed
for TArr
<V
, A
> {}