Skip to content

Commit 0185609

Browse files
committed
Remove some usages of unsafeCoerce
1 parent d93b826 commit 0185609

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-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: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
module Data.Array.NonEmpty.Internal (NonEmptyArray) where
1+
-- | This module exports the `NonEmptyArray` constructor, which can be
2+
-- | overlooked when searching for issues in one's code. See the
3+
-- | constructor's documentation for more information.
4+
module Data.Array.NonEmpty.Internal (NonEmptyArray(..)) where
25

36
import Prelude
47

@@ -14,6 +17,13 @@ import Data.Traversable (class Traversable)
1417
import Data.TraversableWithIndex (class TraversableWithIndex)
1518
import Data.Unfoldable1 (class Unfoldable1)
1619

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

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

0 commit comments

Comments
 (0)