Compare commits

..

10 Commits

Author SHA1 Message Date
George Thomas
7bbba202af basic enum example 2026-03-30 22:44:31 +01:00
George Thomas
209f674541 wip file dependency reload stuff 2026-03-30 22:44:31 +01:00
George Thomas
111b65c708 format 2026-03-30 13:07:16 +01:00
George Thomas
a06f3076c7 clippy fixes 2026-03-30 13:07:16 +01:00
George Thomas
7d9982112a maybe/optional example 2026-03-30 13:07:16 +01:00
George Thomas
31ae16784a minor refactor for consistency (maybe we should go the other way given sumSlice...) 2026-03-30 13:07:16 +01:00
George Thomas
b788d6685d vector/slice example 2026-03-30 13:07:16 +01:00
George Thomas
63efd03382 pass by (immutable) reference for all non-primitive types 2026-03-30 13:07:16 +01:00
George Thomas
bf0fc90272 dedup base dep 2026-03-30 13:07:16 +01:00
George Thomas
6028db040d Simplify build script
Seeing as we now no longer need to modify generated header files.
2026-03-30 13:07:16 +01:00
4 changed files with 17 additions and 9 deletions

View File

@ -29,7 +29,15 @@ 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,6 +19,8 @@
}; };
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,7 +14,6 @@ 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::<_, &BTree<i64>>(t) }).sum() (unsafe { std::mem::transmute::<&BTreeC, &BTree<i64>>(t) }).sum()
} }
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
@ -102,9 +102,8 @@ 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>) {
match x { if let Some(x) = x {
Some(x) => println!("{}", x / 2), println!("{}", x / 2)
None => {}
} }
} }