@pkmn/engine 0.1.0-dev.d97c6e03 → 0.1.0-dev.da4a0515

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/LICENSE +1 -3
  2. package/README.md +102 -68
  3. package/build/pkg/addon.d.ts +1 -1
  4. package/build/pkg/addon.js +13 -13
  5. package/build/pkg/addon.js.map +1 -1
  6. package/build/pkg/data/ids.json +12 -12
  7. package/build/pkg/data/layout.json +27 -5
  8. package/build/pkg/data/protocol.json +69 -12
  9. package/build/pkg/data.d.ts +4 -4
  10. package/build/pkg/data.js.map +1 -1
  11. package/build/pkg/gen1.d.ts +5 -4
  12. package/build/pkg/gen1.js +48 -32
  13. package/build/pkg/gen1.js.map +1 -1
  14. package/build/pkg/index.d.ts +17 -13
  15. package/build/pkg/index.js.map +1 -1
  16. package/build/pkg/protocol.d.ts +4 -2
  17. package/build/pkg/protocol.js +65 -44
  18. package/build/pkg/protocol.js.map +1 -1
  19. package/build/test/display.js +31 -14
  20. package/build/test/display.js.map +1 -1
  21. package/build/tools/debug.js +5 -4
  22. package/build/tools/debug.js.map +1 -1
  23. package/build.zig +157 -119
  24. package/package.json +56 -26
  25. package/src/bin/install-pkmn-engine +36 -23
  26. package/src/lib/common/battle.zig +17 -0
  27. package/src/lib/common/data.zig +19 -31
  28. package/src/lib/common/debug.zig +21 -11
  29. package/src/lib/common/js.zig +118 -0
  30. package/src/lib/common/optional.zig +67 -0
  31. package/src/lib/common/options.zig +12 -3
  32. package/src/lib/common/protocol.zig +942 -403
  33. package/src/lib/common/rational.zig +357 -0
  34. package/src/lib/common/rng.zig +35 -26
  35. package/src/lib/common/util.zig +12 -0
  36. package/src/lib/gen1/calc.zig +426 -0
  37. package/src/lib/gen1/chance.zig +1061 -0
  38. package/src/lib/gen1/data/moves.zig +202 -190
  39. package/src/lib/gen1/data/species.zig +5 -5
  40. package/src/lib/gen1/data/types.zig +10 -10
  41. package/src/lib/gen1/data.zig +101 -87
  42. package/src/lib/gen1/helpers.zig +602 -37
  43. package/src/lib/gen1/mechanics.zig +1086 -740
  44. package/src/lib/gen2/calc.zig +143 -0
  45. package/src/lib/gen2/chance.zig +1315 -0
  46. package/src/lib/gen2/data/items.zig +14 -20
  47. package/src/lib/gen2/data/moves.zig +664 -570
  48. package/src/lib/gen2/data/species.zig +2 -2
  49. package/src/lib/gen2/data/types.zig +137 -34
  50. package/src/lib/gen2/data.zig +130 -92
  51. package/src/lib/gen2/generated.zig +806 -0
  52. package/src/lib/gen2/helpers.zig +16 -22
  53. package/src/lib/gen2/mechanics.zig +3591 -12
  54. package/src/lib/node.zig +131 -0
  55. package/src/lib/pkmn.zig +88 -6
  56. package/src/lib/wasm.zig +13 -0
  57. package/src/lib/binding/node.zig +0 -229
  58. package/src/lib/binding/wasm.zig +0 -10
  59. package/src/lib/gen1/README.md +0 -791
  60. package/src/lib/gen2/README.md +0 -309
