on master help sign in
Add Serialize instance for tuple
commit8e04e4b9885d573bef9a3ccac0f891a35212aaf6 authorAdithya Kumar <adithya@composewell.com> dateSat 30 Sep 2023, 12:39 (3 years ago) parent763460b
2 files changed+19
core/src/Streamly/Internal/Data/Serialize/Type.hs+16 test/Streamly/Test/Data/Serialize.hs+3
core/src/Streamly/Internal/Data/Serialize/Type.hs+16
@@ -267,6 +267,22 @@ instance Serialize (Array a) where
267267 Unbox.putSliceUnsafe arrContents arrStart arr off1 arrLen
268268 pure (off1 + arrLen)
269269
270+instance (Serialize a, Serialize b) => Serialize (a, b) where
271+
272+ {-# INLINE size #-}
273+ size acc (a, b) = size (size acc a) b
274+
275+ {-# INLINE serialize #-}
276+ serialize off arr (a, b) = do
277+ off1 <- serialize off arr a
278+ serialize off1 arr b
279+
280+ {-# INLINE deserialize #-}
281+ deserialize off arr end = do
282+ (off1, a) <- deserialize off arr end
283+ (off2, b) <- deserialize off1 arr end
284+ pure (off2, (a, b))
285+
270286--------------------------------------------------------------------------------
271287-- High level functions
272288--------------------------------------------------------------------------------
test/Streamly/Test/Data/Serialize.hs+3
@@ -239,6 +239,9 @@ testCases = do
239239 prop "Integer"
240240 $ \(x :: Integer) -> roundtrip x
241241
242+ prop "([Integer], [Int])"
243+ $ \(x :: ([Integer], [Int])) -> roundtrip x
244+
242245 prop "Array Int"
243246 $ \(x :: [Int]) -> roundtrip (Array.fromList x)
244247