Skip to content

Commit 46f5a9c

Browse files
andyarvanitishdgarrood
authored andcommitted
Replace use of unsafeCoerce in freeze/thaw functions with discrete foreign functions (#159)
1 parent 1bca4c0 commit 46f5a9c

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

src/Data/Array/ST.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,28 @@ exports.splice = function (i) {
7676
};
7777
};
7878

79-
exports.copyImpl = function (xs) {
79+
exports.unsafeFreeze = function (xs) {
8080
return function () {
81-
return xs.slice();
81+
return xs;
82+
};
83+
};
84+
85+
exports.unsafeThaw = function (xs) {
86+
return function () {
87+
return xs;
8288
};
8389
};
8490

91+
function copyImpl(xs) {
92+
return function () {
93+
return xs.slice();
94+
};
95+
}
96+
97+
exports.freeze = copyImpl;
98+
99+
exports.thaw = copyImpl;
100+
85101
exports.sortByImpl = function (comp) {
86102
return function (xs) {
87103
return function () {

src/Data/Array/ST.purs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import Prelude
3333
import Control.Monad.ST as ST
3434
import Control.Monad.ST (ST, kind Region)
3535
import Data.Maybe (Maybe(..))
36-
import Unsafe.Coerce (unsafeCoerce)
3736

3837
-- | A reference to a mutable array.
3938
-- |
@@ -68,20 +67,17 @@ withArray f xs = do
6867

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

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

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

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

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

129125
-- | Create an immutable copy of a mutable array.
130-
freeze :: forall h a. STArray h a -> ST h (Array a)
131-
freeze = copyImpl
132-
133-
foreign import copyImpl :: forall h a b. a -> ST h b
126+
foreign import freeze :: forall h a. STArray h a -> ST h (Array a)
134127

135128
-- | Read the value at the specified index in a mutable array.
136129
peek

0 commit comments

Comments
 (0)