Skip to content

Commit bf70fb9

Browse files
authored
Remove some usages of unsafeCoerce (#184)
* Remove some usages of unsafeCoerce and discourage uses of Data.Array.NonEmpty.Internal
1 parent d93b826 commit bf70fb9

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

src/Data/Array.purs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ import Control.Alternative (class Alternative)
123123
import Control.Lazy (class Lazy, defer)
124124
import Control.Monad.Rec.Class (class MonadRec, Step(..), tailRecM2)
125125
import Control.Monad.ST as ST
126-
import Data.Array.NonEmpty.Internal (NonEmptyArray)
126+
import Data.Array.NonEmpty.Internal (NonEmptyArray(..))
127127
import Data.Array.ST as STA
128128
import Data.Array.ST.Iterator as STAI
129129
import Data.Foldable (class Foldable, foldl, foldr, traverse_)
@@ -134,7 +134,6 @@ import Data.Traversable (sequence, traverse)
134134
import Data.Tuple (Tuple(..), fst, snd)
135135
import Data.Unfoldable (class Unfoldable, unfoldr)
136136
import Partial.Unsafe (unsafePartial)
137-
import Unsafe.Coerce (unsafeCoerce)
138137

139138
-- | Convert an `Array` into an `Unfoldable` structure.
140139
toUnfoldable :: forall f. Unfoldable f => Array ~> f
@@ -895,7 +894,7 @@ groupBy op xs =
895894
_ <- STA.push x sub
896895
STAI.pushWhile (op x) iter sub
897896
grp <- STA.unsafeFreeze sub
898-
STA.push ((unsafeCoerce :: Array ~> NonEmptyArray) grp) result
897+
STA.push (NonEmptyArray grp) result
899898
STA.unsafeFreeze result
900899

901900
-- | Remove the duplicates from an array, creating a new array.

src/Data/Array/NonEmpty.purs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Data.Array.NonEmpty
2-
( module Data.Array.NonEmpty.Internal
2+
( module Internal
33
, fromArray
44
, fromNonEmpty
55
, toArray
@@ -101,7 +101,8 @@ import Control.Alternative (class Alternative)
101101
import Control.Lazy (class Lazy)
102102
import Control.Monad.Rec.Class (class MonadRec)
103103
import Data.Array as A
104-
import Data.Array.NonEmpty.Internal (NonEmptyArray)
104+
import Data.Array.NonEmpty.Internal (NonEmptyArray(..))
105+
import Data.Array.NonEmpty.Internal (NonEmptyArray) as Internal
105106
import Data.Bifunctor (bimap)
106107
import Data.Foldable (class Foldable)
107108
import Data.Maybe (Maybe(..), fromJust)
@@ -139,7 +140,7 @@ fromArray xs
139140

140141
-- | INTERNAL
141142
unsafeFromArray :: forall a. Array a -> NonEmptyArray a
142-
unsafeFromArray = unsafeCoerce
143+
unsafeFromArray = NonEmptyArray
143144

144145
unsafeFromArrayF :: forall f a. f (Array a) -> f (NonEmptyArray a)
145146
unsafeFromArrayF = unsafeCoerce
@@ -148,7 +149,7 @@ fromNonEmpty :: forall a. NonEmpty Array a -> NonEmptyArray a
148149
fromNonEmpty (x :| xs) = cons' x xs
149150

150151
toArray :: forall a. NonEmptyArray a -> Array a
151-
toArray = unsafeCoerce
152+
toArray (NonEmptyArray xs) = xs
152153

153154
toNonEmpty :: forall a. NonEmptyArray a -> NonEmpty Array a
154155
toNonEmpty = uncons >>> \{head: x, tail: xs} -> x :| xs

src/Data/Array/NonEmpty/Internal.purs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
module Data.Array.NonEmpty.Internal (NonEmptyArray) where
1+
-- | This module exports the `NonEmptyArray` constructor.
2+
-- |
3+
-- | It is **NOT** intended for public use and is **NOT** versioned.
4+
-- |
5+
-- | Its content may change **in any way**, **at any time** and
6+
-- | **without notice**.
7+
8+
module Data.Array.NonEmpty.Internal (NonEmptyArray(..)) where
29

310
import Prelude
411

@@ -14,6 +21,13 @@ import Data.Traversable (class Traversable)
1421
import Data.TraversableWithIndex (class TraversableWithIndex)
1522
import Data.Unfoldable1 (class Unfoldable1)
1623

24+
-- | An array that is known not to be empty.
25+
-- |
26+
-- | You can use the constructor to create a `NonEmptyArray` that isn't
27+
-- | non-empty, breaking the guarantee behind this newtype. It is
28+
-- | provided as an escape hatch mainly for the `Data.Array.NonEmpty`
29+
-- | and `Data.Array` modules. Use this at your own risk when you know
30+
-- | what you are doing.
1731
newtype NonEmptyArray a = NonEmptyArray (Array a)
1832

1933
instance showNonEmptyArray :: Show a => Show (NonEmptyArray a) where

0 commit comments

Comments
 (0)