From 8e04e4b9885d573bef9a3ccac0f891a35212aaf6 Mon Sep 17 00:00:00 2001 From: Adithya Kumar Date: Sat, 30 Sep 2023 12:39:47 +0530 Subject: [PATCH] Add Serialize instance for tuple --- .../src/Streamly/Internal/Data/Serialize/Type.hs | 16 ++++++++++++++++ test/Streamly/Test/Data/Serialize.hs | 3 +++ 2 files changed, 19 insertions(+) diff --git a/core/src/Streamly/Internal/Data/Serialize/Type.hs b/core/src/Streamly/Internal/Data/Serialize/Type.hs index 4b9eed560..2e4a2e472 100644 --- a/core/src/Streamly/Internal/Data/Serialize/Type.hs +++ b/core/src/Streamly/Internal/Data/Serialize/Type.hs @@ -267,6 +267,22 @@ instance Serialize (Array a) where Unbox.putSliceUnsafe arrContents arrStart arr off1 arrLen pure (off1 + arrLen) +instance (Serialize a, Serialize b) => Serialize (a, b) where + + {-# INLINE size #-} + size acc (a, b) = size (size acc a) b + + {-# INLINE serialize #-} + serialize off arr (a, b) = do + off1 <- serialize off arr a + serialize off1 arr b + + {-# INLINE deserialize #-} + deserialize off arr end = do + (off1, a) <- deserialize off arr end + (off2, b) <- deserialize off1 arr end + pure (off2, (a, b)) + -------------------------------------------------------------------------------- -- High level functions -------------------------------------------------------------------------------- diff --git a/test/Streamly/Test/Data/Serialize.hs b/test/Streamly/Test/Data/Serialize.hs index 7f0ce79e9..372bed79f 100644 --- a/test/Streamly/Test/Data/Serialize.hs +++ b/test/Streamly/Test/Data/Serialize.hs @@ -239,6 +239,9 @@ testCases = do prop "Integer" $ \(x :: Integer) -> roundtrip x + prop "([Integer], [Int])" + $ \(x :: ([Integer], [Int])) -> roundtrip x + prop "Array Int" $ \(x :: [Int]) -> roundtrip (Array.fromList x)