basic enum example
This commit is contained in:
parent
7853204758
commit
4a8a3750fe
@ -9,6 +9,8 @@ main = do
|
||||
hello "Haskell"
|
||||
helloStruct T{a = True, b = 42}
|
||||
helloStruct T{a = False, b = maxBound}
|
||||
helloEnum E1
|
||||
helloEnum E3
|
||||
helloShape $ Circle 3.14
|
||||
helloShape $ Rectangle 10.0 5.0
|
||||
putStrLn $ "3 + 4 = " <> show (add 3 4)
|
||||
|
||||
@ -29,6 +29,7 @@ common common
|
||||
NoMonomorphismRestriction
|
||||
OverloadedRecordDot
|
||||
OverloadedStrings
|
||||
PatternSynonyms
|
||||
RecordWildCards
|
||||
ViewPatterns
|
||||
ghc-options:
|
||||
|
||||
@ -2,10 +2,17 @@
|
||||
-- or look at upstream plans: https://github.com/well-typed/hs-bindgen/issues?q=state%3Aopen%20label%3A%22highlevel%22
|
||||
module GarnetRs.Wrapped (
|
||||
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 (..),
|
||||
BTree (..),
|
||||
hello,
|
||||
helloStruct,
|
||||
helloEnum,
|
||||
helloShape,
|
||||
add,
|
||||
sumTree,
|
||||
@ -62,6 +69,9 @@ hello bs = useAsCString bs \ptr -> Raw.hello $ unsafeFromPtr ptr
|
||||
helloStruct :: T -> IO ()
|
||||
helloStruct t = with (convertT t) \ptr -> Raw.hello_struct $ unsafeFromPtr ptr
|
||||
|
||||
helloEnum :: Raw.E -> IO ()
|
||||
helloEnum e = with e \ptr -> Raw.hello_enum $ unsafeFromPtr ptr
|
||||
|
||||
helloShape :: Shape -> IO ()
|
||||
helloShape s = with (convertShape s) \ptr -> Raw.hello_shape $ unsafeFromPtr ptr
|
||||
|
||||
|
||||
13
rust/lib.rs
13
rust/lib.rs
@ -27,6 +27,19 @@ extern "C" fn hello_struct(t: &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)]
|
||||
#[derive(Debug)]
|
||||
enum Shape {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user