Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion scientific.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ flag integer-simple
description: Use the integer-simple package instead of integer-gmp
default: False

flag hashable
description: Add dependency on `hashable`, and add `Hashable Scientific` instance.
default: True
manual: True

library
exposed-modules:
Data.ByteString.Builder.Scientific
Expand All @@ -93,7 +98,6 @@ library
, binary >=0.5.1
, containers >=0.4.2.1
, deepseq >=1.3.0.0
, hashable >=1.2.7.0
, integer-logarithms >=1.0.3.1
, primitive >=0.7.1.0
, text >=1.2.3.0
Expand All @@ -106,6 +110,10 @@ library
else
build-depends: bytestring >=0.10.4 && <0.12

if flag(hashable)
cpp-options: -DHASHABLE
build-depends: hashable >=1.2.7.0

if impl(ghc >=9.0)
build-depends: base >=4.15

Expand Down
4 changes: 4 additions & 0 deletions src/Data/Scientific/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ import Control.DeepSeq (NFData, rnf)
import Data.Binary (Binary, get, put)
import Data.Char (intToDigit, ord)
import Data.Data (Data)
#ifdef HASHABLE
import Data.Hashable (Hashable(..))
#endif
import Data.Int (Int8, Int16, Int32, Int64)
import qualified Data.Map as M (Map, empty, insert, lookup)
import Data.Ratio ((%), numerator, denominator)
Expand Down Expand Up @@ -160,11 +162,13 @@ unsafeScientificFromNonNormalized c z e = Scientific (c `quotInteger` magnitude
instance NFData Scientific where
rnf (Scientific _ _) = ()

#ifdef HASHABLE
-- | A hash can be safely calculated from a @Scientific@. No magnitude @10^e@ is
-- calculated so there's no risk of a blowup in space or time when hashing
-- scientific numbers coming from untrusted sources.
instance Hashable Scientific where
hashWithSalt salt (Scientific c e) = salt `hashWithSalt` c `hashWithSalt` e
#endif

-- | Note that in the future I intend to change the type of the 'base10Exponent'
-- from @Int@ to @Integer@. To be forward compatible the @Binary@ instance
Expand Down