Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add no_std feature #13

Merged
merged 3 commits into from
Jul 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,14 @@ documentation = "http://mrhooray.github.io/crc-rs/crc/index.html"
license = "MIT"

[dependencies]
lazy_static = "0.2"

[dependencies.build_const]
version = "0.2"
default-features = false

[build-dependencies]
build_const = "0.2"

[features]
std = []
default = ["std"]
73 changes: 73 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@

extern crate build_const;

pub fn make_table_crc32(poly: u32) -> [u32; 256] {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it make sense to move these two make table fns to a util file/module so that we don't have to repeat them in crc32.rs and crc64.rs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could move them into an internal "crate" which both build.rs and the individual modules depend on. I can do that if you would like.

I also didn't like copy/pasting them.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moving make_table out of crc32/crc64 is a breaking change since they're public
i'll see weather we could reference them in build.rs after merging.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking we could have an internal crate which build.rs used and some of the functions were re-exported in crc32 and crc64 -- making it non-breaking.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point. let me try that

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#16 was created to address this

let mut table = [0u32; 256];
for i in 0..256 {
let mut value = i as u32;
for _ in 0..8 {
value = if (value & 1) == 1 {
(value >> 1) ^ poly
} else {
value >> 1
}
}
table[i] = value;
}
table
}

pub fn make_table_crc64(poly: u64) -> [u64; 256] {
let mut table = [0u64; 256];
for i in 0..256 {
let mut value = i as u64;
for _ in 0..8 {
value = if (value & 1) == 1 {
(value >> 1) ^ poly
} else {
value >> 1
}
}
table[i] = value;
}
table
}

#[allow(non_snake_case)]
fn create_constants() {
let mut crc32 = build_const::ConstWriter::for_build("crc32_constants")
.unwrap()
.finish_dependencies();
let CASTAGNOLI: u32 = 0x82f63b78;
crc32.add_value("CASTAGNOLI", "u32", CASTAGNOLI);
crc32.add_array("CASTAGNOLI_TABLE", "u32", &make_table_crc32(CASTAGNOLI));

let IEEE: u32 = 0xedb88320;
crc32.add_value("IEEE", "u32", IEEE);
crc32.add_array("IEEE_TABLE", "u32", &make_table_crc32(IEEE));

let KOOPMAN: u32 = 0xeb31d82e;
crc32.add_value("KOOPMAN", "u32", KOOPMAN);
crc32.add_array("KOOPMAN_TABLE", "u32", &make_table_crc32(KOOPMAN));

crc32.finish();

let mut crc64 = build_const::ConstWriter::for_build("crc64_constants")
.unwrap()
.finish_dependencies();

let ECMA: u64 = 0xc96c5795d7870f42;
crc64.add_value("ECMA", "u64", ECMA);
crc64.add_array("ECMA_TABLE", "u64", &make_table_crc64(ECMA));

let ISO: u64 = 0xd800000000000000;
crc64.add_value("ISO", "u64", ISO);
crc64.add_array("ISO_TABLE", "u64", &make_table_crc64(ISO));

crc64.finish();
}

fn main() {
create_constants();
}

13 changes: 4 additions & 9 deletions src/crc32.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
#[cfg(feature = "std")]
use std::hash::Hasher;
#[cfg(not(feature = "std"))]
use core::hash::Hasher;

pub const CASTAGNOLI: u32 = 0x82f63b78;
pub const IEEE: u32 = 0xedb88320;
pub const KOOPMAN: u32 = 0xeb31d82e;

lazy_static! {
pub static ref IEEE_TABLE: [u32; 256] = make_table(IEEE);
pub static ref CASTAGNOLI_TABLE: [u32; 256] = make_table(CASTAGNOLI);
pub static ref KOOPMAN_TABLE: [u32; 256] = make_table(KOOPMAN);
}
build_const!("crc32_constants");

pub struct Digest {
table: [u32; 256],
Expand Down
11 changes: 4 additions & 7 deletions src/crc64.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
#[cfg(feature = "std")]
use std::hash::Hasher;
#[cfg(not(feature = "std"))]
use core::hash::Hasher;

pub const ECMA: u64 = 0xc96c5795d7870f42;
pub const ISO: u64 = 0xd800000000000000;

lazy_static! {
pub static ref ECMA_TABLE: [u64; 256] = make_table(ECMA);
pub static ref ISO_TABLE: [u64; 256] = make_table(ISO);
}
build_const!("crc64_constants");

pub struct Digest {
table: [u64; 256],
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@
//! assert_eq!(digest.sum64(), 0x995dc9bbdf1939fa);
//! ```

#![cfg_attr(not(feature = "std"), no_std)]

#[macro_use]
extern crate lazy_static;
extern crate build_const;

pub mod crc32;
pub mod crc64;
Expand Down