Debian Codegraph @

.cargo/vendor/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 #[cfg_attr(
81 feature = "force_unix_path_separator",
82 deprecated(
83 since = "1.17.0",
84 note = "the `force_unix_path_separator` flag is no longer necessary and will be removed in the future"
85 )
86 )]
87 pub fn force_unix_path_separator() {}
88
89 const HIGHEST: u64 = 1024;
90 fn uints() -> impl Iterator<Item = u64> {
91 // Use hardcoded values to avoid issues with cross-compilation.
92 // See https://github.com/paholg/typenum/issues/162
93 let first2: u32 = 11; // (highest as f64).log(2.0).round() as u32 + 1;
94 let first10: u32 = 4; // (highest as f64).log(10.0) as u32 + 1;
95 (0..(HIGHEST + 1))
96 .chain((first2..64).map(|i| 2u64.pow(i)))
97 .chain((first10..20).map(|i| 10u64.pow(i)))
98 }
99
100 // fixme: get a warning when testing without this
101 #[allow(dead_code)]
102 fn main() {
103 println!("cargo:rerun-if-changed=build/main.rs"); // Allow caching the generation if `src/*` files change.
104
105 let out_dir = env::var("OUT_DIR").unwrap();
106 let dest = Path::new(&out_dir).join("consts.rs");
107
108 let mut f = File::create(&dest).unwrap();
109
110 no_std();
111 force_unix_path_separator();
112
113 // Header stuff here!
114 write!(
115 f,
116 "
117 /**
118 Type aliases for many constants.
119
120 This file is generated by typenum's build script.
121
122 For unsigned integers, the format is `U` followed by the number. We define aliases for
123
124 - Numbers 0 through {highest}
125 - Powers of 2 below `u64::MAX`
126 - Powers of 10 below `u64::MAX`
127
128 These alias definitions look like this:
129
130 ```rust
131 use typenum::{{B0, B1, UInt, UTerm}};
132
133 # #[allow(dead_code)]
134 type U6 = UInt<UInt<UInt<UTerm, B1>, B1>, B0>;
135 ```
136
137 For positive signed integers, the format is `P` followed by the number and for negative
138 signed integers it is `N` followed by the number. For the signed integer zero, we use
139 `Z0`. We define aliases for
140
141 - Numbers -{highest} through {highest}
142 - Powers of 2 between `i64::MIN` and `i64::MAX`
143 - Powers of 10 between `i64::MIN` and `i64::MAX`
144
145 These alias definitions look like this:
146
147 ```rust
148 use typenum::{{B0, B1, UInt, UTerm, PInt, NInt}};
149
150 # #[allow(dead_code)]
151 type P6 = PInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>>;
152 # #[allow(dead_code)]
153 type N6 = NInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>>;
154 ```
155
156 # Example
157 ```rust
158 # #[allow(unused_imports)]
159 use typenum::{{U0, U1, U2, U3, U4, U5, U6}};
160 # #[allow(unused_imports)]
161 use typenum::{{N3, N2, N1, Z0, P1, P2, P3}};
162 # #[allow(unused_imports)]
163 use typenum::{{U774, N17, N10000, P1024, P4096}};
164 ```
165
166 We also define the aliases `False` and `True` for `B0` and `B1`, respectively.
167 */
168 #[allow(missing_docs)]
169 pub mod consts {{
170 use crate::uint::{{UInt, UTerm}};
171 use crate::int::{{PInt, NInt}};
172
173 pub use crate::bit::{{B0, B1}};
174 pub use crate::int::Z0;
175
176 pub type True = B1;
177 pub type False = B0;
178 ",
179 highest = HIGHEST,
180 )
181 .unwrap();
182
183 for u in uints() {
184 writeln!(f, " pub type U{} = {};", u, gen_uint(u)).unwrap();
185 if u <= ::std::i64::MAX as u64 && u != 0 {
186 let i = u as i64;
187 writeln!(
188 f,
189 " pub type P{i} = PInt<U{i}>; pub type N{i} = NInt<U{i}>;",
190 i = i
191 )
192 .unwrap();
193 }
194 }
195 write!(f, "}}").unwrap();
196
197 tests::build_tests().unwrap();
198
199 op::write_op_macro().unwrap();
200
201 #[cfg(feature = "const-generics")]
202 generic_const_mappings::emit_impls().unwrap();
203 }