Skip to content

Commit

Permalink
Replace use of unsafeCoerce in freeze/thaw functions with discrete fo…
Browse files Browse the repository at this point in the history
…reign functions (#159)
  • Loading branch information
andyarvanitis authored and hdgarrood committed Oct 12, 2019
1 parent 1bca4c0 commit 46f5a9c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
20 changes: 18 additions & 2 deletions src/Data/Array/ST.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,28 @@ exports.splice = function (i) {
};
};

exports.copyImpl = function (xs) {
exports.unsafeFreeze = function (xs) {
return function () {
return xs.slice();
return xs;
};
};

exports.unsafeThaw = function (xs) {
return function () {
return xs;
};
};

function copyImpl(xs) {
return function () {
return xs.slice();
};
}

exports.freeze = copyImpl;

exports.thaw = copyImpl;

exports.sortByImpl = function (comp) {
return function (xs) {
return function () {
Expand Down
15 changes: 4 additions & 11 deletions src/Data/Array/ST.purs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import Prelude
import Control.Monad.ST as ST
import Control.Monad.ST (ST, kind Region)
import Data.Maybe (Maybe(..))
import Unsafe.Coerce (unsafeCoerce)

-- | A reference to a mutable array.
-- |
Expand Down Expand Up @@ -68,20 +67,17 @@ withArray f xs = do

-- | O(1). Convert a mutable array to an immutable array, without copying. The mutable
-- | array must not be mutated afterwards.
unsafeFreeze :: forall h a. STArray h a -> ST h (Array a)
unsafeFreeze = pure <<< (unsafeCoerce :: STArray h a -> Array a)
foreign import unsafeFreeze :: forall h a. STArray h a -> ST h (Array a)

-- | O(1) Convert an immutable array to a mutable array, without copying. The input
-- | array must not be used afterward.
unsafeThaw :: forall h a. Array a -> ST h (STArray h a)
unsafeThaw = pure <<< (unsafeCoerce :: Array a -> STArray h a)
foreign import unsafeThaw :: forall h a. Array a -> ST h (STArray h a)

-- | Create an empty mutable array.
foreign import empty :: forall h a. ST h (STArray h a)

-- | Create a mutable copy of an immutable array.
thaw :: forall h a. Array a -> ST h (STArray h a)
thaw = copyImpl
foreign import thaw :: forall h a. Array a -> ST h (STArray h a)

-- | Sort a mutable array in place.
sort :: forall a h. Ord a => STArray h a -> ST h (STArray h a)
Expand Down Expand Up @@ -127,10 +123,7 @@ sortWith
sortWith f = sortBy (comparing f)

-- | Create an immutable copy of a mutable array.
freeze :: forall h a. STArray h a -> ST h (Array a)
freeze = copyImpl

foreign import copyImpl :: forall h a b. a -> ST h b
foreign import freeze :: forall h a. STArray h a -> ST h (Array a)

-- | Read the value at the specified index in a mutable array.
peek
Expand Down

0 comments on commit 46f5a9c

Please sign in to comment.