refactor: integrate algorithms-c with rust crate
This commit is contained in:
parent
d2476c2cd6
commit
84a7fc294b
8 changed files with 40 additions and 29 deletions
|
@ -7,6 +7,14 @@ fn main() {
|
||||||
if !status.success() {
|
if !status.success() {
|
||||||
panic!("make returned an error")
|
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::rustc-link-lib=algorithms");
|
||||||
|
println!("cargo::rerun-if-changed={cwd}/algorithms-c/src/");
|
||||||
|
println!(
|
||||||
|
"cargo::rustc-link-search={}",
|
||||||
|
libpath.parent().unwrap().display()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
//!
|
//!
|
||||||
//! [Link](https://datatracker.ietf.org/doc/html/rfc1662#appendix-C.3)
|
//! [Link](https://datatracker.ietf.org/doc/html/rfc1662#appendix-C.3)
|
||||||
|
|
||||||
use crate::Crc;
|
use super::Crc;
|
||||||
|
|
||||||
/// Lookup table from RFC1662 C.3
|
/// Lookup table from RFC1662 C.3
|
||||||
pub const CRC32_TABLE: [u32; 256] = [
|
pub const CRC32_TABLE: [u32; 256] = [
|
||||||
|
@ -69,7 +69,7 @@ impl Crc for Crc32 {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::CHECK_DATA;
|
use crate::crc::CHECK_DATA;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use core::ffi::c_void;
|
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
|
pub type ChecksumCrc32 = u32; // uint32_t in C
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@ impl Crc for Crc32 {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::CHECK_DATA;
|
use crate::crc::CHECK_DATA;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
3
crates/algorithms/src/crc/ffi/mod.rs
Executable file
3
crates/algorithms/src/crc/ffi/mod.rs
Executable file
|
@ -0,0 +1,3 @@
|
||||||
|
mod crc32;
|
||||||
|
|
||||||
|
pub use crc32::*;
|
22
crates/algorithms/src/crc/mod.rs
Executable file
22
crates/algorithms/src/crc/mod.rs
Executable 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()
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,8 +1,5 @@
|
||||||
mod crc32;
|
|
||||||
use core::ffi::c_void;
|
use core::ffi::c_void;
|
||||||
|
|
||||||
pub use crc32::*;
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn ref_to_voidptr<T: ?Sized>(r: &T) -> *const c_void {
|
pub(crate) fn ref_to_voidptr<T: ?Sized>(r: &T) -> *const c_void {
|
||||||
r as *const T as *const c_void
|
r as *const T as *const c_void
|
||||||
|
|
|
@ -9,25 +9,6 @@
|
||||||
|
|
||||||
#![cfg_attr(not(test), no_std)]
|
#![cfg_attr(not(test), no_std)]
|
||||||
|
|
||||||
/// CRCs from this module are implemented in C instead of Rust
|
pub mod crc;
|
||||||
pub mod ffi;
|
|
||||||
|
|
||||||
mod crc_32;
|
pub(crate) mod ffi;
|
||||||
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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue