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