@@ -30,6 +30,7 @@ import Data.Binary.Get
3030import Data.Binary.Put
3131import Data.Bits
3232import Data.Word
33+ import Data.Proxy
3334#if !MIN_VERSION_base(4,11,0)
3435import Data.Monoid ((<>) )
3536#endif
@@ -84,8 +85,8 @@ instance Binary a => GBinaryGet (K1 i a) where
8485-- use two bytes, and so on till 2^64-1.
8586
8687#define GUARD(WORD) (size - 1) <= fromIntegral (maxBound :: WORD)
87- #define PUTSUM(WORD) GUARD(WORD) = putSum (0 :: WORD) (fromIntegral size)
88- #define GETSUM(WORD) GUARD(WORD) = (get :: Get WORD) >>= checkGetSum (fromIntegral size)
88+ #define PUTSUM(WORD) GUARD(WORD) = putSum (Proxy :: Proxy WORD) 0 size
89+ #define GETSUM(WORD) GUARD(WORD) = (get :: Get WORD) >>= checkGetSum size . fromIntegral
8990
9091instance ( GSumPut a , GSumPut b
9192 , SumSize a , SumSize b ) => GBinaryPut (a :+: b ) where
@@ -109,39 +110,41 @@ sizeError s size =
109110
110111------------------------------------------------------------------------
111112
112- checkGetSum :: (Ord word , Num word , Bits word , GSumGet f )
113- => word -> word -> Get ( f a )
114- checkGetSum size code | code < size = getSum code size
115- | otherwise = fail " Unknown encoding for constructor"
113+ checkGetSum :: (GSumGet f ) => Word64 -> Word64 -> Get ( f a )
114+ checkGetSum size code
115+ | code < size = getSum code size
116+ | otherwise = fail " Unknown encoding for constructor"
116117{-# INLINE checkGetSum #-}
117118
118119class GSumGet f where
119- getSum :: ( Ord word , Num word , Bits word ) => word -> word -> Get (f a )
120+ getSum :: Word64 -> Word64 -> Get (f a )
120121
121122class GSumPut f where
122- putSum :: (Num w , Bits w , Binary w ) => w -> w -> f a -> Put
123+ putSum :: (Binary word , Num word ) => Proxy word -> Word64 -> Word64 -> f a -> Put
123124
124125instance (GSumGet a , GSumGet b ) => GSumGet (a :+: b ) where
125- getSum ! code ! size | code < sizeL = L1 <$> getSum code sizeL
126- | otherwise = R1 <$> getSum (code - sizeL) sizeR
127- where
128- sizeL = size `shiftR` 1
129- sizeR = size - sizeL
126+ getSum ! code ! size
127+ | code < sizeL = L1 <$> getSum code sizeL
128+ | otherwise = R1 <$> getSum (code - sizeL) sizeR
129+ where
130+ sizeL = size `shiftR` 1
131+ sizeR = size - sizeL
130132 {-# INLINE getSum #-}
131133
132134instance (GSumPut a , GSumPut b ) => GSumPut (a :+: b ) where
133- putSum ! code ! size s = case s of
134- L1 x -> putSum code sizeL x
135- R1 x -> putSum (code + sizeL) sizeR x
136- where
137- sizeL = size `shiftR` 1
138- sizeR = size - sizeL
135+ putSum p ! code ! size s = case s of
136+ L1 x -> putSum p code sizeL x
137+ R1 x -> putSum p (code + sizeL) sizeR x
138+ where
139+ sizeL = size `shiftR` 1
140+ sizeR = size - sizeL
141+ {-# INLINE putSum #-}
139142
140143instance GBinaryGet a => GSumGet (C1 c a ) where
141144 getSum _ _ = gget
142145
143146instance GBinaryPut a => GSumPut (C1 c a ) where
144- putSum ! code _ x = put code <> gput x
147+ putSum (_ :: Proxy word ) ! code _ x = put ( fromIntegral code :: word ) <> gput x
145148
146149------------------------------------------------------------------------
147150
0 commit comments