Skip to content

Commit 1ae49cb

Browse files
author
Rui Hu
authored
Merge pull request #13 from vitiral/no_std
add no_std feature
2 parents 1e727b3 + c014965 commit 1ae49cb

File tree

5 files changed

+95
-18
lines changed

5 files changed

+95
-18
lines changed

Diff for: Cargo.toml

+11-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,14 @@ documentation = "http://mrhooray.github.io/crc-rs/crc/index.html"
1010
license = "MIT"
1111

1212
[dependencies]
13-
lazy_static = "0.2"
13+
14+
[dependencies.build_const]
15+
version = "0.2"
16+
default-features = false
17+
18+
[build-dependencies]
19+
build_const = "0.2"
20+
21+
[features]
22+
std = []
23+
default = ["std"]

Diff for: build.rs

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2+
extern crate build_const;
3+
4+
pub fn make_table_crc32(poly: u32) -> [u32; 256] {
5+
let mut table = [0u32; 256];
6+
for i in 0..256 {
7+
let mut value = i as u32;
8+
for _ in 0..8 {
9+
value = if (value & 1) == 1 {
10+
(value >> 1) ^ poly
11+
} else {
12+
value >> 1
13+
}
14+
}
15+
table[i] = value;
16+
}
17+
table
18+
}
19+
20+
pub fn make_table_crc64(poly: u64) -> [u64; 256] {
21+
let mut table = [0u64; 256];
22+
for i in 0..256 {
23+
let mut value = i as u64;
24+
for _ in 0..8 {
25+
value = if (value & 1) == 1 {
26+
(value >> 1) ^ poly
27+
} else {
28+
value >> 1
29+
}
30+
}
31+
table[i] = value;
32+
}
33+
table
34+
}
35+
36+
#[allow(non_snake_case)]
37+
fn create_constants() {
38+
let mut crc32 = build_const::ConstWriter::for_build("crc32_constants")
39+
.unwrap()
40+
.finish_dependencies();
41+
let CASTAGNOLI: u32 = 0x82f63b78;
42+
crc32.add_value("CASTAGNOLI", "u32", CASTAGNOLI);
43+
crc32.add_array("CASTAGNOLI_TABLE", "u32", &make_table_crc32(CASTAGNOLI));
44+
45+
let IEEE: u32 = 0xedb88320;
46+
crc32.add_value("IEEE", "u32", IEEE);
47+
crc32.add_array("IEEE_TABLE", "u32", &make_table_crc32(IEEE));
48+
49+
let KOOPMAN: u32 = 0xeb31d82e;
50+
crc32.add_value("KOOPMAN", "u32", KOOPMAN);
51+
crc32.add_array("KOOPMAN_TABLE", "u32", &make_table_crc32(KOOPMAN));
52+
53+
crc32.finish();
54+
55+
let mut crc64 = build_const::ConstWriter::for_build("crc64_constants")
56+
.unwrap()
57+
.finish_dependencies();
58+
59+
let ECMA: u64 = 0xc96c5795d7870f42;
60+
crc64.add_value("ECMA", "u64", ECMA);
61+
crc64.add_array("ECMA_TABLE", "u64", &make_table_crc64(ECMA));
62+
63+
let ISO: u64 = 0xd800000000000000;
64+
crc64.add_value("ISO", "u64", ISO);
65+
crc64.add_array("ISO_TABLE", "u64", &make_table_crc64(ISO));
66+
67+
crc64.finish();
68+
}
69+
70+
fn main() {
71+
create_constants();
72+
}
73+

Diff for: src/crc32.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1+
#[cfg(feature = "std")]
12
use std::hash::Hasher;
3+
#[cfg(not(feature = "std"))]
4+
use core::hash::Hasher;
25

3-
pub const CASTAGNOLI: u32 = 0x82f63b78;
4-
pub const IEEE: u32 = 0xedb88320;
5-
pub const KOOPMAN: u32 = 0xeb31d82e;
6-
7-
lazy_static! {
8-
pub static ref IEEE_TABLE: [u32; 256] = make_table(IEEE);
9-
pub static ref CASTAGNOLI_TABLE: [u32; 256] = make_table(CASTAGNOLI);
10-
pub static ref KOOPMAN_TABLE: [u32; 256] = make_table(KOOPMAN);
11-
}
6+
build_const!("crc32_constants");
127

138
pub struct Digest {
149
table: [u32; 256],

Diff for: src/crc64.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1+
#[cfg(feature = "std")]
12
use std::hash::Hasher;
3+
#[cfg(not(feature = "std"))]
4+
use core::hash::Hasher;
25

3-
pub const ECMA: u64 = 0xc96c5795d7870f42;
4-
pub const ISO: u64 = 0xd800000000000000;
5-
6-
lazy_static! {
7-
pub static ref ECMA_TABLE: [u64; 256] = make_table(ECMA);
8-
pub static ref ISO_TABLE: [u64; 256] = make_table(ISO);
9-
}
6+
build_const!("crc64_constants");
107

118
pub struct Digest {
129
table: [u64; 256],

Diff for: src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@
4040
//! assert_eq!(digest.sum64(), 0x995dc9bbdf1939fa);
4141
//! ```
4242
43+
#![cfg_attr(not(feature = "std"), no_std)]
44+
4345
#[macro_use]
44-
extern crate lazy_static;
46+
extern crate build_const;
4547

4648
pub mod crc32;
4749
pub mod crc64;

0 commit comments

Comments
 (0)