move c++ bindings definition and usage to lib file

This commit is contained in:
2023-08-08 13:56:54 +02:00
parent 14001a9297
commit 060d52b1d8
4 changed files with 29 additions and 18 deletions

View File

View File

@@ -1,7 +1,7 @@
fn main() -> miette::Result<()> {
let path = std::path::PathBuf::from("src/cpp"); // include path
let mut b = autocxx_build::Builder::new("src/main.rs", &[&path]).build()?;
let mut b = autocxx_build::Builder::new("src/lib.rs", &[&path]).build()?;
// This assumes all your C++ bindings are in main.rs
b.flag_if_supported("-std=c++14")
.flag("-fopenmp")

24
src/lib.rs Normal file
View File

@@ -0,0 +1,24 @@
use autocxx::prelude::*;
include_cpp! {
#include "data.hpp"
safety!(unsafe)
generate!("Data")
}
use ffi::Data as Data_;
pub fn generate_data(samples: &[i8]) -> UniquePtr<Data_> {
let samples_length = samples.len() as i32;
let bits_per_symbol = 8;
unsafe {
ffi::Data::new2(
samples.as_ptr(),
samples_length.into(),
bits_per_symbol.into(),
false,
)
.within_unique_ptr()
}
}

View File

@@ -1,29 +1,16 @@
use autocxx::prelude::*;
use SP800_90B_rs::generate_data;
use rand::Rng;
include_cpp! {
#include "data.hpp"
safety!(unsafe)
generate!("Data")
}
fn main() {
// println!("Hello, world!");
let mut rng = rand::thread_rng();
let samples: [i8; 1_000_000] = rng.gen();
let samples_length = samples.len() as i32;
let bits_per_symbol = 8;
let data = generate_data(&samples);
let data = unsafe {
ffi::Data::new2(
samples.as_ptr(),
samples_length.into(),
bits_per_symbol.into(),
false,
)
.within_unique_ptr()
};
let a = data.iid_tests();
println!("{a:?}");