HORIZON HASKELLDocslts/ghc-9.14.xd82dee82026-10-12Search names, modules, packages, or :: a typeCtrl K

GHC 9.14.1 · lts/ghc-9.14.x · d82dee8 · 2026-10-12

Modulemempack-0.2.1.0Haskell2010

Data.MemPack

  • 7 types
  • 1 class
  • 29 values
  • Packagemempack-0.2.1.0
  • Exports37
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceMemPack.hs
newtypenewtype Pack s a
#

Monad that is used for serializing data into a MutableByteArray. It is based on StateT that tracks the current index into the MutableByteArray where next write is expected to happen.

Constructors

Instances5MonadState, Monad, Functor, Applicative, MonadReader
newtypenewtype Unpack s b a
#

Monad that is used for deserializing data from a memory Buffer. It is based on StateT that tracks the current index into the Buffer a, from where the next read suppose to happen. Unpacking can fail with MonadFail instance or with failUnpack that provides a more type safe way of failing using Error interface.

Constructors

Instances7MonadReader, MonadState, Monad, Functor, MonadFail, Applicative, …
classclass MemPack a where
#

Efficient serialization interface that operates directly on memory buffers.

Methods

  • typeName :: String

    Name of the type that is being deserialized for error reporting. Default implementation relies on Typeable.

  • packedByteCount :: a -> Int

    Report the exact size in number of bytes that packed version of this type will occupy. It is very important to get this right, otherwise packM will result in a runtime exception. Another words this is the expected property that it should hold:

    Property
    packedByteCount a == bufferByteCount (pack a)
  • packM :: a -> Pack s ()

    Write binary representation of a type into the MutableByteArray which can be accessed with ask, whenever direct operations on it are necessary.

  • unpackM :: Buffer b => Unpack s b a

    Read binary representation of the type directly from the buffer, which can be accessed with ask when necessary.

    Warning - Direct reads from the buffer should be preceded with advancing the buffer offset within MonadState by the exact number of bytes that gets consumed from that buffer.

    ⚠ - Violation of the above rule will lead to segfaults.

Instances44MemPack, …

Packing

4 declarations
valuepack :: (MemPack a, HasCallStack) => a -> ByteArray
#

Serialize a type into an unpinned ByteArray

Examples
Example2 expressions
:set -XTypeApplicationsunpack @[Int] $ pack ([1,2,3,4,5] :: [Int])Right [1,2,3,4,5]
valuepackBuffer :: (MemPack a, Buffer b, HasCallStack) => a -> b
#

Serialize a type into any Buffer

Property
pack xs == packBuffer xs
Property
packByteString xs == packBuffer xs
Examples
Example4 expressions
:set -XTypeApplicationsimport qualified Data.Vector.Primitive as VPimport Data.Word (Word8)unpack @[Int] $ packBuffer @[Int] @(VP.Vector Word8) [1,2,3,4,5]Right [1,2,3,4,5]

Generalized

Helpers

Unpacking

5 declarations

Helpers

Helper packers

7 declarations
newtypenewtype VarLen a
#

Variable length encoding for bounded types. This type of encoding will use less memory for small values, but for larger values it will consume more memory and will be slower during packing/unpacking.

Constructors

Instances14Bounded, Enum, Eq, Integral, Num, Ord, …
newtypenewtype Length
#

This is a helper type useful for serializing number of elements in data structures. It uses VarLen underneath, since sizes of common data structures aren't too big. It also prevents negative values from being serialized and deserialized.

Constructors

Instances6Bounded, Enum, Eq, Num, Show, MemPack
  • Bounded LengthDefined in mempack-0.2.1.0 · Data.MemPack
  • Enum LengthDefined in mempack-0.2.1.0 · Data.MemPack
  • Eq LengthDefined in mempack-0.2.1.0 · Data.MemPack
  • Num LengthDefined in mempack-0.2.1.0 · Data.MemPack
  • Show LengthDefined in mempack-0.2.1.0 · Data.MemPack
  • MemPack LengthDefined in mempack-0.2.1.0 · Data.MemPack
newtypenewtype Tag
#

This is a helper type that is useful for creating MemPack instances for sum types.

Constructors

Instances7Bounded, Enum, Eq, Num, Ord, Show, …
  • Bounded TagDefined in mempack-0.2.1.0 · Data.MemPack
  • Enum TagDefined in mempack-0.2.1.0 · Data.MemPack
  • Eq TagDefined in mempack-0.2.1.0 · Data.MemPack
  • Num TagDefined in mempack-0.2.1.0 · Data.MemPack
  • Ord TagDefined in mempack-0.2.1.0 · Data.MemPack
  • Show TagDefined in mempack-0.2.1.0 · Data.MemPack
  • MemPack TagDefined in mempack-0.2.1.0 · Data.MemPack

Internal utilities

3 declarations

Re-exports for GeneralizedNewtypeDeriving

2 declarations
newtypenewtype StateT s (m :: Type -> Type) a
#

A state transformer monad parameterized by:

  • s - The state.

  • m - The inner monad.

The return function leaves the state unchanged, while >>= uses the final state of the first computation as the initial state of the second.

Constructors

Instances24MonadAccum, MonadError, MonadReader, MonadState, MonadWriter, MonadSelect, …
newtypenewtype FailT e (m :: Type -> Type) a
#

Fail monad transformer that plays well with MonadFail type class.

Constructors

Instances31MonadRWS, MonadAccum, MonadError, MonadReader, MonadState, MonadWriter, …