@@ -0,0 +1,357 @@
1
+ const builtin = @import("builtin");
2
+ const std = @import("std");
3
+
4
+ const assert = std.debug.assert;
5
+ const expect = std.testing.expect;
6
+ const expectEqual = std.testing.expectEqual;
7
+ const expectError = std.testing.expectError;
8
+
9
+ /// Specialization of a rational number used by the engine to compute probabilties.
10
+ /// For performance reasons the rational is only reduced lazily and thus `reduce` must be
11
+ /// invoked explicitly before reading.
12
+ pub fn Rational(comptime T: type) type {
13
+ return extern struct {
14
+ const Self = @This();
15
+
16
+ /// Numerator. Must always be >= 1. Not guaranteed to be reduced in all cases.
17
+ p: T = 1,
18
+ /// Denominator. Must always be >= 1. Not guaranteed to be reduced in all cases.
19
+ q: T = 1,
20
+
21
+ // With floats we can't rely on an overflow bit to let us know when to reduce, so we instead
22
+ // start reducing when we get sufficiently close to the limit of the mantissa (in our domain
23
+ // we expect updates to involve numbers < 2**10, so we should be safe not reducing before we
24
+ // are 2**10 away from "overflowing" the mantissa)
25
+ const REDUCE = if (@typeInfo(T) == .Float)
26
+ std.math.pow(T, 2, std.math.floatMantissaBits(T) - 10)
27
+ else
28
+ 0;
29
+
30
+ /// Possible error returned by operations on the Rational.
31
+ pub const Error = switch (@typeInfo(T)) {
32
+ .Int => error{Overflow},
33
+ .Float => error{},
34
+ else => unreachable,
35
+ };
36
+
37
+ /// Resets the rational back to 1.
38
+ pub fn reset(r: *Self) void {
39
+ r.p = 1;
40
+ r.q = 1;
41
+ }
42
+
43
+ /// Update the rational by multiplying its numerator by p and its denominator by q.
44
+ /// Both p and q must be >= 1, and if computable at comptime must have no common factors.
45
+ pub fn update(r: *Self, p: anytype, q: anytype) Error!void {
46
+ // FIXME
47
+ // std.debug.print("({d}/{d}) * ", .{ p, q });
48
+ assert(p >= 1);
49
+ assert(q >= 1);
50
+
51
+ // If our parameters are not fully reduced they may prematurely
52
+ // cause overflow/loss of precision after the multiplication below
53
+ assert(switch (@typeInfo(@TypeOf(p, q))) {
54
+ .ComptimeInt, .ComptimeFloat => comptime gcd(p, q),
55
+ else => 1,
56
+ } == 1);
57
+
58
+ switch (@typeInfo(T)) {
59
+ .Int => {
60
+ // Greedily attempt to multiply and if it fails, reduce and try again
61
+ r.multiplication(p, q) catch |err| switch (err) {
62
+ error.Overflow => {
63
+ r.reduce();
64
+ try r.multiplication(p, q);
65
+ },
66
+ else => unreachable,
67
+ };
68
+ },
69
+ .Float => {
70
+ // Reduce in situations where we're likely to start losing precision
71
+ if (r.q > REDUCE or r.p > REDUCE) r.reduce();
72
+
73
+ r.p *= switch (@typeInfo(@TypeOf(p))) {
74
+ .Float, .ComptimeFloat => p,
75
+ else => @floatFromInt(p),
76
+ };
77
+ r.q *= switch (@typeInfo(@TypeOf(q))) {
78
+ .Float, .ComptimeFloat => q,
79
+ else => @floatFromInt(q),
80
+ };
81
+
82
+ // We should always be dealing with whole numbers
83
+ assert(std.math.modf(r.p).fpart == 0);
84
+ assert(std.math.modf(r.q).fpart == 0);
85
+ },
86
+ else => unreachable,
87
+ }
88
+ }
89
+
90
+ /// Add two rationals using the identity (a/b) + (c/d) = (ad+bc)/(bd).
91
+ pub fn add(r: *Self, s: *Self) Error!void {
92
+ switch (@typeInfo(T)) {
93
+ .Int => {
94
+ if (r.q == s.q) {
95
+ r.p = std.math.add(T, r.p, s.p) catch |err| switch (err) {
96
+ error.Overflow => val: {
97
+ r.reduce();
98
+ s.reduce();
99
+ break :val try std.math.add(T, r.p, s.p);
100
+ },
101
+ else => unreachable,
102
+ };
103
+ } else {
104
+ r.addition(s.p, s.q) catch |err| switch (err) {
105
+ error.Overflow => {
106
+ r.reduce();
107
+ s.reduce();
108
+ try r.addition(s.p, s.q);
109
+ },
110
+ else => unreachable,
111
+ };
112
+ }
113
+ },
114
+ .Float => {
115
+ if (r.q == s.q) {
116
+ if (r.p > REDUCE) r.reduce();
117
+ if (s.p > REDUCE) s.reduce();
118
+
119
+ r.p += s.p;
120
+ } else {
121
+ // Always reduce to minimize loss of precision from the multiplications
122
+ r.reduce();
123
+ s.reduce();
124
+
125
+ r.p = (r.p * s.q) + (r.q * s.p);
126
+ r.q *= s.q;
127
+ }
128
+
129
+ assert(std.math.modf(r.p).fpart == 0);
130
+ assert(std.math.modf(r.q).fpart == 0);
131
+ },
132
+ else => unreachable,
133
+ }
134
+ }
135
+
136
+ /// Multiplies two rationals.
137
+ pub fn mul(r: *Self, s: *Self) Error!void {
138
+ switch (@typeInfo(T)) {
139
+ .Int => {
140
+ r.multiplication(s.p, s.q) catch |err| switch (err) {
141
+ error.Overflow => {
142
+ r.reduce();
143
+ s.reduce();
144
+ try r.multiplication(s.p, s.q);
145
+ },
146
+ else => unreachable,
147
+ };
148
+ },
149
+ .Float => {
150
+ if (r.q > REDUCE or r.p > REDUCE) r.reduce();
151
+ if (s.q > REDUCE or s.p > REDUCE) s.reduce();
152
+
153
+ r.p *= s.p;
154
+ r.q *= s.q;
155
+
156
+ assert(std.math.modf(r.p).fpart == 0);
157
+ assert(std.math.modf(r.q).fpart == 0);
158
+ },
159
+ else => unreachable,
160
+ }
161
+ }
162
+
163
+ /// Normalize the rational by reducing by the greatest common divisor.
164
+ pub fn reduce(r: *Self) void {
165
+ const d = gcd(r.p, r.q);
166
+ if (d == 1) return;
167
+
168
+ assert(@mod(r.p, d) == 0);
169
+ assert(@mod(r.q, d) == 0);
170
+
171
+ r.p /= d;
172
+ r.q /= d;
173
+
174
+ assert(r.p >= 1);
175
+ assert(r.q >= 1);
176
+ }
177
+
178
+ pub fn format(
179
+ self: Self,
180
+ comptime fmt: []const u8,
181
+ opts: std.fmt.FormatOptions,
182
+ writer: anytype,
183
+ ) !void {
184
+ _ = .{ fmt, opts };
185
+ try writer.print("{d}/{d}", .{ self.p, self.q });
186
+ }
187
+
188
+ fn multiplication(r: *Self, p: anytype, q: anytype) !void {
189
+ r.p = try std.math.mul(T, r.p, p);
190
+ r.q = try std.math.mul(T, r.q, q);
191
+ }
192
+
193
+ fn addition(r: *Self, p: anytype, q: anytype) !void {
194
+ // (a/b) + (c/d) = (ad+bc)/(bd)
195
+ const d = try std.math.mul(T, r.q, q);
196
+ const n1 = try std.math.mul(T, r.p, q);
197
+ const n2 = try std.math.mul(T, r.q, p);
198
+
199
+ r.p = try std.math.add(T, n1, n2);
200
+ r.q = d;
201
+ }
202
+ };
203
+ }
204
+
205
+ // https://en.wikipedia.org/wiki/Euclidean_algorithm
206
+ fn gcd(p: anytype, q: anytype) @TypeOf(p, q) {
207
+ assert(p >= 1);
208
+ assert(q >= 1);
209
+
210
+ var a = p;
211
+ var b = q;
212
+ var c: @TypeOf(p, q) = undefined;
213
+
214
+ while (b != 0) {
215
+ c = b;
216
+ b = @mod(a, b);
217
+ a = c;
218
+ }
219
+
220
+ assert(a > 0);
221
+ return a;
222
+ }
223
+
224
+ fn doTurn(r: anytype) !void {
225
+ try r.update(1, 163); // Metronome
226
+ try r.update(33, 256); // Critical Hit (67 Speed)
227
+ try r.update(1, 39); // Damage roll
228
+ try r.update(89, 128); // Thunder accuracy (178/256)
229
+ try r.update(77, 256); // Thunder secondary proc
230
+ }
231
+
232
+ test Rational {
233
+ inline for (.{ u64, u128, f64 }) |t| {
234
+ var r: Rational(t) = .{};
235
+
236
+ var c: t = 128;
237
+ _ = &c;
238
+
239
+ try r.update(c, 256);
240
+ try doTurn(&r);
241
+
242
+ r.reduce();
243
+ try expectEqual(Rational(t){ .p = 75383, .q = 35550920704 }, r);
244
+
245
+ try r.update(1, 4);
246
+ if (t == u64) {
247
+ try expectError(error.Overflow, doTurn(&r));
248
+ } else {
249
+ try doTurn(&r);
250
+ r.reduce();
251
+ try expectEqual(Rational(t){ .p = 5682596689, .q = 2527735925804191711232 }, r);
252
+ }
253
+
254
+ r.reset();
255
+
256
+ var s = Rational(t){ .p = 10, .q = 13 };
257
+ try r.mul(&s);
258
+ s = Rational(t){ .p = 3, .q = 4 };
259
+ try r.mul(&s);
260
+ try expectEqual(Rational(t){ .p = 30, .q = 52 }, r);
261
+
262
+ s = Rational(t){ .p = 1, .q = 3 };
263
+ try r.add(&s);
264
+ r.reduce();
265
+ try expectEqual(Rational(t){ .p = 71, .q = 78 }, r);
266
+ }
267
+ }
268
+
269
+ /// Adapter of std.math.big.Rational to the engines Rational interface.
270
+ pub const BigRational = struct {
271
+ /// The underlying rational number.
272
+ val: std.math.big.Rational,
273
+
274
+ /// Possible error returned by operations on the rational.
275
+ pub const Error = error{OutOfMemory};
276
+
277
+ /// Create a new BigRational wrapper. A small amount of memory will be allocated on
278
+ /// initialization. Not default initialized to 1 - you must explicitly `reset` first.
279
+ pub fn init(alloc: std.mem.Allocator) !BigRational {
280
+ return .{ .val = try std.math.big.Rational.init(alloc) };
281
+ }
282
+
283
+ /// Frees all memory associated with a rational.
284
+ pub fn deinit(r: *BigRational) void {
285
+ r.val.deinit();
286
+ }
287
+
288
+ /// Resets the rational back to 1.
289
+ pub fn reset(r: *BigRational) !void {
290
+ try r.val.setInt(1);
291
+ }
292
+
293
+ /// Update the rational by multiplying its numerator by p and its denominator by q.
294
+ /// Both p and q must be >= 1.
295
+ pub fn update(r: *BigRational, p: anytype, q: anytype) !void {
296
+ assert(p >= 1);
297
+ assert(q >= 1);
298
+
299
+ var s = try std.math.big.Rational.init(r.val.p.allocator);
300
+ defer s.deinit();
301
+ try s.setRatio(p, q);
302
+ try r.val.mul(r.val, s);
303
+ }
304
+
305
+ /// Adds two rationals.
306
+ pub fn add(r: *BigRational, s: *const BigRational) !void {
307
+ try r.val.add(r.val, s.val);
308
+ }
309
+
310
+ /// Multiplies two rationals.
311
+ pub fn mul(r: *BigRational, s: *const BigRational) !void {
312
+ try r.val.mul(r.val, s.val);
313
+ }
314
+ };
315
+
316
+ test BigRational {
317
+ var r = try BigRational.init(std.testing.allocator);
318
+ defer r.deinit();
319
+ var s = try std.math.big.Rational.init(std.testing.allocator);
320
+ defer s.deinit();
321
+
322
+ try r.reset();
323
+
324
+ var c: u8 = 128;
325
+ _ = &c;
326
+
327
+ try r.update(c, 256);
328
+ try doTurn(&r);
329
+
330
+ try s.setRatio(75383, 35550920704);
331
+ try expect((try s.order(r.val)) == .eq);
332
+
333
+ try r.update(1, 4);
334
+ try doTurn(&r);
335
+
336
+ try s.setRatio(5682596689, 2527735925804191711232);
337
+ try expect((try s.order(r.val)) == .eq);
338
+
339
+ try r.reset();
340
+
341
+ var t = try BigRational.init(std.testing.allocator);
342
+ defer t.deinit();
343
+
344
+ try t.val.setRatio(10, 13);
345
+ try r.mul(&t);
346
+ try t.val.setRatio(3, 4);
347
+ try r.mul(&t);
348
+
349
+ try s.setRatio(15, 26);
350
+ try expect((try s.order(r.val)) == .eq);
351
+
352
+ try t.val.setRatio(1, 3);
353
+ try r.add(&t);
354
+
355
+ try s.setRatio(71, 78);
356
+ try expect((try s.order(r.val)) == .eq);
357
+ }
@@ -1,6 +1,6 @@
1
1
  const std = @import("std");
