hashable-generics 1.1.4 → 1.1.5
raw patch · 3 files changed
+18/−14 lines, 3 files
Files
- Data/Hashable/Generic.hs +15/−11
- hashable-generics.cabal +1/−1
- test/Suite.hs +2/−2
Data/Hashable/Generic.hs view
@@ -1,9 +1,6 @@ {-# LANGUAGE TypeOperators, FlexibleContexts, BangPatterns #-} -- | Stability: stable -- Portability: GHC------ NOTE: This module exports the Hashable class for convenience. If this--- becomes a problem, just use a qualified import. module Data.Hashable.Generic ( gHashWithSalt , Hashable(..) ) where@@ -39,24 +36,33 @@ -- > type Name = String -- > newtype AccountId = AccountId Int -- >+-- > -- Note: Even though gHashWithSalt could be curried, we explicitly list+-- > -- the parameters. If you don't do this, GHC will not inline the+-- > -- definition of gHashWithSalt, and the performance will not match+-- > -- a non-generic implementation. If you use this method, the generic+-- > -- hashWithSalt will generate the exact same code as a hand-rolled+-- > -- one. -- > instance Hashable AccountId--- > instance Hashable Foo where hashWithSalt = gHashWithSalt+-- > instance Hashable Foo where+-- > hashWithSalt s x = gHashWithSalt s x -- > -- > -- recursive list-like type -- > data N = Z | S N deriving Generic -- >--- > instance Hashable N where hashWithSalt = gHashWithSalt+-- > instance Hashable N where+-- > hashWithSalt s x = gHashWithSalt s x -- > -- > -- parametric and recursive type -- > data Bar a = Bar0 | Bar1 a | Bar2 (Bar a) -- > deriving Generic -- >--- > instance Hashable a => Hashable (Bar a) where hashWithSalt = gHashWithSalt+-- > instance Hashable a => Hashable (Bar a) where+-- > hashWithSalt s x = gHashWithSalt s x -- -- Note: The 'GHashable' type-class showing up in the type-signature is--- used internally and not exported on purpose currently+-- used internally and not exported on purpose. gHashWithSalt :: (Generic a, GHashable (Rep a)) => Int -> a -> Int-gHashWithSalt salt x = gHashWithSalt_ salt $ from x+gHashWithSalt = gHashWithSalt_ salt $ from x {-# INLINE gHashWithSalt #-} -- | A value with bit pattern (01)* (or 5* in hexa), for any size of Int.@@ -73,7 +79,7 @@ gHashWithSalt_ :: Int -> f a -> Int instance GHashable U1 where- gHashWithSalt_ !salt U1 = hashWithSalt salt ()+ gHashWithSalt_ salt _ = hashWithSalt salt () {-# INLINE gHashWithSalt_ #-} instance Hashable a => GHashable (K1 i a) where@@ -88,8 +94,6 @@ gHashWithSalt_ !salt (x :*: y) = gHashWithSalt_ (gHashWithSalt_ salt x) y {-# INLINE gHashWithSalt_ #-} --- This instance is suboptimal (with the salt+1 hackery). Is there a better way--- to be doing this so that both choices can be unique? instance (GHashable a, GHashable b) => GHashable (a :+: b) where gHashWithSalt_ !salt (L1 x) = gHashWithSalt_ (salt `combine` 0) x gHashWithSalt_ !salt (R1 x) = gHashWithSalt_ (salt `combine` distinguisher) x
hashable-generics.cabal view
@@ -1,5 +1,5 @@ name: hashable-generics-version: 1.1.4+version: 1.1.5 synopsis: Automatically generates Hashable instances with GHC.Generics. description: This package provides a "GHC.Generics"-based 'Data.Hashable.Generic.gHashWithSalt'
test/Suite.hs view
@@ -73,7 +73,7 @@ data NB = ZB | SB NB instance Hashable NA where- hashWithSalt = gHashWithSalt+ hashWithSalt s x = gHashWithSalt s x instance Hashable NB where hashWithSalt !salt ZB = hashWithSalt (salt `combine` 0) ()@@ -106,7 +106,7 @@ ] instance Hashable a => Hashable (BarA a) where- hashWithSalt = gHashWithSalt+ hashWithSalt s x = gHashWithSalt s x instance Hashable a => Hashable (BarB a) where hashWithSalt !salt BarB0 = hashWithSalt (salt `combine` 0) ()