Skip to content

Commit 847e7f4

Browse files
committed
std: use non-exhaustive enums from crc module
Un-reverts PR ziglang#3118
1 parent b5ac079 commit 847e7f4

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

lib/std/hash/benchmark.zig

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ const hashes = [_]Hash{
4747
.name = "adler32",
4848
},
4949
Hash{
50-
.ty = hash.crc.Crc32WithPoly(hash.crc.Polynomial.IEEE),
50+
.ty = hash.crc.Crc32WithPoly(.IEEE),
5151
.name = "crc32-slicing-by-8",
5252
},
5353
Hash{
54-
.ty = hash.crc.Crc32SmallWithPoly(hash.crc.Polynomial.IEEE),
54+
.ty = hash.crc.Crc32SmallWithPoly(.IEEE),
5555
.name = "crc32-half-byte-lookup",
5656
},
5757
Hash{

lib/std/hash/crc.zig

+14-13
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@ const std = @import("../std.zig");
99
const debug = std.debug;
1010
const testing = std.testing;
1111

12-
pub const Polynomial = struct {
13-
pub const IEEE = 0xedb88320;
14-
pub const Castagnoli = 0x82f63b78;
15-
pub const Koopman = 0xeb31d82e;
12+
pub const Polynomial = enum(u32) {
13+
IEEE = 0xedb88320,
14+
Castagnoli = 0x82f63b78,
15+
Koopman = 0xeb31d82e,
16+
_,
1617
};
1718

1819
// IEEE is by far the most common CRC and so is aliased by default.
19-
pub const Crc32 = Crc32WithPoly(Polynomial.IEEE);
20+
pub const Crc32 = Crc32WithPoly(.IEEE);
2021

2122
// slicing-by-8 crc32 implementation.
22-
pub fn Crc32WithPoly(comptime poly: u32) type {
23+
pub fn Crc32WithPoly(comptime poly: Polynomial) type {
2324
return struct {
2425
const Self = @This();
2526
const lookup_tables = comptime block: {
@@ -31,7 +32,7 @@ pub fn Crc32WithPoly(comptime poly: u32) type {
3132
var j: usize = 0;
3233
while (j < 8) : (j += 1) {
3334
if (crc & 1 == 1) {
34-
crc = (crc >> 1) ^ poly;
35+
crc = (crc >> 1) ^ @enumToInt(poly);
3536
} else {
3637
crc = (crc >> 1);
3738
}
@@ -100,23 +101,23 @@ pub fn Crc32WithPoly(comptime poly: u32) type {
100101
}
101102

102103
test "crc32 ieee" {
103-
const Crc32Ieee = Crc32WithPoly(Polynomial.IEEE);
104+
const Crc32Ieee = Crc32WithPoly(.IEEE);
104105

105106
testing.expect(Crc32Ieee.hash("") == 0x00000000);
106107
testing.expect(Crc32Ieee.hash("a") == 0xe8b7be43);
107108
testing.expect(Crc32Ieee.hash("abc") == 0x352441c2);
108109
}
109110

110111
test "crc32 castagnoli" {
111-
const Crc32Castagnoli = Crc32WithPoly(Polynomial.Castagnoli);
112+
const Crc32Castagnoli = Crc32WithPoly(.Castagnoli);
112113

113114
testing.expect(Crc32Castagnoli.hash("") == 0x00000000);
114115
testing.expect(Crc32Castagnoli.hash("a") == 0xc1d04330);
115116
testing.expect(Crc32Castagnoli.hash("abc") == 0x364b3fb7);
116117
}
117118

118119
// half-byte lookup table implementation.
119-
pub fn Crc32SmallWithPoly(comptime poly: u32) type {
120+
pub fn Crc32SmallWithPoly(comptime poly: Polynomial) type {
120121
return struct {
121122
const Self = @This();
122123
const lookup_table = comptime block: {
@@ -127,7 +128,7 @@ pub fn Crc32SmallWithPoly(comptime poly: u32) type {
127128
var j: usize = 0;
128129
while (j < 8) : (j += 1) {
129130
if (crc & 1 == 1) {
130-
crc = (crc >> 1) ^ poly;
131+
crc = (crc >> 1) ^ @enumToInt(poly);
131132
} else {
132133
crc = (crc >> 1);
133134
}
@@ -164,15 +165,15 @@ pub fn Crc32SmallWithPoly(comptime poly: u32) type {
164165
}
165166

166167
test "small crc32 ieee" {
167-
const Crc32Ieee = Crc32SmallWithPoly(Polynomial.IEEE);
168+
const Crc32Ieee = Crc32SmallWithPoly(.IEEE);
168169

169170
testing.expect(Crc32Ieee.hash("") == 0x00000000);
170171
testing.expect(Crc32Ieee.hash("a") == 0xe8b7be43);
171172
testing.expect(Crc32Ieee.hash("abc") == 0x352441c2);
172173
}
173174

174175
test "small crc32 castagnoli" {
175-
const Crc32Castagnoli = Crc32SmallWithPoly(Polynomial.Castagnoli);
176+
const Crc32Castagnoli = Crc32SmallWithPoly(.Castagnoli);
176177

177178
testing.expect(Crc32Castagnoli.hash("") == 0x00000000);
178179
testing.expect(Crc32Castagnoli.hash("a") == 0xc1d04330);

0 commit comments

Comments
 (0)