Repository: mrhooray/crc-rs (language: rust)

Issue:
expose tables in API
I'm writing a RAM stub in assembly for an embedded system that I'm going to use to quickly CRC regions of flash memory as part of the process of flashing an MCU. I was going to use this crate to generate CRC lookup tables that I would also load via the debug probe at a known address to accelerate this, but the table-access functionality is private &mdash; I just need the whole table that the crate already generates.

I can work around this -- I've just copy-pasted the `crc8` and `crc8_table` functions into my crate, but for my use-case it'd be a nice, useful addition to this crate's API.

Would you accept a PR to expose the generated tables and table generation functions? I'd like to:

- Either make the `table` field of `Crc` public or add a `pub const fn Crc::table(&self) -> I::Table`
- Make the `table` module and its functions public


Discussion / hints:
+1

(I also want this :))

Fix patch:
diff --git a/src/crc128/bytewise.rs b/src/crc128/bytewise.rs
index 27d80fe..514be8c 100644
--- a/src/crc128/bytewise.rs
+++ b/src/crc128/bytewise.rs
@@ -35,6 +35,10 @@ impl Crc<u128, Table<1>> {
         let value = init(self.algorithm, initial);
         Digest::new(self, value)
     }
+
+    pub const fn table(&self) -> &<Table<1> as Implementation>::Data<u128> {
+        &self.data
+    }
 }
 
 impl<'a> Digest<'a, u128, Table<1>> {
diff --git a/src/crc128/slice16.rs b/src/crc128/slice16.rs
index 7d845fd..93bc0ed 100644
--- a/src/crc128/slice16.rs
+++ b/src/crc128/slice16.rs
@@ -1,5 +1,5 @@
 use crate::table::crc128_table_slice_16;
-use crate::{Algorithm, Crc, Digest, Table};
+use crate::*;
 
 use super::{finalize, init, update_slice16};
 
@@ -32,6 +32,10 @@ impl Crc<u128, Table<16>> {
         let value = init(self.algorithm, initial);
         Digest::new(self, value)
     }
+
+    pub const fn table(&self) -> &<Table<16> as Implementation>::Data<u128> {
+        &self.data
+    }
 }
 
 impl<'a> Digest<'a, u128, Table<16>> {
diff --git a/src/crc16/bytewise.rs b/src/crc16/bytewise.rs
index 85fe830..3b4f13a 100644
--- a/src/crc16/bytewise.rs
+++ b/src/crc16/bytewise.rs
@@ -34,6 +34,10 @@ impl Crc<u16, Table<1>> {
         let value = init(self.algorithm, initial);
         Digest::new(self, value)
     }
+
+    pub const fn table(&self) -> &<Table<1> as Implementation>::Data<u16> {
+        &self.data
+    }
 }
 
 impl<'a> Digest<'a, u16, Table<1>> {
diff --git a/src/crc16/slice16.rs b/src/crc16/slice16.rs
index 7b855fa..bcd5993 100644
--- a/src/crc16/slice16.rs
+++ b/src/crc16/slice16.rs
@@ -1,6 +1,6 @@
 use crate::crc16::{finalize, init, update_slice16};
 use crate::table::crc16_table_slice_16;
-use crate::{Algorithm, Crc, Digest, Table};
+use crate::*;
 
 impl Crc<u16, Table<16>> {
     pub const fn new(algorithm: &'static Algorithm<u16>) -> Self {
@@ -31,6 +31,10 @@ impl Crc<u16, Table<16>> {
         let value = init(self.algorithm, initial);
         Digest::new(self, value)
     }
+
+    pub const fn table(&self) -> &<Table<16> as Implementation>::Data<u16> {
+        &self.data
+    }
 }
 
 impl<'a> Digest<'a, u16, Table<16>> {
diff --git a/src/crc32/bytewise.rs b/src/crc32/bytewise.rs
index 586631c..eaf5744 100644
--- a/src/crc32/bytewise.rs
+++ b/src/crc32/bytewise.rs
@@ -35,6 +35,10 @@ impl Crc<u32, Table<1>> {
         let value = init(self.algorithm, initial);
         Digest::new(self, value)
     }
+
+    pub const fn table(&self) -> &<Table<1> as Implementation>::Data<u32> {
+        &self.data
+    }
 }
 
 impl<'a> Digest<'a, u32, Table<1>> {
diff --git a/src/crc32/slice16.rs b/src/crc32/slice16.rs
index cd4a139..7320032 100644
--- a/src/crc32/slice16.rs
+++ b/src/crc32/slice16.rs
@@ -1,5 +1,5 @@
 use crate::table::crc32_table_slice_16;
-use crate::{Algorithm, Crc, Digest, Table};
+use crate::*;
 
 use super::{finalize, init, update_slice16};
 
@@ -32,6 +32,10 @@ impl Crc<u32, Table<16>> {
         let value = init(self.algorithm, initial);
         Digest::new(self, value)
     }
+
+    pub const fn table(&self) -> &<Table<16> as Implementation>::Data<u32> {
+        &self.data
+    }
 }
 
 impl<'a> Digest<'a, u32, Table<16>> {
diff --git a/src/crc64/bytewise.rs b/src/crc64/bytewise.rs
index 50adb56..6ef2941 100644
--- a/src/crc64/bytewise.rs
+++ b/src/crc64/bytewise.rs
@@ -35,6 +35,10 @@ impl Crc<u64, Table<1>> {
         let value = init(self.algorithm, initial);
         Digest::new(self, value)
     }
+
+    pub const fn table(&self) -> &<Table<1> as Implementation>::Data<u64> {
+        &self.data
+    }
 }
 
 impl<'a> Digest<'a, u64, Table<1>> {
diff --git a/src/crc64/slice16.rs b/src/crc64/slice16.rs
index e14ba1d..53a8f57 100644
--- a/src/crc64/slice16.rs
+++ b/src/crc64/slice16.rs
@@ -1,5 +1,5 @@
 use crate::table::crc64_table_slice_16;
-use crate::{Algorithm, Crc, Digest, Table};
+use crate::*;
 
 use super::{finalize, init, update_slice16};
 
@@ -32,6 +32,10 @@ impl Crc<u64, Table<16>> {
         let value = init(self.algorithm, initial);
         Digest::new(self, value)
     }
+
+    pub const fn table(&self) -> &<Table<16> as Implementation>::Data<u64> {
+        &self.data
+    }
 }
 
 impl<'a> Digest<'a, u64, Table<16>> {
diff --git a/src/crc8/bytewise.rs b/src/crc8/bytewise.rs
index ca30f99..64a76a4 100644
--- a/src/crc8/bytewise.rs
+++ b/src/crc8/bytewise.rs
@@ -34,6 +34,10 @@ impl Crc<u8, Table<1>> {
         let value = init(self.algorithm, initial);
         Digest::new(self, value)
     }
+
+    pub const fn table(&self) -> &<Table<1> as Implementation>::Data<u8> {
+        &self.data
+    }
 }
 
 impl<'a> Digest<'a, u8, Table<1>> {
diff --git a/src/crc8/slice16.rs b/src/crc8/slice16.rs
index 1c820e0..ee20f82 100644
--- a/src/crc8/slice16.rs
+++ b/src/crc8/slice16.rs
@@ -1,6 +1,6 @@
 use crate::crc8::{finalize, init, update_slice16};
 use crate::table::crc8_table_slice_16;
-use crate::{Algorithm, Crc, Digest, Table};
+use crate::*;
 
 impl Crc<u8, Table<16>> {
     pub const fn new(algorithm: &'static Algorithm<u8>) -> Self {
@@ -31,6 +31,10 @@ impl Crc<u8, Table<16>> {
         let value = init(self.algorithm, initial);
         Digest::new(self, value)
     }
+
+    pub const fn table(&self) -> &<Table<16> as Implementation>::Data<u8> {
+        &self.data
+    }
 }
 
 impl<'a> Digest<'a, u8, Table<16>> {


Test patch: