@pkmn/engine 0.1.0-dev.d3e796d5 → 0.1.0-dev.d4bd80b1

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 (84) hide show
  1. package/LICENSE +1 -3
  2. package/README.md +51 -23
  3. package/build/pkg/addon/browser.d.ts +3 -0
  4. package/build/pkg/addon/browser.js +47 -0
  5. package/build/pkg/addon/browser.js.map +1 -0
  6. package/build/pkg/addon/node.d.ts +3 -0
  7. package/build/pkg/addon/node.js +110 -0
  8. package/build/pkg/addon/node.js.map +1 -0
  9. package/build/pkg/addon/wasm.d.ts +2 -0
  10. package/build/pkg/addon/wasm.js +53 -0
  11. package/build/pkg/addon/wasm.js.map +1 -0
  12. package/build/pkg/addon.d.ts +25 -3
  13. package/build/pkg/addon.js +39 -71
  14. package/build/pkg/addon.js.map +1 -1
  15. package/build/pkg/data/ids.json +12 -12
  16. package/build/pkg/data/layout.json +17 -4
  17. package/build/pkg/data/protocol.json +84 -13
  18. package/build/pkg/data.d.ts +27 -5
  19. package/build/pkg/data.js.map +1 -1
  20. package/build/pkg/gen1.d.ts +15 -18
  21. package/build/pkg/gen1.js +30 -21
  22. package/build/pkg/gen1.js.map +1 -1
  23. package/build/pkg/index.d.ts +42 -91
  24. package/build/pkg/index.js +23 -70
  25. package/build/pkg/index.js.map +1 -1
  26. package/build/pkg/protocol.d.ts +20 -1
  27. package/build/pkg/protocol.js +123 -102
  28. package/build/pkg/protocol.js.map +1 -1
  29. package/build/tools/debug.js +53 -76
  30. package/build/tools/debug.js.map +1 -1
  31. package/build/tools/display/debug.jsx +152 -0
  32. package/build/tools/display/debug.jsx.map +1 -0
  33. package/build/tools/display/index.js +152 -0
  34. package/build/tools/display/index.js.map +1 -0
  35. package/build/tools/display/ui.jsx +359 -0
  36. package/build/tools/display/ui.jsx.map +1 -0
  37. package/build/tools/display/util.js +33 -0
  38. package/build/tools/display/util.js.map +1 -0
  39. package/build.zig +347 -104
  40. package/package.json +62 -123
  41. package/src/bin/install-pkmn-engine +31 -20
  42. package/src/bin/pkmn-debug +1 -1
  43. package/src/lib/bindings/c.zig +255 -0
  44. package/src/lib/{binding → bindings}/node.zig +9 -22
  45. package/src/lib/bindings/wasm.zig +59 -0
  46. package/src/lib/common/array.zig +77 -0
  47. package/src/lib/common/data.zig +12 -24
  48. package/src/lib/common/debug.zig +39 -8
  49. package/src/lib/common/js.zig +4 -2
  50. package/src/lib/common/optional.zig +21 -10
  51. package/src/lib/common/options.zig +6 -0
  52. package/src/lib/common/protocol.zig +746 -324
  53. package/src/lib/common/rational.zig +132 -36
  54. package/src/lib/common/rng.zig +22 -15
  55. package/src/lib/common/util.zig +50 -1
  56. package/src/lib/gen1/calc.zig +688 -161
  57. package/src/lib/gen1/chance.zig +436 -295
  58. package/src/lib/gen1/data/moves.zig +193 -183
  59. package/src/lib/gen1/data/species.zig +4 -6
  60. package/src/lib/gen1/data/types.zig +8 -9
  61. package/src/lib/gen1/data.zig +55 -70
  62. package/src/lib/gen1/helpers.zig +91 -562
  63. package/src/lib/gen1/mechanics.zig +658 -575
  64. package/src/lib/gen2/calc.zig +153 -0
  65. package/src/lib/gen2/chance.zig +902 -0
  66. package/src/lib/gen2/data/items.zig +13 -21
  67. package/src/lib/gen2/data/moves.zig +650 -571
  68. package/src/lib/gen2/data/species.zig +2 -4
  69. package/src/lib/gen2/data/types.zig +135 -33
  70. package/src/lib/gen2/data.zig +88 -88
  71. package/src/lib/gen2/generated.zig +801 -0
  72. package/src/lib/gen2/helpers.zig +50 -34
  73. package/src/lib/gen2/mechanics.zig +3212 -125
  74. package/src/lib/node.zig +18 -0
  75. package/src/lib/pkmn.zig +37 -15
  76. package/src/lib/wasm.zig +17 -0
  77. package/src/tools/display/dom.ts +95 -0
  78. package/src/tools/display/ui.css +354 -0
  79. package/build/test/display.js +0 -400
  80. package/build/test/display.js.map +0 -1
  81. package/src/lib/binding/wasm.zig +0 -12
  82. package/src/lib/gen1/README.md +0 -892
  83. package/src/lib/gen2/README.md +0 -383
  84. package/src/test/display/pkmn.html.tmpl +0 -368
