2-- Module : Streamly.Data.Serialize
3-- Copyright : (c) 2023 Composewell Technologies
4-- License : BSD3-3-Clause
5-- Maintainer : streamly@composewell.com
8module Streamly.Data.Serialize
35import Data.Int (Int64)
36import Data.Word (Word8)
37import Streamly.Internal.Data.Array.Type (Array(..))
38import Streamly.Internal.Data.Serialize (Serialize(..), Size(..))
39import Streamly.Internal.Data.Serialize.TH
43import Streamly.Internal.Data.Unbox
48import Streamly.Internal.Data.Unbox.TH (deriveUnbox, deriveUnboxWith)
49import Streamly.Internal.System.IO (unsafeInlineIO)
51import qualified Streamly.Internal.Data.Unbox as Unbox
52import qualified Streamly.Internal.Data.Array as Array
54--------------------------------------------------------------------------------
56--------------------------------------------------------------------------------
58{-# INLINE encodeAs #-}
59encodeAs :: forall a. Serialize a => PinnedState -> a -> Array Word8
63 case size :: Size a of
66 -- We encode the length of the encoding as a header hence the 8 extra
67 -- bytes to encode Int64
68 mbarr <- Unbox.newBytesAs ps (8 + len)
69 off1 <- serialize 0 mbarr (fromIntegral len :: Int64)
70 off2 <- serialize off1 mbarr a
71 assertM(off2 == len + off1)
72 pure $ Array mbarr 0 len
75encode :: Serialize a => a -> Array Word8
76encode = encodeAs Unpinned
78{-# INLINE pinnedEncode #-}
79pinnedEncode :: Serialize a => a -> Array Word8
80pinnedEncode = encodeAs Pinned
82--------------------------------------------------------------------------------
84--------------------------------------------------------------------------------
87decode :: Serialize a => Array Word8 -> a
88decode arr@(Array {..}) = unsafeInlineIO $ do
89 let lenArr = Array.length arr
90 (off1, lenEncoding :: Int64) <- deserialize 0 arrContents
91 (off2, val) <- deserialize off1 arrContents
92 assertM(fromIntegral lenEncoding + off1 == off2)
93 assertM(lenArr == off2)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list