diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..8a2467a --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "rs-unsafe"] + path = rs-unsafe + url = https://git.cscherr.de/PlexSheep/rs-unsafe.git diff --git a/members/c-bindings/Cargo.toml b/members/c-bindings/Cargo.toml deleted file mode 100644 index 23a9a38..0000000 --- a/members/c-bindings/Cargo.toml +++ /dev/null @@ -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" diff --git a/members/c-bindings/lib/CMakeLists.txt b/members/c-bindings/lib/CMakeLists.txt deleted file mode 100644 index 66e5a57..0000000 --- a/members/c-bindings/lib/CMakeLists.txt +++ /dev/null @@ -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) diff --git a/members/c-bindings/lib/test.c b/members/c-bindings/lib/test.c deleted file mode 100644 index 494180a..0000000 --- a/members/c-bindings/lib/test.c +++ /dev/null @@ -1,24 +0,0 @@ -#include "test.h" -#include -#include -#include - -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; -} diff --git a/members/c-bindings/lib/test.h b/members/c-bindings/lib/test.h deleted file mode 100644 index b669d58..0000000 --- a/members/c-bindings/lib/test.h +++ /dev/null @@ -1,6 +0,0 @@ -int ret19(); -typedef struct MyStruct { - int foo; - char bar; -} MyStruct; -char* stuctInfo(MyStruct st); diff --git a/members/c-bindings/src/build.rs b/members/c-bindings/src/build.rs deleted file mode 100644 index 04d6c3a..0000000 --- a/members/c-bindings/src/build.rs +++ /dev/null @@ -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"); -} diff --git a/members/c-bindings/src/main.rs b/members/c-bindings/src/main.rs deleted file mode 100644 index 50c9968..0000000 --- a/members/c-bindings/src/main.rs +++ /dev/null @@ -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}"); -} diff --git a/rs-unsafe b/rs-unsafe new file mode 160000 index 0000000..fdd538a --- /dev/null +++ b/rs-unsafe @@ -0,0 +1 @@ +Subproject commit fdd538a64fd9fceffc87824d45c4f631090955f3