@@ -76,13 +76,19 @@ module Streamly.Internal.Data.Array
76 -- ** Folding
76 -- ** Folding
77 , streamFold
77 , streamFold
82#include "inline.hs"
89#include "inline.hs"
83#include "ArrayMacros.h"
90#include "ArrayMacros.h"
85−import Control.Exception (assert)
86import Control.Monad (when)
92import Control.Monad (when)
87import Control.Monad.IO.Class (MonadIO(..))
93import Control.Monad.IO.Class (MonadIO(..))
88import Data.Functor.Identity (Identity)
94import Data.Functor.Identity (Identity)
@@ -94,6 +100,8 @@ import Foreign.Storable (Storable)
94import Streamly.Internal.Data.Unbox (Unbox(..))
100import Streamly.Internal.Data.Unbox (Unbox(..))
95import Prelude hiding (length, null, last, map, (!!), read, concat)
101import Prelude hiding (length, null, last, map, (!!), read, concat)
103+import Streamly.Internal.Data.MutByteArray.Type (PinnedState(..))
104+import Streamly.Internal.Data.Serialize.Type (Serialize)
97import Streamly.Internal.Data.Fold.Type (Fold(..))
105import Streamly.Internal.Data.Fold.Type (Fold(..))
98import Streamly.Internal.Data.Producer.Type (Producer(..))
106import Streamly.Internal.Data.Producer.Type (Producer(..))
99import Streamly.Internal.Data.Stream (Stream)
107import Streamly.Internal.Data.Stream (Stream)
@@ -101,7 +109,8 @@ import Streamly.Internal.Data.Tuple.Strict (Tuple3Fused'(..))
101import Streamly.Internal.Data.Unfold.Type (Unfold(..))
109import Streamly.Internal.Data.Unfold.Type (Unfold(..))
102import Streamly.Internal.System.IO (unsafeInlineIO)
110import Streamly.Internal.System.IO (unsafeInlineIO)
104−import qualified Streamly.Internal.Data.MutArray.Type as MA
112+import qualified Streamly.Internal.Data.Serialize.Type as Serialize
113+import qualified Streamly.Internal.Data.MutByteArray.Type as MBA
105import qualified Streamly.Internal.Data.MutArray as MA
114import qualified Streamly.Internal.Data.MutArray as MA
106import qualified Streamly.Internal.Data.Array.Type as A
115import qualified Streamly.Internal.Data.Array.Type as A
107import qualified Streamly.Internal.Data.Fold as FL
116import qualified Streamly.Internal.Data.Fold as FL
@@ -543,3 +552,38 @@ fold f arr = Stream.fold f (A.read arr)
543{-# INLINE streamFold #-}
552{-# INLINE streamFold #-}
544streamFold :: (Monad m, Unbox a) => (Stream m a -> m b) -> Array a -> m b
553streamFold :: (Monad m, Unbox a) => (Stream m a -> m b) -> Array a -> m b
545streamFold f arr = f (A.read arr)
554streamFold f arr = f (A.read arr)
556+--------------------------------------------------------------------------------
558+--------------------------------------------------------------------------------
560+{-# INLINE encodeAs #-}
561+encodeAs :: forall a. Serialize a => PinnedState -> a -> Array Word8
564+ let len = Serialize.size 0 a
565+ mbarr <- MBA.newBytesAs ps len
566+ off <- Serialize.serialize 0 mbarr a
568+ pure $ Array mbarr 0 off
570+{-# INLINE serialize #-}
571+serialize :: Serialize a => a -> Array Word8
572+serialize = encodeAs Unpinned
574+-- | Serialize a Haskell type to a pinned byte array. The array is allocated
575+-- using pinned memory so that it can be used directly in OS APIs for writing
576+-- to file or sending over the network.
577+{-# INLINE pinnedSerialize #-}
578+pinnedSerialize :: Serialize a => a -> Array Word8
579+pinnedSerialize = encodeAs Pinned
581+-- | Decode a Haskell type from a byte array containing its serialized
583+{-# INLINE deserialize #-}
584+deserialize :: Serialize a => Array Word8 -> a
585+deserialize arr@(Array {..}) = unsafeInlineIO $ do
586+ let lenArr = length arr
587+ (off, val) <- Serialize.deserialize arrStart arrContents (arrStart + lenArr)
588+ assertM(off == arrStart + lenArr)