Skip to content

Commit cf92d10

Browse files
committed
crc-fast: wasm compatibilty fix
1 parent aec7d02 commit cf92d10

File tree

5 files changed

+85
-8
lines changed

5 files changed

+85
-8
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@
44
.idea
55
.DS_Store
66
.git
7-
.vscode
7+
.vscode
8+
9+
# personal
10+
.claude/
11+
CLAUDE.md

Cargo.toml

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ bench = true
2424
[dependencies]
2525
crc = "3"
2626
digest = { version = "0.10", features = ["alloc"] }
27-
rand = "0.9"
28-
regex = "1.12"
2927

3028
# will be removed once Rust 1.89 is the minimum supported version
3129
rustversion = "1.0"
@@ -36,6 +34,8 @@ indexmap = { version = ">=2.11.0, <2.12.0", optional = true }
3634
[dev-dependencies]
3735
criterion = "0.7"
3836
cbindgen = "0.29"
37+
rand = "0.9"
38+
regex = "1.12"
3939

4040
# lto=true has a big improvement in performance
4141
[profile.release]
@@ -44,11 +44,29 @@ strip = true
4444
codegen-units = 1
4545
opt-level = 3
4646

47+
[[bin]]
48+
name = "checksum"
49+
path = "src/bin/checksum.rs"
50+
required-features = ["cli"]
51+
52+
[[bin]]
53+
name = "arch-check"
54+
path = "src/bin/arch-check.rs"
55+
required-features = ["cli"]
56+
57+
[[bin]]
58+
name = "get-custom-params"
59+
path = "src/bin/get-custom-params.rs"
60+
required-features = ["cli"]
61+
4762
[[bench]]
4863
name = "benchmark"
4964
harness = false
5065

5166
[features]
67+
default = ["std"]
68+
std = []
69+
cli = ["std"]
5270
alloc = []
5371

5472
# the features below are deprecated, aren't in use, and will be removed in the next MAJOR version (v2)
@@ -60,4 +78,13 @@ optimize_crc32_neon_v3s4x2e_v2 = [] # deprecated
6078
optimize_crc32_neon_blended = [] # deprecated
6179
optimize_crc32_avx512_vpclmulqdq_v3x2 = [] # deprecated
6280
optimize_crc32_avx512_v4s3x3 = [] # deprecated
63-
optimize_crc32_sse_v4s3x3 = [] # deprecated
81+
optimize_crc32_sse_v4s3x3 = [] # deprecated
82+
83+
[package.metadata.docs.rs]
84+
features = ["std"]
85+
rustdoc-args = ["--cfg", "docsrs"]
86+
87+
[[test]]
88+
name = "checksum_integration_tests"
89+
path = "tests/checksum_integration_tests.rs"
90+
required-features = ["cli"]

src/bin/checksum.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
//! This is a simple program to calculate a checksum from the command line
44
55
use crc_fast::{checksum, checksum_file, CrcAlgorithm};
6-
use rand::RngCore;
76
use std::env;
87
use std::process::ExitCode;
98
use std::str::FromStr;
@@ -151,6 +150,21 @@ impl BenchmarkResult {
151150
}
152151
}
153152

