on master help sign in
Move out code from CPP macro to Haskell
da249d8Harendra Kumar
core/src/Streamly/Internal/Data/Serialize.hs+25−14
@@ -28,7 +28,11 @@ import Streamly.Internal.Data.Unbox (Unbox(..), sizeOfMutableByteArray)
28import Control.Monad (void)
28import Control.Monad (void)
29import Data.List (foldl')
29import Data.List (foldl')
30import Data.Proxy (Proxy (..))
30import Data.Proxy (Proxy (..))
31−import Streamly.Internal.Data.Unbox (MutableByteArray(..), PinnedState(..))
31+import Streamly.Internal.Data.Unbox
32+ ( MutableByteArray(..)
33+ , PinnedState(..)
34+ , Unbox
35+ )
32import Streamly.Internal.Data.Array.Type (Array(..))
36import Streamly.Internal.Data.Array.Type (Array(..))
33import Streamly.Internal.System.IO (unsafeInlineIO)
37import Streamly.Internal.System.IO (unsafeInlineIO)
34import GHC.Int (Int16(..), Int32(..), Int64(..), Int8(..))
38import GHC.Int (Int16(..), Int32(..), Int64(..), Int8(..))
@@ -126,25 +130,32 @@ checkBounds _label _off _arr = do
126 return ()
130 return ()
127#endif
131#endif
128
132
133+{-# INLINE deserializeUnsafe #-}
134+deserializeUnsafe :: forall a. Unbox a => Int -> MutableByteArray -> IO (Int, a)
135+deserializeUnsafe off arr =
136+ let next = off + Unbox.sizeOf (Proxy :: Proxy a)
137+ in do
138+ checkBounds "deserialize" next arr
139+ Unbox.peekByteIndex off arr >>= \val -> pure (next, val)
140+
141+{-# INLINE serializeUnsafe #-}
142+serializeUnsafe :: forall a. Unbox a => Int -> MutableByteArray -> a -> IO Int
143+serializeUnsafe off arr val =
144+ let next = off + Unbox.sizeOf (Proxy :: Proxy a)
145+ in do
146+ checkBounds "serialize" next arr
147+ Unbox.pokeByteIndex off arr val
148+ pure next
149+
129#define DERIVE_SERIALIZE_FROM_UNBOX(_type) \
150#define DERIVE_SERIALIZE_FROM_UNBOX(_type) \
130instance Serialize _type where \
151instance Serialize _type where \
131; {-# INLINE size #-} \
152; {-# INLINE size #-} \
132; size = Size (\acc _ -> acc + Unbox.sizeOf (Proxy :: Proxy _type)) \
153; size = Size (\acc _ -> acc + Unbox.sizeOf (Proxy :: Proxy _type)) \
133; {-# INLINE deserialize #-} \
154; {-# INLINE deserialize #-} \
134−; deserialize off arr = \
155+; deserialize off arr = deserializeUnsafe off arr :: IO (Int, _type) \
135− let next = off + Unbox.sizeOf (Proxy :: Proxy _type) \
136− in do { \
137− checkBounds "deserialize" next arr; \
138− Unbox.peekByteIndex off arr >>= \val -> pure (next, val); \
139− } \
140; {-# INLINE serialize #-} \
156; {-# INLINE serialize #-} \
141−; serialize off arr val = \
157+; serialize = \
142− let next = off + Unbox.sizeOf (Proxy :: Proxy _type) \
158+ serializeUnsafe :: Int -> MutableByteArray -> _type -> IO Int
143− in do { \
144− checkBounds "serialize" next arr; \
145− Unbox.pokeByteIndex off arr val; \
146− pure next \
147− }
148
159
149DERIVE_SERIALIZE_FROM_UNBOX(Bool)
160DERIVE_SERIALIZE_FROM_UNBOX(Bool)
150DERIVE_SERIALIZE_FROM_UNBOX(Char)
161DERIVE_SERIALIZE_FROM_UNBOX(Char)