@@ -362,34 +362,43 @@ deriveSerializeInternal preds headTy cons = do
363 return [plainInstanceD preds (AppT (ConT ''Serialize) headTy) methods]
363 return [plainInstanceD preds (AppT (ConT ''Serialize) headTy) methods]
365−-- | Template haskell helper to create instances of 'Serialize' automatically.
365+-- | Similar to 'deriveSerialize,' but with the ability to specify which type
366+-- variables should not be subjected to the 'Serialize' constraint and how to
367+-- replace these variables when defining the 'Serialize' instance for the
368+-- primary data type.
367-- Consider the datatype:
370-- Consider the datatype:
369−-- data CustomDataType a b c = ...
372+-- data CustomDataType a f c = CDT a (f Int)
372−-- Usage: @$(deriveSerialize ''CustomDataType)@
375+-- Usage: @$(deriveSerializeWith ["c", "f"] [("f", ConT ''Identity)] ''CustomDataType)@
374−-- Note: All type variables automatcally get an "Serialize" constraint.
375−-- The derived code will look like the following,
377−-- instance (Serialize a, Serialize b, Serialize c) => Serialize (CustomDataType a b c) where
378+-- instance (Serialize a) => Serialize (CustomDataType a Identity c) where
381−-- To control which type variables get the Serialize constraint, use
382+-- In the above example, 'Serialize' constraint is omitted for @f@ and @c@, and
382−-- 'deriveSerializeWith'.
383+-- @f@ is replaced with 'Identity' when defining the 'Serialize' instance for
383−deriveSerialize :: Name -> Q [Dec]
384+-- the primary data type.
384−deriveSerialize name = do
385+deriveSerializeWith :: [String] -> [(String, Type)] -> Name -> Q [Dec]
386+deriveSerializeWith ignoring substitutions name = do
385 dt <- reifyDataType name
387 dt <- reifyDataType name
386− let preds = map (unboxPred . VarT) (dtTvs dt)
388+ let preds = map (unboxPred . VarT) (filterOutVars (dtTvs dt))
387− headTy = appsT (ConT name) (map VarT (dtTvs dt))
389+ headTy = appsT (ConT name) (map substituteVar (dtTvs dt))
388 cons = dtCons dt
390 cons = dtCons dt
389 deriveSerializeInternal preds headTy cons
391 deriveSerializeInternal preds headTy cons
396+ map mkName $ filter (not . flip elem ignoring) $ map nameBase vs
398+ case lookup (nameBase v) substitutions of
393 unboxPred ty =
402 unboxPred ty =
394#if MIN_VERSION_template_haskell(2,10,0)
403#if MIN_VERSION_template_haskell(2,10,0)
395 AppT (ConT ''Serialize) ty
404 AppT (ConT ''Serialize) ty
@@ -397,34 +406,25 @@ deriveSerialize name = do
397 ClassP ''Serialize [ty]
406 ClassP ''Serialize [ty]
400−-- | Like 'deriveSerialize' but control which types variables get the 'Serialize'
409+-- | Template haskell helper to create instances of 'Serialize' automatically.
403-- Consider the datatype:
411-- Consider the datatype:
405-- data CustomDataType a b c = ...
413-- data CustomDataType a b c = ...
408−-- Usage: @$(deriveSerializeWith ["a", "c"] ''CustomDataType)@
416+-- Usage: @$(deriveSerialize ''CustomDataType)@
418+-- Note: All type variables automatcally get an "Serialize" constraint.
419+-- The derived code will look like the following,
411−-- instance (Serialize a, Serialize c) => Serialize (CustomDataType a b c) where
421+-- instance (Serialize a, Serialize b, Serialize c) => Serialize (CustomDataType a b c) where
425+-- To control which type variables don't get the Serialize constraint, use
426+-- 'deriveSerializeWith'.
415−deriveSerializeWith :: [String] -> Name -> Q [Dec]
428+-- >>> deriveSerialize = deriveSerializeWith [] []
416−deriveSerializeWith vars name = do
429+deriveSerialize :: Name -> Q [Dec]
417− dt <- reifyDataType name
430+deriveSerialize name = deriveSerializeWith [] [] name
418− let preds = map (unboxPred . VarT) (fmap mkName vars)
419− headTy = appsT (ConT name) (map VarT (dtTvs dt))
421− deriveSerializeInternal preds headTy cons
426−#if MIN_VERSION_template_haskell(2,10,0)
427− AppT (ConT ''Serialize) ty
429− ClassP ''Serialize [ty]