@@ -6,9 +6,18 @@ const expect = std.testing.expect;
6
6
  const expectEqual = std.testing.expectEqual;
7
7
  const expectError = std.testing.expectError;
8
8
 
9
+ const Int = if (@hasField(std.builtin.Type, "int")) .int else .Int;
10
+ const Float = if (@hasField(std.builtin.Type, "float")) .float else .Float;
11
+ const ComptimeInt =
12
+ if (@hasField(std.builtin.Type, "comptime_int")) .comptime_int else .ComptimeInt;
13
+ const ComptimeFloat =
14
+ if (@hasField(std.builtin.Type, "comptime_float")) .comptime_float else .ComptimeFloat;
15
+
9
16
  /// Specialization of a rational number used by the engine to compute probabilties.
10
17
  /// For performance reasons the rational is only reduced lazily and thus `reduce` must be
11
- /// invoked explicitly before reading.
18
+ /// invoked explicitly before reading. Note that this laziness means that this implementation
19
+ /// sometimes overflows in places where an eager reduced implementation would not - to achieve
20
+ /// the same behavior as an eager implementation simply call `reduce` after each operation.
12
21
  pub fn Rational(comptime T: type) type {
13
22
  return extern struct {
14
23
  const Self = @This();
@@ -18,19 +27,19 @@ pub fn Rational(comptime T: type) type {
18
27
  /// Denominator. Must always be >= 1. Not guaranteed to be reduced in all cases.
19
28
  q: T = 1,
20
29
 
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)
30
+ // With floats we can't rely on an overflow bit to let us know when to reduce, so we
31
+ // instead start reducing when we get sufficiently close to the limit of the mantissa
32
+ // (in our domain we expect updates to involve numbers < 2**10, so we should be safe
33
+ // not reducing before we are 2**10 away from "overflowing" the mantissa)
34
+ const REDUCE = if (@typeInfo(T) == Float)
26
35
  std.math.pow(T, 2, std.math.floatMantissaBits(T) - 10)
27
36
  else
28
37
  0;
29
38
 
30
39
  /// Possible error returned by operations on the Rational.
31
40
  pub const Error = switch (@typeInfo(T)) {
32
- .Int => error{Overflow},
33
- .Float => error{},
41
+ Int => error{Overflow},
42
+ Float => error{},
34
43
  else => unreachable,
35
44
  };
36
45
 
@@ -43,20 +52,19 @@ pub fn Rational(comptime T: type) type {
43
52
  /// Update the rational by multiplying its numerator by p and its denominator by q.
44
53
  /// Both p and q must be >= 1, and if computable at comptime must have no common factors.
45
54
  pub fn update(r: *Self, p: anytype, q: anytype) Error!void {
46
- // FIXME
47
- // std.debug.print("({d}/{d}) * ", .{p, q});
55
+ // std.debug.print("({d}/{d}) * ", .{ p, q }); // DEBUG
48
56
  assert(p >= 1);
49
57
  assert(q >= 1);
50
58
 
51
59
  // If our parameters are not fully reduced they may prematurely
52
60
  // cause overflow/loss of precision after the multiplication below
53
61
  assert(switch (@typeInfo(@TypeOf(p, q))) {
54
- .ComptimeInt, .ComptimeFloat => comptime gcd(p, q),
62
+ ComptimeInt, ComptimeFloat => comptime gcd(p, q),
55
63
  else => 1,
56
64
  } == 1);
57
65
 
58
66
  switch (@typeInfo(T)) {
59
- .Int => {
67
+ Int => {
60
68
  // Greedily attempt to multiply and if it fails, reduce and try again
61
69
  r.multiplication(p, q) catch |err| switch (err) {
62
70
  error.Overflow => {
@@ -66,16 +74,16 @@ pub fn Rational(comptime T: type) type {
66
74
  else => unreachable,
67
75
  };
68
76
  },
69
- .Float => {
77
+ Float => {
70
78
  // Reduce in situations where we're likely to start losing precision
71
79
  if (r.q > REDUCE or r.p > REDUCE) r.reduce();
72
80
 
73
81
  r.p *= switch (@typeInfo(@TypeOf(p))) {
74
- .Float, .ComptimeFloat => p,
82
+ Float, ComptimeFloat => p,
75
83
  else => @floatFromInt(p),
76
84
  };
77
85
  r.q *= switch (@typeInfo(@TypeOf(q))) {
78
- .Float, .ComptimeFloat => q,
86
+ Float, ComptimeFloat => q,
79
87
  else => @floatFromInt(q),
80
88
  };
81
89
 
@@ -90,7 +98,7 @@ pub fn Rational(comptime T: type) type {
90
98
  /// Add two rationals using the identity (a/b) + (c/d) = (ad+bc)/(bd).
91
99
  pub fn add(r: *Self, s: *Self) Error!void {
92
100
  switch (@typeInfo(T)) {
93
- .Int => {
101
+ Int => {
94
102
  if (r.q == s.q) {
95
103
  r.p = std.math.add(T, r.p, s.p) catch |err| switch (err) {
96
104
  error.Overflow => val: {
@@ -111,7 +119,7 @@ pub fn Rational(comptime T: type) type {
111
119
  };
112
120
  }
113
121
  },
114
- .Float => {
122
+ Float => {
115
123
  if (r.q == s.q) {
116
124
  if (r.p > REDUCE) r.reduce();
117
125
  if (s.p > REDUCE) s.reduce();
@@ -136,7 +144,7 @@ pub fn Rational(comptime T: type) type {
136
144
  /// Multiplies two rationals.
137
145
  pub fn mul(r: *Self, s: *Self) Error!void {
138
146
  switch (@typeInfo(T)) {
139
- .Int => {
147
+ Int => {
140
148
  r.multiplication(s.p, s.q) catch |err| switch (err) {
141
149
  error.Overflow => {
142
150
  r.reduce();
@@ -146,7 +154,7 @@ pub fn Rational(comptime T: type) type {
146
154
  else => unreachable,
147
155
  };
148
156
  },
149
- .Float => {
157
+ Float => {
150
158
  if (r.q > REDUCE or r.p > REDUCE) r.reduce();
151
159
  if (s.q > REDUCE or s.p > REDUCE) s.reduce();
152
160
 
@@ -185,16 +193,16 @@ pub fn Rational(comptime T: type) type {
185
193
  try writer.print("{d}/{d}", .{ self.p, self.q });
186
194
  }
187
195
 
188
- inline fn multiplication(r: *Self, p: anytype, q: anytype) !void {
196
+ fn multiplication(r: *Self, p: anytype, q: anytype) !void {
189
197
  r.p = try std.math.mul(T, r.p, p);
190
198
  r.q = try std.math.mul(T, r.q, q);
191
199
  }
192
200
 
193
- inline fn addition(r: *Self, p: anytype, q: anytype) !void {
201
+ fn addition(r: *Self, p: anytype, q: anytype) !void {
194
202
  // (a/b) + (c/d) = (ad+bc)/(bd)
195
- var d = try std.math.mul(T, r.q, q);
196
- var n1 = try std.math.mul(T, r.p, q);
197
- var n2 = try std.math.mul(T, r.q, p);
203
+ const d = try std.math.mul(T, r.q, q);
204
+ const n1 = try std.math.mul(T, r.p, q);
205
+ const n2 = try std.math.mul(T, r.q, p);
198
206
 
199
207
  r.p = try std.math.add(T, n1, n2);
200
208
  r.q = d;
@@ -202,23 +210,74 @@ pub fn Rational(comptime T: type) type {
202
210
  };
203
211
  }
204
212
 
205
- // https://en.wikipedia.org/wiki/Euclidean_algorithm
206
- inline fn gcd(p: anytype, q: anytype) @TypeOf(p, q) {
213
+ fn gcd(p: anytype, q: anytype) @TypeOf(p, q) {
207
214
  assert(p >= 1);
208
215
  assert(q >= 1);
209
216
 
210
- var a = p;
211
- var b = q;
212
- var c: @TypeOf(p, q) = undefined;
217
+ // convert comptime_int to a sized integer within this function so that @ctz will work
218
+ const T = switch (@TypeOf(p, q)) {
219
+ comptime_int => std.math.IntFittingRange(@min(p, q), @max(p, q)),
220
+ else => |U| U,
221
+ };
222
+
223
+ switch (@typeInfo(T)) {
224
+ Int => {
225
+ // std.math.gcd but without some checks because we have a stricter range
226
+ var x: T = @intCast(p);
227
+ var y: T = @intCast(q);
228
+
229
+ const xz = @ctz(x);
230
+ const yz = @ctz(y);
231
+ const shift = @min(xz, yz);
232
+ x >>= @intCast(xz);
233
+ y >>= @intCast(yz);
234
+
235
+ var diff = y -% x;
236
+ while (diff != 0) : (diff = y -% x) {
237
+ const zeros = @ctz(diff);
238
+ if (x > y) diff = -%diff;
239
+ y = @min(x, y);
240
+ x = diff >> @intCast(zeros);
241
+ }
242
+
243
+ const result = y << @intCast(shift);
244
+ assert(result > 0);
245
+ return result;
246
+ },
247
+ else => {
248
+ var a = p;
249
+ var b = q;
250
+ var c: T = undefined;
251
+
252
+ while (b != 0) {
253
+ c = b;
254
+ b = @mod(a, b);
255
+ a = c;
256
+ }
257
+
258
+ assert(a > 0);
259
+ return a;
260
+ },
261
+ }
262
+ }
263
+
264
+ test gcd {
265
+ const seed = if (@hasDecl(std.testing, "random_seed")) std.testing.random_seed else 0x12345678;
266
+ var prng = (if (@hasDecl(std, "Random")) std.Random else std.rand).DefaultPrng.init(seed);
267
+ var random = prng.random();
268
+
269
+ for (0..1000) |_| {
270
+ const a = random.int(u32);
271
+ const b = random.int(u32);
272
+ if (a == 0 or b == 0) continue;
213
273
 
214
- while (b != 0) {
215
- c = b;
216
- b = @mod(a, b);
217
- a = c;
274
+ try expectEqual(
275
+ gcd(a, b),
276
+ @as(u32, @intFromFloat(gcd(@as(f64, @floatFromInt(a)), @as(f64, @floatFromInt(b))))),
277
+ );
218
278
  }
219
279
 
220
- assert(a > 0);
221
- return a;
280
+ try expectEqual(gcd(300_000, @as(u32, 2_300_000)), 100_000); // NB: @ctz requires an @intCast
222
281
  }
223
282
 
224
283
  fn doTurn(r: anytype) !void {
@@ -234,6 +293,8 @@ test Rational {
234
293
  var r: Rational(t) = .{};
235
294
 
236
295
  var c: t = 128;
296
+ _ = &c;
297
+
237
298
  try r.update(c, 256);
238
299
  try doTurn(&r);
239
300
 
@@ -275,7 +336,7 @@ pub const BigRational = struct {
275
336
  /// Create a new BigRational wrapper. A small amount of memory will be allocated on
276
337
  /// initialization. Not default initialized to 1 - you must explicitly `reset` first.
277
338
  pub fn init(alloc: std.mem.Allocator) !BigRational {
278
- return BigRational{ .val = try std.math.big.Rational.init(alloc) };
339
+ return .{ .val = try std.math.big.Rational.init(alloc) };
279
340
  }
280
341
 
281
342
  /// Frees all memory associated with a rational.
@@ -320,6 +381,8 @@ test BigRational {
320
381
  try r.reset();
321
382
 
322
383
  var c: u8 = 128;
384
+ _ = &c;
385
+
323
386
  try r.update(c, 256);
324
387
  try doTurn(&r);
325
388
 
@@ -351,3 +414,36 @@ test BigRational {
351
414
  try s.setRatio(71, 78);
352
415
  try expect((try s.order(r.val)) == .eq);
353
416
  }
417
+
418
+ test "minimum" {
419
+ // TODO: ziglang/zig#21069
420
+ if (builtin.mode == .ReleaseFast or builtin.mode == .ReleaseSmall) {
421
+ return error.SkipZigTest;
422
+ }
423
+ var r = try BigRational.init(std.testing.allocator);
424
+ defer r.deinit();
425
+ var s = try std.math.big.Rational.init(std.testing.allocator);
426
+ defer s.deinit();
427
+
428
+ try r.reset();
429
+
430
+ try r.update(1, 2); // Speed Tie
431
+ for (0..2) |_| {
432
+ try r.update(1, 2); // Confusion
433
+ try r.update(1, 2); // Infatuation
434
+ try r.update(3, 4); // Paralysis
435
+ // Population Bomb
436
+ for (0..10) |_| {
437
+ try r.update(9, 10); // Accuracy
438
+ try r.update(1, 15); // Damage roll
439
+ try r.update(1, 24); // Critical Hit
440
+ try r.update(9, 10); // King's Rock non-proc
441
+ }
442
+ }
443
+
444
+ const ZEROS = std.math.pow(u310, 10, 31);
445
+ const n = 36472996377170786403;
446
+ const d: u310 = 146267071761884981524886149560653378220687632500762654519853056 * ZEROS;
447
+ try s.setRatio(n, d);
448
+ try expect((try s.order(r.val)) == .eq);
449
+ }
@@ -1,10 +1,8 @@
1
+ const options = @import("options.zig");
1
2
  const std = @import("std");
2
3
 
3
- const options = @import("./options.zig");
4
-
5
4
  const assert = std.debug.assert;
6
5
  const expectEqual = std.testing.expectEqual;
7
-
8
6
  const showdown = options.showdown;
9
7
 
10
8
  pub fn PRNG(comptime gen: comptime_int) type {
@@ -25,6 +23,11 @@ pub fn PRNG(comptime gen: comptime_int) type {
25
23
  pub fn next(self: *Self) Output(gen) {
26
24
  return self.src.next();
27
25
  }
26
+
27
+ pub fn advance(self: *Self, n: usize) void {
28
+ if (!options.advance) return;
29
+ for (0..n) |_| self.src.advance();
30
+ }
28
31
  };
29
32
  }
30
33
 
@@ -61,10 +64,14 @@ pub const PSRNG = extern struct {
61
64
  (@as(u64, self.range(u16, 0, 0x10000)) << 16) |
62
65
  (@as(u64, self.range(u16, 0, 0x10000)));
63
66
  }
67
+
68
+ pub fn Bound(comptime T: type) type {
69
+ return std.math.IntFittingRange(0, std.math.maxInt(T) + 1);
70
+ }
64
71
  };
65
72
 
66
73
  test PSRNG {
67
- var psrng = PSRNG{ .src = .{ .seed = 0x0001000200030004 } };
74
+ var psrng: PSRNG = .{ .src = .{ .seed = 0x0001000200030004 } };
68
75
  try expectEqual(@as(u8, 121), psrng.range(u8, 0, 256));
69
76
  try expectEqual(false, psrng.chance(u8, 128, 256)); // 226 < 128
70
77
  }
@@ -79,7 +86,7 @@ pub const Gen12 = extern struct {
79
86
  assert(@sizeOf(Gen12) == 10);
80
87
  }
81
88
 
82
- pub fn percent(p: u8) u8 {
89
+ pub fn percent(comptime p: comptime_int) u8 {
83
90
  return @intCast((@as(u16, p) * 0xFF) / 100);
84
91
  }
85
92
 
@@ -89,11 +96,15 @@ pub const Gen12 = extern struct {
89
96
  self.index = (self.index + 1) % 9;
90
97
  return val;
91
98
  }
99
+
100
+ pub fn advance(self: *Gen12) void {
101
+ _ = self.next();
102
+ }
92
103
  };
93
104
 
94
105
  test Gen12 {
95
106
  const expected = [_]u8{ 6, 11, 16, 21, 26, 31, 36, 41, 46, 31, 56, 81 };
96
- var rng = Gen12{ .seed = .{ 1, 2, 3, 4, 5, 6, 7, 8, 9 } };
107
+ var rng: Gen12 = .{ .seed = .{ 1, 2, 3, 4, 5, 6, 7, 8, 9 } };
97
108
  for (expected) |e| {
98
109
  try expectEqual(e, rng.next());
99
110
  }
@@ -129,7 +140,7 @@ test Gen34 {
129
140
  .{ 0x80000000, 5, 0x0E425287 }, .{ 0x80000000, 10, 0x6F2CF4B2 },
130
141
  };
131
142
  for (data) |d| {
132
- var rng = Gen34{ .seed = d[0] };
143
+ var rng: Gen34 = .{ .seed = d[0] };
133
144
  for (0..d[1]) |_| _ = rng.next();
134
145
  try expectEqual(d[2], rng.seed);
135
146
  }
@@ -161,7 +172,7 @@ test Gen56 {
161
172
  .{ 0x8000000000000000, 10, 0xE7795501267F125A },
162
173
  };
163
174
  for (data) |d| {
164
- var rng = Gen56{ .seed = d[0] };
175
+ var rng: Gen56 = .{ .seed = d[0] };
165
176
  for (0..d[1]) |_| _ = rng.next();
166
177
  try expectEqual(d[2], rng.seed);
167
178
  }
@@ -188,7 +199,7 @@ pub fn FixedRNG(comptime gen: comptime_int, comptime len: usize) type {
188
199
  for (0..n) |_| _ = self.next();
189
200
  }
190
201
 
191
- pub fn range(self: *Self, comptime T: type, from: T, to: Bound(T)) T {
202
+ pub fn range(self: *Self, comptime T: type, from: T, to: PSRNG.Bound(T)) T {
192
203
  assert(showdown);
193
204
  return @intCast(@as(u64, self.next()) * (to - from) / divisor + from);
194
205
  }
@@ -197,7 +208,7 @@ pub fn FixedRNG(comptime gen: comptime_int, comptime len: usize) type {
197
208
  self: *Self,
198
209
  comptime T: type,
199
210
  numerator: T,
200
- denominator: Bound(T),
211
+ denominator: PSRNG.Bound(T),
201
212
  ) bool {
202
213
  assert(showdown);
203
214
  assert(denominator > 0);
@@ -213,7 +224,7 @@ pub fn FixedRNG(comptime gen: comptime_int, comptime len: usize) type {
213
224
  test FixedRNG {
214
225
  const Type = if (showdown) u32 else u8;
215
226
  const expected = [_]Type{ 42, 255, 0 };
216
- var rng = FixedRNG(1, expected.len){ .rolls = expected };
227
+ var rng: FixedRNG(1, expected.len) = .{ .rolls = expected };
217
228
  for (expected) |e| {
218
229
  try expectEqual(e, rng.next());
219
230
  }
@@ -228,7 +239,3 @@ fn Output(comptime gen: comptime_int) type {
228
239
  else => unreachable,
229
240
  };
230
241
  }
231
-
232
- fn Bound(comptime T: type) type {
233
- return std.math.IntFittingRange(0, std.math.maxInt(T) + 1);
234
- }
@@ -1,12 +1,61 @@
1
1
  const std = @import("std");
2
2
 
3
+ const expect = std.testing.expect;
3
4
  const expectEqual = std.testing.expectEqual;
4
5
 
6
+ const Pointer = if (@hasField(std.builtin.Type, "pointer")) .pointer else .Pointer;
7
+
5
8
  pub fn PointerType(comptime P: type, comptime C: type) type {
6
- return if (@typeInfo(P).Pointer.is_const) *const C else *C;
9
+ return if (@field(@typeInfo(P), @tagName(Pointer)).is_const) *const C else *C;
7
10
  }
8
11
 
9
12
  test PointerType {
10
13
  try expectEqual(*bool, PointerType(*u8, bool));
11
14
  try expectEqual(*const f64, PointerType(*const i32, f64));
12
15
  }
16
+
17
+ pub fn isPointerTo(p: anytype, comptime P: type) bool {
18
+ const info = @typeInfo(@TypeOf(p));
19
+ return switch (info) {
20
+ Pointer => @field(info, @tagName(Pointer)).child == P,
21
+ else => false,
22
+ };
23
+ }
24
+
25
+ test isPointerTo {
26
+ const S = struct {};
27
+ const s: S = .{};
28
+ try expect(!isPointerTo(s, S));
29
+ try expect(isPointerTo(&s, S));
30
+ }
31
+
32
+ // NOTE: std.mem.bytesAsValue backported from ziglang/zig#18061
33
+ pub fn bytesAsValue(comptime T: type, bytes: anytype) BytesAsValueReturnType(T, @TypeOf(bytes)) {
34
+ return @as(BytesAsValueReturnType(T, @TypeOf(bytes)), @ptrCast(bytes));
35
+ }
36
+
37
+ fn BytesAsValueReturnType(comptime T: type, comptime B: type) type {
38
+ return CopyPtrAttrs(B, .One, T);
39
+ }
40
+
41
+ fn CopyPtrAttrs(
42
+ comptime source: type,
43
+ comptime size: std.builtin.Type.Pointer.Size,
44
+ comptime child: type,
45
+ ) type {
46
+ const info = @field(@typeInfo(source), @tagName(Pointer));
47
+ const args: std.builtin.Type.Pointer = .{
48
+ .size = size,
49
+ .is_const = info.is_const,
50
+ .is_volatile = info.is_volatile,
51
+ .is_allowzero = info.is_allowzero,
52
+ .alignment = info.alignment,
53
+ .address_space = info.address_space,
54
+ .child = child,
55
+ .sentinel = null,
56
+ };
57
+ return @Type(if (@hasField(std.builtin.Type, "pointer"))
58
+ .{ .pointer = args }
59
+ else
60
+ .{ .Pointer = args });
61
+ }