153+
/// Fills a buffer with pseudo-random data using xorshift64* algorithm.
154+
/// This is a deterministic PRNG that's tiny, fast, and suitable for benchmark data generation.
155+
/// The seed is derived from the buffer size to provide some variability.
156+
#[inline]
157+
fn fill_pseudo_random(buf: &mut [u8], seed: u64) {
158+
let mut x = seed;
159+
for b in buf {
160+
x ^= x >> 12;
161+
x ^= x << 25;
162+
x ^= x >> 27;
163+
let r = x.wrapping_mul(0x2545_F491_4F6C_DD1D);
164+
*b = r as u8;
165+
}
166+
}
167+
154168
fn generate_random_data(size: usize) -> Result<Vec<u8>, String> {
155169
// Check for reasonable size limits to prevent memory issues
156170
if size > 1_073_741_824 {
@@ -160,8 +174,11 @@ fn generate_random_data(size: usize) -> Result<Vec<u8>, String> {
160174

161175
// Use vec! macro to avoid clippy warning about slow initialization
162176
let mut buf = vec![0u8; size];
163-
let mut rng = rand::rng();
164-
rng.fill_bytes(&mut buf);
177+
178+
// Derive seed from size for deterministic but varied data
179+
let seed = 0x9E37_79B9_7F4A_7C15_u64.wrapping_add(size as u64);
180+
fill_pseudo_random(&mut buf, seed);
181+
165182
Ok(buf)
166183
}
167184

src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Copyright 2025 Don MacAskill. Licensed under MIT or Apache-2.0.
22

3+
#![cfg_attr(not(feature = "std"), no_std)]
4+
35
//! `crc-fast`
46
//! ===========
57
//!
@@ -144,7 +146,10 @@ use crate::crc64::consts::{
144146
use crate::structs::Calculator;
145147
use crate::traits::CrcCalculator;
146148
use digest::{DynDigest, InvalidBufferSize};
149+
150+
#[cfg(feature = "std")]
147151
use std::fs::File;
152+
#[cfg(feature = "std")]
148153
use std::io::{Read, Write};
149154

150155
mod algorithm;
@@ -545,6 +550,7 @@ impl Digest {
545550
}
546551
}
547552

553+
#[cfg(feature = "std")]
548554
impl Write for Digest {
549555
#[inline(always)]
550556
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
@@ -643,6 +649,7 @@ pub fn checksum_with_params(params: CrcParams, buf: &[u8]) -> u64 {
643649
///
644650
/// assert_eq!(checksum.unwrap(), 0xcbf43926);
645651
/// ```
652+
#[cfg(feature = "std")]
646653
#[inline(always)]
647654
pub fn checksum_file(
648655
algorithm: CrcAlgorithm,
@@ -685,6 +692,7 @@ pub fn checksum_file(
685692
///
686693
/// assert_eq!(checksum.unwrap(), 0xcbf43926);
687694
/// ```
695+
#[cfg(feature = "std")]
688696
pub fn checksum_file_with_params(
689697
params: CrcParams,
690698
path: &str,
@@ -698,6 +706,7 @@ pub fn checksum_file_with_params(
698706
/// # Errors
699707
///
700708
/// This function will return an error if the file cannot be read.
709+
#[cfg(feature = "std")]
701710
fn checksum_file_with_digest(
702711
mut digest: Digest,
703712
path: &str,

tests/checksum_integration_tests.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
// Copyright 2025 Don MacAskill. Licensed under MIT or Apache-2.0.
22

3+
#![cfg(feature = "cli")]
4+
35
use std::fs;
46
use std::process::Command;
57

68
#[test]
79
fn test_benchmark_flag_parsing() {
810
let output = Command::new("cargo")
9-
.args(&["run", "--bin", "checksum", "--", "-a", "CRC-32/ISCSI", "-b"])
11+
.args(&["run", "--features", "cli", "--bin", "checksum", "--", "-a", "CRC-32/ISCSI", "-b"])
1012
.output()
1113
.expect("Failed to execute command");
1214

@@ -25,6 +27,8 @@ fn test_benchmark_with_size_parameter() {
2527
let output = Command::new("cargo")
2628
.args(&[
2729
"run",
30+
"--features",
31+
"cli",
2832
"--bin",
2933
"checksum",
3034
"--",
@@ -47,6 +51,8 @@ fn test_benchmark_with_duration_parameter() {
4751
let output = Command::new("cargo")
4852
.args(&[
4953
"run",
54+
"--features",
55+
"cli",
5056
"--bin",
5157
"checksum",
5258
"--",
@@ -69,6 +75,8 @@ fn test_benchmark_invalid_size() {
6975
let output = Command::new("cargo")
7076
.args(&[
7177
"run",
78+
"--features",
79+
"cli",
7280
"--bin",
7381
"checksum",
7482
"--",
@@ -91,6 +99,8 @@ fn test_benchmark_invalid_duration() {
9199
let output = Command::new("cargo")
92100
.args(&[
93101
"run",
102+
"--features",
103+
"cli",
94104
"--bin",
95105
"checksum",
96106
"--",
@@ -117,6 +127,8 @@ fn test_benchmark_with_file_input() {
117127
let output = Command::new("cargo")
118128
.args(&[
119129
"run",
130+
"--features",
131+
"cli",
120132
"--bin",
121133
"checksum",
122134
"--",
@@ -144,6 +156,8 @@ fn test_benchmark_with_string_input() {
144156
let output = Command::new("cargo")
145157
.args(&[
146158
"run",
159+
"--features",
160+
"cli",
147161
"--bin",
148162
"checksum",
149163
"--",
@@ -171,6 +185,8 @@ fn test_benchmark_different_algorithms() {
171185
let output = Command::new("cargo")
172186
.args(&[
173187
"run",
188+
"--features",
189+
"cli",
174190
"--bin",
175191
"checksum",
176192
"--",
@@ -198,6 +214,8 @@ fn test_benchmark_size_without_benchmark_flag() {
198214
let output = Command::new("cargo")
199215
.args(&[
200216
"run",
217+
"--features",
218+
"cli",
201219
"--bin",
202220
"checksum",
203221
"--",
@@ -219,6 +237,8 @@ fn test_benchmark_nonexistent_file() {
219237
let output = Command::new("cargo")
220238
.args(&[
221239
"run",
240+
"--features",
241+
"cli",
222242
"--bin",
223243
"checksum",
224244
"--",

0 commit comments

Comments
 (0)