Add Serialize instance for (Array a)

This commit is contained in:
Adithya Kumar 2023-09-10 12:33:17 +05:30
parent b5667ae59f
commit 3043a3f699
2 changed files with 23 additions and 0 deletions

View File

@ -39,6 +39,7 @@ import GHC.Stable (StablePtr(..))
import qualified Streamly.Internal.Data.Unbox as Unbox
import qualified Streamly.Internal.Data.Array as Array
import qualified Streamly.Internal.Data.MutArray as MutArray
import GHC.Exts
@ -247,6 +248,25 @@ instance forall a. Serialize a => Serialize [a] where
pokeList o1 xs
pokeList off1 val
instance Serialize (Array a) where
{-# INLINE size #-}
size i (Array {..}) = i + (arrEnd - arrStart) + 8
{-# INLINE deserialize #-}
deserialize off arr end = do
(off1, byteLen) <- deserialize off arr end :: IO (Int, Int)
let off2 = off1 + byteLen
let slice = MutArray.MutArray arr off1 off2 off2
newArr <- MutArray.clone slice
pure (off2, Array.unsafeFreeze newArr)
{-# INLINE serialize #-}
serialize off arr (Array {..}) = do
let arrLen = arrEnd - arrStart
off1 <- serialize off arr arrLen
Unbox.putSliceUnsafe arrContents arrStart arr off1 arrLen
pure (off1 + arrLen)
--------------------------------------------------------------------------------
-- High level functions
--------------------------------------------------------------------------------

View File

@ -170,6 +170,9 @@ testCases = do
it "HigherOrderType"
$ roundtrip $ HigherOrderType (Identity 5) (Identity 'e')
prop "Array Int"
$ \(x :: [Int]) -> roundtrip (Array.fromList x)
limitQC
$ prop "CustomDatatype"
$ \(x :: CustomDatatype) -> roundtrip x