Skip to content

Commit

Permalink
Use coerce instead of unsafeCoerce where appropriate (#130)
Browse files Browse the repository at this point in the history
* Replace unsafeCoerce with safer alternative where possible

* Remove unused import

* Add docs to the Internal module and NonEmptyString constructor

* Remove 'usage of this constructor...' sentence

* Move constructor docs to type docs

Constructor docs don't yet appear in the generated documentation

* Add backticks around NonEmptyString in docs

* Add "For internal use only. Do not export" above to/fromNEString & listS
  • Loading branch information
JordanMartinez authored Nov 26, 2020
1 parent 10a3ddc commit e897bb7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
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 eqNonEmptyStringEq NonEmptyString
Expand Down

0 comments on commit e897bb7

Please sign in to comment.