Debian Codegraph @

third_party/rust/typenum/build/main.rs

raw
1 use std::env;
2 use std::fmt;
3 use std::fs::File;
4 use std::io::Write;
5 use std::path::Path;
6
7 #[cfg(feature = "const-generics")]
8 mod generic_const_mappings;
9 mod op;
10 mod tests;
11
12 pub enum UIntCode {
13 Term,
14 Zero(Box<UIntCode>),
15 One(Box<UIntCode>),
16 }
17
18 pub enum IntCode {
19 Zero,
20 Pos(Box<UIntCode>),
21 Neg(Box<UIntCode>),
22 }
23
24 impl fmt::Display for UIntCode {
25 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26 match *self {
27 UIntCode::Term => write!(f, "UTerm"),
28 UIntCode::Zero(ref inner) => write!(f, "UInt<{}, B0>", inner),
29 UIntCode::One(ref inner) => write!(f, "UInt<{}, B1>", inner),
30 }
31 }
32 }
33
34 impl fmt::Display for IntCode {
35 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36 match *self {
37 IntCode::Zero => write!(f, "Z0"),
38 IntCode::Pos(ref inner) => write!(f, "PInt<{}>", inner),
39 IntCode::Neg(ref inner) => write!(f, "NInt<{}>", inner),
40 }
41 }
42 }
43
44 pub fn gen_uint(u: u64) -> UIntCode {
45 let mut result = UIntCode::Term;
46 let mut x = 1u64 << 63;
47 while x > u {
48 x >>= 1
49 }
50 while x > 0 {
51 result = if x & u > 0 {
52 UIntCode::One(Box::new(result))
53 } else {
54 UIntCode::Zero(Box::new(result))
55 };
56 x >>= 1;
57 }
58 result
59 }
60
61 pub fn gen_int(i: i64) -> IntCode {
62 use std::cmp::Ordering::{Equal, Greater, Less};
63
64 match i.cmp(&0) {
65 Greater => IntCode::Pos(Box::new(gen_uint(i as u64))),
66 Less => IntCode::Neg(Box::new(gen_uint(i.abs() as u64))),
67 Equal => IntCode::Zero,
68 }
69 }
70
71 #[cfg_attr(
72 feature = "no_std",
73 deprecated(
74 since = "1.3.0",
75 note = "the `no_std` flag is no longer necessary and will be removed in the future"
76 )
77 )]
78 pub fn no_std() {}
79
80 const HIGHEST: u64 = 1024;
81 fn uints() -> impl Iterator<Item = u64> {
82 // Use hardcoded values to avoid issues with cross-compilation.
83 // See https://github.com/paholg/typenum/issues/162
84 let first2: u32 = 11; // (highest as f64).log(2.0).round() as u32 + 1;
85 let first10: u32 = 4; // (highest as f64).log(10.0) as u32 + 1;
86 (0..(HIGHEST + 1))
87 .chain((first2..64).map(|i| 2u64.pow(i)))
88 .chain((first10..20).map(|i| 10u64.pow(i)))
89 }
90
91 // fixme: get a warning when testing without this
92 #[allow(dead_code)]
93 fn main() {
94 println!("cargo:rerun-if-changed=build/main.rs"); // Allow caching the generation if `src/*` files change.
95
96 let out_dir = env::var("OUT_DIR").unwrap();
97 let dest = Path::new(&out_dir).join("consts.rs");
98 #[cfg(not(feature = "force_unix_path_separator"))]
99 println!("cargo:rustc-env=TYPENUM_BUILD_CONSTS={}", dest.display());
100
101 let mut f = File::create(&dest).unwrap();
102
103 no_std();
104
105 // Header stuff here!
106 write!(
107 f,
108 "
109 /**
110 Type aliases for many constants.
111
112 This file is generated by typenum's build script.
113
114 For unsigned integers, the format is `U` followed by the number. We define aliases for
115
116 - Numbers 0 through {highest}
117 - Powers of 2 below `u64::MAX`
118 - Powers of 10 below `u64::MAX`
119
120 These alias definitions look like this:
121
122 ```rust
123 use typenum::{{B0, B1, UInt, UTerm}};
124
125 # #[allow(dead_code)]
126 type U6 = UInt<UInt<UInt<UTerm, B1>, B1>, B0>;
127 ```
128
129 For positive signed integers, the format is `P` followed by the number and for negative
130 signed integers it is `N` followed by the number. For the signed integer zero, we use
131 `Z0`. We define aliases for
132
133 - Numbers -{highest} through {highest}
134 - Powers of 2 between `i64::MIN` and `i64::MAX`
135 - Powers of 10 between `i64::MIN` and `i64::MAX`
136
137 These alias definitions look like this:
138
139 ```rust
140 use typenum::{{B0, B1, UInt, UTerm, PInt, NInt}};
141
142 # #[allow(dead_code)]
143 type P6 = PInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>>;
144 # #[allow(dead_code)]
145 type N6 = NInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>>;
146 ```
147
148 # Example
149 ```rust
150 # #[allow(unused_imports)]
151 use typenum::{{U0, U1, U2, U3, U4, U5, U6}};
152 # #[allow(unused_imports)]
153 use typenum::{{N3, N2, N1, Z0, P1, P2, P3}};
154 # #[allow(unused_imports)]
155 use typenum::{{U774, N17, N10000, P1024, P4096}};
156 ```
157
158 We also define the aliases `False` and `True` for `B0` and `B1`, respectively.
159 */
160 #[allow(missing_docs)]
161 pub mod consts {{
162 use crate::uint::{{UInt, UTerm}};
163 use crate::int::{{PInt, NInt}};
164
165 pub use crate::bit::{{B0, B1}};
166 pub use crate::int::Z0;
167
168 pub type True = B1;
169 pub type False = B0;
170 ",
171 highest = HIGHEST,
172 )
173 .unwrap();
174
175 for u in uints() {
176 writeln!(f, " pub type U{} = {};", u, gen_uint(u)).unwrap();
177 if u <= ::std::i64::MAX as u64 && u != 0 {
178 let i = u as i64;
179 writeln!(
180 f,
181 " pub type P{i} = PInt<U{i}>; pub type N{i} = NInt<U{i}>;",
182 i = i
183 )
184 .unwrap();
185 }
186 }
187 write!(f, "}}").unwrap();
188
189 tests::build_tests().unwrap();
190
191 op::write_op_macro().unwrap();
192
193 #[cfg(feature = "const-generics")]
194 generic_const_mappings::emit_impls().unwrap();
195 }