Skip to content

Use coerce instead of unsafeCoerce where appropriate #130

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 7 commits into from
Nov 26, 2020
12 changes: 7 additions & 5 deletions src/Data/String/NonEmpty/CodePoints.purs
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,21 @@ import Data.Semigroup.Foldable (class Foldable1)
import Data.Semigroup.Foldable as F1
import Data.String.CodePoints (CodePoint)
import Data.String.CodePoints as CP
import Data.String.NonEmpty.Internal (NonEmptyString, fromString)
import Data.String.NonEmpty.Internal (NonEmptyString(..), fromString)
import Data.String.Pattern (Pattern)
import Partial.Unsafe (unsafePartial)
import Unsafe.Coerce (unsafeCoerce)

-- For internal use only. Do not export.
toNonEmptyString :: String -> NonEmptyString
toNonEmptyString = unsafeCoerce
toNonEmptyString = NonEmptyString

-- For internal use only. Do not export.
fromNonEmptyString :: NonEmptyString -> String
fromNonEmptyString = unsafeCoerce
fromNonEmptyString (NonEmptyString s) = s

-- For internal use only. Do not export.
liftS :: forall r. (String -> r) -> NonEmptyString -> r
liftS = unsafeCoerce
liftS f (NonEmptyString s) = f s

fromCodePointArray :: Array CodePoint -> Maybe NonEmptyString
fromCodePointArray = case _ of
Expand Down
11 changes: 7 additions & 4 deletions src/Data/String/NonEmpty/CodeUnits.purs
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,23 @@ import Data.Maybe (Maybe(..), fromJust)
import Data.Semigroup.Foldable (class Foldable1)
import Data.Semigroup.Foldable as F1
import Data.String.CodeUnits as CU
import Data.String.NonEmpty.Internal (NonEmptyString, fromString)
import Data.String.NonEmpty.Internal (NonEmptyString(..), fromString)
import Data.String.Pattern (Pattern)
import Data.String.Unsafe as U
import Partial.Unsafe (unsafePartial)
import Unsafe.Coerce (unsafeCoerce)

-- For internal use only. Do not export.
toNonEmptyString :: String -> NonEmptyString
toNonEmptyString = unsafeCoerce
toNonEmptyString = NonEmptyString

-- For internal use only. Do not export.
fromNonEmptyString :: NonEmptyString -> String
fromNonEmptyString = unsafeCoerce
fromNonEmptyString (NonEmptyString s) = s

-- For internal use only. Do not export.
liftS :: forall r. (String -> r) -> NonEmptyString -> r
liftS = unsafeCoerce
liftS f (NonEmptyString s) = f s

-- | Creates a `NonEmptyString` from a character array `String`, returning
-- | `Nothing` if the input is empty.
Expand Down
12 changes: 12 additions & 0 deletions src/Data/String/NonEmpty/Internal.purs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
-- | While most of the code in this module is safe, this module does
-- | export a few partial functions and the `NonEmptyString` constructor.
-- | While the partial functions are obvious from the `Partial` constraint in
-- | their type signature, the `NonEmptyString` constructor can be overlooked
-- | when searching for issues in one's code. See the constructor's
-- | documentation for more information.
module Data.String.NonEmpty.Internal where

import Prelude
Expand All @@ -13,6 +19,12 @@ import Prim.TypeError as TE
import Unsafe.Coerce (unsafeCoerce)

-- | A string that is known not to be empty.
-- |
-- | You can use this constructor to create a `NonEmptyString` that isn't
-- | non-empty, breaking the guarantee behind this newtype. It is
-- | provided as an escape hatch mainly for the `Data.NonEmpty.CodeUnits`
-- | and `Data.NonEmpty.CodePoints` modules. Use this at your own risk
-- | when you know what you are doing.
newtype NonEmptyString = NonEmptyString String

derive newtype instance eqNonEmptyString ∷ Eq NonEmptyString
Expand Down