on master help sign in
Move out code from CPP macro to Haskell
commitda249d8fd191f2fd24ceb1961d89dac07aa605ec authorHarendra Kumar <harendra@composewell.com> dateThu 17 Aug 2023, 17:02 (3 years ago) parentd09a236
1 file changed+25−14
core/src/Streamly/Internal/Data/Serialize.hs+25−14
core/src/Streamly/Internal/Data/Serialize.hs+25−14
@@ -28,7 +28,11 @@ import Streamly.Internal.Data.Unbox (Unbox(..), sizeOfMutableByteArray)
2828import Control.Monad (void)
2929import Data.List (foldl')
3030import Data.Proxy (Proxy (..))
31−import Streamly.Internal.Data.Unbox (MutableByteArray(..), PinnedState(..))
31+import Streamly.Internal.Data.Unbox
32+ ( MutableByteArray(..)
33+ , PinnedState(..)
34+ , Unbox
35+ )
3236import Streamly.Internal.Data.Array.Type (Array(..))
3337import Streamly.Internal.System.IO (unsafeInlineIO)
3438import GHC.Int (Int16(..), Int32(..), Int64(..), Int8(..))
@@ -126,25 +130,32 @@ checkBounds _label _off _arr = do
126130 return ()
127131#endif
128132
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+
129150#define DERIVE_SERIALIZE_FROM_UNBOX(_type) \
130151instance Serialize _type where \
131152; {-# INLINE size #-} \
132153; size = Size (\acc _ -> acc + Unbox.sizeOf (Proxy :: Proxy _type)) \
133154; {-# INLINE deserialize #-} \
134−; deserialize off arr = \
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− } \
155+; deserialize off arr = deserializeUnsafe off arr :: IO (Int, _type) \
140156; {-# INLINE serialize #-} \
141−; serialize off arr val = \
142− let next = off + Unbox.sizeOf (Proxy :: Proxy _type) \
143− in do { \
144− checkBounds "serialize" next arr; \
145− Unbox.pokeByteIndex off arr val; \
146− pure next \
147− }
157+; serialize = \
158+ serializeUnsafe :: Int -> MutableByteArray -> _type -> IO Int
148159
149160DERIVE_SERIALIZE_FROM_UNBOX(Bool)
150161DERIVE_SERIALIZE_FROM_UNBOX(Char)