2
2
 
3
- const options = @import("./options.zig");
3
+ const options = @import("options.zig");
4
4
 
5
5
  const assert = std.debug.assert;
6
6
  const expectEqual = std.testing.expectEqual;
@@ -25,6 +25,11 @@ pub fn PRNG(comptime gen: comptime_int) type {
25
25
  pub fn next(self: *Self) Output(gen) {
26
26
  return self.src.next();
27
27
  }
28
+
29
+ pub fn advance(self: *Self, n: usize) void {
30
+ if (!options.advance) return;
31
+ for (0..n) |_| self.src.advance();
32
+ }
28
33
  };
29
34
  }
30
35
 
@@ -47,7 +52,7 @@ pub const PSRNG = extern struct {
47
52
  }
48
53
 
49
54
  pub fn range(self: *PSRNG, comptime T: type, from: T, to: Bound(T)) T {
50
- return @intCast(T, @as(u64, self.src.next()) * (to - from) / divisor + from);
55
+ return @intCast(@as(u64, self.src.next()) * (to - from) / divisor + from);
51
56
  }
52
57
 
53
58
  pub fn chance(self: *PSRNG, comptime T: type, numerator: T, denominator: Bound(T)) bool {
@@ -61,46 +66,54 @@ pub const PSRNG = extern struct {
61
66
  (@as(u64, self.range(u16, 0, 0x10000)) << 16) |
62
67
  (@as(u64, self.range(u16, 0, 0x10000)));
63
68
  }
69
+
70
+ pub fn Bound(comptime T: type) type {
71
+ return std.math.IntFittingRange(0, std.math.maxInt(T) + 1);
72
+ }
64
73
  };
