@@ -12,7 +12,8 @@
1212-- use in heavily tested code.
1313module Data.Text.Unsafe
1414 (
15− inlinePerformIO
15+ inlineInterleaveST
16+ , inlinePerformIO
1617 , iter
1718 , iter_
1819 , reverseIter
@@ -21,9 +22,10 @@ module Data.Text.Unsafe
2122 ) where
2223
2324import Control.Exception (assert)
25+import Data.Text.Encoding.Utf16 (chr2)
2426import Data.Text.Internal (Text(..))
2527import Data.Text.UnsafeChar (unsafeChr)
26−import Data.Text.Encoding.Utf16 (chr2)
28+import GHC.ST (ST(..))
2729import qualified Data.Text.Array as A
2830#if defined(__GLASGOW_HASKELL__)
2931# if __GLASGOW_HASKELL__ >= 611
@@ -100,3 +102,17 @@ inlinePerformIO (IO m) = case m realWorld# of (# _, r #) -> r
100102#else
101103inlinePerformIO = unsafePerformIO
102104#endif
105+
106+-- | Allow an 'ST' computation to be deferred lazily. When passed an
107+-- action of type 'ST' @s@ @a@, the action will only be performed when
108+-- the value of @a@ is demanded.
109+--
110+-- This function is identical to the normal unsafeInterleaveST, but is
111+-- inlined and hence faster.
112+--
113+-- /Note/: This operation is highly unsafe, as it can introduce
114+-- externally visible non-determinism into an 'ST' action.
115+inlineInterleaveST :: ST s a -> ST s a
116+inlineInterleaveST (ST m) = ST $ \ s ->
117+ let r = case m s of (# _, res #) -> res in (# s, r #)
118+{-# INLINE inlineInterleaveST #-}