Skip to content

Commit e897bb7

Browse files
Use coerce instead of unsafeCoerce where appropriate (#130)
* 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
1 parent 10a3ddc commit e897bb7

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

src/Data/String/NonEmpty/CodePoints.purs

+7-5
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,21 @@ import Data.Semigroup.Foldable (class Foldable1)
3333
import Data.Semigroup.Foldable as F1
3434
import Data.String.CodePoints (CodePoint)
3535
import Data.String.CodePoints as CP
36-
import Data.String.NonEmpty.Internal (NonEmptyString, fromString)
36+
import Data.String.NonEmpty.Internal (NonEmptyString(..), fromString)
3737
import Data.String.Pattern (Pattern)
3838
import Partial.Unsafe (unsafePartial)
39-
import Unsafe.Coerce (unsafeCoerce)
4039

40+
-- For internal use only. Do not export.
4141
toNonEmptyString :: String -> NonEmptyString
42-
toNonEmptyString = unsafeCoerce
42+
toNonEmptyString = NonEmptyString
4343

44+
-- For internal use only. Do not export.
4445
fromNonEmptyString :: NonEmptyString -> String
45-
fromNonEmptyString = unsafeCoerce
46+
fromNonEmptyString (NonEmptyString s) = s
4647

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

5052
fromCodePointArray :: Array CodePoint -> Maybe NonEmptyString
5153
fromCodePointArray = case _ of

src/Data/String/NonEmpty/CodeUnits.purs

+7-4
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,23 @@ import Data.Maybe (Maybe(..), fromJust)
3333
import Data.Semigroup.Foldable (class Foldable1)
3434
import Data.Semigroup.Foldable as F1
3535
import Data.String.CodeUnits as CU
36-
import Data.String.NonEmpty.Internal (NonEmptyString, fromString)
36+
import Data.String.NonEmpty.Internal (NonEmptyString(..), fromString)
3737
import Data.String.Pattern (Pattern)
3838
import Data.String.Unsafe as U
3939
import Partial.Unsafe (unsafePartial)
4040
import Unsafe.Coerce (unsafeCoerce)
4141

42+
-- For internal use only. Do not export.
4243
toNonEmptyString :: String -> NonEmptyString
43-
toNonEmptyString = unsafeCoerce
44+
toNonEmptyString = NonEmptyString
4445

46+
-- For internal use only. Do not export.
4547
fromNonEmptyString :: NonEmptyString -> String
46-
fromNonEmptyString = unsafeCoerce
48+
fromNonEmptyString (NonEmptyString s) = s
4749

50+
-- For internal use only. Do not export.
4851
liftS :: forall r. (String -> r) -> NonEmptyString -> r
49-
liftS = unsafeCoerce
52+
liftS f (NonEmptyString s) = f s
5053

5154
-- | Creates a `NonEmptyString` from a character array `String`, returning
5255
-- | `Nothing` if the input is empty.

src/Data/String/NonEmpty/Internal.purs

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
-- | While most of the code in this module is safe, this module does
2+
-- | export a few partial functions and the `NonEmptyString` constructor.
3+
-- | While the partial functions are obvious from the `Partial` constraint in
4+
-- | their type signature, the `NonEmptyString` constructor can be overlooked
5+
-- | when searching for issues in one's code. See the constructor's
6+
-- | documentation for more information.
17
module Data.String.NonEmpty.Internal where
28

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

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

1830
derive newtype instance eqNonEmptyStringEq NonEmptyString

0 commit comments

Comments
 (0)