-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRust.hs
50 lines (29 loc) · 1.01 KB
/
Rust.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
{-# LANGUAGE ForeignFunctionInterface #-}
module Main where
import Control.Concurrent (MVar, takeMVar, newMVar)
import Foreign
import Foreign.C
type Callback = CInt -> IO ()
-- | rs_function function from Rust library
foreign import ccall "rs_function"
rs_function :: Int -> IO ()
-- | Register a callback within the rust library via rs_register
foreign import ccall "rs_register"
rs_register :: FunPtr Callback -> IO ()
-- | Create a callback wrapper to be called by the Rust library
foreign import ccall "wrapper"
makeCallback :: Callback -> IO (FunPtr Callback)
callback :: MVar CInt -> CInt -> IO ()
callback mi i = print (msg i)
>> takeMVar mi
>>= print . statemsg
where
msg = (++) "Haskell-callback invoked with value: " . show
statemsg = (++) " Haskell-callback carrying state: " . show
main :: IO ()
main = do
rs_function 0
st <- newMVar 42
rs_register =<< makeCallback (callback st)
rs_function 1
putStrLn "Haskell main done"