65
74
 
66
75
  test PSRNG {
67
- var psrng = PSRNG{ .src = .{ .seed = 0x0001000200030004 } };
68
- try expectEqual(@as(u8, 121), psrng.range(u8, 0, 256));
76
+ var psrng: PSRNG = .{ .src = .{ .seed = 0x0001000200030004 } };
77
+ try expectEqual(121, psrng.range(u8, 0, 256));
69
78
  try expectEqual(false, psrng.chance(u8, 128, 256)); // 226 < 128
70
79
  }
71
80
 
72
81
  // https://pkmn.cc/pokered/engine/battle/core.asm#L6644-L6693
73
82
  // https://pkmn.cc/pokecrystal/engine/battle/core.asm#L6922-L6938
74
83
  pub const Gen12 = extern struct {
75
- seed: [10]u8,
84
+ seed: [9]u8,
76
85
  index: u8 = 0,
77
86
 
78
87
  comptime {
79
- assert(@sizeOf(Gen12) == 11);
88
+ assert(@sizeOf(Gen12) == 10);
80
89
  }
81
90
 
82
- pub fn percent(p: u8) u8 {
83
- return @intCast(u8, (@as(u16, p) * 0xFF) / 100);
91
+ pub fn percent(comptime p: comptime_int) u8 {
92
+ return @intCast((@as(u16, p) * 0xFF) / 100);
84
93
  }
85
94
 
86
95
  pub fn next(self: *Gen12) u8 {
87
96
  const val = 5 *% self.seed[self.index] +% 1;
88
97
  self.seed[self.index] = val;
89
- self.index = (self.index + 1) % 10;
98
+ self.index = (self.index + 1) % 9;
90
99
  return val;
91
100
  }
101
+
102
+ pub fn advance(self: *Gen12) void {
103
+ _ = self.next();
104
+ }
92
105
  };
93
106
 
94
107
  test Gen12 {
95
- const expected = [_]u8{ 6, 11, 16, 21, 26, 31, 36, 41, 46, 51, 31, 56, 81 };
96
- var rng = Gen12{ .seed = .{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } };
108
+ const expected = [_]u8{ 6, 11, 16, 21, 26, 31, 36, 41, 46, 31, 56, 81 };
109
+ var rng: Gen12 = .{ .seed = .{ 1, 2, 3, 4, 5, 6, 7, 8, 9 } };
97
110
  for (expected) |e| {
98
111
  try expectEqual(e, rng.next());
99
112
  }
100
113
 
101
- try expectEqual(@as(u8, 16), Gen12.percent(6) + 1);
102
- try expectEqual(@as(u8, 16), Gen12.percent(7) - 1);
103
- try expectEqual(@as(u8, 128), Gen12.percent(50) + 1);
114
+ try expectEqual(16, Gen12.percent(6) + 1);
115
+ try expectEqual(16, Gen12.percent(7) - 1);
116
+ try expectEqual(128, Gen12.percent(50) + 1);
104
117
  }
105
118
 
106
119
  // https://pkmn.cc/pokeemerald/src/random.c
@@ -114,7 +127,7 @@ pub const Gen34 = extern struct {
114
127
 
115
128
  pub fn next(self: *Gen34) u16 {
116
129
  self.advance();
117
- return @intCast(u16, self.seed >> 16);
130
+ return @intCast(self.seed >> 16);
118
131
  }
119
132
 
120
133
  pub fn advance(self: *Gen34) void {
@@ -129,7 +142,7 @@ test Gen34 {
129
142
  .{ 0x80000000, 5, 0x0E425287 }, .{ 0x80000000, 10, 0x6F2CF4B2 },
130
143
  };
131
144
  for (data) |d| {
132
- var rng = Gen34{ .seed = d[0] };
145
+ var rng: Gen34 = .{ .seed = d[0] };
133
146
  for (0..d[1]) |_| _ = rng.next();
134
147
  try expectEqual(d[2], rng.seed);
135
148
  }
@@ -144,7 +157,7 @@ pub const Gen56 = extern struct {
144
157
 
145
158
  pub fn next(self: *Gen56) u32 {
146
159
  self.advance();
147
- return @intCast(u32, self.seed >> 32);
160
+ return @intCast(self.seed >> 32);
148
161
  }
149
162
 
150
163
  pub fn advance(self: *Gen56) void {
@@ -161,7 +174,7 @@ test Gen56 {
161
174
  .{ 0x8000000000000000, 10, 0xE7795501267F125A },
162
175
  };
163
176
  for (data) |d| {
164
- var rng = Gen56{ .seed = d[0] };
177
+ var rng: Gen56 = .{ .seed = d[0] };
165
178
  for (0..d[1]) |_| _ = rng.next();
166
179
  try expectEqual(d[2], rng.seed);
167
180
  }
@@ -188,16 +201,16 @@ pub fn FixedRNG(comptime gen: comptime_int, comptime len: usize) type {
188
201
  for (0..n) |_| _ = self.next();
189
202
  }
190
203
 
191
- pub fn range(self: *Self, comptime T: type, from: T, to: Bound(T)) T {
204
+ pub fn range(self: *Self, comptime T: type, from: T, to: PSRNG.Bound(T)) T {
192
205
  assert(showdown);
193
- return @intCast(T, @as(u64, self.next()) * (to - from) / divisor + from);
206
+ return @intCast(@as(u64, self.next()) * (to - from) / divisor + from);
194
207
  }
195
208
 
196
209
  pub fn chance(
197
210
  self: *Self,
198
211
  comptime T: type,
199
212
  numerator: T,
200
- denominator: Bound(T),
213
+ denominator: PSRNG.Bound(T),
201
214
  ) bool {
202
215
  assert(showdown);
203
216
  assert(denominator > 0);
@@ -213,7 +226,7 @@ pub fn FixedRNG(comptime gen: comptime_int, comptime len: usize) type {
213
226
  test FixedRNG {
214
227
  const Type = if (showdown) u32 else u8;
215
228
  const expected = [_]Type{ 42, 255, 0 };
216
- var rng = FixedRNG(1, expected.len){ .rolls = expected };
229
+ var rng: FixedRNG(1, expected.len) = .{ .rolls = expected };
217
230
  for (expected) |e| {
218
231
  try expectEqual(e, rng.next());
219
232
  }
@@ -228,7 +241,3 @@ fn Output(comptime gen: comptime_int) type {
228
241
  else => unreachable,
229
242
  };
230
243
  }
231
-
232
- fn Bound(comptime T: type) type {
233
- return std.math.IntFittingRange(0, std.math.maxInt(T) + 1);
234
- }
@@ -0,0 +1,12 @@
1
+ const std = @import("std");
2
+
3
+ const expectEqual = std.testing.expectEqual;
4
+
5
+ pub fn PointerType(comptime P: type, comptime C: type) type {
6
+ return if (@typeInfo(P).Pointer.is_const) *const C else *C;
7
+ }
8
+
9
+ test PointerType {
10
+ try expectEqual(*bool, PointerType(*u8, bool));
11
+ try expectEqual(*const f64, PointerType(*const i32, f64));
12
+ }