authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-01-31 22:25:49-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-01-31 22:25:49-07:00
log75bbc74d56f26da812c47436c6f5712d3cfde12d
treec243bba0c998f3f44afe5e67f1120433117d5ead
parentaa326328d0b4a0d4ee8167be9b2ab25fd560b0c9

a small crusade against std.meta.declarations


4 files changed, 87 insertions(+), 23 deletions(-)

lib/std/crypto.zig+83-21
...@@ -164,31 +164,93 @@ const std = @import("std.zig");...@@ -164,31 +164,93 @@ const std = @import("std.zig");
164164
165pub const errors = @import("crypto/errors.zig");165pub const errors = @import("crypto/errors.zig");
166166
167test "crypto" {167test {
168 const please_windows_dont_oom = @import("builtin").os.tag == .windows;168 const please_windows_dont_oom = @import("builtin").os.tag == .windows;
169 if (please_windows_dont_oom) return error.SkipZigTest;169 if (please_windows_dont_oom) return error.SkipZigTest;
170170
171 inline for (std.meta.declarations(@This())) |decl| {171 _ = aead.aegis.Aegis128L;
172 switch (decl.data) {172 _ = aead.aegis.Aegis256;
173 .Type => |t| {173
174 if (@typeInfo(t) != .ErrorSet) {174 _ = aead.aes_gcm.Aes128Gcm;
175 std.testing.refAllDecls(t);175 _ = aead.aes_gcm.Aes256Gcm;
176 }176
177 },177 _ = aead.aes_ocb.Aes128Ocb;
178 .Var => |v| {178 _ = aead.aes_ocb.Aes256Ocb;
179 _ = v;179
180 },180 _ = aead.Gimli;
181 .Fn => |f| {181
182 _ = f;182 _ = aead.chacha_poly.ChaCha20Poly1305;
183 },183 _ = aead.chacha_poly.ChaCha12Poly1305;
184 }184 _ = aead.chacha_poly.ChaCha8Poly1305;
185 }185 _ = aead.chacha_poly.XChaCha20Poly1305;
186 _ = aead.chacha_poly.XChaCha12Poly1305;
187 _ = aead.chacha_poly.XChaCha8Poly1305;
188
189 _ = aead.isap;
190 _ = aead.salsa_poly.XSalsa20Poly1305;
191
192 _ = auth.hmac;
193 _ = auth.siphash;
194
195 _ = core.aes;
196 _ = core.Gimli;
197 _ = core.modes;
198
199 _ = dh.X25519;
200
201 _ = ecc.Curve25519;
202 _ = ecc.Edwards25519;
203 _ = ecc.P256;
204 _ = ecc.Ristretto255;
205
206 _ = hash.blake2;
207 _ = hash.Blake3;
208 _ = hash.Gimli;
209 _ = hash.Md5;
210 _ = hash.Sha1;
211 _ = hash.sha2;
212 _ = hash.sha3;
213
214 _ = kdf.hkdf;
215
216 _ = onetimeauth.Ghash;
217 _ = onetimeauth.Poly1305;
218
219 _ = pwhash.Encoding;
220
221 _ = pwhash.Error;
222 _ = pwhash.HasherError;
223 _ = pwhash.KdfError;
224
225 _ = pwhash.argon2;
226 _ = pwhash.bcrypt;
227 _ = pwhash.scrypt;
228 _ = pwhash.pbkdf2;
229
230 _ = pwhash.phc_format;
231
232 _ = sign.Ed25519;
233
234 _ = stream.chacha.ChaCha20IETF;
235 _ = stream.chacha.ChaCha12IETF;
236 _ = stream.chacha.ChaCha8IETF;
237 _ = stream.chacha.ChaCha20With64BitNonce;
238 _ = stream.chacha.ChaCha12With64BitNonce;
239 _ = stream.chacha.ChaCha8With64BitNonce;
240 _ = stream.chacha.XChaCha20IETF;
241 _ = stream.chacha.XChaCha12IETF;
242 _ = stream.chacha.XChaCha8IETF;
243
244 _ = stream.salsa.Salsa20;
245 _ = stream.salsa.XSalsa20;
246
247 _ = nacl.Box;
248 _ = nacl.SecretBox;
249 _ = nacl.SealedBox;
186250
187 _ = @import("crypto/aegis.zig");251 _ = utils;
188 _ = @import("crypto/aes_gcm.zig");252 _ = random;
189 _ = @import("crypto/aes_ocb.zig");253 _ = errors;
190 _ = @import("crypto/blake2.zig");
191 _ = @import("crypto/chacha20.zig");
192}254}
193255
194test "CSPRNG" {256test "CSPRNG" {
lib/std/meta.zig+2
...@@ -361,6 +361,8 @@ test "std.meta.containerLayout" {...@@ -361,6 +361,8 @@ test "std.meta.containerLayout" {
361 try testing.expect(containerLayout(U3) == .Extern);361 try testing.expect(containerLayout(U3) == .Extern);
362}362}
363363
364/// Instead of this function, prefer to use e.g. `@TypeInfo(foo).Struct.decls`
365/// directly when you know what kind of type it is.
364pub fn declarations(comptime T: type) []const TypeInfo.Declaration {366pub fn declarations(comptime T: type) []const TypeInfo.Declaration {
365 return switch (@typeInfo(T)) {367 return switch (@typeInfo(T)) {
366 .Struct => |info| info.decls,368 .Struct => |info| info.decls,
lib/std/target.zig+1-1
...@@ -1236,7 +1236,7 @@ pub const Target = struct {...@@ -1236,7 +1236,7 @@ pub const Target = struct {
1236 }1236 }
12371237
1238 fn allCpusFromDecls(comptime cpus: type) []const *const Cpu.Model {1238 fn allCpusFromDecls(comptime cpus: type) []const *const Cpu.Model {
1239 const decls = std.meta.declarations(cpus);1239 const decls = @typeInfo(cpus).Struct.decls;
1240 var array: [decls.len]*const Cpu.Model = undefined;1240 var array: [decls.len]*const Cpu.Model = undefined;
1241 for (decls) |decl, i| {1241 for (decls) |decl, i| {
1242 array[i] = &@field(cpus, decl.name);1242 array[i] = &@field(cpus, decl.name);
lib/std/testing.zig+1-1
...@@ -465,7 +465,7 @@ test {...@@ -465,7 +465,7 @@ test {
465/// Given a type, reference all the declarations inside, so that the semantic analyzer sees them.465/// Given a type, reference all the declarations inside, so that the semantic analyzer sees them.
466pub fn refAllDecls(comptime T: type) void {466pub fn refAllDecls(comptime T: type) void {
467 if (!builtin.is_test) return;467 if (!builtin.is_test) return;
468 inline for (std.meta.declarations(T)) |decl| {468 inline for (comptime std.meta.declarations(T)) |decl| {
469 _ = decl;469 _ = decl;
470 }470 }
471}471}