Mutable List builder in the ST Monad
at master 24 lines 961 B view raw
1{-# LANGUAGE MagicHash, UnboxedTuples, GHCForeignImportPrim, UnliftedFFITypes, BangPatterns #-} 2-- | This module allows for the (obviously unsafe) overwriting of one haskell value with another. 3module Data.ListBuilder.Unsafe ( 4 unsafeSetField 5 , unsafeGetField 6 ) where 7 8import GHC.Prim 9import GHC.Exts 10 11foreign import prim "unsafeSetFieldzh" unsafeSetField# :: Int# -> Any -> Any -> (##) 12foreign import prim "unsafeGetFieldzh" unsafeGetField# :: Int# -> Any -> (# Any #) 13 14-- Set the value of a certain constructor field. 15-- You'd better be careful that it isn't being shared 16unsafeSetField :: Int -> a -> b -> IO () 17unsafeSetField (I# i) !x y = case unsafeSetField# i (unsafeCoerce# x :: Any) (unsafeCoerce# y :: Any) of 18 (##) -> return () 19{-# INLINE unsafeSetField #-} 20 21unsafeGetField :: Int -> a -> IO b 22unsafeGetField (I# i) !x = case unsafeGetField# i (unsafeCoerce# x :: Any) of 23 (# y #) -> return (unsafeCoerce# y) 24{-# INLINE unsafeGetField #-}