Commits

Bryan O'Sullivan committed 181d5b4

Move inlineInterleaveST and document it.

Comments (0)

Files changed (2)

Data/Text/Lazy/Builder.hs

    ) where
 
 import Control.Exception (assert)
-import Control.Monad.ST
+import Control.Monad.ST (ST, runST)
 import Data.Bits ((.&.))
 import Data.Char (ord)
 import Data.Monoid (Monoid(..))
 import Data.Text.Internal (Text(..))
 import Data.Text.Lazy.Internal (defaultChunkSize)
+import Data.Text.Unsafe (inlineInterleaveST)
 import Data.Text.UnsafeShift (shiftR)
-import Data.Word
-import GHC.ST (ST(..))
+import Data.Word (Word16)
 import Prelude hiding (map, putChar)
 
 import qualified Data.Text as S
     return $! Buffer arr 0 0 size
 {-# INLINE newBuffer #-}
 
-inlineInterleaveST :: ST s a -> ST s a
-inlineInterleaveST (ST m) = ST $ \ s ->
-    let r = case m s of (# _, res #) -> res in (# s, r #)
-{-# INLINE inlineInterleaveST #-}
-
-------------------------------------------------------------------------
-
 -- | Unsafely copy the elements of an array.
 unsafeCopy :: A.Elt e =>
               A.Array e -> Int -> A.MArray s e -> Int -> Int -> ST s ()

Data/Text/Unsafe.hs

 -- use in heavily tested code.
 module Data.Text.Unsafe
     (
-      inlinePerformIO
+      inlineInterleaveST
+    , inlinePerformIO
     , iter
     , iter_
     , reverseIter
     ) where
      
 import Control.Exception (assert)
+import Data.Text.Encoding.Utf16 (chr2)
 import Data.Text.Internal (Text(..))
 import Data.Text.UnsafeChar (unsafeChr)
-import Data.Text.Encoding.Utf16 (chr2)
+import GHC.ST (ST(..))
 import qualified Data.Text.Array as A
 #if defined(__GLASGOW_HASKELL__)
 # if __GLASGOW_HASKELL__ >= 611
 #else
 inlinePerformIO = unsafePerformIO
 #endif
+
+-- | Allow an 'ST' computation to be deferred lazily. When passed an
+-- action of type 'ST' @s@ @a@, the action will only be performed when
+-- the value of @a@ is demanded.
+--
+-- This function is identical to the normal unsafeInterleaveST, but is
+-- inlined and hence faster.
+--
+-- /Note/: This operation is highly unsafe, as it can introduce
+-- externally visible non-determinism into an 'ST' action.
+inlineInterleaveST :: ST s a -> ST s a
+inlineInterleaveST (ST m) = ST $ \ s ->
+    let r = case m s of (# _, res #) -> res in (# s, r #)
+{-# INLINE inlineInterleaveST #-}