Compare commits

..

8 Commits

Author SHA1 Message Date
George Thomas
c1d30b0e61 refactor 2026-04-13 15:50:03 +01:00
George Thomas
ea5f816425 basic enum example 2026-04-13 15:48:41 +01:00
George Thomas
5c3aa088f7 Add maybe/optional example 2026-04-13 13:04:06 +01:00
George Thomas
6203e7570f Add vector/slice example 2026-04-13 13:04:06 +01:00
George Thomas
9e8bccdafd Pass all non-primitive types by (immutable) reference 2026-04-13 13:04:06 +01:00
George Thomas
8cb0bf9c5e Deduplicate base dependency 2026-04-13 13:04:06 +01:00
George Thomas
2ff3ad93e5 Simplify build script
Seeing as we now no longer need to modify generated header files.
2026-04-13 13:04:06 +01:00
George Thomas
8a5bf61d6b wip file dependency reload stuff 2026-04-13 13:04:06 +01:00
4 changed files with 9 additions and 17 deletions

View File

@ -29,15 +29,7 @@ import HsBindgen.TH
import Language.Haskell.TH import Language.Haskell.TH
import System.Process import System.Process
import Control.Monad.IO.Class (MonadIO (liftIO))
import Language.Haskell.TH.Syntax (addDependentFile)
import System.Directory.Extra (getCurrentDirectory)
do do
-- not sure this does anything - hs-bindgen should already be doing the tracking, and the issues are from elsewhere
-- dir <- liftIO getCurrentDirectory
-- liftIO $ print dir
-- addDependentFile $ dir <> "/rust/target/debug/garnet_rs.h"
systemDirs <- -- TODO bit of a hack systemDirs <- -- TODO bit of a hack
map (Dir . T.unpack . T.strip) map (Dir . T.unpack . T.strip)
. concatMap (takeWhile (maybe False ((== ' ') . fst) . T.uncons) . dropWhile T.null . T.lines) . concatMap (takeWhile (maybe False ((== ' ') . fst) . T.uncons) . dropWhile T.null . T.lines)

View File

@ -19,8 +19,6 @@
}; };
overrides = [ overrides = [
({ pkgs, ... }: { ({ pkgs, ... }: {
# TODO get this upstreamed:
# https://input-output-hk.github.io/haskell.nix/tutorials/pkg-map.html#mapping-in-haskellnix
packages.libclang-bindings.components.library = { packages.libclang-bindings.components.library = {
build-tools = [ pkgs.llvmPackages.llvm ]; build-tools = [ pkgs.llvmPackages.llvm ];
libs = [ pkgs.llvmPackages.libclang ]; libs = [ pkgs.llvmPackages.libclang ];

View File

@ -14,6 +14,7 @@ fn main() {
// void print_optional(const int8_t *x); // void print_optional(const int8_t *x);
// and that's only an issue because in HLS TH dependent-file watching gives up after an error // and that's only an issue because in HLS TH dependent-file watching gives up after an error
// i.e. once the containing splice has thrown an exception once, the containing file needs a manual edit to kick it // i.e. once the containing splice has thrown an exception once, the containing file needs a manual edit to kick it
// and that's really not helped by Rust Analyzer mostly only showing diagnostics on save
// P.S. strings to stdout?! what a terrible API // P.S. strings to stdout?! what a terrible API
// don't get me started in the discoverability of actually then using the terminal for debugging: // don't get me started in the discoverability of actually then using the terminal for debugging:
// println!("cargo::warning={:?}", env::var("OUT_DIR")); // println!("cargo::warning={:?}", env::var("OUT_DIR"));

View File

@ -11,7 +11,7 @@ fn say_hello(name: &str) {
} }
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
extern "C" fn hello(c: *const c_char) { extern "C" fn hello(c: *const c_char) -> () {
say_hello(unsafe { CStr::from_ptr(c) }.to_str().unwrap()) say_hello(unsafe { CStr::from_ptr(c) }.to_str().unwrap())
} }
@ -23,7 +23,7 @@ struct T {
} }
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
extern "C" fn hello_struct(t: &T) { extern "C" fn hello_struct(t: &T) -> () {
say_hello(&format!("{:?}", t)) say_hello(&format!("{:?}", t))
} }
@ -48,7 +48,7 @@ enum Shape {
} }
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
extern "C" fn hello_shape(s: &Shape) { extern "C" fn hello_shape(s: &Shape) -> () {
say_hello(&format!("{:?}", s)) say_hello(&format!("{:?}", s))
} }
@ -93,7 +93,7 @@ enum BTreeC {
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
extern "C" fn sum_tree(t: &BTreeC) -> i64 { extern "C" fn sum_tree(t: &BTreeC) -> i64 {
(unsafe { std::mem::transmute::<&BTreeC, &BTree<i64>>(t) }).sum() (unsafe { std::mem::transmute::<_, &BTree<i64>>(t) }).sum()
} }
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
@ -102,8 +102,9 @@ extern "C" fn sum_slice(v: *const i64, s: usize) -> i64 {
} }
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
extern "C" fn print_optional(x: Option<&i8>) { extern "C" fn print_optional(x: Option<&i8>) -> () {
if let Some(x) = x { match x {
println!("{}", x / 2) Some(x) => println!("{}", x / 2),
None => {}
} }
} }