refactor: integrate algorithms-c with rust crate

This commit is contained in:
cscherr 2025-07-10 17:42:00 +02:00
parent d2476c2cd6
commit 84a7fc294b
Signed by: cscherrNT
GPG key ID: 8E2B45BC51A27EA7
8 changed files with 40 additions and 29 deletions

View file

@ -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()
);
}

View file

@ -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::*;

View file

@ -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::*;

View file

@ -0,0 +1,3 @@
mod crc32;
pub use crc32::*;

View file

@ -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()
}
}

View file

@ -1,8 +1,5 @@
mod crc32;
use core::ffi::c_void;
pub use crc32::*;
#[inline]
pub(crate) fn ref_to_voidptr<T: ?Sized>(r: &T) -> *const c_void {
r as *const T as *const c_void

View file

@ -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;