add Rust example

This commit is contained in:
Rene Vergara 2025-04-16 08:22:17 -05:00
parent bbe19caf27
commit f95c538d84
Signed by: pitmutt
SSH key fingerprint: SHA256:vNa8FIqbBZjV9hOCkXyOzd7gqWCMCfkcfiPH2zaGfQ0
6 changed files with 133 additions and 17 deletions

42
flake.lock generated
View file

@ -1,18 +1,17 @@
{
"nodes": {
"borsh": {
"flake": false,
"crane": {
"locked": {
"lastModified": 1716294836,
"narHash": "sha256-dEv3m7U7buQr9kX68U+r5Ro4KRXesGsSpqBs2CLddaY=",
"owner": "khazaddum",
"repo": "borsh",
"rev": "b8e5cfd6ed875707fa82698d5c356ac062203e71",
"lastModified": 1744386647,
"narHash": "sha256-DXwQEJllxpYeVOiSlBhQuGjfvkoGHTtILLYO2FvcyzQ=",
"owner": "ipetkov",
"repo": "crane",
"rev": "d02c1cdd7ec539699aa44e6ff912e15535969803",
"type": "github"
},
"original": {
"owner": "khazaddum",
"repo": "borsh",
"owner": "ipetkov",
"repo": "crane",
"type": "github"
}
},
@ -82,10 +81,31 @@
},
"root": {
"inputs": {
"borsh": "borsh",
"crane": "crane",
"flake-parts": "flake-parts",
"haskell-flake": "haskell-flake",
"nixpkgs": "nixpkgs"
"nixpkgs": "nixpkgs",
"rust-overlay": "rust-overlay"
}
},
"rust-overlay": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1744803954,
"narHash": "sha256-f+gE6JtLhPzyDWOCEHbN/S30GEGHMtXEt41+Va7wzEU=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "c564fb830c7d5b3e4fde5ea829a62f0e41e43a20",
"type": "github"
},
"original": {
"owner": "oxalica",
"repo": "rust-overlay",
"type": "github"
}
}
},

View file

@ -4,10 +4,11 @@
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
flake-parts.url = "github:hercules-ci/flake-parts";
haskell-flake.url = "github:srid/haskell-flake";
borsh = {
url = "github:khazaddum/borsh";
flake = false;
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
crane.url = "github:ipetkov/crane";
};
outputs = inputs @ {
self,
@ -27,7 +28,42 @@
system,
...
}: let
craneLib = (inputs.crane.mkLib pkgs).overrideToolchain (p: p.rust-bin.selectLatestNightlyWith (toolchain: toolchain.default));
rustbits = pkgs: let
cargoCbuild = {
cargoArtifacts,
cargoAwesomeExtraArgs ? "", # Arguments that are generally useful default
cargoExtraArgs ? "", # Other cargo-general flags (e.g. for features or targets)
} @ origArgs: let
# Clean the original arguments for good hygiene (i.e. so the flags specific
# to this helper don't pollute the environment variables of the derivation)
args = builtins.removeAttrs origArgs [
"cargoAwesomeExtraArgs"
"cargoExtraArgs"
];
in
craneLib.mkCargoDerivation (args
// {
inherit cargoArtifacts;
src = cargoArtifacts;
pname = "rustbits";
version = "0.1.0";
pnameSuffix = "-cbuild";
buildPhaseCargoCommand = "cargo cinstall --prefix=$out --libdir=$out/lib --includedir=$out/include --pkgconfigdir=$out/lib/pkgconfig ${cargoAwesomeExtraArgs}";
nativeBuildInputs = (args.nativeBuildInputs or []) ++ [pkgs.cargo-c];
});
in
cargoCbuild {
cargoArtifacts = craneLib.cleanCargoSource ./rustbits/.;
};
in {
_module.args.pkgs = import inputs.nixpkgs {
inherit system;
overlays = [(import inputs.rust-overlay)];
};
haskellProjects.default = {
# The base package set representing a specific GHC version.
# By default, this is pkgs.haskellPackages.
@ -44,15 +80,17 @@
# shower.source = inputs.shower;
# };
packages = {
borsh.source = inputs.borsh;
};
settings = {
rustbits.custom = _: rustbits pkgs;
foreign-rust = {
check = false;
#check = false;
};
borsh = {
broken = false;
};
autoWire = ["packages" "devShells"];
};
devShell = {

View file

@ -121,6 +121,8 @@ test-suite test-foreign-rust
, tasty
, tasty-hunit
, template-haskell
pkgconfig-depends:
rustbits
--test-suite demo-annotated
--import:

13
rustbits/Cargo.toml Normal file
View file

@ -0,0 +1,13 @@
[package]
name = "rustbits"
version = "0.1.0"
edition = "2021"
[dependencies]
haskell-ffi = { git = "https://code.vergara.tech/Vergara_Tech/haskell-rust-ffi.git", rev = "2bf292e2e56eac8e9fb0fb2e1450cf4a4bd01274" }
[features]
capi = []
[package.metadata.capi.library]
versioning = false

4
rustbits/cbindgen.toml Normal file
View file

@ -0,0 +1,4 @@
include_guard = "RUST_WRAPPER_H"
include_version = false
language = "C"

39
rustbits/src/lib.rs Normal file
View file

@ -0,0 +1,39 @@
use haskell_ffi::{
error::Result,
from_haskell::marshall_from_haskell_var,
to_haskell::marshall_to_haskell_var,
FromHaskell, ToHaskell
};
pub enum RW {}
pub const RW: PhantomData<RW> = PhantomData;
#[derive(Debug, BorshSerialize, BorshDeserialize)]
pub struct Hhex {
bytes: Vec<u8>
}
impl<RW> ToHaskell<RW> for Hhex {
fn to_haskell<W: Write>(&self, writer: &mut W, _tag: PhantomData<RW>) -> Result<()> {
self.serialize(writer)?;
Ok(())
}
}
impl<RW> FromHaskell<RW> for Hhex {
fn from_haskell(buf: &mut &[u8], _tag: PhantomData<RW>) -> Result<Self> {
let x = Hhex::deserialize(buf)?;
Ok(x)
}
}
#[no_mangle]
pub extern "C" fn marshalling_test (
input: *const u8,
input_len: usize,
out: *mut u8,
out_len: &mut usize
){
let x: Vec<u8> = marshall_from_haskell_var(input, input_len, RW);
marshall_to_haskell_var(&x, out, out_len, RW);
}