basic enum example
This commit is contained in:
parent
209f674541
commit
7bbba202af
@ -9,6 +9,8 @@ main = do
|
|||||||
hello "Haskell"
|
hello "Haskell"
|
||||||
helloStruct T{a = True, b = 42}
|
helloStruct T{a = True, b = 42}
|
||||||
helloStruct T{a = False, b = maxBound}
|
helloStruct T{a = False, b = maxBound}
|
||||||
|
helloEnum E1
|
||||||
|
helloEnum E3
|
||||||
helloShape $ Circle 3.14
|
helloShape $ Circle 3.14
|
||||||
helloShape $ Rectangle 10.0 5.0
|
helloShape $ Rectangle 10.0 5.0
|
||||||
putStrLn $ "3 + 4 = " <> show (add 3 4)
|
putStrLn $ "3 + 4 = " <> show (add 3 4)
|
||||||
|
|||||||
@ -1,11 +1,20 @@
|
|||||||
|
{-# LANGUAGE PatternSynonyms #-}
|
||||||
|
|
||||||
-- TODO automate this sort of high level wrapper boilerplate
|
-- TODO automate this sort of high level wrapper boilerplate
|
||||||
-- or look at upstream plans: https://github.com/well-typed/hs-bindgen/issues?q=state%3Aopen%20label%3A%22highlevel%22
|
-- or look at upstream plans: https://github.com/well-typed/hs-bindgen/issues?q=state%3Aopen%20label%3A%22highlevel%22
|
||||||
module GarnetRs.Wrapped (
|
module GarnetRs.Wrapped (
|
||||||
T (..),
|
T (..),
|
||||||
|
Raw.E (..),
|
||||||
|
-- TODO hmm, we don't really want to have to list all of these...
|
||||||
|
-- is there an option to make them not be patterns at all?
|
||||||
|
pattern Raw.E1,
|
||||||
|
pattern Raw.E2,
|
||||||
|
pattern Raw.E3,
|
||||||
Shape (..),
|
Shape (..),
|
||||||
BTree (..),
|
BTree (..),
|
||||||
hello,
|
hello,
|
||||||
helloStruct,
|
helloStruct,
|
||||||
|
helloEnum,
|
||||||
helloShape,
|
helloShape,
|
||||||
add,
|
add,
|
||||||
sumTree,
|
sumTree,
|
||||||
@ -62,6 +71,9 @@ hello = flip useAsCString $ Raw.hello . unsafeFromPtr
|
|||||||
helloStruct :: T -> IO ()
|
helloStruct :: T -> IO ()
|
||||||
helloStruct = flip with (Raw.hello_struct . unsafeFromPtr) . convertT
|
helloStruct = flip with (Raw.hello_struct . unsafeFromPtr) . convertT
|
||||||
|
|
||||||
|
helloEnum :: Raw.E -> IO ()
|
||||||
|
helloEnum = flip with (Raw.hello_enum . unsafeFromPtr)
|
||||||
|
|
||||||
helloShape :: Shape -> IO ()
|
helloShape :: Shape -> IO ()
|
||||||
helloShape = flip with (Raw.hello_shape . unsafeFromPtr) . convertShape
|
helloShape = flip with (Raw.hello_shape . unsafeFromPtr) . convertShape
|
||||||
|
|
||||||
|
|||||||
13
rust/lib.rs
13
rust/lib.rs
@ -27,6 +27,19 @@ extern "C" fn hello_struct(t: &T) {
|
|||||||
say_hello(&format!("{:?}", t))
|
say_hello(&format!("{:?}", t))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[repr(C)]
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum E {
|
||||||
|
E1,
|
||||||
|
E2,
|
||||||
|
E3,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[unsafe(no_mangle)]
|
||||||
|
extern "C" fn hello_enum(e: &E) {
|
||||||
|
say_hello(&format!("{:?}", e))
|
||||||
|
}
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum Shape {
|
enum Shape {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user