on master help sign in
Move Config to the top of the module in Serialize.TH
00990ebAdithya Kumar
core/src/Streamly/Internal/Data/Serialize/TH.hs+73−69
@@ -39,6 +39,79 @@ import Streamly.Internal.Data.Unbox.TH
39 , reifyDataType
39 , reifyDataType
40 )
40 )
41
41
42+--------------------------------------------------------------------------------
43+-- Config
44+--------------------------------------------------------------------------------
45+
46+-- | Config to control how the 'Serialize' instance is generated.
47+data Config =
48+ Config
49+ { unconstrained :: [String]
50+ -- ^ Type variables that should not have the 'Serialize' constraint.
51+ --
52+ -- @
53+ -- data CustomDataType a b c d = CustomDataType a c
54+ -- @
55+ --
56+ -- @
57+ -- conf = defaultConf {unconstrained = ["b", "d"]}
58+ -- \$(deriveSerializeWith conf ''CustomDataType)
59+ -- @
60+ --
61+ -- @
62+ -- instance (Serialize a, Serialize c) => Serialize (CustomDataType a b c d) where
63+ -- ...
64+ -- @
65+ , specializations :: [(String, Type)]
66+ -- ^ Specialize the type variable with the given type. All type
67+ -- variables listed here will not have the 'Serialize' constriant as
68+ -- they are specialized.
69+ --
70+ -- @
71+ -- data CustomDataType f a = CustomDataType (f a)
72+ -- @
73+ --
74+ -- @
75+ -- conf = defaultConf {specializations = [("f", ''Identity)]}
76+ -- \$(deriveSerializeWith conf ''CustomDataType)
77+ -- @
78+ --
79+ -- @
80+ -- instance (Serialize a) => Serialize (CustomDataType Identity a) where
81+ -- ...
82+ -- @
83+ --
84+ -- @f@ is replaced with 'Identity' and becomes unconstrained.
85+ , inlineSize :: Inline
86+ -- ^ Inline value for 'size'. Default is Inline.
87+ , inlineSerialize :: Inline
88+ -- ^ Inline value for 'serialize'. Default is Inline.
89+ , inlineDeserialize :: Inline
90+ -- ^ Inline value for 'deserialize'. Default is Inline.
91+ , constructorTagAsString :: Bool
92+ -- ^ __Experimental__
93+ --
94+ -- If True, encode constructors using the constructor names as Latin-1
95+ -- byte sequence.
96+ , recordSyntaxWithHeader :: Bool
97+ -- ^ __Experimental__
98+ --
99+ -- If True, encode the keys of the record as a header and then
100+ -- serialize the data.
101+ }
102+
103+defaultConfig :: Config
104+defaultConfig =
105+ Config
106+ { unconstrained = []
107+ , specializations = []
108+ , inlineSize = Inline
109+ , inlineSerialize = Inline
110+ , inlineDeserialize = Inline
111+ , constructorTagAsString = False
112+ , recordSyntaxWithHeader = False
113+ }
114+
42--------------------------------------------------------------------------------
115--------------------------------------------------------------------------------
43-- Helpers
116-- Helpers
44--------------------------------------------------------------------------------
117--------------------------------------------------------------------------------
@@ -633,75 +706,6 @@ deriveSerializeInternal (Config{..}) preds headTy cons = do
633 ]
706 ]
634 return [plainInstanceD preds (AppT (ConT ''Serialize) headTy) methods]
707 return [plainInstanceD preds (AppT (ConT ''Serialize) headTy) methods]
635
708
636−-- | Config to control how the 'Serialize' instance is generated.
637−data Config =
638− Config
639− { unconstrained :: [String]
640− -- ^ Type variables that should not have the 'Serialize' constraint.
641− --
642− -- @
643− -- data CustomDataType a b c d = CustomDataType a c
644− -- @
645− --
646− -- @
647− -- conf = defaultConf {unconstrained = ["b", "d"]}
648− -- \$(deriveSerializeWith conf ''CustomDataType)
649− -- @
650− --
651− -- @
652− -- instance (Serialize a, Serialize c) => Serialize (CustomDataType a b c d) where
653− -- ...
654− -- @
655− , specializations :: [(String, Type)]
656− -- ^ Specialize the type variable with the given type. All type
657− -- variables listed here will not have the 'Serialize' constriant as
658− -- they are specialized.
659− --
660− -- @
661− -- data CustomDataType f a = CustomDataType (f a)
662− -- @
663− --
664− -- @
665− -- conf = defaultConf {specializations = [("f", ''Identity)]}
666− -- \$(deriveSerializeWith conf ''CustomDataType)
667− -- @
668− --
669− -- @
670− -- instance (Serialize a) => Serialize (CustomDataType Identity a) where
671− -- ...
672− -- @
673− --
674− -- @f@ is replaced with 'Identity' and becomes unconstrained.
675− , inlineSize :: Inline
676− -- ^ Inline value for 'size'. Default is Inline.
677− , inlineSerialize :: Inline
678− -- ^ Inline value for 'serialize'. Default is Inline.
679− , inlineDeserialize :: Inline
680− -- ^ Inline value for 'deserialize'. Default is Inline.
681− , constructorTagAsString :: Bool
682− -- ^ __Experimental__
683− --
684− -- If True, encode constructors using the constructor names as Latin-1
685− -- byte sequence.
686− , recordSyntaxWithHeader :: Bool
687− -- ^ __Experimental__
688− --
689− -- If True, encode the keys of the record as a header and then
690− -- serialize the data.
691− }
692−
693−defaultConfig :: Config
694−defaultConfig =
695− Config
696− { unconstrained = []
697− , specializations = []
698− , inlineSize = Inline
699− , inlineSerialize = Inline
700− , inlineDeserialize = Inline
701− , constructorTagAsString = False
702− , recordSyntaxWithHeader = False
703− }
704−
705-- | Similar to 'deriveSerialize,' but take a 'Config' to control how
709-- | Similar to 'deriveSerialize,' but take a 'Config' to control how
706-- the instance is generated.
710-- the instance is generated.
707--
711--