@@ -20,9 +20,20 @@ module Streamly.Internal.Data.Serialize
20--------------------------------------------------------------------------------
20--------------------------------------------------------------------------------
22import Data.Proxy (Proxy)
22import Data.Proxy (Proxy)
23−import Streamly.Internal.Data.Serialize.Type
23+import Streamly.Internal.Data.Array (Array(..))
24+import Streamly.Internal.Data.Unbox (MutableByteArray(..))
25+import GHC.Exts (Int(..), sizeofByteArray#, unsafeCoerce#)
24import qualified Streamly.Internal.Data.Serialize.TH as Serialize
27import qualified Streamly.Internal.Data.Serialize.TH as Serialize
29+#if __GLASGOW_HASKELL__ >= 900
30+import GHC.Num.Integer (Integer(..))
32+import GHC.Integer.GMP.Internals (Integer(..), BigNat(..))
35+import Streamly.Internal.Data.Serialize.Type
26--------------------------------------------------------------------------------
37--------------------------------------------------------------------------------
27-- Common instances
38-- Common instances
28--------------------------------------------------------------------------------
39--------------------------------------------------------------------------------
@@ -52,3 +63,56 @@ $(Serialize.deriveSerialize ''Either)
52$(Serialize.deriveSerializeWith
63$(Serialize.deriveSerializeWith
53 (Serialize.defaultConfig {Serialize.unconstrained = ["t"]})
64 (Serialize.defaultConfig {Serialize.unconstrained = ["t"]})
67+--------------------------------------------------------------------------------
69+--------------------------------------------------------------------------------
76+$(Serialize.deriveSerialize ''LiftedInteger)
78+#if __GLASGOW_HASKELL__ >= 900
80+liftInteger :: Integer -> LiftedInteger
81+liftInteger (IS x) = LIS (I# x)
83+ LIP (Array (MutableByteArray (unsafeCoerce# x)) 0 (I# (sizeofByteArray# x)))
85+ LIN (Array (MutableByteArray (unsafeCoerce# x)) 0 (I# (sizeofByteArray# x)))
87+unliftInteger :: LiftedInteger -> Integer
88+unliftInteger (LIS (I# x)) = IS x
89+unliftInteger (LIP (Array (MutableByteArray x) _ _)) = IP (unsafeCoerce# x)
90+unliftInteger (LIN (Array (MutableByteArray x) _ _)) = IN (unsafeCoerce# x)
94+liftInteger :: Integer -> LiftedInteger
95+liftInteger (S# x) = LIS (I# x)
96+liftInteger (Jp# (BN# x)) =
97+ LIP (Array (MutableByteArray (unsafeCoerce# x)) 0 (I# (sizeofByteArray# x)))
98+liftInteger (Jn# (BN# x)) =
99+ LIN (Array (MutableByteArray (unsafeCoerce# x)) 0 (I# (sizeofByteArray# x)))
101+unliftInteger :: LiftedInteger -> Integer
102+unliftInteger (LIS (I# x)) = S# x
103+unliftInteger (LIP (Array (MutableByteArray x) _ _)) =
104+ Jp# (BN# (unsafeCoerce# x))
105+unliftInteger (LIN (Array (MutableByteArray x) _ _)) =
106+ Jn# (BN# (unsafeCoerce# x))
110+instance Serialize Integer where
112+ size i a = size i (liftInteger a)
114+ {-# INLINE deserialize #-}
115+ deserialize off arr end = fmap unliftInteger <$> deserialize off arr end
117+ {-# INLINE serialize #-}
118+ serialize off arr val = serialize off arr (liftInteger val)