diff --git a/flake.lock b/flake.lock index 80d6cec..f30be4c 100644 --- a/flake.lock +++ b/flake.lock @@ -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" } } }, diff --git a/flake.nix b/flake.nix index f9de731..f0abb3b 100644 --- a/flake.nix +++ b/flake.nix @@ -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 = { diff --git a/foreign-rust.cabal b/foreign-rust.cabal index c4c3c1f..e1b6757 100644 --- a/foreign-rust.cabal +++ b/foreign-rust.cabal @@ -121,6 +121,8 @@ test-suite test-foreign-rust , tasty , tasty-hunit , template-haskell + pkgconfig-depends: + rustbits --test-suite demo-annotated --import: diff --git a/rustbits/Cargo.toml b/rustbits/Cargo.toml new file mode 100644 index 0000000..6daf994 --- /dev/null +++ b/rustbits/Cargo.toml @@ -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 diff --git a/rustbits/cbindgen.toml b/rustbits/cbindgen.toml new file mode 100644 index 0000000..5641615 --- /dev/null +++ b/rustbits/cbindgen.toml @@ -0,0 +1,4 @@ +include_guard = "RUST_WRAPPER_H" +include_version = false +language = "C" + diff --git a/rustbits/src/lib.rs b/rustbits/src/lib.rs new file mode 100644 index 0000000..86cdaf3 --- /dev/null +++ b/rustbits/src/lib.rs @@ -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 = PhantomData; + +#[derive(Debug, BorshSerialize, BorshDeserialize)] +pub struct Hhex { + bytes: Vec +} + +impl ToHaskell for Hhex { + fn to_haskell(&self, writer: &mut W, _tag: PhantomData) -> Result<()> { + self.serialize(writer)?; + Ok(()) + } +} + +impl FromHaskell for Hhex { + fn from_haskell(buf: &mut &[u8], _tag: PhantomData) -> Result { + 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 = marshall_from_haskell_var(input, input_len, RW); + marshall_to_haskell_var(&x, out, out_len, RW); +}