Skip to content

Replace use of unsafeCoerce in freeze/thaw functions with discrete fo… #159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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