[
  {
    "path": "LICENSE",
    "content": "This is free and unencumbered software released into the public domain.\n\nAnyone is free to copy, modify, publish, use, compile, sell, or\ndistribute this software, either in source code form or as a compiled\nbinary, for any purpose, commercial or non-commercial, and by any\nmeans.\n\nIn jurisdictions that recognize copyright laws, the author or authors\nof this software dedicate any and all copyright interest in the\nsoftware to the public domain. We make this dedication for the benefit\nof the public at large and to the detriment of our heirs and\nsuccessors. We intend this dedication to be an overt act of\nrelinquishment in perpetuity of all present and future rights to this\nsoftware under copyright law.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR\nOTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,\nARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR\nOTHER DEALINGS IN THE SOFTWARE.\n\nFor more information, please refer to <http://unlicense.org>\n\n"
  },
  {
    "path": "Makefile",
    "content": "RC = LD_LIBRARY_PATH=/usr/local/lib rustc\nGHC = ghc\n\nall: main\n\nlibfact.a: fact.rs\n\t$(RC) --crate-type staticlib fact.rs\n\nlibpoint.a: point.rs\n\t$(RC) --crate-type staticlib point.rs\n\nmain: libfact.a libpoint.a main.hs\n\t$(GHC) main.hs libfact.a libpoint.a -lpthread -o main\n\nclean:\n\trm -f libfact.a libpoint.a main.hi main.o main\n"
  },
  {
    "path": "README.md",
    "content": "# Rust-Haskell FFI Example\n\nA lot of Haskell people I've talked to are excited about the prospect of\nusing Rust as a replacement for C in certain speed-critical or low-level\nparts of their application. To that end, and in light of the recent\n(as of this writing) Rust 1.0 alpha release, I've shown here a small\nexample of calling Rust from Haskell.\n\nThis contains a single Haskell file and two Rust libraries which Haskell\ncan call out to.\nThe first Rust library is contained in `fact.rs` and implements a simple\nfactorial, which is easier to wrap; the second is contained in `point.rs`\nand demonstrates allocating memory in Rust, passing it to Haskell, using\nwrapped Rust functions to manipulate it, and finally allowing Haskell's\nGC to call back into Rust to free it.\n\nThis of course requires GHC and a reasonably recent version of rustc\ninstalled. This version has been tested with GHC versions `7.8.4` and\n`7.10.1`, and the following rustc versions:\n\n    1.0.0 (a59de37e9 2015-05-13) (built 2015-05-14)\n\nAll the examples here I release into the public domain.\n"
  },
  {
    "path": "fact.rs",
    "content": "#![crate_type = \"lib\"]\n\n#[no_mangle]\npub extern fn fact(x: u64) -> u64 {\n    match x {\n        0 => 1,\n        _ => x * fact(x-1),\n    }\n}\n"
  },
  {
    "path": "main.hs",
    "content": "{-# LANGUAGE ForeignFunctionInterface #-}\n\nimport Foreign\nimport Foreign.C.Types\nimport Foreign.ForeignPtr\n\n-- Wrapping the fact.rs module...\nforeign import ccall \"fact\"\n  c_fact :: CULong -> CULong\n\nfact :: Int -> Int\nfact = fromIntegral . c_fact . fromIntegral\n\n-- A little bit more work to wrap the point.rs module; I also\n-- don't bother to write a proper representation for the\n-- underlying Point, which ought to be an instance of Storable\n-- if we want to manipulate it in Rust-land or do pointer\n-- arithmetic.\ntype Point = ForeignPtr PointRepr\ndata PointRepr\n\nforeign import ccall safe \"mk_point\"\n  mk_point :: CULong -> CULong -> IO (Ptr PointRepr)\n\nforeign import ccall safe \"add_point\"\n  add_point :: Ptr PointRepr -> Ptr PointRepr -> IO ()\n\nforeign import ccall safe \"print_point\"\n  print_point :: Ptr PointRepr -> IO ()\n\nforeign import ccall safe \"&free_point\"\n  free_point :: FunPtr (Ptr PointRepr -> IO ())\n\n-- We use the free_point function as a finalizer when we\n-- create a point; this lets the Haskell GC take over for\n-- us.\nmkPoint :: Int -> Int -> IO Point\nmkPoint x y = do\n  ptr <- mk_point (fromIntegral x) (fromIntegral y)\n  newForeignPtr free_point ptr\n\n-- This modifies the first point in-place.\naddPoint :: Point -> Point -> IO ()\naddPoint x y = withForeignPtr x (\\ x' ->\n               withForeignPtr y (\\ y' ->\n                 add_point x' y'))\n\n-- This prints its argument\nprintPoint :: Point -> IO ()\nprintPoint p = withForeignPtr p print_point\n\n-- Testing the point functions; we don't need to free\n-- explicitly because GHC should call our finalizer\npoints :: IO ()\npoints = do\n  a <- mkPoint 2 3\n  b <- mkPoint 1 1\n  addPoint a b\n  printPoint a\n\n-- And here we go!\nmain :: IO ()\nmain = do\n  putStr \"fact(5) = \"\n  print (fact 5)\n  points\n"
  },
  {
    "path": "point.rs",
    "content": "#![crate_type = \"lib\"]\n\n// We aren't exposing the internals of this, so I don't bother giving it\n// a C representation.\npub struct Point {\n    x: u64,\n    y: u64,\n}\n\n// Rust-land functions...\nimpl Point {\n    fn new(x: u64, y: u64) -> Point {\n        Point { x: x, y: y }\n    }\n\n    fn add_mut(&mut self, p: &Point) {\n        self.x = self.x + p.x;\n        self.y = self.y + p.y;\n    }\n}\n\n// What we're doing here is a little bit unsafe---by exposing\n// these elsewhere, we effectively transfer the 'ownership' out\n// of Rust-land. In this case, we'll have a corresponding free\n// function which takes ownership back again.\n#[no_mangle]\npub extern fn mk_point(x: u64, y: u64) -> Box<Point> {\n    Box::new(Point::new(x, y))\n}\n\n// We free something in Rust by… taking ownership of it and doing nothing.\n#[no_mangle]\npub extern fn free_point(_: Box<Point>) {}\n\n// Wrap our add_mut method for exporting.\n#[no_mangle]\npub extern fn add_point(p1: &mut Point, p2: &Point) {\n    p1.add_mut(p2);\n}\n\n// Wrap our print method for exporting.\n#[no_mangle]\npub extern fn print_point(p: &Point) {\n    println!(\"Point(x={}, y={})\", p.x, p.y);\n}\n"
  }
]