From 84a7fc294b7ad71eb89e9e332cd5976eb44978f1 Mon Sep 17 00:00:00 2001 From: cscherr Date: Thu, 10 Jul 2025 17:42:00 +0200 Subject: [PATCH] refactor: integrate algorithms-c with rust crate --- crates/algorithms/build.rs | 10 ++++++++- crates/algorithms/src/{ => crc}/crc_32.rs | 4 ++-- crates/algorithms/src/{ => crc}/ffi/crc32.rs | 4 ++-- crates/algorithms/src/crc/ffi/mod.rs | 3 +++ crates/algorithms/src/crc/mod.rs | 22 +++++++++++++++++++ crates/algorithms/src/crc_32_ffi.rs | 0 crates/algorithms/src/ffi/mod.rs | 3 --- crates/algorithms/src/lib.rs | 23 ++------------------ 8 files changed, 40 insertions(+), 29 deletions(-) rename crates/algorithms/src/{ => crc}/crc_32.rs (98%) rename crates/algorithms/src/{ => crc}/ffi/crc32.rs (93%) create mode 100755 crates/algorithms/src/crc/ffi/mod.rs create mode 100755 crates/algorithms/src/crc/mod.rs delete mode 100755 crates/algorithms/src/crc_32_ffi.rs diff --git a/crates/algorithms/build.rs b/crates/algorithms/build.rs index fe83f40..b75f118 100755 --- a/crates/algorithms/build.rs +++ b/crates/algorithms/build.rs @@ -7,6 +7,14 @@ fn main() { if !status.success() { panic!("make returned an error") } - println!("cargo::rerun-if-changed=./algorithms-c/src/"); + let cwd = std::env::current_dir().unwrap().display().to_string(); + let libpath_s = format!("{cwd}/algorithms-c/build/artifacts/release/libalgorithms.a"); + let libpath = std::path::Path::new(&libpath_s); + assert!(libpath.exists()); println!("cargo::rustc-link-lib=algorithms"); + println!("cargo::rerun-if-changed={cwd}/algorithms-c/src/"); + println!( + "cargo::rustc-link-search={}", + libpath.parent().unwrap().display() + ); } diff --git a/crates/algorithms/src/crc_32.rs b/crates/algorithms/src/crc/crc_32.rs similarity index 98% rename from crates/algorithms/src/crc_32.rs rename to crates/algorithms/src/crc/crc_32.rs index 990eb0e..4eee4bb 100755 --- a/crates/algorithms/src/crc_32.rs +++ b/crates/algorithms/src/crc/crc_32.rs @@ -2,7 +2,7 @@ //! //! [Link](https://datatracker.ietf.org/doc/html/rfc1662#appendix-C.3) -use crate::Crc; +use super::Crc; /// Lookup table from RFC1662 C.3 pub const CRC32_TABLE: [u32; 256] = [ @@ -69,7 +69,7 @@ impl Crc for Crc32 { #[cfg(test)] mod tests { - use crate::CHECK_DATA; + use crate::crc::CHECK_DATA; use super::*; diff --git a/crates/algorithms/src/ffi/crc32.rs b/crates/algorithms/src/crc/ffi/crc32.rs similarity index 93% rename from crates/algorithms/src/ffi/crc32.rs rename to crates/algorithms/src/crc/ffi/crc32.rs index 37e6cae..0579371 100755 --- a/crates/algorithms/src/ffi/crc32.rs +++ b/crates/algorithms/src/crc/ffi/crc32.rs @@ -1,6 +1,6 @@ use core::ffi::c_void; -use crate::{Crc, ffi::ref_to_voidptr}; +use crate::{crc::Crc, ffi::ref_to_voidptr}; pub type ChecksumCrc32 = u32; // uint32_t in C @@ -44,7 +44,7 @@ impl Crc for Crc32 { #[cfg(test)] mod tests { - use crate::CHECK_DATA; + use crate::crc::CHECK_DATA; use super::*; diff --git a/crates/algorithms/src/crc/ffi/mod.rs b/crates/algorithms/src/crc/ffi/mod.rs new file mode 100755 index 0000000..75374b6 --- /dev/null +++ b/crates/algorithms/src/crc/ffi/mod.rs @@ -0,0 +1,3 @@ +mod crc32; + +pub use crc32::*; diff --git a/crates/algorithms/src/crc/mod.rs b/crates/algorithms/src/crc/mod.rs new file mode 100755 index 0000000..7f14d5e --- /dev/null +++ b/crates/algorithms/src/crc/mod.rs @@ -0,0 +1,22 @@ +/// CRCs from this module are implemented in C instead of Rust +pub mod ffi; + +mod crc_32; +pub use crc_32::*; + +pub const CHECK_DATA: [u8; 9] = *b"123456789"; + +pub trait Crc: Sized { + type Input: ?Sized; + type Checksum: Eq + Default + Copy; + + fn new() -> Self; + fn process(&mut self, data: &Self::Input); + fn shift_reg(&mut self) -> &mut Self::Checksum; + #[inline] + fn checksum(data: &Self::Input) -> Self::Checksum { + let mut c = Self::new(); + c.process(data); + *c.shift_reg() + } +} diff --git a/crates/algorithms/src/crc_32_ffi.rs b/crates/algorithms/src/crc_32_ffi.rs deleted file mode 100755 index e69de29..0000000 diff --git a/crates/algorithms/src/ffi/mod.rs b/crates/algorithms/src/ffi/mod.rs index 5d15c94..5890b9e 100755 --- a/crates/algorithms/src/ffi/mod.rs +++ b/crates/algorithms/src/ffi/mod.rs @@ -1,8 +1,5 @@ -mod crc32; use core::ffi::c_void; -pub use crc32::*; - #[inline] pub(crate) fn ref_to_voidptr(r: &T) -> *const c_void { r as *const T as *const c_void diff --git a/crates/algorithms/src/lib.rs b/crates/algorithms/src/lib.rs index 5ade189..28b4a53 100755 --- a/crates/algorithms/src/lib.rs +++ b/crates/algorithms/src/lib.rs @@ -9,25 +9,6 @@ #![cfg_attr(not(test), no_std)] -/// CRCs from this module are implemented in C instead of Rust -pub mod ffi; +pub mod crc; -mod crc_32; -pub use crc_32::*; - -pub const CHECK_DATA: [u8; 9] = *b"123456789"; - -pub trait Crc: Sized { - type Input: ?Sized; - type Checksum: Eq + Default + Copy; - - fn new() -> Self; - fn process(&mut self, data: &Self::Input); - fn shift_reg(&mut self) -> &mut Self::Checksum; - #[inline] - fn checksum(data: &Self::Input) -> Self::Checksum { - let mut c = Self::new(); - c.process(data); - *c.shift_reg() - } -} +pub(crate) mod ffi;