on master help sign in
Fix serialization derivation for unit constructor
12d8502Adithya Kumar
core/src/Streamly/Internal/Data/Array.hs+10
@@ -564,6 +564,12 @@ encodeAs ps a =
564 assertM(len == off)
564 assertM(len == off)
565 pure $ Array mbarr 0 off
565 pure $ Array mbarr 0 off
566
566
567+-- XXX The "Encoded Equivality" property is not (cannot be) true for Unbox.
568+
569+-- |
570+-- Properties:
571+-- 1. Identity: @deserialize . serialize == id@
572+-- 2. Encoded Equivality: @serialize a == serialize a@
567{-# INLINE serialize #-}
573{-# INLINE serialize #-}
568serialize :: Serialize a => a -> Array Word8
574serialize :: Serialize a => a -> Array Word8
569serialize = encodeAs Unpinned
575serialize = encodeAs Unpinned
@@ -571,6 +577,10 @@ serialize = encodeAs Unpinned
571-- | Serialize a Haskell type to a pinned byte array. The array is allocated
577-- | Serialize a Haskell type to a pinned byte array. The array is allocated
572-- using pinned memory so that it can be used directly in OS APIs for writing
578-- using pinned memory so that it can be used directly in OS APIs for writing
573-- to file or sending over the network.
579-- to file or sending over the network.
580+--
581+-- Properties:
582+-- 1. Identity: @deserialize . pinnedSerialize == id@
583+-- 2. Encoded Equivality: @pinnedSerialize a == serialize a@
574{-# INLINE pinnedSerialize #-}
584{-# INLINE pinnedSerialize #-}
575pinnedSerialize :: Serialize a => a -> Array Word8
585pinnedSerialize :: Serialize a => a -> Array Word8
576pinnedSerialize = encodeAs Pinned
586pinnedSerialize = encodeAs Pinned
core/src/Streamly/Internal/Data/Serialize/TH.hs+4−2
@@ -217,6 +217,7 @@ mkDeserializeExpr True False headTy tyOfTy =
217mkDeserializeExpr False False headTy tyOfTy =
217mkDeserializeExpr False False headTy tyOfTy =
218 case tyOfTy of
218 case tyOfTy of
219 -- Unit constructor
219 -- Unit constructor
220+ -- XXX Should we peek and check if the byte value is 0?
220 UnitType cname ->
221 UnitType cname ->
221 [|pure ($(varE _initialOffset) + 1, $(conE cname))|]
222 [|pure ($(varE _initialOffset) + 1, $(conE cname))|]
222 -- Product type
223 -- Product type
@@ -341,7 +342,8 @@ mkSerializeExpr True False tyOfTy =
341mkSerializeExpr False False tyOfTy =
342mkSerializeExpr False False tyOfTy =
342 case tyOfTy of
343 case tyOfTy of
343 -- Unit type
344 -- Unit type
345+ UnitType _ ->
344− UnitType _ -> [|pure ($(varE _initialOffset) + 1)|]
346+ [|serializeAt $(varE _initialOffset) $(varE _arr) (0 :: Word8)|]
345 -- Product type
347 -- Product type
346 (TheType (SimpleDataCon cname fields)) ->
348 (TheType (SimpleDataCon cname fields)) ->
347 letE
349 letE
@@ -395,7 +397,7 @@ mkSerializeDec (SerializeConfig {..}) headTy cons = do
395 'serializeAt
397 'serializeAt
396 [ Clause
398 [ Clause
397 (if isUnitType cons && not cfgConstructorTagAsString
399 (if isUnitType cons && not cfgConstructorTagAsString
398− then [VarP _initialOffset, WildP, WildP]
400+ then [VarP _initialOffset, VarP _arr, WildP]
399 else [VarP _initialOffset, VarP _arr, VarP _val])
401 else [VarP _initialOffset, VarP _arr, VarP _val])
400 (NormalB pokeMethod)
402 (NormalB pokeMethod)
401 []
403 []
core/src/Streamly/Internal/Data/Unbox/TH.hs+4−1
@@ -241,6 +241,7 @@ mkSizeOfExpr headTy constructors =
241mkPeekExprOne :: Int -> DataCon -> Q Exp
241mkPeekExprOne :: Int -> DataCon -> Q Exp
242mkPeekExprOne tagSize (DataCon cname _ _ fields) =
242mkPeekExprOne tagSize (DataCon cname _ _ fields) =
243 case fields of
243 case fields of
244+ -- XXX Should we peek and check if the byte value is 0?
244 [] -> [|pure $(conE cname)|]
245 [] -> [|pure $(conE cname)|]
245 _ ->
246 _ ->
246 letE
247 letE
@@ -336,7 +337,9 @@ mkPokeExpr headTy cons =
336 [|error
337 [|error
337 ("Attempting to poke type with no constructors (" ++
338 ("Attempting to poke type with no constructors (" ++
338 $(lift (pprint headTy)) ++ ")")|]
339 $(lift (pprint headTy)) ++ ")")|]
340+ -- XXX We don't gaurentee encoded equivalilty for Unbox. Does it still
341+ -- make sense to encode a default value for unit constructor?
339− [(DataCon _ _ _ [])] -> [|pure ()|]
342+ [(DataCon _ _ _ [])] -> [|pure ()|] -- mkPokeExprTag ''Word8 0
340 [(DataCon cname _ _ fields)] ->
343 [(DataCon cname _ _ fields)] ->
341 caseE
344 caseE
342 (varE _val)
345 (varE _val)