std.hash.crc: get rid of usingnamespace
This flips things around such that std/hash/crc.zig is generated by the catalog-based generation tool, and the real code that used to be in that file is moved out to std/hash/crc/impl.zig. The generated tests are moved to std/hash/crc/test.zig. By going this route, we eliminate the need for usingnamespace without changing anything for callers of these interfaces. The Crc32 tests are simply added to the fixed part of the generated output and compactified a bit. This was the second-to-last usage of usingnamespace left in std.
This commit is contained in:
committed by
Andrew Kelley
parent
786876c05e
commit
271d896446
@@ -23,11 +23,15 @@ pub fn main() anyerror!void {
|
||||
var zig_src_dir = try fs.cwd().openDir(zig_src_root, .{});
|
||||
defer zig_src_dir.close();
|
||||
|
||||
const target_sub_path = try fs.path.join(arena, &.{ "lib", "std", "hash", "crc" });
|
||||
var target_dir = try zig_src_dir.makeOpenPath(target_sub_path, .{});
|
||||
defer target_dir.close();
|
||||
const hash_sub_path = try fs.path.join(arena, &.{ "lib", "std", "hash" });
|
||||
var hash_target_dir = try zig_src_dir.makeOpenPath(hash_sub_path, .{});
|
||||
defer hash_target_dir.close();
|
||||
|
||||
var zig_code_file = try target_dir.createFile("catalog.zig", .{});
|
||||
const crc_sub_path = try fs.path.join(arena, &.{ "lib", "std", "hash", "crc" });
|
||||
var crc_target_dir = try zig_src_dir.makeOpenPath(crc_sub_path, .{});
|
||||
defer crc_target_dir.close();
|
||||
|
||||
var zig_code_file = try hash_target_dir.createFile("crc.zig", .{});
|
||||
defer zig_code_file.close();
|
||||
|
||||
var cbw = std.io.bufferedWriter(zig_code_file.writer());
|
||||
@@ -37,15 +41,23 @@ pub fn main() anyerror!void {
|
||||
try code_writer.writeAll(
|
||||
\\//! This file is auto-generated by tools/update_crc_catalog.zig.
|
||||
\\
|
||||
\\const Crc = @import("../crc.zig").Crc;
|
||||
\\const impl = @import("crc/impl.zig");
|
||||
\\
|
||||
\\pub const Crc = impl.Crc;
|
||||
\\pub const Polynomial = impl.Polynomial;
|
||||
\\pub const Crc32WithPoly = impl.Crc32WithPoly;
|
||||
\\pub const Crc32SmallWithPoly = impl.Crc32SmallWithPoly;
|
||||
\\
|
||||
\\// IEEE is by far the most common CRC and so is aliased by default.
|
||||
\\pub const Crc32 = Crc32WithPoly(.IEEE);
|
||||
\\
|
||||
\\test {
|
||||
\\ _ = @import("catalog_test.zig");
|
||||
\\ _ = @import("crc/test.zig");
|
||||
\\}
|
||||
\\
|
||||
);
|
||||
|
||||
var zig_test_file = try target_dir.createFile("catalog_test.zig", .{});
|
||||
var zig_test_file = try crc_target_dir.createFile("test.zig", .{});
|
||||
defer zig_test_file.close();
|
||||
|
||||
var tbw = std.io.bufferedWriter(zig_test_file.writer());
|
||||
@@ -57,7 +69,26 @@ pub fn main() anyerror!void {
|
||||
\\
|
||||
\\const std = @import("std");
|
||||
\\const testing = std.testing;
|
||||
\\const catalog = @import("catalog.zig");
|
||||
\\const verify = @import("../verify.zig");
|
||||
\\const crc = @import("../crc.zig");
|
||||
\\
|
||||
\\test "crc32 ieee" {
|
||||
\\ inline for ([2]type{ crc.Crc32WithPoly(.IEEE), crc.Crc32SmallWithPoly(.IEEE) }) |ieee| {
|
||||
\\ try testing.expect(ieee.hash("") == 0x00000000);
|
||||
\\ try testing.expect(ieee.hash("a") == 0xe8b7be43);
|
||||
\\ try testing.expect(ieee.hash("abc") == 0x352441c2);
|
||||
\\ try verify.iterativeApi(ieee);
|
||||
\\ }
|
||||
\\}
|
||||
\\
|
||||
\\test "crc32 castagnoli" {
|
||||
\\ inline for ([2]type{ crc.Crc32WithPoly(.Castagnoli), crc.Crc32SmallWithPoly(.Castagnoli) }) |casta| {
|
||||
\\ try testing.expect(casta.hash("") == 0x00000000);
|
||||
\\ try testing.expect(casta.hash("a") == 0xc1d04330);
|
||||
\\ try testing.expect(casta.hash("abc") == 0x364b3fb7);
|
||||
\\ try verify.iterativeApi(casta);
|
||||
\\ }
|
||||
\\}
|
||||
\\
|
||||
);
|
||||
|
||||
@@ -146,7 +177,7 @@ pub fn main() anyerror!void {
|
||||
try test_writer.writeAll(try std.fmt.allocPrint(arena,
|
||||
\\
|
||||
\\test "{0s}" {{
|
||||
\\ const {1s} = catalog.{1s};
|
||||
\\ const {1s} = crc.{1s};
|
||||
\\
|
||||
\\ try testing.expectEqual(@as(u{2s}, {3s}), {1s}.hash("123456789"));
|
||||
\\
|
||||
|
||||
Reference in New Issue
Block a user