forked from harpocrates/inline-rust
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSort.hs
35 lines (27 loc) · 819 Bytes
/
Sort.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
{-# LANGUAGE TemplateHaskell, QuasiQuotes #-}
module Main where
import Language.Rust.Inline ( rustIO, extendContext, setCrateRoot, basic, pointers, withStorableArrayLen )
import Data.Array.Storable ( StorableArray, newListArray, getElems )
import Data.Int ( Int64 )
extendContext basic
extendContext pointers
setCrateRoot []
main :: IO ()
main = do
arr <- newListArray (0,9) [5,1,9,0,3,4,7,6,1,8]
print =<< getElems arr
sortStorableArray arr
print =<< getElems arr
-- | Sort a storable array
sortStorableArray :: StorableArray Word Int64 -> IO ()
sortStorableArray arr = withStorableArrayLen arr $ \ptr (lo,hi) ->
[rustIO|
() {
unsafe {
std::slice::from_raw_parts_mut(
$(ptr: *mut i64),
$(hi: usize) - $(lo: usize) + 1,
).sort()
}
}
|]