@@ -10,8 +10,8 @@
11module Streamly.Internal.Data.Serialize.TH
11module Streamly.Internal.Data.Serialize.TH
12 ( deriveSerialize
12 ( deriveSerialize
13− , SerializeTHConfig(..)
13+ , Config(..)
14− , defaultSerializeTHConfig
14+ , defaultConfig
15 , deriveSerializeWith
15 , deriveSerializeWith
@@ -324,7 +324,7 @@ mkSerializeExpr tyOfTy =
326-- $(deriveSerializeInternal
326-- $(deriveSerializeInternal
327−-- defaultSerializeTHConfig
327+-- defaultConfig
328-- [AppT (ConT ''Serialize) (VarT (mkName "b"))]
328-- [AppT (ConT ''Serialize) (VarT (mkName "b"))]
330-- (AppT (ConT ''CustomDataType) (VarT (mkName "a")))
330-- (AppT (ConT ''CustomDataType) (VarT (mkName "a")))
@@ -339,8 +339,8 @@ mkSerializeExpr tyOfTy =
341deriveSerializeInternal ::
341deriveSerializeInternal ::
342− SerializeTHConfig -> Cxt -> Type -> [DataCon] -> Q [Dec]
342+ Config -> Cxt -> Type -> [DataCon] -> Q [Dec]
343−deriveSerializeInternal (SerializeTHConfig{..}) preds headTy cons = do
343+deriveSerializeInternal (Config{..}) preds headTy cons = do
344 sizeOfMethod <- mkSizeOfExpr (typeOfType headTy cons)
344 sizeOfMethod <- mkSizeOfExpr (typeOfType headTy cons)
345 peekMethod <- mkDeserializeExpr headTy (typeOfType headTy cons)
345 peekMethod <- mkDeserializeExpr headTy (typeOfType headTy cons)
346 pokeMethod <- mkSerializeExpr (typeOfType headTy cons)
346 pokeMethod <- mkSerializeExpr (typeOfType headTy cons)
@@ -373,9 +373,9 @@ deriveSerializeInternal (SerializeTHConfig{..}) preds headTy cons = do
373 return [plainInstanceD preds (AppT (ConT ''Serialize) headTy) methods]
373 return [plainInstanceD preds (AppT (ConT ''Serialize) headTy) methods]
375-- | Config to control how the 'Serialize' instance is generated.
375-- | Config to control how the 'Serialize' instance is generated.
376−data SerializeTHConfig =
376+data Config =
377− SerializeTHConfig
377+ Config
378− { unconstrainedTypeVars :: [String]
378+ { unconstrained :: [String]
379 -- ^ Type variables that should not have the 'Serialize' constraint.
379 -- ^ Type variables that should not have the 'Serialize' constraint.
@@ -383,26 +383,26 @@ data SerializeTHConfig =
386− -- conf = defaultConf {unconstrainedTypeVars = ["b", "d"]}
386+ -- conf = defaultConf {unconstrained = ["b", "d"]}
387− -- $(deriveSerializeWith conf ''CustomDataType)
387+ -- \$(deriveSerializeWith conf ''CustomDataType)
391 -- instance (Serialize a, Serialize c) => Serialize (CustomDataType a b c d) where
391 -- instance (Serialize a, Serialize c) => Serialize (CustomDataType a b c d) where
394− , typeVarSubstitutions :: [(String, Type)]
394+ , specializations :: [(String, Type)]
395− -- ^ Substitute the type variable with the given type. All type
395+ -- ^ Specialize the type variable with the given type. All type
396 -- variables listed here will not have the 'Serialize' constriant as
396 -- variables listed here will not have the 'Serialize' constriant as
397− -- they are replaced.
397+ -- they are specialized.
400 -- data CustomDataType f a = CustomDataType (f a)
400 -- data CustomDataType f a = CustomDataType (f a)
404− -- conf = defaultConf {typeVarSubstitutions = [("f", ''Identity)]}
404+ -- conf = defaultConf {specializations = [("f", ''Identity)]}
405− -- $(deriveSerializeWith conf ''CustomDataType)
405+ -- \$(deriveSerializeWith conf ''CustomDataType)
@@ -418,20 +418,22 @@ data SerializeTHConfig =
418 , inlineDeserialize :: Inline
418 , inlineDeserialize :: Inline
419 -- ^ Inline value for 'deserialize'. Default is Inline.
419 -- ^ Inline value for 'deserialize'. Default is Inline.
420 , constructorTagAsString :: Bool
420 , constructorTagAsString :: Bool
421+ -- ^ __Experimental__
421− -- ^ If True, encode constructors as Latin-1 byte sequence. This
423+ -- If True, encode constructors using the constructor names as Latin-1
422− -- allows addition, removal, and reordering of constructors. If False
424+ -- byte sequence.
423− -- encode them as numbers. The default value is 'False'.
424 , recordSyntaxWithHeader :: Bool
425 , recordSyntaxWithHeader :: Bool
425− -- ^ If True, constructors with record syntax will be encoded in a
426+ -- ^ __Experimental__
426− -- more compatible way. Allows addition, removal, and reordering of
427+ --
427− -- fields. The default value is 'False'.
428+ -- If True, encode the keys of the record as a header and then
429+ -- serialize the data.
430−defaultSerializeTHConfig :: SerializeTHConfig
432+defaultConfig :: Config
431−defaultSerializeTHConfig =
433+defaultConfig =
432− SerializeTHConfig
434+ Config
433− { unconstrainedTypeVars = []
435+ { unconstrained = []
434− , typeVarSubstitutions = []
436+ , specializations = []
435 , inlineSize = Inline
437 , inlineSize = Inline
436 , inlineSerialize = Inline
438 , inlineSerialize = Inline
437 , inlineDeserialize = Inline
439 , inlineDeserialize = Inline
@@ -439,12 +441,12 @@ defaultSerializeTHConfig =
439 , recordSyntaxWithHeader = False
441 , recordSyntaxWithHeader = False
442−-- | Similar to 'deriveSerialize,' but take a 'SerializeTHConfig' to control how
444+-- | Similar to 'deriveSerialize,' but take a 'Config' to control how
443-- the instance is generated.
445-- the instance is generated.
445-- Usage: @$(deriveSerializeWith config ''CustomDataType)@
447-- Usage: @$(deriveSerializeWith config ''CustomDataType)@
446−deriveSerializeWith :: SerializeTHConfig -> Name -> Q [Dec]
448+deriveSerializeWith :: Config -> Name -> Q [Dec]
447−deriveSerializeWith conf@(SerializeTHConfig {..}) name = do
449+deriveSerializeWith conf@(Config {..}) name = do
448 dt <- reifyDataType name
450 dt <- reifyDataType name
449 let preds = map (unboxPred . VarT) (filterOutVars (dtTvs dt))
451 let preds = map (unboxPred . VarT) (filterOutVars (dtTvs dt))
450 headTy = appsT (ConT name) (map substituteVar (dtTvs dt))
452 headTy = appsT (ConT name) (map substituteVar (dtTvs dt))
@@ -453,13 +455,13 @@ deriveSerializeWith conf@(SerializeTHConfig {..}) name = do
455 allUnconstrainedTypeVars =
457 allUnconstrainedTypeVars =
456− unconstrainedTypeVars ++ map fst typeVarSubstitutions
458+ unconstrained ++ map fst specializations
457 filterOutVars vs =
459 filterOutVars vs =
458 map mkName
460 map mkName
459 $ filter (not . flip elem allUnconstrainedTypeVars)
461 $ filter (not . flip elem allUnconstrainedTypeVars)
460 $ map nameBase vs
462 $ map nameBase vs
461 substituteVar v =
463 substituteVar v =
462− case lookup (nameBase v) typeVarSubstitutions of
464+ case lookup (nameBase v) specializations of
463 Nothing -> VarT v
465 Nothing -> VarT v
464 Just ty -> ty
466 Just ty -> ty
@@ -489,6 +491,7 @@ deriveSerializeWith conf@(SerializeTHConfig {..}) name = do
489-- To control which type variables don't get the Serialize constraint, use
491-- To control which type variables don't get the Serialize constraint, use
490-- 'deriveSerializeWith'.
492-- 'deriveSerializeWith'.
494+-- >>> import qualified Streamly.Internal.Data.Serialize.TH as Serialize
492−-- >>> deriveSerialize = deriveSerializeWith 'defaultSerializeTHConfig'
495+-- >>> deriveSerialize = Serialize.deriveSerializeWith Serialize.defaultConfig
493deriveSerialize :: Name -> Q [Dec]
496deriveSerialize :: Name -> Q [Dec]
494−deriveSerialize name = deriveSerializeWith defaultSerializeTHConfig name
497+deriveSerialize name = deriveSerializeWith defaultConfig name