Skip to content

Commit 016a0dd

Browse files
committed
Add unsafeFreeze, deprecate runSTArray
1 parent a5c3d05 commit 016a0dd

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/Data/Array/ST.purs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@ module Data.Array.ST
1313
, pushAllSTArray
1414
, spliceSTArray
1515
, freeze, thaw
16+
, unsafeFreeze
1617
, toAssocArray
1718
) where
1819

20+
import Prelude
1921
import Control.Monad.Eff (Eff)
2022
import Control.Monad.ST (ST)
2123

2224
import Data.Maybe (Maybe(..))
25+
import Unsafe.Coerce (unsafeCoerce)
2326

2427
-- | A reference to a mutable array.
2528
-- |
@@ -33,6 +36,8 @@ foreign import data STArray :: * -> * -> *
3336
-- | An element and its index.
3437
type Assoc a = { value :: a, index :: Int }
3538

39+
-- | **DEPRECATED**: Use `unsafeFreeze` together with `runST` instead.
40+
-- |
3641
-- | Freeze a mutable array, creating an immutable array. Use this function as you would use
3742
-- | `runST` to freeze a mutable reference.
3843
-- |
@@ -42,6 +47,11 @@ foreign import runSTArray
4247
. (forall h. Eff (st :: ST h | r) (STArray h a))
4348
-> Eff r (Array a)
4449

50+
-- | O(1). Convert a mutable array to an immutable array, without copying. The mutable
51+
-- | array must not be mutated afterwards.
52+
unsafeFreeze :: forall a r h. STArray h a -> Eff (st :: ST h | r) (Array a)
53+
unsafeFreeze = pure <<< (unsafeCoerce :: STArray h a -> Array a)
54+
4555
-- | Create an empty mutable array.
4656
foreign import emptySTArray :: forall a h r. Eff (st :: ST h | r) (STArray h a)
4757

0 commit comments

Comments
 (0)