move c-bindings to rs-unsafe

This commit is contained in:
Christoph J. Scherr 2023-09-19 17:12:51 +02:00
parent 15cafc6dae
commit 321df1cdf2
8 changed files with 4 additions and 112 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "rs-unsafe"]
path = rs-unsafe
url = https://git.cscherr.de/PlexSheep/rs-unsafe.git

View File

@ -1,14 +0,0 @@
[package]
name = "c-bindings"
version = "0.1.0"
edition = "2021"
links = "test" # this is the important part! cargo will search for some library called `test` (i.e. libtest.a)
build = "src/build.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
cty = "0.2.2"
[build-dependencies]
cc = "1.0.83"

View File

@ -1,15 +0,0 @@
cmake_minimum_required(VERSION 3.9)
project(test VERSION 1.0.1 DESCRIPTION "test lib")
include(GNUInstallDirs)
add_library(test STATIC test.c)
set_target_properties(test PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION 1
PUBLIC_HEADER test.h)
# configure_file(test.pc.in test.pc @ONLY)
target_include_directories(test PRIVATE .)
install(TARGETS test
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(FILES ${CMAKE_BINARY_DIR}/test.pc
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig)

View File

@ -1,24 +0,0 @@
#include "test.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int ret19() {
return 19;
}
char* structInfo(MyStruct *st) {
char* buf = malloc(1024*sizeof(char));
sprintf(buf,
"Infos about the struct:\n"
"\tfoo:\t%d\n"
"\tbar:\t%c\n"
"\n"
"greetings from C"
,
st->foo,
st->bar
);
return buf;
}

View File

@ -1,6 +0,0 @@
int ret19();
typedef struct MyStruct {
int foo;
char bar;
} MyStruct;
char* stuctInfo(MyStruct st);

View File

@ -1,11 +0,0 @@
/// we could simply do this to compile the test lib to a static lib,
/// but that solution might not scale as good as calling a professional
/// build system
#[allow(unused)]
fn main() {
// Tell Cargo that if the given file changes, to rerun this build script.
println!("cargo:rerun-if-changed=src/hello.c");
cc::Build::new()
.file("lib/test.c")
.compile("test");
}

View File

@ -1,42 +0,0 @@
use cty;
// we first need to declare bindings for our C code.
// This can be automated too, but I did it myself here.
#[derive(Debug)]
#[repr(C)]
pub struct MyStruct {
pub foo: cty::c_int,
pub bar: cty::c_char,
}
// The functions too of course
extern "C" {
pub fn ret19() -> isize;
pub fn structInfo(st: &MyStruct) -> *const cty::c_char;
}
///////////////////////////////////////////////////////////////////////
fn main() {
// calling random C functions is generally treated as unsafe.
// Best practice is programming wrapper functions that give it
// confirmed safe inputs
let qux = unsafe { ret19() };
println!("`ret19()` returned {qux}");
let st = MyStruct {
foo: 17, bar: 0x41
};
// converting a c "string" to a real rust String is
// a bit complicated
let info: &str = unsafe {
// convert the returned value to a rust internal CStr.
std::ffi::CStr::from_ptr(
// the function returns a pointer to a array of chars on the heap
structInfo(&st)
)
// now to a string slice (result)
.to_str()
.unwrap()
};
println!("{info}");
}

1
rs-unsafe Submodule

@ -0,0 +1 @@
Subproject commit fdd538a64fd9fceffc87824d45c4f631090955f3