Skip to content

Commit

Permalink
Restrict when to create orphan Hashable ByteArray (#2485)
Browse files Browse the repository at this point in the history
This is a backport of a combination of PR's #2448, #2449 and #2460.

For some combinations of `hashable` and `primitive`, there is already a
`Hashable ByteArray` instance and we should not create one.
  • Loading branch information
DigitalBrains1 committed Jun 2, 2023
1 parent 3646f04 commit dbd3f7c
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions clash-lib/src/Data/Primitive/ByteArray/Extra.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,34 @@

module Data.Primitive.ByteArray.Extra where

import Data.Binary (Binary(..))
import Data.Primitive.ByteArray (ByteArray)
import GHC.Exts (IsList(..))

#if !MIN_VERSION_primitive(0,7,1)
import Control.DeepSeq (NFData(..))
#endif
import Data.Binary (Binary(..))

-- hashable 1.4.2 defines Hashable for Data.Array.Byte.ByteArray, either from
-- base or from the data-array-byte compat package for GHC < 9.4.
-- primitive 0.8.0.0 re-exports this ByteArray.
#if !MIN_VERSION_primitive(0,8,0)
-- In primitive < 0.8.0.0, its ByteArray is a distinct type from
-- Data.Array.Byte.ByteArray (insofar as the latter even exists).
#define DEFINE_HASHABLE_BYTEARRAY
#elif !MIN_VERSION_hashable(1,4,1)
-- hashable < 1.4.1 doesn't define a Hashable ByteArray instance at all.
#define DEFINE_HASHABLE_BYTEARRAY
#elif !MIN_VERSION_hashable(1,4,2)
-- hashable 1.4.1 defines hashable for the ByteArray added to base 4.17.
#if !MIN_VERSION_base(4,17,0)
#define DEFINE_HASHABLE_BYTEARRAY
#endif
#endif

#ifdef DEFINE_HASHABLE_BYTEARRAY
import Data.Hashable (Hashable(..))
import Data.Primitive.ByteArray (ByteArray)
import GHC.Exts (IsList(..))
#endif

#if !MIN_VERSION_primitive(0,7,1)
instance NFData ByteArray where
Expand All @@ -20,6 +41,7 @@ instance Binary ByteArray where
get = fmap fromList get
put = put . toList

#ifdef DEFINE_HASHABLE_BYTEARRAY
instance Hashable ByteArray where
hashWithSalt salt = hashWithSalt salt . toList

#endif

0 comments on commit dbd3f7c

Please sign in to comment.