std: use non-exhaustive enums from crc module

Un-reverts PR #3118
This commit is contained in:
daurnimator 2020-01-18 11:58:29 +10:00 committed by Andrew Kelley
parent b72f858194
commit 9e6e1e58bb
2 changed files with 16 additions and 15 deletions

View File

@ -47,11 +47,11 @@ const hashes = [_]Hash{
.name = "adler32",
},
Hash{
.ty = hash.crc.Crc32WithPoly(hash.crc.Polynomial.IEEE),
.ty = hash.crc.Crc32WithPoly(.IEEE),
.name = "crc32-slicing-by-8",
},
Hash{
.ty = hash.crc.Crc32SmallWithPoly(hash.crc.Polynomial.IEEE),
.ty = hash.crc.Crc32SmallWithPoly(.IEEE),
.name = "crc32-half-byte-lookup",
},
Hash{

View File

@ -9,17 +9,18 @@ const std = @import("../std.zig");
const debug = std.debug;
const testing = std.testing;
pub const Polynomial = struct {
pub const IEEE = 0xedb88320;
pub const Castagnoli = 0x82f63b78;
pub const Koopman = 0xeb31d82e;
pub const Polynomial = enum(u32) {
IEEE = 0xedb88320,
Castagnoli = 0x82f63b78,
Koopman = 0xeb31d82e,
_,
};
// IEEE is by far the most common CRC and so is aliased by default.
pub const Crc32 = Crc32WithPoly(Polynomial.IEEE);
pub const Crc32 = Crc32WithPoly(.IEEE);
// slicing-by-8 crc32 implementation.
pub fn Crc32WithPoly(comptime poly: u32) type {
pub fn Crc32WithPoly(comptime poly: Polynomial) type {
return struct {
const Self = @This();
const lookup_tables = comptime block: {
@ -31,7 +32,7 @@ pub fn Crc32WithPoly(comptime poly: u32) type {
var j: usize = 0;
while (j < 8) : (j += 1) {
if (crc & 1 == 1) {
crc = (crc >> 1) ^ poly;
crc = (crc >> 1) ^ @enumToInt(poly);
} else {
crc = (crc >> 1);
}
@ -100,7 +101,7 @@ pub fn Crc32WithPoly(comptime poly: u32) type {
}
test "crc32 ieee" {
const Crc32Ieee = Crc32WithPoly(Polynomial.IEEE);
const Crc32Ieee = Crc32WithPoly(.IEEE);
testing.expect(Crc32Ieee.hash("") == 0x00000000);
testing.expect(Crc32Ieee.hash("a") == 0xe8b7be43);
@ -108,7 +109,7 @@ test "crc32 ieee" {
}
test "crc32 castagnoli" {
const Crc32Castagnoli = Crc32WithPoly(Polynomial.Castagnoli);
const Crc32Castagnoli = Crc32WithPoly(.Castagnoli);
testing.expect(Crc32Castagnoli.hash("") == 0x00000000);
testing.expect(Crc32Castagnoli.hash("a") == 0xc1d04330);
@ -116,7 +117,7 @@ test "crc32 castagnoli" {
}
// half-byte lookup table implementation.
pub fn Crc32SmallWithPoly(comptime poly: u32) type {
pub fn Crc32SmallWithPoly(comptime poly: Polynomial) type {
return struct {
const Self = @This();
const lookup_table = comptime block: {
@ -127,7 +128,7 @@ pub fn Crc32SmallWithPoly(comptime poly: u32) type {
var j: usize = 0;
while (j < 8) : (j += 1) {
if (crc & 1 == 1) {
crc = (crc >> 1) ^ poly;
crc = (crc >> 1) ^ @enumToInt(poly);
} else {
crc = (crc >> 1);
}
@ -164,7 +165,7 @@ pub fn Crc32SmallWithPoly(comptime poly: u32) type {
}
test "small crc32 ieee" {
const Crc32Ieee = Crc32SmallWithPoly(Polynomial.IEEE);
const Crc32Ieee = Crc32SmallWithPoly(.IEEE);
testing.expect(Crc32Ieee.hash("") == 0x00000000);
testing.expect(Crc32Ieee.hash("a") == 0xe8b7be43);
@ -172,7 +173,7 @@ test "small crc32 ieee" {
}
test "small crc32 castagnoli" {
const Crc32Castagnoli = Crc32SmallWithPoly(Polynomial.Castagnoli);
const Crc32Castagnoli = Crc32SmallWithPoly(.Castagnoli);
testing.expect(Crc32Castagnoli.hash("") == 0x00000000);
testing.expect(Crc32Castagnoli.hash("a") == 0xc1d04330);